]> git.llucax.com Git - software/dgc/cdgc.git/commitdiff
Make allocation functions that can fail return bool
authorLeandro Lucarella <llucax@gmail.com>
Sun, 17 Jan 2010 00:48:12 +0000 (21:48 -0300)
committerLeandro Lucarella <llucax@gmail.com>
Sun, 17 Jan 2010 00:57:20 +0000 (21:57 -0300)
There is no point on returning int, since no error code is returned, just
failure or success.

gc/alloc.d
gc/gc.d

index c6a8bd1680708c5bcb91e8c9715800eb2a4e7c89..575095b30bbe457eaf9c6ab0f550ecd191e5975d 100644 (file)
@@ -97,7 +97,7 @@ version (D_Ddoc)
      *      true  success
      *      false failure
      */
-    int os_mem_commit(void* base, size_t offset, size_t nbytes);
+    bool os_mem_commit(void* base, size_t offset, size_t nbytes);
 
     /**
      * Decommit memory.
@@ -105,7 +105,7 @@ version (D_Ddoc)
      *      true  success
      *      false failure
      */
-    int os_mem_decommit(void* base, size_t offset, size_t nbytes);
+    bool os_mem_decommit(void* base, size_t offset, size_t nbytes);
 
     /**
      * Unmap memory allocated with os_mem_map().
@@ -114,7 +114,7 @@ version (D_Ddoc)
      *      true  success
      *      false failure
      */
-    int os_mem_unmap(void* base, size_t nbytes);
+    bool os_mem_unmap(void* base, size_t nbytes);
 }
 // Implementations
 else static if (is(typeof(VirtualAlloc)))
@@ -124,20 +124,20 @@ else static if (is(typeof(VirtualAlloc)))
         return VirtualAlloc(null, nbytes, MEM_RESERVE, PAGE_READWRITE);
     }
 
-    int os_mem_commit(void* base, size_t offset, size_t nbytes)
+    bool os_mem_commit(void* base, size_t offset, size_t nbytes)
     {
         void* p = VirtualAlloc(base + offset, nbytes, MEM_COMMIT, PAGE_READWRITE);
-        return cast(int)(p is null);
+        return p !is null;
     }
 
-    int os_mem_decommit(void* base, size_t offset, size_t nbytes)
+    bool os_mem_decommit(void* base, size_t offset, size_t nbytes)
     {
-        return cast(int)(VirtualFree(base + offset, nbytes, MEM_DECOMMIT) == 0);
+        return VirtualFree(base + offset, nbytes, MEM_DECOMMIT) != 0;
     }
 
-    int os_mem_unmap(void* base, size_t nbytes)
+    bool os_mem_unmap(void* base, size_t nbytes)
     {
-        return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0);
+        return VirtualFree(base, 0, MEM_RELEASE) != 0;
     }
 }
 else static if (is(typeof(mmap)) && is(typeof(MAP_ANON)))
@@ -149,19 +149,19 @@ else static if (is(typeof(mmap)) && is(typeof(MAP_ANON)))
         return (p == MAP_FAILED) ? null : p;
     }
 
-    int os_mem_commit(void* base, size_t offset, size_t nbytes)
+    bool os_mem_commit(void* base, size_t offset, size_t nbytes)
     {
-        return 0;
+        return true;
     }
 
-    int os_mem_decommit(void* base, size_t offset, size_t nbytes)
+    bool os_mem_decommit(void* base, size_t offset, size_t nbytes)
     {
-        return 0;
+        return true;
     }
 
-    int os_mem_unmap(void* base, size_t nbytes)
+    bool os_mem_unmap(void* base, size_t nbytes)
     {
-        return munmap(base, nbytes);
+        return munmap(base, nbytes) == 0;
     }
 }
 else static if (is(typeof(malloc)))
@@ -184,20 +184,20 @@ else static if (is(typeof(malloc)))
         return q;
     }
 
-    int os_mem_commit(void* base, size_t offset, size_t nbytes)
+    bool os_mem_commit(void* base, size_t offset, size_t nbytes)
     {
-        return 0;
+        return true;
     }
 
-    int os_mem_decommit(void* base, size_t offset, size_t nbytes)
+    bool os_mem_decommit(void* base, size_t offset, size_t nbytes)
     {
-        return 0;
+        return true;
     }
 
-    int os_mem_unmap(void* base, size_t nbytes)
+    bool os_mem_unmap(void* base, size_t nbytes)
     {
         free(*cast(void**)(cast(byte*) base + nbytes));
-        return 0;
+        return true;
     }
 }
 else
diff --git a/gc/gc.d b/gc/gc.d
index 8cd56095b98dd4d42b08b149097f107f0e7dc405..3dff4eaa74a66d9a55b830cf962d2455ae6155fc 100644 (file)
--- a/gc/gc.d
+++ b/gc/gc.d
@@ -2791,14 +2791,14 @@ struct Pool
             if (ncommitted)
             {
                 result = os_mem_decommit(baseAddr, 0, ncommitted * PAGESIZE);
-                assert(result == 0);
+                assert(result);
                 ncommitted = 0;
             }
 
             if (npages)
             {
                 result = os_mem_unmap(baseAddr, npages * PAGESIZE);
-                assert(result == 0);
+                assert(result);
                 npages = 0;
             }
 
@@ -2885,7 +2885,7 @@ struct Pool
                 tocommit = npages - ncommitted;
             //debug(PRINTF) printf("\tlooking to commit %d more pages\n", tocommit);
             //fflush(stdout);
-            if (os_mem_commit(baseAddr, ncommitted * PAGESIZE, tocommit * PAGESIZE) == 0)
+            if (os_mem_commit(baseAddr, ncommitted * PAGESIZE, tocommit * PAGESIZE))
             {
                 .memset(pagetable + ncommitted, B_FREE, tocommit);
                 auto i = ncommitted;