import gc.arch: push_registers, pop_registers;
// Standard imports
-import cstdlib = tango.stdc.stdlib;
import cstring = tango.stdc.string;
// Debug imports
{
foreach (cell; this.free_list) {
this.free_list.unlink(cell);
- cstdlib.free(cell);
+ Cell.free(cell);
}
}
uint getAttr(void* ptr)
{
auto cell = this.live_list.find(ptr);
- if (cell)
+ if (cell !is null)
return cell.attr;
return 0;
}
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;
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;
// 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;
}
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) {
// 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;
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;
}
/**
return;
auto cell = this.live_list.pop(ptr);
- assert (cell);
+ assert (cell !is null);
this.free_list.link(cell);
}
}
auto cell = this.live_list.find(&in_range);
- if (cell)
+ if (cell !is null)
return cell.ptr;
return null;
size_t sizeOf(void* ptr)
{
auto cell = this.live_list.find(ptr);
- if (cell)
+ if (cell !is null)
return cell.capacity;
return 0;
}
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;