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