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
167 int opCmp(in Range other)
169 if (pbot < other.pbot)
172 return cast(int)(pbot > other.pbot);
177 const uint binsize[B_MAX] = [ 16,32,64,128,256,512,1024,2048,4096 ];
178 const uint notbinsize[B_MAX] = [ ~(16u-1),~(32u-1),~(64u-1),~(128u-1),~(256u-1),
179 ~(512u-1),~(1024u-1),~(2048u-1),~(4096u-1) ];
182 /* ============================ GC =============================== */
185 class GCLock {} // just a dummy so we can get a global lock
196 // !=0 means don't scan stack
201 /// Turn off collections if > 0
204 /// min(pool.baseAddr)
206 /// max(pool.topAddr)
209 /// Free list for each size
210 List*[B_MAX] free_list;
212 dynarray.DynArray!(void*) roots;
213 dynarray.DynArray!(Range) ranges;
214 dynarray.DynArray!(Pool*) pools;
219 // call locked if necessary
220 private T locked(T, alias Code)()
222 if (thread_needLock())
223 synchronized (gc.lock) return Code();
232 assert (gc !is null);
234 for (size_t i = 0; i < gc.pools.length; i++) {
235 Pool* pool = gc.pools[i];
238 assert(gc.min_addr == pool.baseAddr);
239 if (i + 1 < gc.pools.length)
240 assert(*pool < *gc.pools[i + 1]);
241 else if (i + 1 == gc.pools.length)
242 assert(gc.max_addr == pool.topAddr);
245 gc.roots.Invariant();
246 gc.ranges.Invariant();
248 for (size_t i = 0; i < gc.ranges.length; i++) {
249 assert(gc.ranges[i].pbot);
250 assert(gc.ranges[i].ptop);
251 assert(gc.ranges[i].pbot <= gc.ranges[i].ptop);
254 for (size_t i = 0; i < B_PAGE; i++) {
255 for (List *list = gc.free_list[i]; list; list = list.next) {
256 assert (list.pool !is null);
257 auto p = cast(byte*) list;
258 assert (p >= list.pool.baseAddr);
259 assert (p < list.pool.topAddr);
268 * Find Pool that pointer is in.
269 * Return null if not in a Pool.
270 * Assume pools is sorted.
272 Pool* findPool(void* p)
274 if (p < gc.min_addr || p >= gc.max_addr)
276 if (gc.pools.length == 0)
278 if (gc.pools.length == 1)
280 /// The pooltable[] is sorted by address, so do a binary search
282 size_t high = gc.pools.length - 1;
283 while (low <= high) {
284 size_t mid = (low + high) / 2;
285 auto pool = gc.pools[mid];
286 if (p < pool.baseAddr)
288 else if (p >= pool.topAddr)
299 * Determine the base address of the block containing p. If p is not a gc
300 * allocated pointer, return null.
302 BlkInfo getInfo(void* p)
305 Pool* pool = findPool(p);
309 info.base = pool.findBase(p);
310 info.size = pool.findSize(info.base);
311 info.attr = getAttr(pool, cast(size_t)(info.base - pool.baseAddr) / 16u);
312 if (has_pointermap(info.attr)) {
313 info.size -= size_t.sizeof; // PointerMap bitmask
314 // Points to the PointerMap bitmask pointer, not user data
315 if (p >= (info.base + info.size)) {
319 if (opts.options.sentinel) {
320 info.base = sentinel_add(info.base);
321 // points to sentinel data, not user data
322 if (p < info.base || p >= sentinel_post(info.base))
324 info.size -= SENTINEL_EXTRA;
331 * Compute bin for size.
333 Bins findBin(size_t size)
377 * Allocate a new pool of at least size bytes.
378 * Sort it into pools.
379 * Mark all memory in the pool as B_FREE.
380 * Return the actual number of bytes reserved or 0 on error.
382 size_t reserve(size_t size)
385 size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
386 Pool* pool = newPool(npages);
390 return pool.npages * PAGESIZE;
395 * Minimizes physical memory usage by returning free pools to the OS.
403 for (n = 0; n < gc.pools.length; n++)
406 for (pn = 0; pn < pool.npages; pn++)
408 if (cast(Bins)pool.pagetable[pn] != B_FREE)
411 if (pn < pool.npages)
415 gc.pools.remove_at(n);
418 gc.min_addr = gc.pools[0].baseAddr;
419 gc.max_addr = gc.pools[gc.pools.length - 1].topAddr;
424 * Allocate a chunk of memory that is larger than a page.
425 * Return null if out of memory.
427 void* bigAlloc(size_t size, out Pool* pool)
436 npages = (size + PAGESIZE - 1) / PAGESIZE;
440 // This code could use some refinement when repeatedly
441 // allocating very large arrays.
443 for (n = 0; n < gc.pools.length; n++)
446 pn = pool.allocPages(npages);
461 freedpages = fullcollectshell();
462 if (freedpages >= gc.pools.length * ((POOLSIZE / PAGESIZE) / 4))
467 // Release empty pools to prevent bloat
470 pool = newPool(npages);
476 pn = pool.allocPages(npages);
477 assert(pn != OPFAIL);
480 // Release empty pools to prevent bloat
483 pool = newPool(npages);
486 pn = pool.allocPages(npages);
487 assert(pn != OPFAIL);
497 pool.pagetable[pn] = B_PAGE;
499 memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1);
500 p = pool.baseAddr + pn * PAGESIZE;
501 memset(cast(char *)p + size, 0, npages * PAGESIZE - size);
502 if (opts.options.mem_stomp)
503 memset(p, 0xF1, size);
507 return null; // let mallocNoSync handle the error
512 * Allocate a new pool with at least npages in it.
513 * Sort it into pools.
514 * Return null if failed.
516 Pool *newPool(size_t npages)
518 // Minimum of POOLSIZE
519 if (npages < POOLSIZE/PAGESIZE)
520 npages = POOLSIZE/PAGESIZE;
521 else if (npages > POOLSIZE/PAGESIZE)
523 // Give us 150% of requested size, so there's room to extend
524 auto n = npages + (npages >> 1);
525 if (n < size_t.max/PAGESIZE)
529 // Allocate successively larger pools up to 8 megs
532 size_t n = gc.pools.length;
534 n = 8; // cap pool size at 8 megs
535 n *= (POOLSIZE / PAGESIZE);
540 auto pool = cast(Pool*) cstdlib.calloc(1, Pool.sizeof);
543 pool.initialize(npages);
550 auto inserted_pool = *gc.pools.insert_sorted!("*a < *b")(pool);
551 if (inserted_pool is null) {
555 assert (inserted_pool is pool);
556 gc.min_addr = gc.pools[0].baseAddr;
557 gc.max_addr = gc.pools[gc.pools.length - 1].topAddr;
563 * Allocate a page of bin's.
567 int allocPage(Bins bin)
575 for (n = 0; n < gc.pools.length; n++)
578 pn = pool.allocPages(1);
585 pool.pagetable[pn] = cast(ubyte)bin;
587 // Convert page to free list
588 size_t size = binsize[bin];
589 auto list_head = &gc.free_list[bin];
591 p = pool.baseAddr + pn * PAGESIZE;
593 for (; p < ptop; p += size)
595 List* l = cast(List *) p;
605 * Search a range of memory values and mark any pointers into the GC pool using
606 * type information (bitmask of pointer locations).
608 void mark_range(void *pbot, void *ptop, size_t* pm_bitmask)
610 // TODO: make our own assert because assert uses the GC
611 assert (pbot <= ptop);
613 const BITS_PER_WORD = size_t.sizeof * 8;
615 void **p1 = cast(void **)pbot;
616 void **p2 = cast(void **)ptop;
620 size_t type_size = pm_bitmask[0];
621 size_t* pm_bits = pm_bitmask + 1;
622 bool has_type_info = type_size != 1 || pm_bits[0] != 1 || pm_bits[1] != 0;
624 //printf("marking range: %p -> %p\n", pbot, ptop);
625 for (; p1 + type_size <= p2; p1 += type_size) {
626 for (size_t n = 0; n < type_size; n++) {
627 // scan bit set for this word
629 !(pm_bits[n / BITS_PER_WORD] & (1 << (n % BITS_PER_WORD))))
634 if (p < gc.min_addr || p >= gc.max_addr)
637 if ((cast(size_t)p & ~(PAGESIZE-1)) == pcache)
640 Pool* pool = findPool(p);
643 size_t offset = cast(size_t)(p - pool.baseAddr);
645 size_t pn = offset / PAGESIZE;
646 Bins bin = cast(Bins)pool.pagetable[pn];
648 // Cache B_PAGE, B_PAGEPLUS and B_FREE lookups
650 pcache = cast(size_t)p & ~(PAGESIZE-1);
652 // Adjust bit to be at start of allocated memory block
654 bit_i = (offset & notbinsize[bin]) >> 4;
655 else if (bin == B_PAGEPLUS)
661 while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
662 bit_i = pn * (PAGESIZE / 16);
664 else // Don't mark bits in B_FREE pages
667 if (!pool.mark.test(bit_i))
669 pool.mark.set(bit_i);
670 if (!pool.noscan.test(bit_i))
672 pool.scan.set(bit_i);
680 gc.any_changes = true;
684 * Return number of full pages free'd.
686 size_t fullcollectshell()
688 gc.stats.collection_started();
690 gc.stats.collection_finished();
692 // The purpose of the 'shell' is to ensure all the registers
693 // get put on the stack so they'll be scanned
698 gcc.builtins.__builtin_unwind_init();
705 uint eax,ecx,edx,ebx,ebp,esi,edi;
718 else version (X86_64)
720 ulong rax,rbx,rcx,rdx,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15;
743 static assert( false, "Architecture not supported." );
754 result = fullcollect(sp);
777 size_t fullcollect(void *stackTop)
779 debug(COLLECT_PRINTF) printf("Gcx.fullcollect()\n");
781 // we always need to stop the world to make threads save the CPU registers
782 // in the stack and prepare themselves for thread_scanAll()
784 gc.stats.world_stopped();
786 if (opts.options.fork) {
787 os.pid_t child_pid = os.fork();
788 assert (child_pid != -1); // don't accept errors in non-release mode
790 case -1: // if fork() fails, fallback to stop-the-world
791 opts.options.fork = false;
793 case 0: // child process (i.e. the collectors mark phase)
796 break; // bogus, will never reach here
797 default: // parent process (i.e. the mutator)
798 // start the world again and wait for the mark phase to finish
800 gc.stats.world_started();
802 os.pid_t wait_pid = os.waitpid(child_pid, &status, 0);
803 assert (wait_pid == child_pid);
809 // if we reach here, we are using the standard stop-the-world collection
812 gc.stats.world_started();
821 void mark(void *stackTop)
823 debug(COLLECT_PRINTF) printf("\tmark()\n");
828 gc.any_changes = false;
829 for (size_t n = 0; n < gc.pools.length; n++)
831 Pool* pool = gc.pools[n];
834 pool.freebits.zero();
837 // Mark each free entry, so it doesn't get scanned
838 for (size_t n = 0; n < B_PAGE; n++)
840 for (List *list = gc.free_list[n]; list; list = list.next)
842 Pool* pool = list.pool;
843 auto ptr = cast(byte*) list;
845 assert (pool.baseAddr <= ptr);
846 assert (ptr < pool.topAddr);
847 size_t bit_i = cast(size_t)(ptr - pool.baseAddr) / 16;
848 pool.freebits.set(bit_i);
852 for (size_t n = 0; n < gc.pools.length; n++)
854 Pool* pool = gc.pools[n];
855 pool.mark.copy(&pool.freebits);
858 /// Marks a range of memory in conservative mode.
859 void mark_conservative_range(void* pbot, void* ptop)
861 mark_range(pbot, ptop, PointerMap.init.bits.ptr);
864 rt_scanStaticData(&mark_conservative_range);
868 // Scan stacks and registers for each paused thread
869 thread_scanAll(&mark_conservative_range, stackTop);
873 debug(COLLECT_PRINTF) printf("scan roots[]\n");
874 mark_conservative_range(gc.roots.ptr, gc.roots.ptr + gc.roots.length);
877 debug(COLLECT_PRINTF) printf("scan ranges[]\n");
878 for (size_t n = 0; n < gc.ranges.length; n++)
880 debug(COLLECT_PRINTF) printf("\t%x .. %x\n", gc.ranges[n].pbot, gc.ranges[n].ptop);
881 mark_conservative_range(gc.ranges[n].pbot, gc.ranges[n].ptop);
884 debug(COLLECT_PRINTF) printf("\tscan heap\n");
885 while (gc.any_changes)
887 gc.any_changes = false;
888 for (size_t n = 0; n < gc.pools.length; n++)
894 Pool* pool = gc.pools[n];
896 bbase = pool.scan.base();
897 btop = bbase + pool.scan.nwords;
898 for (b = bbase; b < btop;)
914 o = pool.baseAddr + (b - bbase) * 32 * 16;
915 if (!(bitm & 0xFFFF))
920 for (; bitm; o += 16, bitm >>= 1)
925 pn = cast(size_t)(o - pool.baseAddr) / PAGESIZE;
926 bin = cast(Bins)pool.pagetable[pn];
928 if (opts.options.conservative)
929 mark_conservative_range(o, o + binsize[bin]);
931 auto end_of_blk = cast(size_t**)(o +
932 binsize[bin] - size_t.sizeof);
933 size_t* pm_bitmask = *end_of_blk;
934 mark_range(o, end_of_blk, pm_bitmask);
937 else if (bin == B_PAGE || bin == B_PAGEPLUS)
939 if (bin == B_PAGEPLUS)
941 while (pool.pagetable[pn - 1] != B_PAGE)
945 while (pn + u < pool.npages &&
946 pool.pagetable[pn + u] == B_PAGEPLUS)
949 size_t blk_size = u * PAGESIZE;
950 if (opts.options.conservative)
951 mark_conservative_range(o, o + blk_size);
953 auto end_of_blk = cast(size_t**)(o + blk_size -
955 size_t* pm_bitmask = *end_of_blk;
956 mark_range(o, end_of_blk, pm_bitmask);
971 // Free up everything not marked
972 debug(COLLECT_PRINTF) printf("\tsweep\n");
973 size_t freedpages = 0;
975 for (size_t n = 0; n < gc.pools.length; n++)
977 Pool* pool = gc.pools[n];
979 uint* bbase = pool.mark.base();
981 for (pn = 0; pn < pool.npages; pn++, bbase += PAGESIZE / (32 * 16))
983 Bins bin = cast(Bins)pool.pagetable[pn];
987 auto size = binsize[bin];
988 byte* p = pool.baseAddr + pn * PAGESIZE;
989 byte* ptop = p + PAGESIZE;
990 size_t bit_i = pn * (PAGESIZE/16);
991 size_t bit_stride = size / 16;
993 version(none) // BUG: doesn't work because freebits() must also be cleared
995 // If free'd entire page
996 if (bbase[0] == 0 && bbase[1] == 0 && bbase[2] == 0 &&
997 bbase[3] == 0 && bbase[4] == 0 && bbase[5] == 0 &&
998 bbase[6] == 0 && bbase[7] == 0)
1000 for (; p < ptop; p += size, bit_i += bit_stride)
1002 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
1003 if (opts.options.sentinel)
1004 rt_finalize(sentinel_add(p), false/*gc.no_stack > 0*/);
1006 rt_finalize(p, false/*gc.no_stack > 0*/);
1008 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1010 if (opts.options.mem_stomp)
1011 memset(p, 0xF3, size);
1013 pool.pagetable[pn] = B_FREE;
1018 for (; p < ptop; p += size, bit_i += bit_stride)
1020 if (!pool.mark.test(bit_i))
1022 if (opts.options.sentinel)
1023 sentinel_Invariant(sentinel_add(p));
1025 pool.freebits.set(bit_i);
1026 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
1027 if (opts.options.sentinel)
1028 rt_finalize(sentinel_add(p), false/*gc.no_stack > 0*/);
1030 rt_finalize(p, false/*gc.no_stack > 0*/);
1032 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1034 if (opts.options.mem_stomp)
1035 memset(p, 0xF3, size);
1041 else if (bin == B_PAGE)
1043 size_t bit_i = pn * (PAGESIZE / 16);
1044 if (!pool.mark.test(bit_i))
1046 byte *p = pool.baseAddr + pn * PAGESIZE;
1047 if (opts.options.sentinel)
1048 sentinel_Invariant(sentinel_add(p));
1049 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
1050 if (opts.options.sentinel)
1051 rt_finalize(sentinel_add(p), false/*gc.no_stack > 0*/);
1053 rt_finalize(p, false/*gc.no_stack > 0*/);
1055 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1057 debug(COLLECT_PRINTF) printf("\tcollecting big %x\n", p);
1058 pool.pagetable[pn] = B_FREE;
1060 if (opts.options.mem_stomp)
1061 memset(p, 0xF3, PAGESIZE);
1062 while (pn + 1 < pool.npages && pool.pagetable[pn + 1] == B_PAGEPLUS)
1065 pool.pagetable[pn] = B_FREE;
1068 if (opts.options.mem_stomp)
1071 memset(p, 0xF3, PAGESIZE);
1080 gc.free_list[] = null;
1082 // Free complete pages, rebuild free list
1083 debug(COLLECT_PRINTF) printf("\tfree complete pages\n");
1084 size_t recoveredpages = 0;
1085 for (size_t n = 0; n < gc.pools.length; n++)
1087 Pool* pool = gc.pools[n];
1088 for (size_t pn = 0; pn < pool.npages; pn++)
1090 Bins bin = cast(Bins)pool.pagetable[pn];
1096 size_t size = binsize[bin];
1097 size_t bit_stride = size / 16;
1098 size_t bit_base = pn * (PAGESIZE / 16);
1099 size_t bit_top = bit_base + (PAGESIZE / 16);
1103 for (; bit_i < bit_top; bit_i += bit_stride)
1105 if (!pool.freebits.test(bit_i))
1108 pool.pagetable[pn] = B_FREE;
1113 p = pool.baseAddr + pn * PAGESIZE;
1114 for (u = 0; u < PAGESIZE; u += size)
1116 bit_i = bit_base + u / 16;
1117 if (pool.freebits.test(bit_i))
1119 assert ((p+u) >= pool.baseAddr);
1120 assert ((p+u) < pool.topAddr);
1121 List* list = cast(List*) (p + u);
1122 // avoid unnecesary writes (it really saves time)
1123 if (list.next != gc.free_list[bin])
1124 list.next = gc.free_list[bin];
1125 if (list.pool != pool)
1127 gc.free_list[bin] = list;
1134 debug(COLLECT_PRINTF) printf("recovered pages = %d\n", recoveredpages);
1135 debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, gc.pools.length);
1137 return freedpages + recoveredpages;
1144 uint getAttr(Pool* pool, size_t bit_i)
1153 if (pool.finals.nbits &&
1154 pool.finals.test(bit_i))
1155 attrs |= BlkAttr.FINALIZE;
1156 if (pool.noscan.test(bit_i))
1157 attrs |= BlkAttr.NO_SCAN;
1158 // if (pool.nomove.nbits &&
1159 // pool.nomove.test(bit_i))
1160 // attrs |= BlkAttr.NO_MOVE;
1168 void setAttr(Pool* pool, size_t bit_i, uint mask)
1175 if (mask & BlkAttr.FINALIZE)
1177 if (!pool.finals.nbits)
1178 pool.finals.alloc(pool.mark.nbits);
1179 pool.finals.set(bit_i);
1181 if (mask & BlkAttr.NO_SCAN)
1183 pool.noscan.set(bit_i);
1185 // if (mask & BlkAttr.NO_MOVE)
1187 // if (!pool.nomove.nbits)
1188 // pool.nomove.alloc(pool.mark.nbits);
1189 // pool.nomove.set(bit_i);
1197 void clrAttr(Pool* pool, size_t bit_i, uint mask)
1204 if (mask & BlkAttr.FINALIZE && pool.finals.nbits)
1205 pool.finals.clear(bit_i);
1206 if (mask & BlkAttr.NO_SCAN)
1207 pool.noscan.clear(bit_i);
1208 // if (mask & BlkAttr.NO_MOVE && pool.nomove.nbits)
1209 // pool.nomove.clear(bit_i);
1217 gc.stack_bottom = cast(char*)&dummy;
1218 opts.parse(cstdlib.getenv("D_GC_OPTS"));
1219 // If we are going to fork, make sure we have the needed OS support
1220 if (opts.options.fork)
1221 opts.options.fork = os.HAVE_SHARED && os.HAVE_FORK;
1222 gc.lock = GCLock.classinfo;
1224 setStackBottom(rt_stackBottom());
1225 gc.stats = Stats(gc);
1232 private void *malloc(size_t size, uint attrs, size_t* pm_bitmask)
1236 gc.stats.malloc_started(size, attrs, pm_bitmask);
1238 gc.stats.malloc_finished(p);
1243 if (opts.options.sentinel)
1244 size += SENTINEL_EXTRA;
1246 bool has_pm = has_pointermap(attrs);
1248 size += size_t.sizeof;
1251 // Cache previous binsize lookup - Dave Fladebo.
1252 static size_t lastsize = -1;
1253 static Bins lastbin;
1254 if (size == lastsize)
1258 bin = findBin(size);
1264 size_t capacity = void; // to figure out where to store the bitmask
1267 p = gc.free_list[bin];
1270 if (!allocPage(bin) && !gc.disabled) // try to find a new page
1272 if (!thread_needLock())
1274 /* Then we haven't locked it yet. Be sure
1275 * and gc.lock for a collection, since a finalizer
1276 * may start a new thread.
1278 synchronized (gc.lock)
1283 else if (!fullcollectshell()) // collect to find a new page
1288 if (!gc.free_list[bin] && !allocPage(bin))
1290 newPool(1); // allocate new pool to find a new page
1291 // TODO: hint allocPage() to use the pool we just created
1292 int result = allocPage(bin);
1294 onOutOfMemoryError();
1296 p = gc.free_list[bin];
1298 capacity = binsize[bin];
1300 // Return next item from free list
1301 List* list = cast(List*) p;
1302 assert ((cast(byte*)list) >= list.pool.baseAddr);
1303 assert ((cast(byte*)list) < list.pool.topAddr);
1304 gc.free_list[bin] = list.next;
1306 if (!(attrs & BlkAttr.NO_SCAN))
1307 memset(p + size, 0, capacity - size);
1308 if (opts.options.mem_stomp)
1309 memset(p, 0xF0, size);
1313 p = bigAlloc(size, pool);
1315 onOutOfMemoryError();
1316 assert (pool !is null);
1317 // Round the size up to the number of pages needed to store it
1318 size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
1319 capacity = npages * PAGESIZE;
1322 // Store the bit mask AFTER SENTINEL_POST
1323 // TODO: store it BEFORE, so the bitmask is protected too
1325 auto end_of_blk = cast(size_t**)(p + capacity - size_t.sizeof);
1326 *end_of_blk = pm_bitmask;
1327 size -= size_t.sizeof;
1330 if (opts.options.sentinel) {
1331 size -= SENTINEL_EXTRA;
1332 p = sentinel_add(p);
1333 sentinel_init(p, size);
1337 setAttr(pool, cast(size_t)(p - pool.baseAddr) / 16, attrs);
1346 private void *calloc(size_t size, uint attrs, size_t* pm_bitmask)
1350 void *p = malloc(size, attrs, pm_bitmask);
1359 private void *realloc(void *p, size_t size, uint attrs,
1372 p = malloc(size, attrs, pm_bitmask);
1376 Pool* pool = findPool(p);
1380 // Set or retrieve attributes as appropriate
1381 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1383 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1384 setAttr(pool, bit_i, attrs);
1387 attrs = getAttr(pool, bit_i);
1389 void* blk_base_addr = pool.findBase(p);
1390 size_t blk_size = pool.findSize(p);
1391 bool has_pm = has_pointermap(attrs);
1392 size_t pm_bitmask_size = 0;
1394 pm_bitmask_size = size_t.sizeof;
1395 // Retrieve pointer map bit mask if appropriate
1396 if (pm_bitmask is null) {
1397 auto end_of_blk = cast(size_t**)(blk_base_addr +
1398 blk_size - size_t.sizeof);
1399 pm_bitmask = *end_of_blk;
1403 if (opts.options.sentinel)
1405 sentinel_Invariant(p);
1406 size_t sentinel_stored_size = *sentinel_size(p);
1407 if (sentinel_stored_size != size)
1409 void* p2 = malloc(size, attrs, pm_bitmask);
1410 if (sentinel_stored_size < size)
1411 size = sentinel_stored_size;
1412 cstring.memcpy(p2, p, size);
1418 size += pm_bitmask_size;
1419 if (blk_size >= PAGESIZE && size >= PAGESIZE)
1421 auto psz = blk_size / PAGESIZE;
1422 auto newsz = (size + PAGESIZE - 1) / PAGESIZE;
1426 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
1431 if (opts.options.mem_stomp)
1432 memset(p + size - pm_bitmask_size, 0xF2,
1433 blk_size - size - pm_bitmask_size);
1434 pool.freePages(pagenum + newsz, psz - newsz);
1436 auto end_of_blk = cast(size_t**)(
1437 blk_base_addr + (PAGESIZE * newsz) -
1439 *end_of_blk = pm_bitmask;
1443 else if (pagenum + newsz <= pool.npages)
1445 // Attempt to expand in place
1446 for (size_t i = pagenum + psz; 1;)
1448 if (i == pagenum + newsz)
1450 if (opts.options.mem_stomp)
1451 memset(p + blk_size - pm_bitmask_size,
1452 0xF0, size - blk_size
1454 memset(pool.pagetable + pagenum +
1455 psz, B_PAGEPLUS, newsz - psz);
1457 auto end_of_blk = cast(size_t**)(
1459 (PAGESIZE * newsz) -
1461 *end_of_blk = pm_bitmask;
1465 if (i == pool.npages)
1469 if (pool.pagetable[i] != B_FREE)
1475 // if new size is bigger or less than half
1476 if (blk_size < size || blk_size > size * 2)
1478 size -= pm_bitmask_size;
1479 blk_size -= pm_bitmask_size;
1480 void* p2 = malloc(size, attrs, pm_bitmask);
1481 if (blk_size < size)
1483 cstring.memcpy(p2, p, size);
1493 * Attempt to in-place enlarge the memory block pointed to by p by at least
1494 * min_size beyond its current capacity, up to a maximum of max_size. This
1495 * does not attempt to move the memory block (like realloc() does).
1498 * 0 if could not extend p,
1499 * total size of entire memory block if successful.
1501 private size_t extend(void* p, size_t minsize, size_t maxsize)
1504 assert( minsize <= maxsize );
1508 if (opts.options.sentinel)
1511 Pool* pool = findPool(p);
1515 // Retrieve attributes
1516 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1517 uint attrs = getAttr(pool, bit_i);
1519 void* blk_base_addr = pool.findBase(p);
1520 size_t blk_size = pool.findSize(p);
1521 bool has_pm = has_pointermap(attrs);
1522 size_t* pm_bitmask = null;
1523 size_t pm_bitmask_size = 0;
1525 pm_bitmask_size = size_t.sizeof;
1526 // Retrieve pointer map bit mask
1527 auto end_of_blk = cast(size_t**)(blk_base_addr +
1528 blk_size - size_t.sizeof);
1529 pm_bitmask = *end_of_blk;
1531 minsize += size_t.sizeof;
1532 maxsize += size_t.sizeof;
1535 if (blk_size < PAGESIZE)
1536 return 0; // cannot extend buckets
1538 auto psz = blk_size / PAGESIZE;
1539 auto minsz = (minsize + PAGESIZE - 1) / PAGESIZE;
1540 auto maxsz = (maxsize + PAGESIZE - 1) / PAGESIZE;
1542 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
1545 for (sz = 0; sz < maxsz; sz++)
1547 auto i = pagenum + psz + sz;
1548 if (i == pool.npages)
1550 if (pool.pagetable[i] != B_FREE)
1560 size_t new_size = (psz + sz) * PAGESIZE;
1562 if (opts.options.mem_stomp)
1563 memset(p + blk_size - pm_bitmask_size, 0xF0,
1564 new_size - blk_size - pm_bitmask_size);
1565 memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz);
1570 new_size -= size_t.sizeof;
1571 auto end_of_blk = cast(size_t**)(blk_base_addr + new_size);
1572 *end_of_blk = pm_bitmask;
1581 private void free(void *p)
1590 // Find which page it is in
1592 if (!pool) // if not one of ours
1594 if (opts.options.sentinel) {
1595 sentinel_Invariant(p);
1596 p = sentinel_sub(p);
1598 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1599 bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1600 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1602 bin = cast(Bins)pool.pagetable[pagenum];
1603 if (bin == B_PAGE) // if large alloc
1608 while (++n < pool.npages && pool.pagetable[n] == B_PAGEPLUS)
1610 if (opts.options.mem_stomp)
1611 memset(p, 0xF2, npages * PAGESIZE);
1612 pool.freePages(pagenum, npages);
1617 List* list = cast(List*) p;
1619 if (opts.options.mem_stomp)
1620 memset(p, 0xF2, binsize[bin]);
1622 list.next = gc.free_list[bin];
1624 gc.free_list[bin] = list;
1630 * Determine the allocated size of pointer p. If p is an interior pointer
1631 * or not a gc allocated pointer, return 0.
1633 private size_t sizeOf(void *p)
1637 if (opts.options.sentinel)
1638 p = sentinel_sub(p);
1640 Pool* pool = findPool(p);
1644 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
1645 uint attrs = getAttr(pool, biti);
1647 size_t size = pool.findSize(p);
1648 size_t pm_bitmask_size = 0;
1649 if (has_pointermap(attrs))
1650 pm_bitmask_size = size_t.sizeof;
1652 if (opts.options.sentinel) {
1653 // Check for interior pointer
1655 // 1) size is a power of 2 for less than PAGESIZE values
1656 // 2) base of memory pool is aligned on PAGESIZE boundary
1657 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
1659 return size - SENTINEL_EXTRA - pm_bitmask_size;
1662 if (p == gc.p_cache)
1663 return gc.size_cache;
1665 // Check for interior pointer
1667 // 1) size is a power of 2 for less than PAGESIZE values
1668 // 2) base of memory pool is aligned on PAGESIZE boundary
1669 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
1673 gc.size_cache = size - pm_bitmask_size;
1675 return gc.size_cache;
1681 * Verify that pointer p:
1682 * 1) belongs to this memory pool
1683 * 2) points to the start of an allocated piece of memory
1684 * 3) is not on a free list
1686 private void checkNoSync(void *p)
1690 if (opts.options.sentinel)
1691 sentinel_Invariant(p);
1699 if (opts.options.sentinel)
1700 p = sentinel_sub(p);
1703 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1704 bin = cast(Bins)pool.pagetable[pagenum];
1705 assert(bin <= B_PAGE);
1706 size = binsize[bin];
1707 assert((cast(size_t)p & (size - 1)) == 0);
1713 // Check that p is not on a free list
1714 for (List* list = gc.free_list[bin]; list; list = list.next)
1716 assert(cast(void*)list != p);
1727 private void setStackBottom(void *p)
1729 version (STACKGROWSDOWN)
1731 //p = (void *)((uint *)p + 4);
1732 if (p > gc.stack_bottom)
1734 gc.stack_bottom = p;
1739 //p = (void *)((uint *)p - 4);
1740 if (p < gc.stack_bottom)
1742 gc.stack_bottom = cast(char*)p;
1749 * Retrieve statistics about garbage collection.
1750 * Useful for debugging and tuning.
1752 private GCStats getStats()
1762 for (n = 0; n < gc.pools.length; n++)
1764 Pool* pool = gc.pools[n];
1765 psize += pool.npages * PAGESIZE;
1766 for (size_t j = 0; j < pool.npages; j++)
1768 Bins bin = cast(Bins)pool.pagetable[j];
1771 else if (bin == B_PAGE)
1773 else if (bin < B_PAGE)
1778 for (n = 0; n < B_PAGE; n++)
1780 for (List* list = gc.free_list[n]; list; list = list.next)
1781 flsize += binsize[n];
1784 usize = bsize - flsize;
1786 stats.poolsize = psize;
1787 stats.usedsize = bsize - flsize;
1788 stats.freelistsize = flsize;
1792 /******************* weak-reference support *********************/
1794 private struct WeakPointer
1798 void ondestroy(Object r)
1800 assert(r is reference);
1801 // lock for memory consistency (parallel readers)
1802 // also ensures that weakpointerDestroy can be called while another
1803 // thread is freeing the reference with "delete"
1804 return locked!(void, () {
1811 * Create a weak pointer to the given object.
1812 * Returns a pointer to an opaque struct allocated in C memory.
1814 void* weakpointerCreate( Object r )
1818 // must be allocated in C memory
1819 // 1. to hide the reference from the GC
1820 // 2. the GC doesn't scan delegates added by rt_attachDisposeEvent
1822 auto wp = cast(WeakPointer*)(cstdlib.malloc(WeakPointer.sizeof));
1824 onOutOfMemoryError();
1826 rt_attachDisposeEvent(r, &wp.ondestroy);
1833 * Destroy a weak pointer returned by weakpointerCreate().
1834 * If null is passed, nothing happens.
1836 void weakpointerDestroy( void* p )
1840 auto wp = cast(WeakPointer*)p;
1841 // must be extra careful about the GC or parallel threads
1842 // finalizing the reference at the same time
1843 return locked!(void, () {
1845 rt_detachDisposeEvent(wp.reference, &wp.ondestroy);
1852 * Query a weak pointer and return either the object passed to
1853 * weakpointerCreate, or null if it was free'd in the meantime.
1854 * If null is passed, null is returned.
1856 Object weakpointerGet( void* p )
1860 // NOTE: could avoid the lock by using Fawzi style GC counters but
1861 // that'd require core.sync.Atomic and lots of care about memory
1862 // consistency it's an optional optimization see
1863 // http://dsource.org/projects/tango/browser/trunk/user/tango/core/Lifetime.d?rev=5100#L158
1864 return locked!(Object, () {
1865 return (cast(WeakPointer*)p).reference;
1871 /* ============================ Pool =============================== */
1878 GCBits mark; // entries already scanned, or should not be scanned
1879 GCBits scan; // entries that need to be scanned
1880 GCBits freebits; // entries that are on the free list
1881 GCBits finals; // entries that need finalizer run on them
1882 GCBits noscan; // entries that should not be scanned
1887 /// Cache for findSize()
1893 this.cached_ptr = null;
1894 this.cached_size = 0;
1897 void initialize(size_t npages)
1899 size_t poolsize = npages * PAGESIZE;
1900 assert(poolsize >= POOLSIZE);
1901 baseAddr = cast(byte *) os.alloc(poolsize);
1903 // Some of the code depends on page alignment of memory pools
1904 assert((cast(size_t)baseAddr & (PAGESIZE - 1)) == 0);
1911 topAddr = baseAddr + poolsize;
1913 size_t nbits = cast(size_t)poolsize / 16;
1915 // if the GC will run in parallel in a fork()ed process, we need to
1916 // share the mark bits
1917 os.Vis vis = os.Vis.PRIV;
1918 if (opts.options.fork)
1919 vis = os.Vis.SHARED;
1920 mark.alloc(nbits, vis); // shared between mark and sweep
1921 freebits.alloc(nbits, vis); // ditto
1922 scan.alloc(nbits); // only used in the mark phase
1923 finals.alloc(nbits); // mark phase *MUST* have a snapshot
1924 noscan.alloc(nbits); // ditto
1926 pagetable = cast(ubyte*) cstdlib.malloc(npages);
1928 onOutOfMemoryError();
1929 memset(pagetable, B_FREE, npages);
1931 this.npages = npages;
1943 result = os.dealloc(baseAddr, npages * PAGESIZE);
1951 // See Gcx.Dtor() for the rationale of the null check.
1953 cstdlib.free(pagetable);
1955 os.Vis vis = os.Vis.PRIV;
1956 if (opts.options.fork)
1957 vis = os.Vis.SHARED;
1976 //freebits.Invariant();
1977 //finals.Invariant();
1978 //noscan.Invariant();
1982 //if (baseAddr + npages * PAGESIZE != topAddr)
1983 //printf("baseAddr = %p, npages = %d, topAddr = %p\n", baseAddr, npages, topAddr);
1984 assert(baseAddr + npages * PAGESIZE == topAddr);
1987 for (size_t i = 0; i < npages; i++)
1989 Bins bin = cast(Bins)pagetable[i];
1990 assert(bin < B_MAX);
1996 * Allocate n pages from Pool.
1997 * Returns OPFAIL on failure.
1999 size_t allocPages(size_t n)
2005 for (i = 0; i < npages; i++)
2007 if (pagetable[i] == B_FREE)
2020 * Free npages pages starting with pagenum.
2022 void freePages(size_t pagenum, size_t npages)
2024 memset(&pagetable[pagenum], B_FREE, npages);
2029 * Find base address of block containing pointer p.
2030 * Returns null if the pointer doesn't belong to this pool
2032 void* findBase(void *p)
2034 size_t offset = cast(size_t)(p - this.baseAddr);
2035 size_t pagenum = offset / PAGESIZE;
2036 Bins bin = cast(Bins)this.pagetable[pagenum];
2037 // Adjust bit to be at start of allocated memory block
2039 return this.baseAddr + (offset & notbinsize[bin]);
2040 if (bin == B_PAGEPLUS) {
2042 --pagenum, offset -= PAGESIZE;
2043 } while (cast(Bins)this.pagetable[pagenum] == B_PAGEPLUS);
2044 return this.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
2046 // we are in a B_FREE page
2052 * Find size of pointer p.
2053 * Returns 0 if p doesn't belong to this pool if if it's block size is less
2056 size_t findSize(void *p)
2058 size_t pagenum = cast(size_t)(p - this.baseAddr) / PAGESIZE;
2059 Bins bin = cast(Bins)this.pagetable[pagenum];
2061 return binsize[bin];
2062 if (this.cached_ptr == p)
2063 return this.cached_size;
2064 size_t i = pagenum + 1;
2065 for (; i < this.npages; i++)
2066 if (this.pagetable[i] != B_PAGEPLUS)
2068 this.cached_ptr = p;
2069 this.cached_size = (i - pagenum) * PAGESIZE;
2070 return this.cached_size;
2075 * Used for sorting pools
2077 int opCmp(in Pool other)
2079 if (baseAddr < other.baseAddr)
2082 return cast(int)(baseAddr > other.baseAddr);
2087 /* ============================ SENTINEL =============================== */
2090 const size_t SENTINEL_PRE = cast(size_t) 0xF4F4F4F4F4F4F4F4UL; // 32 or 64 bits
2091 const ubyte SENTINEL_POST = 0xF5; // 8 bits
2092 const uint SENTINEL_EXTRA = 2 * size_t.sizeof + 1;
2095 size_t* sentinel_size(void *p) { return &(cast(size_t *)p)[-2]; }
2096 size_t* sentinel_pre(void *p) { return &(cast(size_t *)p)[-1]; }
2097 ubyte* sentinel_post(void *p) { return &(cast(ubyte *)p)[*sentinel_size(p)]; }
2100 void sentinel_init(void *p, size_t size)
2102 *sentinel_size(p) = size;
2103 *sentinel_pre(p) = SENTINEL_PRE;
2104 *sentinel_post(p) = SENTINEL_POST;
2108 void sentinel_Invariant(void *p)
2110 if (*sentinel_pre(p) != SENTINEL_PRE ||
2111 *sentinel_post(p) != SENTINEL_POST)
2116 void *sentinel_add(void *p)
2118 return p + 2 * size_t.sizeof;
2122 void *sentinel_sub(void *p)
2124 return p - 2 * size_t.sizeof;
2129 /* ============================ C Public Interface ======================== */
2132 private int _termCleanupLevel=1;
2136 /// sets the cleanup level done by gc
2139 /// 2: fullCollect ignoring stack roots (might crash daemonThreads)
2140 /// result !=0 if the value was invalid
2141 int gc_setTermCleanupLevel(int cLevel)
2143 if (cLevel<0 || cLevel>2) return cLevel;
2144 _termCleanupLevel=cLevel;
2148 /// returns the cleanup level done by gc
2149 int gc_getTermCleanupLevel()
2151 return _termCleanupLevel;
2156 scope (exit) assert (Invariant());
2157 gc = cast(GC*) cstdlib.calloc(1, GC.sizeof);
2160 version (DigitalMars) version(OSX) {
2161 _d_osx_image_init();
2163 // NOTE: The GC must initialize the thread library
2164 // before its first collection.
2170 assert (Invariant());
2171 if (_termCleanupLevel<1) {
2173 } else if (_termCleanupLevel==2){
2174 // a more complete cleanup
2175 // NOTE: There may be daemons threads still running when this routine is
2176 // called. If so, cleaning memory out from under then is a good
2177 // way to make them crash horribly.
2178 // Often this probably doesn't matter much since the app is
2179 // supposed to be shutting down anyway, but for example tests might
2180 // crash (and be considerd failed even if the test was ok).
2181 // thus this is not the default and should be enabled by
2182 // I'm disabling cleanup for now until I can think about it some
2185 // not really a 'collect all' -- still scans static data area, roots,
2187 return locked!(void, () {
2193 // default (safe) clenup
2194 return locked!(void, () {
2202 return locked!(void, () {
2203 assert (Invariant()); scope (exit) assert (Invariant());
2204 assert (gc.disabled > 0);
2211 return locked!(void, () {
2212 assert (Invariant()); scope (exit) assert (Invariant());
2219 return locked!(void, () {
2220 assert (Invariant()); scope (exit) assert (Invariant());
2228 return locked!(void, () {
2229 assert (Invariant()); scope (exit) assert (Invariant());
2234 uint gc_getAttr(void* p)
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 return getAttr(pool, bit_i);
2248 uint gc_setAttr(void* p, uint attrs)
2252 return locked!(uint, () {
2253 assert (Invariant()); scope (exit) assert (Invariant());
2254 Pool* pool = findPool(p);
2257 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
2258 uint old_attrs = getAttr(pool, bit_i);
2259 setAttr(pool, bit_i, attrs);
2264 uint gc_clrAttr(void* p, uint attrs)
2268 return locked!(uint, () {
2269 assert (Invariant()); scope (exit) assert (Invariant());
2270 Pool* pool = findPool(p);
2273 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
2274 uint old_attrs = getAttr(pool, bit_i);
2275 clrAttr(pool, bit_i, attrs);
2280 void* gc_malloc(size_t size, uint attrs = 0,
2281 PointerMap ptrmap = PointerMap.init)
2285 return locked!(void*, () {
2286 assert (Invariant()); scope (exit) assert (Invariant());
2287 return malloc(size, attrs, ptrmap.bits.ptr);
2291 void* gc_calloc(size_t size, uint attrs = 0,
2292 PointerMap ptrmap = PointerMap.init)
2296 return locked!(void*, () {
2297 assert (Invariant()); scope (exit) assert (Invariant());
2298 return calloc(size, attrs, ptrmap.bits.ptr);
2302 void* gc_realloc(void* p, size_t size, uint attrs = 0,
2303 PointerMap ptrmap = PointerMap.init)
2305 return locked!(void*, () {
2306 assert (Invariant()); scope (exit) assert (Invariant());
2307 return realloc(p, size, attrs, ptrmap.bits.ptr);
2311 size_t gc_extend(void* p, size_t min_size, size_t max_size)
2313 return locked!(size_t, () {
2314 assert (Invariant()); scope (exit) assert (Invariant());
2315 return extend(p, min_size, max_size);
2319 size_t gc_reserve(size_t size)
2323 return locked!(size_t, () {
2324 assert (Invariant()); scope (exit) assert (Invariant());
2325 return reserve(size);
2329 void gc_free(void* p)
2333 return locked!(void, () {
2334 assert (Invariant()); scope (exit) assert (Invariant());
2339 void* gc_addrOf(void* p)
2343 return locked!(void*, () {
2344 assert (Invariant()); scope (exit) assert (Invariant());
2345 Pool* pool = findPool(p);
2348 return pool.findBase(p);
2352 size_t gc_sizeOf(void* p)
2356 return locked!(size_t, () {
2357 assert (Invariant()); scope (exit) assert (Invariant());
2362 BlkInfo gc_query(void* p)
2365 return BlkInfo.init;
2366 return locked!(BlkInfo, () {
2367 assert (Invariant()); scope (exit) assert (Invariant());
2372 // NOTE: This routine is experimental. The stats or function name may change
2373 // before it is made officially available.
2376 return locked!(GCStats, () {
2377 assert (Invariant()); scope (exit) assert (Invariant());
2382 void gc_addRoot(void* p)
2386 return locked!(void, () {
2387 assert (Invariant()); scope (exit) assert (Invariant());
2388 if (gc.roots.append(p) is null)
2389 onOutOfMemoryError();
2393 void gc_addRange(void* p, size_t size)
2395 if (p is null || size == 0)
2397 return locked!(void, () {
2398 assert (Invariant()); scope (exit) assert (Invariant());
2399 if (gc.ranges.append(Range(p, p + size)) is null)
2400 onOutOfMemoryError();
2404 void gc_removeRoot(void* p)
2408 return locked!(void, () {
2409 assert (Invariant()); scope (exit) assert (Invariant());
2410 bool r = gc.roots.remove(p);
2415 void gc_removeRange(void* p)
2419 return locked!(void, () {
2420 assert (Invariant()); scope (exit) assert (Invariant());
2421 bool r = gc.ranges.remove(Range(p, null));
2426 void* gc_weakpointerCreate(Object r)
2428 // weakpointers do their own locking
2429 return weakpointerCreate(r);
2432 void gc_weakpointerDestroy(void* wp)
2434 // weakpointers do their own locking
2435 weakpointerDestroy(wp);
2438 Object gc_weakpointerGet(void* wp)
2440 // weakpointers do their own locking
2441 return weakpointerGet(wp);
2445 // vim: set et sw=4 sts=4 :