2 * Memory Cell header manipulation.
4 * This module has the Cell header definition and other support stuff (like
5 * BlkAttr) for the Naive Garbage Collector implementation. The Cell header has
6 * all the information needed for the bookkeeping of the GC allocated memory,
7 * like the mark bit, if the cell contents should be finalized or if it has
8 * pointers that should be scanned, etc.
11 * Copyright: Public Domain
12 * License: Public Domain
13 * Authors: Leandro Lucarella <llucax@gmail.com>
21 * Iterates a range of memory interpreting it as an array of void*.
23 * This function is designed to be used as a opApply implementation.
25 int op_apply_ptr_range(void* from, void* to, int delegate(ref void*) dg)
28 auto start = cast(void**) from;
29 auto end = cast(void**) to;
30 // since we sweep the memory range in word-sized steps, we need to make
31 // sure we don't scan for pointers beyond the end of the memory range
32 for (auto current = start; current + 1 <= end; current++) {
33 result = dg(*current);
40 /// Memory block (cell) attributes.
43 /// All attributes disabled.
45 /// The cell is an object with a finalizer.
46 FINALIZE = 0b0000_0001,
47 /// The cell has no pointers.
48 NO_SCAN = 0b0000_0010,
49 /// The cell should not be moved (unimplemented).
50 NO_MOVE = 0b0000_0100,
51 /// All attributes enabled.
56 * Memory block (cell) header.
58 * All memory cells in the GC heap have this header.
63 /// Size of the object stored in this memory cell.
66 /// Real size of the memory cell.
73 BlkAttr attr = BlkAttr.NONE;
75 /// Next cell (this is used for free/live lists linking).
80 assert (this.size > 0);
81 assert (this.capacity >= this.size);
85 * Get a cell pointer for the cell that stores the object pointed to by
88 * If ptr is null, null is returned.
90 static Cell* from_ptr(void* ptr)
94 return cast(Cell*) (cast(byte*) ptr - Cell.sizeof);
97 /// Get the base address of the object stored in the cell.
100 return cast(void*) (cast(byte*) this + Cell.sizeof);
103 /// Return true if the cell should be finalized, false otherwise.
106 return cast(bool) (this.attr & BlkAttr.FINALIZE);
109 /// Return true if the cell should may have pointers, false otherwise.
112 return !(this.attr & BlkAttr.NO_SCAN);
116 * Iterates over the objects pointers.
118 * Current implementation interprets the whole object as if it were
121 int opApply(int delegate(ref void*) dg)
123 return op_apply_ptr_range(this.ptr, this.ptr + this.size, dg);
133 import tango.stdc.stdlib: malloc;
135 unittest // op_apply_ptr_range()
142 int r = op_apply_ptr_range(v.ptr, v.ptr + 10,
144 assert (cast (size_t) ptr == i++);
152 auto size = N * size_t.sizeof;
153 auto cell = cast(Cell*) malloc(size + Cell.sizeof);
155 assert (cell.ptr is cell + 1);
157 cell.capacity = size;
158 cell.attr = BlkAttr.FINALIZE | BlkAttr.NO_SCAN;
160 for (int i = 0; i < N; ++i) {
161 auto ptr = cast(size_t*) cell.ptr + i;
165 foreach (void* ptr; *cell) {
166 assert (cast(size_t) ptr == i++);
168 assert (*(cast(size_t*) cell.ptr) == N);
169 assert (cell.has_finalizer());
170 assert (!cell.has_pointers());
171 assert (cell is Cell.from_ptr(cell.ptr));
174 } // debug (UnitTest)
176 // vim: set et sw=4 sts=4 :