]> git.llucax.com Git - software/dgc/naive.git/blobdiff - gc/gc.d
Make calloc() handle null as a malloc() result
[software/dgc/naive.git] / gc / gc.d
diff --git a/gc/gc.d b/gc/gc.d
index 3ee81ac0ff34be2c47c07f9e7b355b4ae4c1885d..f0471debcd85c0762bb66736aee1d41a0cb465e2 100644 (file)
--- a/gc/gc.d
+++ b/gc/gc.d
@@ -533,6 +533,7 @@ public:
 
         // No luck still, allocate new memory
         cell = cast(Cell*) cstdlib.malloc(size + Cell.sizeof);
+        cell.capacity = 0; // so we can later tell it's new
         if (cell)
             goto success;
 
@@ -558,11 +559,12 @@ public:
      */
     void* calloc(size_t size, uint attr=0)
     {
+        if (size == 0)
+            return null;
+
         void* ptr = this.malloc(size, attr);
 
-        if (ptr is null)
-            onOutOfMemoryError();
-        else
+        if (ptr !is null) // in case onOutOfMemoryError didn't throw
             cstring.memset(ptr, 0, size);
 
         return ptr;