2 * This module contains the garbage collector implementation.
4 * Copyright: Copyright (C) 2001-2007 Digital Mars, www.digitalmars.com.
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, in both source and binary form, subject to the following
16 * o The origin of this software must not be misrepresented; you must not
17 * claim that you wrote the original software. If you use this software
18 * in a product, an acknowledgment in the product documentation would be
19 * appreciated but is not required.
20 * o Altered source versions must be plainly marked as such, and must not
21 * be misrepresented as being the original software.
22 * o This notice may not be removed or altered from any source
24 * Authors: Walter Bright, David Friedman, Sean Kelly
29 // D Programming Language Garbage Collector implementation
31 /************** Debugging ***************************/
33 //debug = COLLECT_PRINTF; // turn on printf's
34 //debug = PTRCHECK; // more pointer checking
35 //debug = PTRCHECK2; // thorough but slow pointer checking
37 /*************** Configuration *********************/
39 version = STACKGROWSDOWN; // growing the stack means subtracting from the stack pointer
40 // (use for Intel X86 CPUs)
41 // else growing the stack means adding to the stack pointer
43 /***************************************************/
45 import rt.gc.cdgc.bits: GCBits;
46 import rt.gc.cdgc.stats: GCStats, Stats;
47 import dynarray = rt.gc.cdgc.dynarray;
48 import os = rt.gc.cdgc.os;
49 import opts = rt.gc.cdgc.opts;
51 import cstdlib = tango.stdc.stdlib;
52 import cstring = tango.stdc.string;
55 * This is a small optimization that proved it's usefulness. For small chunks
56 * or memory memset() seems to be slower (probably because of the call) that
57 * simply doing a simple loop to set the memory.
59 void memset(void* dst, int c, size_t n)
61 // This number (32) has been determined empirically
63 cstring.memset(dst, c, n);
66 auto p = cast(ubyte*)(dst);
73 // BUG: The following import will likely not work, since the gcc
74 // subdirectory is elsewhere. Instead, perhaps the functions
75 // could be declared directly or some other resolution could
77 static import gcc.builtins; // for __builtin_unwind_int
87 package enum BlkAttr : uint
89 FINALIZE = 0b0000_0001,
90 NO_SCAN = 0b0000_0010,
91 NO_MOVE = 0b0000_0100,
92 ALL_BITS = 0b1111_1111
95 package bool has_pointermap(uint attrs)
97 return !opts.options.conservative && !(attrs & BlkAttr.NO_SCAN);
102 alias void delegate(Object) DEvent;
103 alias void delegate( void*, void* ) scanFn;
104 enum { OPFAIL = ~cast(size_t)0 }
108 version (DigitalMars) version(OSX)
109 oid _d_osx_image_init();
111 void* rt_stackBottom();
113 void rt_finalize( void* p, bool det = true );
114 void rt_attachDisposeEvent(Object h, DEvent e);
115 bool rt_detachDisposeEvent(Object h, DEvent e);
116 void rt_scanStaticData( scanFn scan );
119 bool thread_needLock();
120 void thread_suspendAll();
121 void thread_resumeAll();
122 void thread_scanAll( scanFn fn, void* curStackTop = null );
124 void onOutOfMemoryError();
132 POOLSIZE = (4096*256),
146 B_PAGE, // start of large alloc
147 B_PAGEPLUS, // continuation of large alloc
166 int opCmp(in Range other)
168 if (pbot < other.pbot)
171 return cast(int)(pbot > other.pbot);
176 const uint binsize[B_MAX] = [ 16,32,64,128,256,512,1024,2048,4096 ];
177 const uint notbinsize[B_MAX] = [ ~(16u-1),~(32u-1),~(64u-1),~(128u-1),~(256u-1),
178 ~(512u-1),~(1024u-1),~(2048u-1),~(4096u-1) ];
181 /* ============================ GC =============================== */
184 class GCLock {} // just a dummy so we can get a global lock
195 // !=0 means don't scan stack
200 /// Turn off collections if > 0
203 /// min(pool.baseAddr)
205 /// max(pool.topAddr)
208 /// Free list for each size
209 List*[B_MAX] free_list;
211 dynarray.DynArray!(void*) roots;
212 dynarray.DynArray!(Range) ranges;
213 dynarray.DynArray!(Pool) pools;
218 // call locked if necessary
219 private T locked(T, alias Code)()
221 if (thread_needLock())
222 synchronized (gc.lock) return Code();
231 assert (gc !is null);
233 for (size_t i = 0; i < gc.pools.length; i++) {
234 Pool* pool = gc.pools[i];
237 assert(gc.min_addr == pool.baseAddr);
238 if (i + 1 < gc.pools.length)
239 assert(*pool < gc.pools[i + 1]);
240 else if (i + 1 == gc.pools.length)
241 assert(gc.max_addr == pool.topAddr);
244 gc.roots.Invariant();
245 gc.ranges.Invariant();
247 for (size_t i = 0; i < gc.ranges.length; i++) {
248 assert(gc.ranges[i].pbot);
249 assert(gc.ranges[i].ptop);
250 assert(gc.ranges[i].pbot <= gc.ranges[i].ptop);
253 for (size_t i = 0; i < B_PAGE; i++)
254 for (List *list = gc.free_list[i]; list; list = list.next)
263 * Find Pool that pointer is in.
264 * Return null if not in a Pool.
265 * Assume pools is sorted.
267 Pool* findPool(void* p)
269 if (p < gc.min_addr || p >= gc.max_addr)
271 if (gc.pools.length == 0)
273 if (gc.pools.length == 1)
275 /// The pooltable[] is sorted by address, so do a binary search
277 size_t high = gc.pools.length - 1;
278 while (low <= high) {
279 size_t mid = (low + high) / 2;
280 auto pool = gc.pools[mid];
281 if (p < pool.baseAddr)
283 else if (p >= pool.topAddr)
294 * Determine the base address of the block containing p. If p is not a gc
295 * allocated pointer, return null.
297 BlkInfo getInfo(void* p)
300 Pool* pool = findPool(p);
304 info.base = pool.findBase(p);
305 info.size = pool.findSize(info.base);
306 info.attr = getAttr(pool, cast(size_t)(info.base - pool.baseAddr) / 16u);
307 if (has_pointermap(info.attr)) {
308 info.size -= size_t.sizeof; // PointerMap bitmask
309 // Points to the PointerMap bitmask pointer, not user data
310 if (p >= (info.base + info.size)) {
314 if (opts.options.sentinel) {
315 info.base = sentinel_add(info.base);
316 // points to sentinel data, not user data
317 if (p < info.base || p >= sentinel_post(info.base))
319 info.size -= SENTINEL_EXTRA;
326 * Compute bin for size.
328 Bins findBin(size_t size)
372 * Allocate a new pool of at least size bytes.
373 * Sort it into pools.
374 * Mark all memory in the pool as B_FREE.
375 * Return the actual number of bytes reserved or 0 on error.
377 size_t reserve(size_t size)
380 size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
381 Pool* pool = newPool(npages);
385 return pool.npages * PAGESIZE;
390 * Minimizes physical memory usage by returning free pools to the OS.
398 for (n = 0; n < gc.pools.length; n++)
401 for (pn = 0; pn < pool.npages; pn++)
403 if (cast(Bins)pool.pagetable[pn] != B_FREE)
406 if (pn < pool.npages)
409 gc.pools.remove_at(n);
412 gc.min_addr = gc.pools[0].baseAddr;
413 gc.max_addr = gc.pools[gc.pools.length - 1].topAddr;
418 * Allocate a chunk of memory that is larger than a page.
419 * Return null if out of memory.
421 void *bigAlloc(size_t size)
431 npages = (size + PAGESIZE - 1) / PAGESIZE;
435 // This code could use some refinement when repeatedly
436 // allocating very large arrays.
438 for (n = 0; n < gc.pools.length; n++)
441 pn = pool.allocPages(npages);
456 freedpages = fullcollectshell();
457 if (freedpages >= gc.pools.length * ((POOLSIZE / PAGESIZE) / 4))
462 // Release empty pools to prevent bloat
465 pool = newPool(npages);
471 pn = pool.allocPages(npages);
472 assert(pn != OPFAIL);
475 // Release empty pools to prevent bloat
478 pool = newPool(npages);
481 pn = pool.allocPages(npages);
482 assert(pn != OPFAIL);
492 pool.pagetable[pn] = B_PAGE;
494 memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1);
495 p = pool.baseAddr + pn * PAGESIZE;
496 memset(cast(char *)p + size, 0, npages * PAGESIZE - size);
497 if (opts.options.mem_stomp)
498 memset(p, 0xF1, size);
502 return null; // let mallocNoSync handle the error
507 * Allocate a new pool with at least npages in it.
508 * Sort it into pools.
509 * Return null if failed.
511 Pool *newPool(size_t npages)
513 // Minimum of POOLSIZE
514 if (npages < POOLSIZE/PAGESIZE)
515 npages = POOLSIZE/PAGESIZE;
516 else if (npages > POOLSIZE/PAGESIZE)
518 // Give us 150% of requested size, so there's room to extend
519 auto n = npages + (npages >> 1);
520 if (n < size_t.max/PAGESIZE)
524 // Allocate successively larger pools up to 8 megs
527 size_t n = gc.pools.length;
529 n = 8; // cap pool size at 8 megs
530 n *= (POOLSIZE / PAGESIZE);
536 p.initialize(npages);
543 Pool* pool = gc.pools.insert_sorted(p);
546 gc.min_addr = gc.pools[0].baseAddr;
547 gc.max_addr = gc.pools[gc.pools.length - 1].topAddr;
554 * Allocate a page of bin's.
558 int allocPage(Bins bin)
566 for (n = 0; n < gc.pools.length; n++)
569 pn = pool.allocPages(1);
576 pool.pagetable[pn] = cast(ubyte)bin;
578 // Convert page to free list
579 size_t size = binsize[bin];
580 List **b = &gc.free_list[bin];
582 p = pool.baseAddr + pn * PAGESIZE;
584 for (; p < ptop; p += size)
586 (cast(List *)p).next = *b;
594 * Search a range of memory values and mark any pointers into the GC pool using
595 * type information (bitmask of pointer locations).
597 void mark_range(void *pbot, void *ptop, size_t* pm_bitmask)
599 // TODO: make our own assert because assert uses the GC
600 assert (pbot <= ptop);
602 const BITS_PER_WORD = size_t.sizeof * 8;
604 void **p1 = cast(void **)pbot;
605 void **p2 = cast(void **)ptop;
609 size_t type_size = pm_bitmask[0];
610 size_t* pm_bits = pm_bitmask + 1;
611 bool has_type_info = type_size != 1 || pm_bits[0] != 1 || pm_bits[1] != 0;
613 //printf("marking range: %p -> %p\n", pbot, ptop);
614 for (; p1 + type_size <= p2; p1 += type_size) {
615 for (size_t n = 0; n < type_size; n++) {
616 // scan bit set for this word
618 !(pm_bits[n / BITS_PER_WORD] & (1 << (n % BITS_PER_WORD))))
623 if (p < gc.min_addr || p >= gc.max_addr)
626 if ((cast(size_t)p & ~(PAGESIZE-1)) == pcache)
629 Pool* pool = findPool(p);
632 size_t offset = cast(size_t)(p - pool.baseAddr);
634 size_t pn = offset / PAGESIZE;
635 Bins bin = cast(Bins)pool.pagetable[pn];
637 // Adjust bit to be at start of allocated memory block
639 bit_i = (offset & notbinsize[bin]) >> 4;
640 else if (bin == B_PAGEPLUS)
646 while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
647 bit_i = pn * (PAGESIZE / 16);
651 // Don't mark bits in B_FREE pages
655 if (bin >= B_PAGE) // Cache B_PAGE and B_PAGEPLUS lookups
656 pcache = cast(size_t)p & ~(PAGESIZE-1);
658 if (!pool.mark.test(bit_i))
660 pool.mark.set(bit_i);
661 if (!pool.noscan.test(bit_i))
663 pool.scan.set(bit_i);
671 gc.any_changes = true;
675 * Return number of full pages free'd.
677 size_t fullcollectshell()
679 gc.stats.collection_started();
681 gc.stats.collection_finished();
683 // The purpose of the 'shell' is to ensure all the registers
684 // get put on the stack so they'll be scanned
689 gcc.builtins.__builtin_unwind_init();
696 uint eax,ecx,edx,ebx,ebp,esi,edi;
709 else version (X86_64)
711 ulong rax,rbx,rcx,rdx,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15;
734 static assert( false, "Architecture not supported." );
745 result = fullcollect(sp);
768 size_t fullcollect(void *stackTop)
770 debug(COLLECT_PRINTF) printf("Gcx.fullcollect()\n");
772 // we always need to stop the world to make threads save the CPU registers
773 // in the stack and prepare themselves for thread_scanAll()
775 gc.stats.world_stopped();
777 if (opts.options.fork) {
778 os.pid_t child_pid = os.fork();
779 assert (child_pid != -1); // don't accept errors in non-release mode
781 case -1: // if fork() fails, fallback to stop-the-world
782 opts.options.fork = false;
784 case 0: // child process (i.e. the collectors mark phase)
787 break; // bogus, will never reach here
788 default: // parent process (i.e. the mutator)
789 // start the world again and wait for the mark phase to finish
791 gc.stats.world_started();
793 os.pid_t wait_pid = os.waitpid(child_pid, &status, 0);
794 assert (wait_pid == child_pid);
800 // if we reach here, we are using the standard stop-the-world collection
803 gc.stats.world_started();
812 void mark(void *stackTop)
814 debug(COLLECT_PRINTF) printf("\tmark()\n");
819 gc.any_changes = false;
820 for (size_t n = 0; n < gc.pools.length; n++)
822 Pool* pool = gc.pools[n];
825 pool.freebits.zero();
828 // Mark each free entry, so it doesn't get scanned
829 for (size_t n = 0; n < B_PAGE; n++)
831 for (List *list = gc.free_list[n]; list; list = list.next)
833 Pool* pool = findPool(list);
835 pool.freebits.set(cast(size_t)(cast(byte*)list - pool.baseAddr) / 16);
839 for (size_t n = 0; n < gc.pools.length; n++)
841 Pool* pool = gc.pools[n];
842 pool.mark.copy(&pool.freebits);
845 /// Marks a range of memory in conservative mode.
846 void mark_conservative_range(void* pbot, void* ptop)
848 mark_range(pbot, ptop, PointerMap.init.bits.ptr);
851 rt_scanStaticData(&mark_conservative_range);
855 // Scan stacks and registers for each paused thread
856 thread_scanAll(&mark_conservative_range, stackTop);
860 debug(COLLECT_PRINTF) printf("scan roots[]\n");
861 mark_conservative_range(gc.roots.ptr, gc.roots.ptr + gc.roots.length);
864 debug(COLLECT_PRINTF) printf("scan ranges[]\n");
865 for (size_t n = 0; n < gc.ranges.length; n++)
867 debug(COLLECT_PRINTF) printf("\t%x .. %x\n", gc.ranges[n].pbot, gc.ranges[n].ptop);
868 mark_conservative_range(gc.ranges[n].pbot, gc.ranges[n].ptop);
871 debug(COLLECT_PRINTF) printf("\tscan heap\n");
872 while (gc.any_changes)
874 gc.any_changes = false;
875 for (size_t n = 0; n < gc.pools.length; n++)
881 Pool* pool = gc.pools[n];
883 bbase = pool.scan.base();
884 btop = bbase + pool.scan.nwords;
885 for (b = bbase; b < btop;)
901 o = pool.baseAddr + (b - bbase) * 32 * 16;
902 if (!(bitm & 0xFFFF))
907 for (; bitm; o += 16, bitm >>= 1)
912 pn = cast(size_t)(o - pool.baseAddr) / PAGESIZE;
913 bin = cast(Bins)pool.pagetable[pn];
915 if (opts.options.conservative)
916 mark_conservative_range(o, o + binsize[bin]);
918 auto end_of_blk = cast(size_t**)(o +
919 binsize[bin] - size_t.sizeof);
920 size_t* pm_bitmask = *end_of_blk;
921 mark_range(o, end_of_blk, pm_bitmask);
924 else if (bin == B_PAGE || bin == B_PAGEPLUS)
926 if (bin == B_PAGEPLUS)
928 while (pool.pagetable[pn - 1] != B_PAGE)
932 while (pn + u < pool.npages &&
933 pool.pagetable[pn + u] == B_PAGEPLUS)
936 size_t blk_size = u * PAGESIZE;
937 if (opts.options.conservative)
938 mark_conservative_range(o, o + blk_size);
940 auto end_of_blk = cast(size_t**)(o + blk_size -
942 size_t* pm_bitmask = *end_of_blk;
943 mark_range(o, end_of_blk, pm_bitmask);
958 // Free up everything not marked
959 debug(COLLECT_PRINTF) printf("\tsweep\n");
960 size_t freedpages = 0;
962 for (size_t n = 0; n < gc.pools.length; n++)
964 Pool* pool = gc.pools[n];
966 uint* bbase = pool.mark.base();
968 for (pn = 0; pn < pool.npages; pn++, bbase += PAGESIZE / (32 * 16))
970 Bins bin = cast(Bins)pool.pagetable[pn];
974 auto size = binsize[bin];
975 byte* p = pool.baseAddr + pn * PAGESIZE;
976 byte* ptop = p + PAGESIZE;
977 size_t bit_i = pn * (PAGESIZE/16);
978 size_t bit_stride = size / 16;
980 version(none) // BUG: doesn't work because freebits() must also be cleared
982 // If free'd entire page
983 if (bbase[0] == 0 && bbase[1] == 0 && bbase[2] == 0 &&
984 bbase[3] == 0 && bbase[4] == 0 && bbase[5] == 0 &&
985 bbase[6] == 0 && bbase[7] == 0)
987 for (; p < ptop; p += size, bit_i += bit_stride)
989 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
990 if (opts.options.sentinel)
991 rt_finalize(cast(List *)sentinel_add(p), false/*gc.no_stack > 0*/);
993 rt_finalize(cast(List *)p, false/*gc.no_stack > 0*/);
995 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
997 List *list = cast(List *)p;
999 if (opts.options.mem_stomp)
1000 memset(p, 0xF3, size);
1002 pool.pagetable[pn] = B_FREE;
1007 for (; p < ptop; p += size, bit_i += bit_stride)
1009 if (!pool.mark.test(bit_i))
1011 if (opts.options.sentinel)
1012 sentinel_Invariant(sentinel_add(p));
1014 pool.freebits.set(bit_i);
1015 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
1016 if (opts.options.sentinel)
1017 rt_finalize(cast(List *)sentinel_add(p), false/*gc.no_stack > 0*/);
1019 rt_finalize(cast(List *)p, false/*gc.no_stack > 0*/);
1021 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1023 List *list = cast(List *)p;
1025 if (opts.options.mem_stomp)
1026 memset(p, 0xF3, size);
1032 else if (bin == B_PAGE)
1034 size_t bit_i = pn * (PAGESIZE / 16);
1035 if (!pool.mark.test(bit_i))
1037 byte *p = pool.baseAddr + pn * PAGESIZE;
1038 if (opts.options.sentinel)
1039 sentinel_Invariant(sentinel_add(p));
1040 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
1041 if (opts.options.sentinel)
1042 rt_finalize(sentinel_add(p), false/*gc.no_stack > 0*/);
1044 rt_finalize(p, false/*gc.no_stack > 0*/);
1046 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1048 debug(COLLECT_PRINTF) printf("\tcollecting big %x\n", p);
1049 pool.pagetable[pn] = B_FREE;
1051 if (opts.options.mem_stomp)
1052 memset(p, 0xF3, PAGESIZE);
1053 while (pn + 1 < pool.npages && pool.pagetable[pn + 1] == B_PAGEPLUS)
1056 pool.pagetable[pn] = B_FREE;
1059 if (opts.options.mem_stomp)
1062 memset(p, 0xF3, PAGESIZE);
1071 gc.free_list[] = null;
1073 // Free complete pages, rebuild free list
1074 debug(COLLECT_PRINTF) printf("\tfree complete pages\n");
1075 size_t recoveredpages = 0;
1076 for (size_t n = 0; n < gc.pools.length; n++)
1078 Pool* pool = gc.pools[n];
1079 for (size_t pn = 0; pn < pool.npages; pn++)
1081 Bins bin = cast(Bins)pool.pagetable[pn];
1087 size_t size = binsize[bin];
1088 size_t bit_stride = size / 16;
1089 size_t bit_base = pn * (PAGESIZE / 16);
1090 size_t bit_top = bit_base + (PAGESIZE / 16);
1094 for (; bit_i < bit_top; bit_i += bit_stride)
1096 if (!pool.freebits.test(bit_i))
1099 pool.pagetable[pn] = B_FREE;
1104 p = pool.baseAddr + pn * PAGESIZE;
1105 for (u = 0; u < PAGESIZE; u += size)
1107 bit_i = bit_base + u / 16;
1108 if (pool.freebits.test(bit_i))
1110 List *list = cast(List *)(p + u);
1111 // avoid unnecessary writes
1112 if (list.next != gc.free_list[bin])
1113 list.next = gc.free_list[bin];
1114 gc.free_list[bin] = list;
1121 debug(COLLECT_PRINTF) printf("recovered pages = %d\n", recoveredpages);
1122 debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, gc.pools.length);
1124 return freedpages + recoveredpages;
1131 uint getAttr(Pool* pool, size_t bit_i)
1140 if (pool.finals.nbits &&
1141 pool.finals.test(bit_i))
1142 attrs |= BlkAttr.FINALIZE;
1143 if (pool.noscan.test(bit_i))
1144 attrs |= BlkAttr.NO_SCAN;
1145 // if (pool.nomove.nbits &&
1146 // pool.nomove.test(bit_i))
1147 // attrs |= BlkAttr.NO_MOVE;
1155 void setAttr(Pool* pool, size_t bit_i, uint mask)
1162 if (mask & BlkAttr.FINALIZE)
1164 if (!pool.finals.nbits)
1165 pool.finals.alloc(pool.mark.nbits);
1166 pool.finals.set(bit_i);
1168 if (mask & BlkAttr.NO_SCAN)
1170 pool.noscan.set(bit_i);
1172 // if (mask & BlkAttr.NO_MOVE)
1174 // if (!pool.nomove.nbits)
1175 // pool.nomove.alloc(pool.mark.nbits);
1176 // pool.nomove.set(bit_i);
1184 void clrAttr(Pool* pool, size_t bit_i, uint mask)
1191 if (mask & BlkAttr.FINALIZE && pool.finals.nbits)
1192 pool.finals.clear(bit_i);
1193 if (mask & BlkAttr.NO_SCAN)
1194 pool.noscan.clear(bit_i);
1195 // if (mask & BlkAttr.NO_MOVE && pool.nomove.nbits)
1196 // pool.nomove.clear(bit_i);
1204 gc.stack_bottom = cast(char*)&dummy;
1205 opts.parse(cstdlib.getenv("D_GC_OPTS"));
1206 // If we are going to fork, make sure we have the needed OS support
1207 if (opts.options.fork)
1208 opts.options.fork = os.HAVE_SHARED && os.HAVE_FORK;
1209 gc.lock = GCLock.classinfo;
1211 setStackBottom(rt_stackBottom());
1212 gc.stats = Stats(gc);
1219 private void *malloc(size_t size, uint attrs, size_t* pm_bitmask)
1223 gc.stats.malloc_started(size, attrs, pm_bitmask);
1225 gc.stats.malloc_finished(p);
1230 if (opts.options.sentinel)
1231 size += SENTINEL_EXTRA;
1233 bool has_pm = has_pointermap(attrs);
1235 size += size_t.sizeof;
1238 // Cache previous binsize lookup - Dave Fladebo.
1239 static size_t lastsize = -1;
1240 static Bins lastbin;
1241 if (size == lastsize)
1245 bin = findBin(size);
1250 size_t capacity; // to figure out where to store the bitmask
1253 p = gc.free_list[bin];
1256 if (!allocPage(bin) && !gc.disabled) // try to find a new page
1258 if (!thread_needLock())
1260 /* Then we haven't locked it yet. Be sure
1261 * and gc.lock for a collection, since a finalizer
1262 * may start a new thread.
1264 synchronized (gc.lock)
1269 else if (!fullcollectshell()) // collect to find a new page
1274 if (!gc.free_list[bin] && !allocPage(bin))
1276 newPool(1); // allocate new pool to find a new page
1277 int result = allocPage(bin);
1279 onOutOfMemoryError();
1281 p = gc.free_list[bin];
1283 capacity = binsize[bin];
1285 // Return next item from free list
1286 gc.free_list[bin] = (cast(List*)p).next;
1287 if (!(attrs & BlkAttr.NO_SCAN))
1288 memset(p + size, 0, capacity - size);
1289 if (opts.options.mem_stomp)
1290 memset(p, 0xF0, size);
1296 onOutOfMemoryError();
1297 // Round the size up to the number of pages needed to store it
1298 size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
1299 capacity = npages * PAGESIZE;
1302 // Store the bit mask AFTER SENTINEL_POST
1303 // TODO: store it BEFORE, so the bitmask is protected too
1305 auto end_of_blk = cast(size_t**)(p + capacity - size_t.sizeof);
1306 *end_of_blk = pm_bitmask;
1307 size -= size_t.sizeof;
1310 if (opts.options.sentinel) {
1311 size -= SENTINEL_EXTRA;
1312 p = sentinel_add(p);
1313 sentinel_init(p, size);
1318 Pool *pool = findPool(p);
1321 setAttr(pool, cast(size_t)(p - pool.baseAddr) / 16, attrs);
1330 private void *calloc(size_t size, uint attrs, size_t* pm_bitmask)
1334 void *p = malloc(size, attrs, pm_bitmask);
1343 private void *realloc(void *p, size_t size, uint attrs,
1356 p = malloc(size, attrs, pm_bitmask);
1360 Pool* pool = findPool(p);
1364 // Set or retrieve attributes as appropriate
1365 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1367 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1368 setAttr(pool, bit_i, attrs);
1371 attrs = getAttr(pool, bit_i);
1373 void* blk_base_addr = pool.findBase(p);
1374 size_t blk_size = pool.findSize(p);
1375 bool has_pm = has_pointermap(attrs);
1376 size_t pm_bitmask_size = 0;
1378 pm_bitmask_size = size_t.sizeof;
1379 // Retrieve pointer map bit mask if appropriate
1380 if (pm_bitmask is null) {
1381 auto end_of_blk = cast(size_t**)(blk_base_addr +
1382 blk_size - size_t.sizeof);
1383 pm_bitmask = *end_of_blk;
1387 if (opts.options.sentinel)
1389 sentinel_Invariant(p);
1390 size_t sentinel_stored_size = *sentinel_size(p);
1391 if (sentinel_stored_size != size)
1393 void* p2 = malloc(size, attrs, pm_bitmask);
1394 if (sentinel_stored_size < size)
1395 size = sentinel_stored_size;
1396 cstring.memcpy(p2, p, size);
1402 size += pm_bitmask_size;
1403 if (blk_size >= PAGESIZE && size >= PAGESIZE)
1405 auto psz = blk_size / PAGESIZE;
1406 auto newsz = (size + PAGESIZE - 1) / PAGESIZE;
1410 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
1415 if (opts.options.mem_stomp)
1416 memset(p + size - pm_bitmask_size, 0xF2,
1417 blk_size - size - pm_bitmask_size);
1418 pool.freePages(pagenum + newsz, psz - newsz);
1420 auto end_of_blk = cast(size_t**)(
1421 blk_base_addr + (PAGESIZE * newsz) -
1423 *end_of_blk = pm_bitmask;
1427 else if (pagenum + newsz <= pool.npages)
1429 // Attempt to expand in place
1430 for (size_t i = pagenum + psz; 1;)
1432 if (i == pagenum + newsz)
1434 if (opts.options.mem_stomp)
1435 memset(p + blk_size - pm_bitmask_size,
1436 0xF0, size - blk_size
1438 memset(pool.pagetable + pagenum +
1439 psz, B_PAGEPLUS, newsz - psz);
1441 auto end_of_blk = cast(size_t**)(
1443 (PAGESIZE * newsz) -
1445 *end_of_blk = pm_bitmask;
1449 if (i == pool.npages)
1453 if (pool.pagetable[i] != B_FREE)
1459 // if new size is bigger or less than half
1460 if (blk_size < size || blk_size > size * 2)
1462 size -= pm_bitmask_size;
1463 blk_size -= pm_bitmask_size;
1464 void* p2 = malloc(size, attrs, pm_bitmask);
1465 if (blk_size < size)
1467 cstring.memcpy(p2, p, size);
1477 * Attempt to in-place enlarge the memory block pointed to by p by at least
1478 * min_size beyond its current capacity, up to a maximum of max_size. This
1479 * does not attempt to move the memory block (like realloc() does).
1482 * 0 if could not extend p,
1483 * total size of entire memory block if successful.
1485 private size_t extend(void* p, size_t minsize, size_t maxsize)
1488 assert( minsize <= maxsize );
1492 if (opts.options.sentinel)
1495 Pool* pool = findPool(p);
1499 // Retrieve attributes
1500 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1501 uint attrs = getAttr(pool, bit_i);
1503 void* blk_base_addr = pool.findBase(p);
1504 size_t blk_size = pool.findSize(p);
1505 bool has_pm = has_pointermap(attrs);
1506 size_t* pm_bitmask = null;
1507 size_t pm_bitmask_size = 0;
1509 pm_bitmask_size = size_t.sizeof;
1510 // Retrieve pointer map bit mask
1511 auto end_of_blk = cast(size_t**)(blk_base_addr +
1512 blk_size - size_t.sizeof);
1513 pm_bitmask = *end_of_blk;
1515 minsize += size_t.sizeof;
1516 maxsize += size_t.sizeof;
1519 if (blk_size < PAGESIZE)
1520 return 0; // cannot extend buckets
1522 auto psz = blk_size / PAGESIZE;
1523 auto minsz = (minsize + PAGESIZE - 1) / PAGESIZE;
1524 auto maxsz = (maxsize + PAGESIZE - 1) / PAGESIZE;
1526 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
1529 for (sz = 0; sz < maxsz; sz++)
1531 auto i = pagenum + psz + sz;
1532 if (i == pool.npages)
1534 if (pool.pagetable[i] != B_FREE)
1544 size_t new_size = (psz + sz) * PAGESIZE;
1546 if (opts.options.mem_stomp)
1547 memset(p + blk_size - pm_bitmask_size, 0xF0,
1548 new_size - blk_size - pm_bitmask_size);
1549 memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz);
1554 new_size -= size_t.sizeof;
1555 auto end_of_blk = cast(size_t**)(blk_base_addr + new_size);
1556 *end_of_blk = pm_bitmask;
1565 private void free(void *p)
1574 // Find which page it is in
1576 if (!pool) // if not one of ours
1578 if (opts.options.sentinel) {
1579 sentinel_Invariant(p);
1580 p = sentinel_sub(p);
1582 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1583 bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1584 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1586 bin = cast(Bins)pool.pagetable[pagenum];
1587 if (bin == B_PAGE) // if large alloc
1592 while (++n < pool.npages && pool.pagetable[n] == B_PAGEPLUS)
1594 if (opts.options.mem_stomp)
1595 memset(p, 0xF2, npages * PAGESIZE);
1596 pool.freePages(pagenum, npages);
1601 List *list = cast(List*)p;
1603 if (opts.options.mem_stomp)
1604 memset(p, 0xF2, binsize[bin]);
1606 list.next = gc.free_list[bin];
1607 gc.free_list[bin] = list;
1613 * Determine the allocated size of pointer p. If p is an interior pointer
1614 * or not a gc allocated pointer, return 0.
1616 private size_t sizeOf(void *p)
1620 if (opts.options.sentinel)
1621 p = sentinel_sub(p);
1623 Pool* pool = findPool(p);
1627 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
1628 uint attrs = getAttr(pool, biti);
1630 size_t size = pool.findSize(p);
1631 size_t pm_bitmask_size = 0;
1632 if (has_pointermap(attrs))
1633 pm_bitmask_size = size_t.sizeof;
1635 if (opts.options.sentinel) {
1636 // Check for interior pointer
1638 // 1) size is a power of 2 for less than PAGESIZE values
1639 // 2) base of memory pool is aligned on PAGESIZE boundary
1640 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
1642 return size - SENTINEL_EXTRA - pm_bitmask_size;
1645 if (p == gc.p_cache)
1646 return gc.size_cache;
1648 // Check for interior pointer
1650 // 1) size is a power of 2 for less than PAGESIZE values
1651 // 2) base of memory pool is aligned on PAGESIZE boundary
1652 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
1656 gc.size_cache = size - pm_bitmask_size;
1658 return gc.size_cache;
1664 * Verify that pointer p:
1665 * 1) belongs to this memory pool
1666 * 2) points to the start of an allocated piece of memory
1667 * 3) is not on a free list
1669 private void checkNoSync(void *p)
1673 if (opts.options.sentinel)
1674 sentinel_Invariant(p);
1682 if (opts.options.sentinel)
1683 p = sentinel_sub(p);
1686 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1687 bin = cast(Bins)pool.pagetable[pagenum];
1688 assert(bin <= B_PAGE);
1689 size = binsize[bin];
1690 assert((cast(size_t)p & (size - 1)) == 0);
1696 // Check that p is not on a free list
1699 for (list = gc.free_list[bin]; list; list = list.next)
1701 assert(cast(void*)list != p);
1712 private void setStackBottom(void *p)
1714 version (STACKGROWSDOWN)
1716 //p = (void *)((uint *)p + 4);
1717 if (p > gc.stack_bottom)
1719 gc.stack_bottom = p;
1724 //p = (void *)((uint *)p - 4);
1725 if (p < gc.stack_bottom)
1727 gc.stack_bottom = cast(char*)p;
1734 * Retrieve statistics about garbage collection.
1735 * Useful for debugging and tuning.
1737 private GCStats getStats()
1747 for (n = 0; n < gc.pools.length; n++)
1749 Pool* pool = gc.pools[n];
1750 psize += pool.npages * PAGESIZE;
1751 for (size_t j = 0; j < pool.npages; j++)
1753 Bins bin = cast(Bins)pool.pagetable[j];
1756 else if (bin == B_PAGE)
1758 else if (bin < B_PAGE)
1763 for (n = 0; n < B_PAGE; n++)
1765 for (List *list = gc.free_list[n]; list; list = list.next)
1766 flsize += binsize[n];
1769 usize = bsize - flsize;
1771 stats.poolsize = psize;
1772 stats.usedsize = bsize - flsize;
1773 stats.freelistsize = flsize;
1777 /******************* weak-reference support *********************/
1779 private struct WeakPointer
1783 void ondestroy(Object r)
1785 assert(r is reference);
1786 // lock for memory consistency (parallel readers)
1787 // also ensures that weakpointerDestroy can be called while another
1788 // thread is freeing the reference with "delete"
1789 return locked!(void, () {
1796 * Create a weak pointer to the given object.
1797 * Returns a pointer to an opaque struct allocated in C memory.
1799 void* weakpointerCreate( Object r )
1803 // must be allocated in C memory
1804 // 1. to hide the reference from the GC
1805 // 2. the GC doesn't scan delegates added by rt_attachDisposeEvent
1807 auto wp = cast(WeakPointer*)(cstdlib.malloc(WeakPointer.sizeof));
1809 onOutOfMemoryError();
1811 rt_attachDisposeEvent(r, &wp.ondestroy);
1818 * Destroy a weak pointer returned by weakpointerCreate().
1819 * If null is passed, nothing happens.
1821 void weakpointerDestroy( void* p )
1825 auto wp = cast(WeakPointer*)p;
1826 // must be extra careful about the GC or parallel threads
1827 // finalizing the reference at the same time
1828 return locked!(void, () {
1830 rt_detachDisposeEvent(wp.reference, &wp.ondestroy);
1837 * Query a weak pointer and return either the object passed to
1838 * weakpointerCreate, or null if it was free'd in the meantime.
1839 * If null is passed, null is returned.
1841 Object weakpointerGet( void* p )
1845 // NOTE: could avoid the lock by using Fawzi style GC counters but
1846 // that'd require core.sync.Atomic and lots of care about memory
1847 // consistency it's an optional optimization see
1848 // http://dsource.org/projects/tango/browser/trunk/user/tango/core/Lifetime.d?rev=5100#L158
1849 return locked!(Object, () {
1850 return (cast(WeakPointer*)p).reference;
1856 /* ============================ Pool =============================== */
1863 GCBits mark; // entries already scanned, or should not be scanned
1864 GCBits scan; // entries that need to be scanned
1865 GCBits freebits; // entries that are on the free list
1866 GCBits finals; // entries that need finalizer run on them
1867 GCBits noscan; // entries that should not be scanned
1872 /// Cache for findSize()
1878 this.cached_ptr = null;
1879 this.cached_size = 0;
1882 void initialize(size_t npages)
1884 size_t poolsize = npages * PAGESIZE;
1885 assert(poolsize >= POOLSIZE);
1886 baseAddr = cast(byte *) os.alloc(poolsize);
1888 // Some of the code depends on page alignment of memory pools
1889 assert((cast(size_t)baseAddr & (PAGESIZE - 1)) == 0);
1897 topAddr = baseAddr + poolsize;
1899 size_t nbits = cast(size_t)poolsize / 16;
1901 // if the GC will run in parallel in a fork()ed process, we need to
1902 // share the mark bits
1903 os.Vis vis = os.Vis.PRIV;
1904 if (opts.options.fork)
1905 vis = os.Vis.SHARED;
1906 mark.alloc(nbits, vis); // shared between mark and sweep
1907 freebits.alloc(nbits, vis); // ditto
1908 scan.alloc(nbits); // only used in the mark phase
1909 finals.alloc(nbits); // mark phase *MUST* have a snapshot
1910 noscan.alloc(nbits); // ditto
1912 pagetable = cast(ubyte*) cstdlib.malloc(npages);
1914 onOutOfMemoryError();
1915 memset(pagetable, B_FREE, npages);
1917 this.npages = npages;
1929 result = os.dealloc(baseAddr, npages * PAGESIZE);
1937 // See Gcx.Dtor() for the rationale of the null check.
1939 cstdlib.free(pagetable);
1941 os.Vis vis = os.Vis.PRIV;
1942 if (opts.options.fork)
1943 vis = os.Vis.SHARED;
1962 //freebits.Invariant();
1963 //finals.Invariant();
1964 //noscan.Invariant();
1968 //if (baseAddr + npages * PAGESIZE != topAddr)
1969 //printf("baseAddr = %p, npages = %d, topAddr = %p\n", baseAddr, npages, topAddr);
1970 assert(baseAddr + npages * PAGESIZE == topAddr);
1973 for (size_t i = 0; i < npages; i++)
1975 Bins bin = cast(Bins)pagetable[i];
1976 assert(bin < B_MAX);
1982 * Allocate n pages from Pool.
1983 * Returns OPFAIL on failure.
1985 size_t allocPages(size_t n)
1991 for (i = 0; i < npages; i++)
1993 if (pagetable[i] == B_FREE)
2006 * Free npages pages starting with pagenum.
2008 void freePages(size_t pagenum, size_t npages)
2010 memset(&pagetable[pagenum], B_FREE, npages);
2015 * Find base address of block containing pointer p.
2016 * Returns null if the pointer doesn't belong to this pool
2018 void* findBase(void *p)
2020 size_t offset = cast(size_t)(p - this.baseAddr);
2021 size_t pagenum = offset / PAGESIZE;
2022 Bins bin = cast(Bins)this.pagetable[pagenum];
2023 // Adjust bit to be at start of allocated memory block
2025 return this.baseAddr + (offset & notbinsize[bin]);
2026 if (bin == B_PAGEPLUS) {
2028 --pagenum, offset -= PAGESIZE;
2029 } while (cast(Bins)this.pagetable[pagenum] == B_PAGEPLUS);
2030 return this.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
2032 // we are in a B_FREE page
2038 * Find size of pointer p.
2039 * Returns 0 if p doesn't belong to this pool if if it's block size is less
2042 size_t findSize(void *p)
2044 size_t pagenum = cast(size_t)(p - this.baseAddr) / PAGESIZE;
2045 Bins bin = cast(Bins)this.pagetable[pagenum];
2047 return binsize[bin];
2048 if (this.cached_ptr == p)
2049 return this.cached_size;
2050 size_t i = pagenum + 1;
2051 for (; i < this.npages; i++)
2052 if (this.pagetable[i] != B_PAGEPLUS)
2054 this.cached_ptr = p;
2055 this.cached_size = (i - pagenum) * PAGESIZE;
2056 return this.cached_size;
2061 * Used for sorting pools
2063 int opCmp(in Pool other)
2065 if (baseAddr < other.baseAddr)
2068 return cast(int)(baseAddr > other.baseAddr);
2073 /* ============================ SENTINEL =============================== */
2076 const size_t SENTINEL_PRE = cast(size_t) 0xF4F4F4F4F4F4F4F4UL; // 32 or 64 bits
2077 const ubyte SENTINEL_POST = 0xF5; // 8 bits
2078 const uint SENTINEL_EXTRA = 2 * size_t.sizeof + 1;
2081 size_t* sentinel_size(void *p) { return &(cast(size_t *)p)[-2]; }
2082 size_t* sentinel_pre(void *p) { return &(cast(size_t *)p)[-1]; }
2083 ubyte* sentinel_post(void *p) { return &(cast(ubyte *)p)[*sentinel_size(p)]; }
2086 void sentinel_init(void *p, size_t size)
2088 *sentinel_size(p) = size;
2089 *sentinel_pre(p) = SENTINEL_PRE;
2090 *sentinel_post(p) = SENTINEL_POST;
2094 void sentinel_Invariant(void *p)
2096 if (*sentinel_pre(p) != SENTINEL_PRE ||
2097 *sentinel_post(p) != SENTINEL_POST)
2102 void *sentinel_add(void *p)
2104 return p + 2 * size_t.sizeof;
2108 void *sentinel_sub(void *p)
2110 return p - 2 * size_t.sizeof;
2115 /* ============================ C Public Interface ======================== */
2118 private int _termCleanupLevel=1;
2122 /// sets the cleanup level done by gc
2125 /// 2: fullCollect ignoring stack roots (might crash daemonThreads)
2126 /// result !=0 if the value was invalid
2127 int gc_setTermCleanupLevel(int cLevel)
2129 if (cLevel<0 || cLevel>2) return cLevel;
2130 _termCleanupLevel=cLevel;
2134 /// returns the cleanup level done by gc
2135 int gc_getTermCleanupLevel()
2137 return _termCleanupLevel;
2142 scope (exit) assert (Invariant());
2143 gc = cast(GC*) cstdlib.calloc(1, GC.sizeof);
2146 version (DigitalMars) version(OSX) {
2147 _d_osx_image_init();
2149 // NOTE: The GC must initialize the thread library
2150 // before its first collection.
2156 assert (Invariant());
2157 if (_termCleanupLevel<1) {
2159 } else if (_termCleanupLevel==2){
2160 // a more complete cleanup
2161 // NOTE: There may be daemons threads still running when this routine is
2162 // called. If so, cleaning memory out from under then is a good
2163 // way to make them crash horribly.
2164 // Often this probably doesn't matter much since the app is
2165 // supposed to be shutting down anyway, but for example tests might
2166 // crash (and be considerd failed even if the test was ok).
2167 // thus this is not the default and should be enabled by
2168 // I'm disabling cleanup for now until I can think about it some
2171 // not really a 'collect all' -- still scans static data area, roots,
2173 return locked!(void, () {
2179 // default (safe) clenup
2180 return locked!(void, () {
2188 return locked!(void, () {
2189 assert (Invariant()); scope (exit) assert (Invariant());
2190 assert (gc.disabled > 0);
2197 return locked!(void, () {
2198 assert (Invariant()); scope (exit) assert (Invariant());
2205 return locked!(void, () {
2206 assert (Invariant()); scope (exit) assert (Invariant());
2214 return locked!(void, () {
2215 assert (Invariant()); scope (exit) assert (Invariant());
2220 uint gc_getAttr(void* p)
2224 return locked!(uint, () {
2225 assert (Invariant()); scope (exit) assert (Invariant());
2226 Pool* pool = findPool(p);
2229 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
2230 return getAttr(pool, bit_i);
2234 uint gc_setAttr(void* p, uint attrs)
2238 return locked!(uint, () {
2239 assert (Invariant()); scope (exit) assert (Invariant());
2240 Pool* pool = findPool(p);
2243 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
2244 uint old_attrs = getAttr(pool, bit_i);
2245 setAttr(pool, bit_i, attrs);
2250 uint gc_clrAttr(void* p, uint attrs)
2254 return locked!(uint, () {
2255 assert (Invariant()); scope (exit) assert (Invariant());
2256 Pool* pool = findPool(p);
2259 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
2260 uint old_attrs = getAttr(pool, bit_i);
2261 clrAttr(pool, bit_i, attrs);
2266 void* gc_malloc(size_t size, uint attrs = 0,
2267 PointerMap ptrmap = PointerMap.init)
2271 return locked!(void*, () {
2272 assert (Invariant()); scope (exit) assert (Invariant());
2273 return malloc(size, attrs, ptrmap.bits.ptr);
2277 void* gc_calloc(size_t size, uint attrs = 0,
2278 PointerMap ptrmap = PointerMap.init)
2282 return locked!(void*, () {
2283 assert (Invariant()); scope (exit) assert (Invariant());
2284 return calloc(size, attrs, ptrmap.bits.ptr);
2288 void* gc_realloc(void* p, size_t size, uint attrs = 0,
2289 PointerMap ptrmap = PointerMap.init)
2291 return locked!(void*, () {
2292 assert (Invariant()); scope (exit) assert (Invariant());
2293 return realloc(p, size, attrs, ptrmap.bits.ptr);
2297 size_t gc_extend(void* p, size_t min_size, size_t max_size)
2299 return locked!(size_t, () {
2300 assert (Invariant()); scope (exit) assert (Invariant());
2301 return extend(p, min_size, max_size);
2305 size_t gc_reserve(size_t size)
2309 return locked!(size_t, () {
2310 assert (Invariant()); scope (exit) assert (Invariant());
2311 return reserve(size);
2315 void gc_free(void* p)
2319 return locked!(void, () {
2320 assert (Invariant()); scope (exit) assert (Invariant());
2325 void* gc_addrOf(void* p)
2329 return locked!(void*, () {
2330 assert (Invariant()); scope (exit) assert (Invariant());
2331 Pool* pool = findPool(p);
2334 return pool.findBase(p);
2338 size_t gc_sizeOf(void* p)
2342 return locked!(size_t, () {
2343 assert (Invariant()); scope (exit) assert (Invariant());
2348 BlkInfo gc_query(void* p)
2351 return BlkInfo.init;
2352 return locked!(BlkInfo, () {
2353 assert (Invariant()); scope (exit) assert (Invariant());
2358 // NOTE: This routine is experimental. The stats or function name may change
2359 // before it is made officially available.
2362 return locked!(GCStats, () {
2363 assert (Invariant()); scope (exit) assert (Invariant());
2368 void gc_addRoot(void* p)
2372 return locked!(void, () {
2373 assert (Invariant()); scope (exit) assert (Invariant());
2374 if (gc.roots.append(p) is null)
2375 onOutOfMemoryError();
2379 void gc_addRange(void* p, size_t size)
2381 if (p is null || size == 0)
2383 return locked!(void, () {
2384 assert (Invariant()); scope (exit) assert (Invariant());
2385 if (gc.ranges.append(Range(p, p + size)) is null)
2386 onOutOfMemoryError();
2390 void gc_removeRoot(void* p)
2394 return locked!(void, () {
2395 assert (Invariant()); scope (exit) assert (Invariant());
2396 bool r = gc.roots.remove(p);
2401 void gc_removeRange(void* p)
2405 return locked!(void, () {
2406 assert (Invariant()); scope (exit) assert (Invariant());
2407 bool r = gc.ranges.remove(Range(p, null));
2412 void* gc_weakpointerCreate(Object r)
2414 // weakpointers do their own locking
2415 return weakpointerCreate(r);
2418 void gc_weakpointerDestroy(void* wp)
2420 // weakpointers do their own locking
2421 weakpointerDestroy(wp);
2424 Object gc_weakpointerGet(void* wp)
2426 // weakpointers do their own locking
2427 return weakpointerGet(wp);
2431 // vim: set et sw=4 sts=4 :