2 * Singly linked list of Cells.
4 * This module implements a simple singly linked list of Cells to be used as
5 * live/free list in the Naive Garbage Collector implementation.
8 * Copyright: Public Domain
9 * License: Public Domain
10 * Authors: Leandro Lucarella <llucax@gmail.com>
16 private import gc.cell: Cell;
22 * Singly linked list of Cells.
24 * This is used for live and free lists.
29 /// First element in the list.
33 * Find the first cell for which predicate(cell) is true.
35 * Returns a pointer to the cell if found, null otherwise.
37 Cell* find(bool delegate(Cell*) predicate)
39 auto cell = this.first;
49 * Find the cell that holds the object pointed by ptr.
51 * Returns a pointer to the cell if found, null otherwise.
55 return this.find((Cell* cell) { return cell.ptr == ptr; });
59 * Link a new cell into the list.
63 cell.next = this.first;
68 * Find and remove the first cell for which predicate(cell) is true.
70 * If no cell is found, no cell is removed.
72 * Returns a pointer to the removed cell if found, null otherwise.
74 Cell* pop(bool delegate(Cell*) predicate)
77 auto cell = this.first;
79 if (predicate(cell)) {
81 prev.next = cell.next;
83 this.first = cell.next;
93 * Find and remove the cell that holds the object pointed by ptr.
95 * If no cell is found, no cell is removed.
97 * Returns a pointer to the removed cell if found, null otherwise.
101 return this.pop((Cell* cell) { return cell.ptr == ptr; });
105 * Find and remove the first cell with a capacity of at least min_size.
107 * If no cell is found, no cell is removed.
109 * Returns a pointer to the removed cell if found, null otherwise.
111 Cell* pop(size_t min_size)
113 return this.pop((Cell* cell) { return cell.capacity >= min_size; });
117 * Remove a cell from the list.
119 * If the cell was not linked into the list, this method has no effect.
121 void unlink(Cell* cell)
123 this.pop((Cell* cell2) { return cell is cell2; });
127 * Iterate over the cells in the list.
129 * unlink()ing cells from the list while iterating is supported, but
130 * link()ing may not work.
132 int opApply(int delegate(ref Cell*) dg)
135 auto cell = this.first;
137 // this is necessary to allow removing a node while iterating
138 auto next = cell.next;
154 import tango.stdc.stdlib: malloc;
159 assert (l.first == null);
160 assert (l.find(cast(void*) null) is null);
161 assert (l.find(cast(void*) &l) is null);
163 foreach (ref c; cells)
167 assert (c is &cells[--i]);
170 } // debug (UnitTest)
172 // vim: set et sw=4 sts=4 :