2 * This module contains a minimal garbage collector implementation according to
3 * Tango requirements. This library is mostly intended to serve as an example,
4 * but it is usable in applications which do not rely on a garbage collector
5 * to clean up memory (ie. when dynamic array resizing is not used, and all
6 * memory allocated with 'new' is freed deterministically with 'delete').
8 * Please note that block attribute data must be tracked, or at a minimum, the
9 * FINALIZE bit must be tracked for any allocated memory block because calling
10 * rt_finalize on a non-object block can result in an access violation. In the
11 * allocator below, this tracking is done via a leading uint bitmask. A real
12 * allocator may do better to store this data separately, similar to the basic
13 * GC normally used by Tango.
15 * Copyright: Public Domain
17 * Authors: Leandro Lucarella
22 private import cell: Cell;
23 private import alloc: mem_free;
30 Cell* find(bool delegate(Cell*) predicate)
32 auto cell = this.first;
43 return this.find((Cell* cell) { return cell.ptr == ptr; });
48 cell.next = this.first;
52 Cell* pop(bool delegate(Cell*) predicate)
55 auto cell = this.first;
57 if (predicate(cell)) {
59 prev.next = cell.next;
61 this.first = cell.next;
72 return this.pop((Cell* cell) { return cell.ptr == ptr; });
75 Cell* pop(size_t min_size)
77 return this.pop((Cell* cell) { return cell.capacity >= min_size; });
80 void unlink(Cell* cell)
82 this.pop((Cell* cell2) { return cell is cell2; });
85 void swap(Cell* old_cell, Cell* new_cell)
88 auto cell = this.first;
90 if (cell is old_cell) {
91 new_cell.next = cell.next;
95 this.first = new_cell;
103 int opApply(int delegate(ref Cell*) dg)
106 auto cell = this.first;
108 // this is necessary to allow removing node while iterating
109 auto next = cell.next;
120 // vim: set et sw=4 sts=4 :