X-Git-Url: https://git.llucax.com/software/dgc/naive.git/blobdiff_plain/6496d94f157af17ccf0f9cf3dee67c663a23313b..refs/heads/master:/gc/gc.d diff --git a/gc/gc.d b/gc/gc.d index 7d33622..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; @@ -594,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) { @@ -653,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; } /** @@ -679,7 +675,7 @@ public: return; auto cell = this.live_list.pop(ptr); - assert (cell); + assert (cell !is null); this.free_list.link(cell); } @@ -700,7 +696,7 @@ public: } auto cell = this.live_list.find(&in_range); - if (cell) + if (cell !is null) return cell.ptr; return null; @@ -719,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; } @@ -738,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;