1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/**
* This module contains a minimal garbage collector implementation according to
* published requirements. This library is mostly intended to serve as an
* example, but it is usable in applications which do not rely on a garbage
* collector to clean up memory (ie. when dynamic array resizing is not used,
* and all memory allocated with 'new' is freed deterministically with
* 'delete').
*
* Please note that block attribute data must be tracked, or at a minimum, the
* FINALIZE bit must be tracked for any allocated memory block because calling
* rt_finalize on a non-object block can result in an access violation. In the
* allocator below, this tracking is done via a leading uint bitmask. A real
* allocator may do better to store this data separately, similar to the basic
* GC.
*
* Copyright: Copyright Sean Kelly 2005 - 2016.
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Sean Kelly
*/
module core.internal.gc.impl.manual.gc;
import core.gc.gcinterface;
import core.internal.container.array;
import cstdlib = core.stdc.stdlib : calloc, free, malloc, realloc;
static import core.memory;
extern (C) noreturn onOutOfMemoryError(void* pretend_sideffect = null, string file = __FILE__, size_t line = __LINE__) @trusted pure nothrow @nogc; /* dmd @@@BUG11461@@@ */
// register GC in C constructor (_STI_)
private pragma(crt_constructor) void gc_manual_ctor()
{
_d_register_manual_gc();
}
extern(C) void _d_register_manual_gc()
{
import core.gc.registry;
registerGCFactory("manual", &initialize);
}
private GC initialize()
{
import core.lifetime : emplace;
auto gc = cast(ManualGC) cstdlib.malloc(__traits(classInstanceSize, ManualGC));
if (!gc)
onOutOfMemoryError();
return emplace(gc);
}
class ManualGC : GC
{
Array!Root roots;
Array!Range ranges;
this()
{
}
~this()
{
// TODO: cannot free as memory is overwritten and
// the monitor is still read in rt_finalize (called by destroy)
// cstdlib.free(cast(void*) this);
}
void enable()
{
}
void disable()
{
}
void collect() nothrow
{
}
void collectNoStack() nothrow
{
}
void minimize() nothrow
{
}
uint getAttr(void* p) nothrow
{
return 0;
}
uint setAttr(void* p, uint mask) nothrow
{
return 0;
}
uint clrAttr(void* p, uint mask) nothrow
{
return 0;
}
void* malloc(size_t size, uint bits, const TypeInfo ti) nothrow
{
void* p = cstdlib.malloc(size);
if (size && p is null)
onOutOfMemoryError();
return p;
}
BlkInfo qalloc(size_t size, uint bits, const scope TypeInfo ti) nothrow
{
BlkInfo retval;
retval.base = malloc(size, bits, ti);
retval.size = size;
retval.attr = bits;
return retval;
}
void* calloc(size_t size, uint bits, const TypeInfo ti) nothrow
{
void* p = cstdlib.calloc(1, size);
if (size && p is null)
onOutOfMemoryError();
return p;
}
void* realloc(void* p, size_t size, uint bits, const TypeInfo ti) nothrow
{
p = cstdlib.realloc(p, size);
if (size && p is null)
onOutOfMemoryError();
return p;
}
size_t extend(void* p, size_t minsize, size_t maxsize, const TypeInfo ti) nothrow
{
return 0;
}
size_t reserve(size_t size) nothrow
{
return 0;
}
void free(void* p) nothrow @nogc
{
cstdlib.free(p);
}
/**
* Determine the base address of the block containing p. If p is not a gc
* allocated pointer, return null.
*/
void* addrOf(void* p) nothrow @nogc
{
return null;
}
/**
* Determine the allocated size of pointer p. If p is an interior pointer
* or not a gc allocated pointer, return 0.
*/
size_t sizeOf(void* p) nothrow @nogc
{
return 0;
}
/**
* Determine the base address of the block containing p. If p is not a gc
* allocated pointer, return null.
*/
BlkInfo query(void* p) nothrow
{
return BlkInfo.init;
}
core.memory.GC.Stats stats() nothrow
{
return typeof(return).init;
}
core.memory.GC.ProfileStats profileStats() nothrow
{
return typeof(return).init;
}
void addRoot(void* p) nothrow @nogc
{
roots.insertBack(Root(p));
}
void removeRoot(void* p) nothrow @nogc
{
foreach (ref r; roots)
{
if (r is p)
{
r = roots.back;
roots.popBack();
return;
}
}
assert(false);
}
@property RootIterator rootIter() return @nogc
{
return &rootsApply;
}
private int rootsApply(scope int delegate(ref Root) nothrow dg)
{
foreach (ref r; roots)
{
if (auto result = dg(r))
return result;
}
return 0;
}
void addRange(void* p, size_t sz, const TypeInfo ti = null) nothrow @nogc
{
ranges.insertBack(Range(p, p + sz, cast() ti));
}
void removeRange(void* p) nothrow @nogc
{
foreach (ref r; ranges)
{
if (r.pbot is p)
{
r = ranges.back;
ranges.popBack();
return;
}
}
assert(false);
}
@property RangeIterator rangeIter() return @nogc
{
return &rangesApply;
}
private int rangesApply(scope int delegate(ref Range) nothrow dg)
{
foreach (ref r; ranges)
{
if (auto result = dg(r))
return result;
}
return 0;
}
void runFinalizers(const scope void[] segment) nothrow
{
}
bool inFinalizer() nothrow
{
return false;
}
ulong allocatedInCurrentThread() nothrow
{
return typeof(return).init;
}
}
|