*
* This module implements a Naive Garbage Collector. The idea behind this
* implementation is to document all the bookkeeping and considerations that
*
* This module implements a Naive Garbage Collector. The idea behind this
* implementation is to document all the bookkeeping and considerations that
- * The garbage collector algorithm itself is extremely simple so focus can be
- * held in the specifics of D, and not the algorithm. A completely naive mark
- * and sweep algorithm is used, with a recursive mark phase. The code is
- * extremely inefficient in order to keep the code clean and easy to read and
- * understand.
+ * The garbage collector algorithm itself is extremely simple to make it
+ * easier to focus on the specifics of D. A completely naive mark and sweep
+ * algorithm is used, with a recursive mark phase. The code is extremely
+ * inefficient in order to keep it clean, and easy to read and understand.
- * The implementation is split in several modules to ease even more the
- * lecture. All architecture/compiler specific code is done in the arch module,
- * in order to avoid confusing version statements all over the places. The cell
- * module has all the code related to the memory cells header. dynarray is
- * another support module which holds the implementation of a simple dynamic
- * array used to store root pointers and ranges. The list module holds a simple
- * singly linked list (of cells) implementation to store the live and free
- * lists. Finally, the iface module is the one with the C interface to comply
- * with the Tango/Druntime GC specification.
+ * The implementation is split in several modules to ease the reading even
+ * more. All architecture/compiler specific code is done in the arch module,
+ * in order to avoid confusing version statements all over the places. The
+ * cell module has all the code related to the memory cells header. dynarray
+ * is another support module which holds the implementation of a simple
+ * dynamic array used to store root pointers and ranges. The list module holds
+ * a simple singly linked list (of cells) implementation to store the live and
+ * free lists. Finally, the iface module is the one with the C interface to
+ * comply with the Tango/Druntime GC specification.
* Wrapper for mark() over a range, needed by some runtime functions.
*
* This function is used as a delegate to be passed to rt_scanStaticData()
* Wrapper for mark() over a range, needed by some runtime functions.
*
* This function is used as a delegate to be passed to rt_scanStaticData()
*
* This extremely inefficient on purpose. The goal of this implementation
* is simplicity, nor performance.
*
* This extremely inefficient on purpose. The goal of this implementation
* is simplicity, nor performance.
*
* This is the mark algorithm itself. It's recursive and dumb as a log. No
* care is taken in regards to stack overflows. This is the first example
*
* This is the mark algorithm itself. It's recursive and dumb as a log. No
* care is taken in regards to stack overflows. This is the first example
* Run a GC collection in order to find unreferenced objects.
*
* This is the simplest stop-the-world mark-sweep algorithm ever. It first
* Run a GC collection in order to find unreferenced objects.
*
* This is the simplest stop-the-world mark-sweep algorithm ever. It first
* that are reachable through the root set (static data, stack, registers
* and custom root), and finally sweeps the live list looking for unmarked
* cells to free.
* that are reachable through the root set (static data, stack, registers
* and custom root), and finally sweeps the live list looking for unmarked
* cells to free.
* $(LI 2: The cell should not be scanned for pointers)
* $(LI 4: The cell should not be moved during a collection
* (unimplemented))
* $(LI 2: The cell should not be scanned for pointers)
* $(LI 4: The cell should not be moved during a collection
* (unimplemented))
uint setAttr(void* ptr, uint attr)
{
auto cell = this.live_list.find(ptr);
uint setAttr(void* ptr, uint attr)
{
auto cell = this.live_list.find(ptr);
uint clrAttr(void* ptr, uint attr)
{
auto cell = this.live_list.find(ptr);
uint clrAttr(void* ptr, uint attr)
{
auto cell = this.live_list.find(ptr);
// 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);
// 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);
- // No luck still, allocate new memory
- cell = cast(Cell*) cstdlib.malloc(size + Cell.sizeof);
- if (cell)
- goto success;
+ // No luck still, allocate a new cell
+ cell = Cell.alloc(size, attr);
+ if (cell !is null)
+ goto link;
* returned. Otherwise a new cell is allocated using malloc() (this can
* trigger a collection), the contents are moved and the old cell is freed.
*
* returned. Otherwise a new cell is allocated using malloc() (this can
* trigger a collection), the contents are moved and the old cell is freed.
*
// We have enough capacity already, just change the size
if (cell.capacity >= size) {
// 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)
// 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);
* Reserve memory to anticipate memory allocations.
*
* This implementation is really dumb, a single cell is allocated with
* Reserve memory to anticipate memory allocations.
*
* This implementation is really dumb, a single cell is allocated with
- * size bytes. If 2 mallocs() follows a call to reserve(size), requesting
- * size/2 bytes each, one allocation will be done still (and half the
- * memory of the first malloc will be wasted =) Since this is a trivial
+ * size bytes. If 2 malloc()s follow a call to reserve(size), requesting
+ * size/2 bytes each, one allocation will still be done (and half the
+ * memory of the first malloc will be wasted =). Since this is a trivial
* perform any connectivity check, if the cell was referenced by others,
* nasty things will happen (much like C/C++).
*
* perform any connectivity check, if the cell was referenced by others,
* nasty things will happen (much like C/C++).
*
*
* ptr should be the base address of a heap allocated object, interior
* pointers are not supported (use addrOf() if you have an interior
*
* ptr should be the base address of a heap allocated object, interior
* pointers are not supported (use addrOf() if you have an interior
*
* ptr should be the base address of a heap allocated object, interior
* pointers are not supported (use addrOf() if you have an interior
*
* ptr should be the base address of a heap allocated object, interior
* pointers are not supported (use addrOf() if you have an interior
blk_info.base = cell.ptr;
blk_info.size = cell.capacity;
blk_info.attr = cell.attr;
blk_info.base = cell.ptr;
blk_info.size = cell.capacity;
blk_info.attr = cell.attr;
- * ptr has to be previously registered using addRoot(), in other case the
- * results of this method is undefined.
+ * ptr has to be previously registered using addRoot(), otherwise the
+ * results are undefined.
- * ptr has to be previously registered using addRange(), in other case the
- * results of this method is undefined.
+ * ptr has to be previously registered using addRange(), otherwise the
+ * results are undefined.