X-Git-Url: https://git.llucax.com/software/dgc/naive.git/blobdiff_plain/2d8639409b4749afd92266347f20b99da80e14c9..refs/heads/master:/gc/gc.d?ds=sidebyside diff --git a/gc/gc.d b/gc/gc.d index a7c8f59..a10143a 100644 --- a/gc/gc.d +++ b/gc/gc.d @@ -36,7 +36,6 @@ import gc.dynarray: DynArray; import gc.arch: push_registers, pop_registers; // Standard imports -import cstdlib = tango.stdc.stdlib; import cstring = tango.stdc.string; // Debug imports @@ -435,7 +434,7 @@ public: { foreach (cell; this.free_list) { this.free_list.unlink(cell); - cstdlib.free(cell); + Cell.free(cell); } } @@ -456,7 +455,7 @@ public: uint getAttr(void* ptr) { auto cell = this.live_list.find(ptr); - if (cell) + if (cell !is null) return cell.attr; return 0; } @@ -472,7 +471,7 @@ public: uint setAttr(void* ptr, uint attr) { auto cell = this.live_list.find(ptr); - if (cell) { + if (cell !is null) { auto old = cell.attr; cell.attr |= attr; return cell.attr; @@ -491,7 +490,7 @@ public: uint clrAttr(void* ptr, uint attr) { auto cell = this.live_list.find(ptr); - if (cell) { + if (cell !is null) { auto old = cell.attr; cell.attr &= ~attr; return cell.attr; @@ -519,34 +518,33 @@ public: // Find a free cell in the free list with enough space auto cell = this.free_list.pop(size); - if (cell) - goto success; + if (cell !is null) + goto reuse; // No room in the free list found, if the GC is enabled, trigger // a collection and try again if (!this.disabled) { this.collect(); cell = this.free_list.pop(size); - if (cell) - goto success; + if (cell !is null) + goto reuse; } - // 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; + // No luck still, allocate a new cell + cell = Cell.alloc(size, attr); + if (cell !is null) + goto link; // No memory onOutOfMemoryError(); return null; - success: + reuse: cell.size = size; - if (cell.capacity == 0) // fresh cell - cell.capacity = size; cell.attr = cast(BlkAttr) attr; + + link: this.live_list.link(cell); return cell.ptr; @@ -559,11 +557,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; @@ -593,7 +592,7 @@ public: } auto cell = this.live_list.find(ptr); - assert (cell); + assert (cell !is null); // We have enough capacity already, just change the size if (cell.capacity >= size) { @@ -603,8 +602,11 @@ public: // We need to move the cell because of the lack of capacity, find // a free cell with the requested capacity (at least) - Cell* new_cell = Cell.from_ptr(this.malloc(size)); - assert (!(new_cell is null)); // out of memory is handled by malloc() + ptr = this.malloc(size, attr); + if (ptr is null) // in case onOutOfMemoryError didn't throw + return null; + Cell* new_cell = Cell.from_ptr(ptr); + assert (new_cell !is null); // Move cell attributes and contents new_cell.attr = cell.attr; @@ -649,13 +651,11 @@ public: size_t reserve(size_t size) { assert (size > 0); - auto cell = cast(Cell*) cstdlib.malloc(size + Cell.sizeof); - if (!cell) + auto cell = Cell.alloc(size); + if (cell is null) return 0; - cell.size = size; - cell.capacity = size; this.free_list.link(cell); - return size; + return cell.capacity; } /** @@ -675,7 +675,7 @@ public: return; auto cell = this.live_list.pop(ptr); - assert (cell); + assert (cell !is null); this.free_list.link(cell); } @@ -696,7 +696,7 @@ public: } auto cell = this.live_list.find(&in_range); - if (cell) + if (cell !is null) return cell.ptr; return null; @@ -715,7 +715,7 @@ public: size_t sizeOf(void* ptr) { auto cell = this.live_list.find(ptr); - if (cell) + if (cell !is null) return cell.capacity; return 0; } @@ -734,7 +734,7 @@ public: BlkInfo blk_info; auto cell = this.live_list.find(ptr); - if (cell) { + if (cell !is null) { blk_info.base = cell.ptr; blk_info.size = cell.capacity; blk_info.attr = cell.attr;