| 1 |
/** |
| 2 |
* Contains the internal GC interface. |
| 3 |
* |
| 4 |
* Copyright: Copyright Digital Mars 2016. |
| 5 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 6 |
* Authors: Walter Bright, Sean Kelly, Jeremy DeHaan |
| 7 |
*/ |
| 8 |
|
| 9 |
/* Copyright Digital Mars 2016. |
| 10 |
* Distributed under the Boost Software License, Version 1.0. |
| 11 |
* (See accompanying file LICENSE or copy at |
| 12 |
* http://www.boost.org/LICENSE_1_0.txt) |
| 13 |
*/ |
| 14 |
module core.gc.gcinterface; |
| 15 |
|
| 16 |
static import core.memory; |
| 17 |
alias BlkAttr = core.memory.GC.BlkAttr; |
| 18 |
alias BlkInfo = core.memory.GC.BlkInfo; |
| 19 |
|
| 20 |
alias RootIterator = int delegate(scope int delegate(ref Root) nothrow dg); |
| 21 |
alias RangeIterator = int delegate(scope int delegate(ref Range) nothrow dg); |
| 22 |
|
| 23 |
|
| 24 |
struct Root |
| 25 |
{ |
| 26 |
void* proot; |
| 27 |
alias proot this; |
| 28 |
} |
| 29 |
|
| 30 |
struct Range |
| 31 |
{ |
| 32 |
void* pbot; |
| 33 |
void* ptop; |
| 34 |
TypeInfo ti; // should be tail const, but doesn't exist for references |
| 35 |
alias pbot this; // only consider pbot for relative ordering (opCmp) |
| 36 |
bool opEquals(const scope Range rhs) nothrow const { return pbot == rhs.pbot; } |
| 37 |
} |
| 38 |
|
| 39 |
interface GC |
| 40 |
{ |
| 41 |
/** |
| 42 |
* |
| 43 |
*/ |
| 44 |
void enable(); |
| 45 |
|
| 46 |
/** |
| 47 |
* |
| 48 |
*/ |
| 49 |
void disable(); |
| 50 |
|
| 51 |
/** |
| 52 |
* |
| 53 |
*/ |
| 54 |
void collect() nothrow; |
| 55 |
|
| 56 |
/** |
| 57 |
* minimize free space usage |
| 58 |
*/ |
| 59 |
void minimize() nothrow; |
| 60 |
|
| 61 |
/** |
| 62 |
* |
| 63 |
*/ |
| 64 |
uint getAttr(void* p) nothrow; |
| 65 |
|
| 66 |
/** |
| 67 |
* |
| 68 |
*/ |
| 69 |
uint setAttr(void* p, uint mask) nothrow; |
| 70 |
|
| 71 |
/** |
| 72 |
* |
| 73 |
*/ |
| 74 |
uint clrAttr(void* p, uint mask) nothrow; |
| 75 |
|
| 76 |
/** |
| 77 |
* |
| 78 |
*/ |
| 79 |
void* malloc(size_t size, uint bits, const TypeInfo ti) nothrow; |
| 80 |
|
| 81 |
/* |
| 82 |
* |
| 83 |
*/ |
| 84 |
BlkInfo qalloc(size_t size, uint bits, const scope TypeInfo ti) nothrow; |
| 85 |
|
| 86 |
/* |
| 87 |
* |
| 88 |
*/ |
| 89 |
void* calloc(size_t size, uint bits, const TypeInfo ti) nothrow; |
| 90 |
|
| 91 |
/* |
| 92 |
* |
| 93 |
*/ |
| 94 |
void* realloc(void* p, size_t size, uint bits, const TypeInfo ti) nothrow; |
| 95 |
|
| 96 |
/** |
| 97 |
* Attempt to in-place enlarge the memory block pointed to by p by at least |
| 98 |
* minsize bytes, up to a maximum of maxsize additional bytes. |
| 99 |
* This does not attempt to move the memory block (like realloc() does). |
| 100 |
* |
| 101 |
* Returns: |
| 102 |
* 0 if could not extend p, |
| 103 |
* total size of entire memory block if successful. |
| 104 |
*/ |
| 105 |
size_t extend(void* p, size_t minsize, size_t maxsize, const TypeInfo ti) nothrow; |
| 106 |
|
| 107 |
/** |
| 108 |
* |
| 109 |
*/ |
| 110 |
size_t reserve(size_t size) nothrow; |
| 111 |
|
| 112 |
/** |
| 113 |
* |
| 114 |
*/ |
| 115 |
void free(void* p) nothrow @nogc; |
| 116 |
|
| 117 |
/** |
| 118 |
* Determine the base address of the block containing p. If p is not a gc |
| 119 |
* allocated pointer, return null. |
| 120 |
*/ |
| 121 |
void* addrOf(void* p) nothrow @nogc; |
| 122 |
|
| 123 |
/** |
| 124 |
* Determine the allocated size of pointer p. If p is an interior pointer |
| 125 |
* or not a gc allocated pointer, return 0. |
| 126 |
*/ |
| 127 |
size_t sizeOf(void* p) nothrow @nogc; |
| 128 |
|
| 129 |
/** |
| 130 |
* Determine the base address of the block containing p. If p is not a gc |
| 131 |
* allocated pointer, return null. |
| 132 |
*/ |
| 133 |
BlkInfo query(void* p) nothrow; |
| 134 |
|
| 135 |
/** |
| 136 |
* Retrieve statistics about garbage collection. |
| 137 |
* Useful for debugging and tuning. |
| 138 |
*/ |
| 139 |
core.memory.GC.Stats stats() @safe nothrow @nogc; |
| 140 |
|
| 141 |
/** |
| 142 |
* Retrieve profile statistics about garbage collection. |
| 143 |
* Useful for debugging and tuning. |
| 144 |
*/ |
| 145 |
core.memory.GC.ProfileStats profileStats() @safe nothrow @nogc; |
| 146 |
|
| 147 |
/** |
| 148 |
* add p to list of roots |
| 149 |
*/ |
| 150 |
void addRoot(void* p) nothrow @nogc; |
| 151 |
|
| 152 |
/** |
| 153 |
* remove p from list of roots |
| 154 |
*/ |
| 155 |
void removeRoot(void* p) nothrow @nogc; |
| 156 |
|
| 157 |
/** |
| 158 |
* |
| 159 |
*/ |
| 160 |
@property RootIterator rootIter() @nogc; |
| 161 |
|
| 162 |
/** |
| 163 |
* add range to scan for roots |
| 164 |
*/ |
| 165 |
void addRange(void* p, size_t sz, const TypeInfo ti) nothrow @nogc; |
| 166 |
|
| 167 |
/** |
| 168 |
* remove range |
| 169 |
*/ |
| 170 |
void removeRange(void* p) nothrow @nogc; |
| 171 |
|
| 172 |
/** |
| 173 |
* |
| 174 |
*/ |
| 175 |
@property RangeIterator rangeIter() @nogc; |
| 176 |
|
| 177 |
/** |
| 178 |
* run finalizers |
| 179 |
*/ |
| 180 |
void runFinalizers(const scope void[] segment) nothrow; |
| 181 |
|
| 182 |
/* |
| 183 |
* |
| 184 |
*/ |
| 185 |
bool inFinalizer() nothrow @nogc @safe; |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns the number of bytes allocated for the current thread |
| 189 |
* since program start. It is the same as |
| 190 |
* GC.stats().allocatedInCurrentThread, but faster. |
| 191 |
*/ |
| 192 |
ulong allocatedInCurrentThread() nothrow; |
| 193 |
|
| 194 |
// ARRAY FUNCTIONS |
| 195 |
/** |
| 196 |
* Get the current used capacity of an array block. |
| 197 |
* |
| 198 |
* Note that this is only needed if you are about to change the array used |
| 199 |
* size and need to deal with the memory that is about to go away. For |
| 200 |
* appending or shrinking arrays that have no destructors, you probably |
| 201 |
* don't need this function. |
| 202 |
* |
| 203 |
* Params: |
| 204 |
* ptr = The pointer to check. This can be an interior pointer, but if it |
| 205 |
* is beyond the end of the used space, the return value may not be |
| 206 |
* valid. |
| 207 |
* atomic = The value is fetched atomically (for shared arrays) |
| 208 |
* Returns: |
| 209 |
* Current array slice, or null if the pointer does not point to a valid |
| 210 |
* appendable GC block. |
| 211 |
*/ |
| 212 |
void[] getArrayUsed(void *ptr, bool atomic = false) nothrow; |
| 213 |
|
| 214 |
/** |
| 215 |
* Expand the array used size in place. |
| 216 |
* |
| 217 |
* Used for appending and expanding the length of the array slice. If the |
| 218 |
* operation can be performed without reallocating, the function succeeds. |
| 219 |
* Newly expanded data is not initialized. Slices that do not point at |
| 220 |
* expandable GC blocks cannot be affected, and this function will always |
| 221 |
* return false. |
| 222 |
* |
| 223 |
* Params: |
| 224 |
* slice = the slice to attempt expanding in place. |
| 225 |
* newUsed = the size that should be stored as used. |
| 226 |
* atomic = if true, the array may be shared between threads, and this |
| 227 |
* operation should be done atomically. |
| 228 |
* Returns: true if successful. |
| 229 |
*/ |
| 230 |
bool expandArrayUsed(void[] slice, size_t newUsed, bool atomic = false) nothrow @safe; |
| 231 |
|
| 232 |
/** |
| 233 |
* Expand the array capacity in place. |
| 234 |
* |
| 235 |
* Used for reserving space that can be used for appending. If the |
| 236 |
* operation can be performed without reallocating, the function succeeds. |
| 237 |
* The used size is not changed. Slices that do not point at expandable GC |
| 238 |
* blocks cannot be affected, and this function will always return zero. |
| 239 |
* |
| 240 |
* Params: |
| 241 |
* slice = the slice to attempt reserving capacity for. |
| 242 |
* request = the requested size to expand to. Includes the existing data. |
| 243 |
* Passing a value less than the current array size will result in no |
| 244 |
* changes, but will return the current capacity. |
| 245 |
* atomic = The array may be shared between threads, and this operation |
| 246 |
* should be done atomically. |
| 247 |
* |
| 248 |
* Returns: |
| 249 |
* Resulting capacity size or 0 if the operation could not be performed. |
| 250 |
*/ |
| 251 |
size_t reserveArrayCapacity(void[] slice, size_t request, bool atomic = false) nothrow @safe; |
| 252 |
|
| 253 |
/** |
| 254 |
* Shrink used space of a slice in place. |
| 255 |
* |
| 256 |
* Unlike the other array functions, the array slice passed in is the |
| 257 |
* target slice, and the existing used space is passed separately. This is |
| 258 |
* to discourage code that ends up with a slice to dangling valid data. |
| 259 |
* |
| 260 |
* If slice.ptr[0 .. existingUsed] does not point to the end of a valid GC |
| 261 |
* appendable slice, then the operation fails. |
| 262 |
* |
| 263 |
* Params: |
| 264 |
* slice = The proposed valid slice data. |
| 265 |
* existingUsed = The amount of data in the block (starting at slice.ptr) |
| 266 |
* that is currently valid in the array. If this amount does not match |
| 267 |
* the current used size, the operation fails. |
| 268 |
* atomic = The slice may be shared between threads, and the operation |
| 269 |
* should be atomic. |
| 270 |
* Returns: true if successful. |
| 271 |
*/ |
| 272 |
bool shrinkArrayUsed(void[] slice, size_t existingUsed, bool atomic = false) nothrow; |
| 273 |
} |