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 tango.stdc.stdlib: realloc;
23 private import tango.stdc.string: memmove;
24 private extern (C) void onOutOfMemoryError();
40 assert (this.capacity >= this.size);
45 if (this.size == this.capacity)
47 this.data[this.size] = x;
53 for (size_t i = 0; i < this.size; i++) {
54 if (this.data[i] == x) {
56 memmove(this.data + i, this.data + i + T.sizeof,
57 (this.size - i) * T.sizeof);
63 void expand(size_t new_capacity=0)
65 if (new_capacity == 0)
66 new_capacity = this.size * 2;
67 if (new_capacity == 0)
69 T* new_data = cast(T*) realloc(this.data, new_capacity);
73 this.capacity = new_capacity;
76 int opApply(int delegate(ref T) dg)
79 for (size_t i = 0; i < this.size; i++) {
80 result = dg(this.data[i]);
89 // vim: set et sw=4 sts=4 :