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 alloc = rt.gc.cdgc.alloc;
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 static 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 * Marks a range of memory using the conservative bit mask. Used for
595 * the stack, for the data segment, and additional memory ranges.
597 void mark_conservative(void* pbot, void* ptop)
599 mark(pbot, ptop, PointerMap.init.bits.ptr);
604 * Search a range of memory values and mark any pointers into the GC pool.
606 void mark(void *pbot, void *ptop, size_t* pm_bitmask)
608 // TODO: make our own assert because assert uses the GC
609 assert (pbot <= ptop);
611 const BITS_PER_WORD = size_t.sizeof * 8;
613 void **p1 = cast(void **)pbot;
614 void **p2 = cast(void **)ptop;
618 size_t type_size = pm_bitmask[0];
619 size_t* pm_bits = pm_bitmask + 1;
620 bool has_type_info = type_size != 1 || pm_bits[0] != 1 || pm_bits[1] != 0;
622 //printf("marking range: %p -> %p\n", pbot, ptop);
623 for (; p1 + type_size <= p2; p1 += type_size) {
626 while (n < type_size && pm_bits[n / BITS_PER_WORD] == 0)
628 if (n < type_size && (pm_bits[n / BITS_PER_WORD] &
629 ((1 << (BITS_PER_WORD / 2)) - 1)) == 0)
630 n += BITS_PER_WORD / 2;
631 else if (n < type_size && (pm_bits[n / BITS_PER_WORD] &
632 ((1 << (BITS_PER_WORD / 4)) - 1)) == 0)
633 n += BITS_PER_WORD / 4;
635 for (; n < type_size; n++) {
636 // scan bit set for this word
638 !(pm_bits[n / BITS_PER_WORD] & (1 << (n % BITS_PER_WORD))))
643 if (p < gc.min_addr || p >= gc.max_addr)
646 if ((cast(size_t)p & ~(PAGESIZE-1)) == pcache)
649 Pool* pool = findPool(p);
652 size_t offset = cast(size_t)(p - pool.baseAddr);
654 size_t pn = offset / PAGESIZE;
655 Bins bin = cast(Bins)pool.pagetable[pn];
657 // Adjust bit to be at start of allocated memory block
659 bit_i = (offset & notbinsize[bin]) >> 4;
660 else if (bin == B_PAGEPLUS)
666 while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
667 bit_i = pn * (PAGESIZE / 16);
671 // Don't mark bits in B_FREE pages
675 if (bin >= B_PAGE) // Cache B_PAGE and B_PAGEPLUS lookups
676 pcache = cast(size_t)p & ~(PAGESIZE-1);
678 if (!pool.mark.test(bit_i))
680 pool.mark.set(bit_i);
681 if (!pool.noscan.test(bit_i))
683 pool.scan.set(bit_i);
691 gc.any_changes = true;
695 * Return number of full pages free'd.
697 size_t fullcollectshell()
699 gc.stats.collection_started();
701 gc.stats.collection_finished();
703 // The purpose of the 'shell' is to ensure all the registers
704 // get put on the stack so they'll be scanned
709 gcc.builtins.__builtin_unwind_init();
716 uint eax,ecx,edx,ebx,ebp,esi,edi;
729 else version (X86_64)
731 ulong rax,rbx,rcx,rdx,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15;
754 static assert( false, "Architecture not supported." );
765 result = fullcollect(sp);
788 size_t fullcollect(void *stackTop)
793 debug(COLLECT_PRINTF) printf("Gcx.fullcollect()\n");
796 gc.stats.world_stopped();
801 gc.any_changes = false;
802 for (n = 0; n < gc.pools.length; n++)
807 pool.freebits.zero();
810 // Mark each free entry, so it doesn't get scanned
811 for (n = 0; n < B_PAGE; n++)
813 for (List *list = gc.free_list[n]; list; list = list.next)
815 pool = findPool(list);
817 pool.freebits.set(cast(size_t)(cast(byte*)list - pool.baseAddr) / 16);
821 for (n = 0; n < gc.pools.length; n++)
824 pool.mark.copy(&pool.freebits);
827 void mark_conservative_dg(void* pbot, void* ptop)
829 mark_conservative(pbot, ptop);
832 rt_scanStaticData(&mark_conservative_dg);
836 // Scan stacks and registers for each paused thread
837 thread_scanAll(&mark_conservative_dg, stackTop);
841 debug(COLLECT_PRINTF) printf("scan roots[]\n");
842 mark_conservative(gc.roots.ptr, gc.roots.ptr + gc.roots.length);
845 debug(COLLECT_PRINTF) printf("scan ranges[]\n");
846 for (n = 0; n < gc.ranges.length; n++)
848 debug(COLLECT_PRINTF) printf("\t%x .. %x\n", gc.ranges[n].pbot, gc.ranges[n].ptop);
849 mark_conservative(gc.ranges[n].pbot, gc.ranges[n].ptop);
852 debug(COLLECT_PRINTF) printf("\tscan heap\n");
853 while (gc.any_changes)
855 gc.any_changes = false;
856 for (n = 0; n < gc.pools.length; n++)
864 bbase = pool.scan.base();
865 btop = bbase + pool.scan.nwords;
866 for (b = bbase; b < btop;)
882 o = pool.baseAddr + (b - bbase) * 32 * 16;
883 if (!(bitm & 0xFFFF))
888 for (; bitm; o += 16, bitm >>= 1)
893 pn = cast(size_t)(o - pool.baseAddr) / PAGESIZE;
894 bin = cast(Bins)pool.pagetable[pn];
896 if (opts.options.conservative)
897 mark_conservative(o, o + binsize[bin]);
899 auto end_of_blk = cast(size_t**)(o +
900 binsize[bin] - size_t.sizeof);
901 size_t* pm_bitmask = *end_of_blk;
902 mark(o, end_of_blk, pm_bitmask);
905 else if (bin == B_PAGE || bin == B_PAGEPLUS)
907 if (bin == B_PAGEPLUS)
909 while (pool.pagetable[pn - 1] != B_PAGE)
913 while (pn + u < pool.npages &&
914 pool.pagetable[pn + u] == B_PAGEPLUS)
917 size_t blk_size = u * PAGESIZE;
918 if (opts.options.conservative)
919 mark_conservative(o, o + blk_size);
921 auto end_of_blk = cast(size_t**)(o + blk_size -
923 size_t* pm_bitmask = *end_of_blk;
924 mark(o, end_of_blk, pm_bitmask);
933 gc.stats.world_started();
935 // Free up everything not marked
936 debug(COLLECT_PRINTF) printf("\tfree'ing\n");
937 size_t freedpages = 0;
939 for (n = 0; n < gc.pools.length; n++)
942 uint* bbase = pool.mark.base();
944 for (pn = 0; pn < pool.npages; pn++, bbase += PAGESIZE / (32 * 16))
946 Bins bin = cast(Bins)pool.pagetable[pn];
950 auto size = binsize[bin];
951 byte* p = pool.baseAddr + pn * PAGESIZE;
952 byte* ptop = p + PAGESIZE;
953 size_t bit_i = pn * (PAGESIZE/16);
954 size_t bit_stride = size / 16;
956 version(none) // BUG: doesn't work because freebits() must also be cleared
958 // If free'd entire page
959 if (bbase[0] == 0 && bbase[1] == 0 && bbase[2] == 0 &&
960 bbase[3] == 0 && bbase[4] == 0 && bbase[5] == 0 &&
961 bbase[6] == 0 && bbase[7] == 0)
963 for (; p < ptop; p += size, bit_i += bit_stride)
965 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
966 if (opts.options.sentinel)
967 rt_finalize(cast(List *)sentinel_add(p), false/*gc.no_stack > 0*/);
969 rt_finalize(cast(List *)p, false/*gc.no_stack > 0*/);
971 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
973 List *list = cast(List *)p;
975 if (opts.options.mem_stomp)
976 memset(p, 0xF3, size);
978 pool.pagetable[pn] = B_FREE;
983 for (; p < ptop; p += size, bit_i += bit_stride)
985 if (!pool.mark.test(bit_i))
987 if (opts.options.sentinel)
988 sentinel_Invariant(sentinel_add(p));
990 pool.freebits.set(bit_i);
991 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
992 if (opts.options.sentinel)
993 rt_finalize(cast(List *)sentinel_add(p), false/*gc.no_stack > 0*/);
995 rt_finalize(cast(List *)p, false/*gc.no_stack > 0*/);
997 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
999 List *list = cast(List *)p;
1001 if (opts.options.mem_stomp)
1002 memset(p, 0xF3, size);
1008 else if (bin == B_PAGE)
1010 size_t bit_i = pn * (PAGESIZE / 16);
1011 if (!pool.mark.test(bit_i))
1013 byte *p = pool.baseAddr + pn * PAGESIZE;
1014 if (opts.options.sentinel)
1015 sentinel_Invariant(sentinel_add(p));
1016 if (pool.finals.nbits && pool.finals.testClear(bit_i)) {
1017 if (opts.options.sentinel)
1018 rt_finalize(sentinel_add(p), false/*gc.no_stack > 0*/);
1020 rt_finalize(p, false/*gc.no_stack > 0*/);
1022 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1024 debug(COLLECT_PRINTF) printf("\tcollecting big %x\n", p);
1025 pool.pagetable[pn] = B_FREE;
1027 if (opts.options.mem_stomp)
1028 memset(p, 0xF3, PAGESIZE);
1029 while (pn + 1 < pool.npages && pool.pagetable[pn + 1] == B_PAGEPLUS)
1032 pool.pagetable[pn] = B_FREE;
1035 if (opts.options.mem_stomp)
1038 memset(p, 0xF3, PAGESIZE);
1047 gc.free_list[] = null;
1049 // Free complete pages, rebuild free list
1050 debug(COLLECT_PRINTF) printf("\tfree complete pages\n");
1051 size_t recoveredpages = 0;
1052 for (n = 0; n < gc.pools.length; n++)
1055 for (size_t pn = 0; pn < pool.npages; pn++)
1057 Bins bin = cast(Bins)pool.pagetable[pn];
1063 size_t size = binsize[bin];
1064 size_t bit_stride = size / 16;
1065 size_t bit_base = pn * (PAGESIZE / 16);
1066 size_t bit_top = bit_base + (PAGESIZE / 16);
1070 for (; bit_i < bit_top; bit_i += bit_stride)
1072 if (!pool.freebits.test(bit_i))
1075 pool.pagetable[pn] = B_FREE;
1080 p = pool.baseAddr + pn * PAGESIZE;
1081 for (u = 0; u < PAGESIZE; u += size)
1083 bit_i = bit_base + u / 16;
1084 if (pool.freebits.test(bit_i))
1086 List *list = cast(List *)(p + u);
1087 // avoid unnecessary writes
1088 if (list.next != gc.free_list[bin])
1089 list.next = gc.free_list[bin];
1090 gc.free_list[bin] = list;
1097 debug(COLLECT_PRINTF) printf("recovered pages = %d\n", recoveredpages);
1098 debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, gc.pools.length);
1100 return freedpages + recoveredpages;
1107 uint getAttr(Pool* pool, size_t bit_i)
1116 if (pool.finals.nbits &&
1117 pool.finals.test(bit_i))
1118 attrs |= BlkAttr.FINALIZE;
1119 if (pool.noscan.test(bit_i))
1120 attrs |= BlkAttr.NO_SCAN;
1121 // if (pool.nomove.nbits &&
1122 // pool.nomove.test(bit_i))
1123 // attrs |= BlkAttr.NO_MOVE;
1131 void setAttr(Pool* pool, size_t bit_i, uint mask)
1138 if (mask & BlkAttr.FINALIZE)
1140 if (!pool.finals.nbits)
1141 pool.finals.alloc(pool.mark.nbits);
1142 pool.finals.set(bit_i);
1144 if (mask & BlkAttr.NO_SCAN)
1146 pool.noscan.set(bit_i);
1148 // if (mask & BlkAttr.NO_MOVE)
1150 // if (!pool.nomove.nbits)
1151 // pool.nomove.alloc(pool.mark.nbits);
1152 // pool.nomove.set(bit_i);
1160 void clrAttr(Pool* pool, size_t bit_i, uint mask)
1167 if (mask & BlkAttr.FINALIZE && pool.finals.nbits)
1168 pool.finals.clear(bit_i);
1169 if (mask & BlkAttr.NO_SCAN)
1170 pool.noscan.clear(bit_i);
1171 // if (mask & BlkAttr.NO_MOVE && pool.nomove.nbits)
1172 // pool.nomove.clear(bit_i);
1180 gc.stack_bottom = cast(char*)&dummy;
1181 opts.parse(cstdlib.getenv("D_GC_OPTS"));
1182 gc.lock = GCLock.classinfo;
1184 setStackBottom(rt_stackBottom());
1185 gc.stats = Stats(gc);
1192 private void *malloc(size_t size, uint attrs, size_t* pm_bitmask)
1196 gc.stats.malloc_started(size, attrs, pm_bitmask);
1198 gc.stats.malloc_finished(p);
1203 if (opts.options.sentinel)
1204 size += SENTINEL_EXTRA;
1206 bool has_pm = has_pointermap(attrs);
1208 size += size_t.sizeof;
1211 // Cache previous binsize lookup - Dave Fladebo.
1212 static size_t lastsize = -1;
1213 static Bins lastbin;
1214 if (size == lastsize)
1218 bin = findBin(size);
1223 size_t capacity; // to figure out where to store the bitmask
1226 p = gc.free_list[bin];
1229 if (!allocPage(bin) && !gc.disabled) // try to find a new page
1231 if (!thread_needLock())
1233 /* Then we haven't locked it yet. Be sure
1234 * and gc.lock for a collection, since a finalizer
1235 * may start a new thread.
1237 synchronized (gc.lock)
1242 else if (!fullcollectshell()) // collect to find a new page
1247 if (!gc.free_list[bin] && !allocPage(bin))
1249 newPool(1); // allocate new pool to find a new page
1250 int result = allocPage(bin);
1252 onOutOfMemoryError();
1254 p = gc.free_list[bin];
1256 capacity = binsize[bin];
1258 // Return next item from free list
1259 gc.free_list[bin] = (cast(List*)p).next;
1260 if (!(attrs & BlkAttr.NO_SCAN))
1261 memset(p + size, 0, capacity - size);
1262 if (opts.options.mem_stomp)
1263 memset(p, 0xF0, size);
1269 onOutOfMemoryError();
1270 // Round the size up to the number of pages needed to store it
1271 size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
1272 capacity = npages * PAGESIZE;
1275 // Store the bit mask AFTER SENTINEL_POST
1276 // TODO: store it BEFORE, so the bitmask is protected too
1278 auto end_of_blk = cast(size_t**)(p + capacity - size_t.sizeof);
1279 *end_of_blk = pm_bitmask;
1280 size -= size_t.sizeof;
1283 if (opts.options.sentinel) {
1284 size -= SENTINEL_EXTRA;
1285 p = sentinel_add(p);
1286 sentinel_init(p, size);
1291 Pool *pool = findPool(p);
1294 setAttr(pool, cast(size_t)(p - pool.baseAddr) / 16, attrs);
1303 private void *calloc(size_t size, uint attrs, size_t* pm_bitmask)
1307 void *p = malloc(size, attrs, pm_bitmask);
1316 private void *realloc(void *p, size_t size, uint attrs,
1329 p = malloc(size, attrs, pm_bitmask);
1333 Pool* pool = findPool(p);
1337 // Set or retrieve attributes as appropriate
1338 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1340 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1341 setAttr(pool, bit_i, attrs);
1344 attrs = getAttr(pool, bit_i);
1346 void* blk_base_addr = pool.findBase(p);
1347 size_t blk_size = pool.findSize(p);
1348 bool has_pm = has_pointermap(attrs);
1349 size_t pm_bitmask_size = 0;
1351 pm_bitmask_size = size_t.sizeof;
1352 // Retrieve pointer map bit mask if appropriate
1353 if (pm_bitmask is null) {
1354 auto end_of_blk = cast(size_t**)(blk_base_addr +
1355 blk_size - size_t.sizeof);
1356 pm_bitmask = *end_of_blk;
1360 if (opts.options.sentinel)
1362 sentinel_Invariant(p);
1363 size_t sentinel_stored_size = *sentinel_size(p);
1364 if (sentinel_stored_size != size)
1366 void* p2 = malloc(size, attrs, pm_bitmask);
1367 if (sentinel_stored_size < size)
1368 size = sentinel_stored_size;
1369 cstring.memcpy(p2, p, size);
1375 size += pm_bitmask_size;
1376 if (blk_size >= PAGESIZE && size >= PAGESIZE)
1378 auto psz = blk_size / PAGESIZE;
1379 auto newsz = (size + PAGESIZE - 1) / PAGESIZE;
1383 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
1388 if (opts.options.mem_stomp)
1389 memset(p + size - pm_bitmask_size, 0xF2,
1390 blk_size - size - pm_bitmask_size);
1391 pool.freePages(pagenum + newsz, psz - newsz);
1393 auto end_of_blk = cast(size_t**)(
1394 blk_base_addr + (PAGESIZE * newsz) -
1396 *end_of_blk = pm_bitmask;
1400 else if (pagenum + newsz <= pool.npages)
1402 // Attempt to expand in place
1403 for (size_t i = pagenum + psz; 1;)
1405 if (i == pagenum + newsz)
1407 if (opts.options.mem_stomp)
1408 memset(p + blk_size - pm_bitmask_size,
1409 0xF0, size - blk_size
1411 memset(pool.pagetable + pagenum +
1412 psz, B_PAGEPLUS, newsz - psz);
1414 auto end_of_blk = cast(size_t**)(
1416 (PAGESIZE * newsz) -
1418 *end_of_blk = pm_bitmask;
1422 if (i == pool.npages)
1426 if (pool.pagetable[i] != B_FREE)
1432 // if new size is bigger or less than half
1433 if (blk_size < size || blk_size > size * 2)
1435 size -= pm_bitmask_size;
1436 blk_size -= pm_bitmask_size;
1437 void* p2 = malloc(size, attrs, pm_bitmask);
1438 if (blk_size < size)
1440 cstring.memcpy(p2, p, size);
1450 * Attempt to in-place enlarge the memory block pointed to by p by at least
1451 * min_size beyond its current capacity, up to a maximum of max_size. This
1452 * does not attempt to move the memory block (like realloc() does).
1455 * 0 if could not extend p,
1456 * total size of entire memory block if successful.
1458 private size_t extend(void* p, size_t minsize, size_t maxsize)
1461 assert( minsize <= maxsize );
1465 if (opts.options.sentinel)
1468 Pool* pool = findPool(p);
1472 // Retrieve attributes
1473 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1474 uint attrs = getAttr(pool, bit_i);
1476 void* blk_base_addr = pool.findBase(p);
1477 size_t blk_size = pool.findSize(p);
1478 bool has_pm = has_pointermap(attrs);
1479 size_t* pm_bitmask = null;
1480 size_t pm_bitmask_size = 0;
1482 pm_bitmask_size = size_t.sizeof;
1483 // Retrieve pointer map bit mask
1484 auto end_of_blk = cast(size_t**)(blk_base_addr +
1485 blk_size - size_t.sizeof);
1486 pm_bitmask = *end_of_blk;
1488 minsize += size_t.sizeof;
1489 maxsize += size_t.sizeof;
1492 if (blk_size < PAGESIZE)
1493 return 0; // cannot extend buckets
1495 auto psz = blk_size / PAGESIZE;
1496 auto minsz = (minsize + PAGESIZE - 1) / PAGESIZE;
1497 auto maxsz = (maxsize + PAGESIZE - 1) / PAGESIZE;
1499 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
1502 for (sz = 0; sz < maxsz; sz++)
1504 auto i = pagenum + psz + sz;
1505 if (i == pool.npages)
1507 if (pool.pagetable[i] != B_FREE)
1517 size_t new_size = (psz + sz) * PAGESIZE;
1519 if (opts.options.mem_stomp)
1520 memset(p + blk_size - pm_bitmask_size, 0xF0,
1521 new_size - blk_size - pm_bitmask_size);
1522 memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz);
1527 new_size -= size_t.sizeof;
1528 auto end_of_blk = cast(size_t**)(blk_base_addr + new_size);
1529 *end_of_blk = pm_bitmask;
1538 private void free(void *p)
1547 // Find which page it is in
1549 if (!pool) // if not one of ours
1551 if (opts.options.sentinel) {
1552 sentinel_Invariant(p);
1553 p = sentinel_sub(p);
1555 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1556 bit_i = cast(size_t)(p - pool.baseAddr) / 16;
1557 clrAttr(pool, bit_i, BlkAttr.ALL_BITS);
1559 bin = cast(Bins)pool.pagetable[pagenum];
1560 if (bin == B_PAGE) // if large alloc
1565 while (++n < pool.npages && pool.pagetable[n] == B_PAGEPLUS)
1567 if (opts.options.mem_stomp)
1568 memset(p, 0xF2, npages * PAGESIZE);
1569 pool.freePages(pagenum, npages);
1574 List *list = cast(List*)p;
1576 if (opts.options.mem_stomp)
1577 memset(p, 0xF2, binsize[bin]);
1579 list.next = gc.free_list[bin];
1580 gc.free_list[bin] = list;
1586 * Determine the allocated size of pointer p. If p is an interior pointer
1587 * or not a gc allocated pointer, return 0.
1589 private size_t sizeOf(void *p)
1593 if (opts.options.sentinel)
1594 p = sentinel_sub(p);
1596 Pool* pool = findPool(p);
1600 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
1601 uint attrs = getAttr(pool, biti);
1603 size_t size = pool.findSize(p);
1604 size_t pm_bitmask_size = 0;
1605 if (has_pointermap(attrs))
1606 pm_bitmask_size = size_t.sizeof;
1608 if (opts.options.sentinel) {
1609 // Check for interior pointer
1611 // 1) size is a power of 2 for less than PAGESIZE values
1612 // 2) base of memory pool is aligned on PAGESIZE boundary
1613 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
1615 return size - SENTINEL_EXTRA - pm_bitmask_size;
1618 if (p == gc.p_cache)
1619 return gc.size_cache;
1621 // Check for interior pointer
1623 // 1) size is a power of 2 for less than PAGESIZE values
1624 // 2) base of memory pool is aligned on PAGESIZE boundary
1625 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
1629 gc.size_cache = size - pm_bitmask_size;
1631 return gc.size_cache;
1637 * Verify that pointer p:
1638 * 1) belongs to this memory pool
1639 * 2) points to the start of an allocated piece of memory
1640 * 3) is not on a free list
1642 private void checkNoSync(void *p)
1646 if (opts.options.sentinel)
1647 sentinel_Invariant(p);
1655 if (opts.options.sentinel)
1656 p = sentinel_sub(p);
1659 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1660 bin = cast(Bins)pool.pagetable[pagenum];
1661 assert(bin <= B_PAGE);
1662 size = binsize[bin];
1663 assert((cast(size_t)p & (size - 1)) == 0);
1669 // Check that p is not on a free list
1672 for (list = gc.free_list[bin]; list; list = list.next)
1674 assert(cast(void*)list != p);
1685 private void setStackBottom(void *p)
1687 version (STACKGROWSDOWN)
1689 //p = (void *)((uint *)p + 4);
1690 if (p > gc.stack_bottom)
1692 gc.stack_bottom = p;
1697 //p = (void *)((uint *)p - 4);
1698 if (p < gc.stack_bottom)
1700 gc.stack_bottom = cast(char*)p;
1707 * Retrieve statistics about garbage collection.
1708 * Useful for debugging and tuning.
1710 private GCStats getStats()
1720 for (n = 0; n < gc.pools.length; n++)
1722 Pool* pool = gc.pools[n];
1723 psize += pool.npages * PAGESIZE;
1724 for (size_t j = 0; j < pool.npages; j++)
1726 Bins bin = cast(Bins)pool.pagetable[j];
1729 else if (bin == B_PAGE)
1731 else if (bin < B_PAGE)
1736 for (n = 0; n < B_PAGE; n++)
1738 for (List *list = gc.free_list[n]; list; list = list.next)
1739 flsize += binsize[n];
1742 usize = bsize - flsize;
1744 stats.poolsize = psize;
1745 stats.usedsize = bsize - flsize;
1746 stats.freelistsize = flsize;
1750 /******************* weak-reference support *********************/
1752 private struct WeakPointer
1756 void ondestroy(Object r)
1758 assert(r is reference);
1759 // lock for memory consistency (parallel readers)
1760 // also ensures that weakpointerDestroy can be called while another
1761 // thread is freeing the reference with "delete"
1762 return locked!(void, () {
1769 * Create a weak pointer to the given object.
1770 * Returns a pointer to an opaque struct allocated in C memory.
1772 void* weakpointerCreate( Object r )
1776 // must be allocated in C memory
1777 // 1. to hide the reference from the GC
1778 // 2. the GC doesn't scan delegates added by rt_attachDisposeEvent
1780 auto wp = cast(WeakPointer*)(cstdlib.malloc(WeakPointer.sizeof));
1782 onOutOfMemoryError();
1784 rt_attachDisposeEvent(r, &wp.ondestroy);
1791 * Destroy a weak pointer returned by weakpointerCreate().
1792 * If null is passed, nothing happens.
1794 void weakpointerDestroy( void* p )
1798 auto wp = cast(WeakPointer*)p;
1799 // must be extra careful about the GC or parallel threads
1800 // finalizing the reference at the same time
1801 return locked!(void, () {
1803 rt_detachDisposeEvent(wp.reference, &wp.ondestroy);
1810 * Query a weak pointer and return either the object passed to
1811 * weakpointerCreate, or null if it was free'd in the meantime.
1812 * If null is passed, null is returned.
1814 Object weakpointerGet( void* p )
1818 // NOTE: could avoid the lock by using Fawzi style GC counters but
1819 // that'd require core.sync.Atomic and lots of care about memory
1820 // consistency it's an optional optimization see
1821 // http://dsource.org/projects/tango/browser/trunk/user/tango/core/Lifetime.d?rev=5100#L158
1822 return locked!(Object, () {
1823 return (cast(WeakPointer*)p).reference;
1829 /* ============================ Pool =============================== */
1836 GCBits mark; // entries already scanned, or should not be scanned
1837 GCBits scan; // entries that need to be scanned
1838 GCBits freebits; // entries that are on the free list
1839 GCBits finals; // entries that need finalizer run on them
1840 GCBits noscan; // entries that should not be scanned
1846 void initialize(size_t npages)
1848 size_t poolsize = npages * PAGESIZE;
1849 assert(poolsize >= POOLSIZE);
1850 baseAddr = cast(byte *) alloc.os_mem_map(poolsize);
1852 // Some of the code depends on page alignment of memory pools
1853 assert((cast(size_t)baseAddr & (PAGESIZE - 1)) == 0);
1861 topAddr = baseAddr + poolsize;
1863 mark.alloc(cast(size_t)poolsize / 16);
1864 scan.alloc(cast(size_t)poolsize / 16);
1865 freebits.alloc(cast(size_t)poolsize / 16);
1866 noscan.alloc(cast(size_t)poolsize / 16);
1868 pagetable = cast(ubyte*) cstdlib.malloc(npages);
1870 onOutOfMemoryError();
1871 memset(pagetable, B_FREE, npages);
1873 this.npages = npages;
1885 result = alloc.os_mem_unmap(baseAddr, npages * PAGESIZE);
1893 // See Gcx.Dtor() for the rationale of the null check.
1895 cstdlib.free(pagetable);
1915 //freebits.Invariant();
1916 //finals.Invariant();
1917 //noscan.Invariant();
1921 //if (baseAddr + npages * PAGESIZE != topAddr)
1922 //printf("baseAddr = %p, npages = %d, topAddr = %p\n", baseAddr, npages, topAddr);
1923 assert(baseAddr + npages * PAGESIZE == topAddr);
1926 for (size_t i = 0; i < npages; i++)
1928 Bins bin = cast(Bins)pagetable[i];
1929 assert(bin < B_MAX);
1935 * Allocate n pages from Pool.
1936 * Returns OPFAIL on failure.
1938 size_t allocPages(size_t n)
1944 for (i = 0; i < npages; i++)
1946 if (pagetable[i] == B_FREE)
1959 * Free npages pages starting with pagenum.
1961 void freePages(size_t pagenum, size_t npages)
1963 memset(&pagetable[pagenum], B_FREE, npages);
1968 * Find base address of block containing pointer p.
1969 * Returns null if the pointer doesn't belong to this pool
1971 void* findBase(void *p)
1973 size_t offset = cast(size_t)(p - this.baseAddr);
1974 size_t pagenum = offset / PAGESIZE;
1975 Bins bin = cast(Bins)this.pagetable[pagenum];
1976 // Adjust bit to be at start of allocated memory block
1978 return this.baseAddr + (offset & notbinsize[bin]);
1979 if (bin == B_PAGEPLUS) {
1981 --pagenum, offset -= PAGESIZE;
1982 } while (cast(Bins)this.pagetable[pagenum] == B_PAGEPLUS);
1983 return this.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
1985 // we are in a B_FREE page
1991 * Find size of pointer p.
1992 * Returns 0 if p doesn't belong to this pool if if it's block size is less
1995 size_t findSize(void *p)
1997 size_t pagenum = cast(size_t)(p - this.baseAddr) / PAGESIZE;
1998 Bins bin = cast(Bins)this.pagetable[pagenum];
2000 return binsize[bin];
2001 for (size_t i = pagenum + 1; i < this.npages; i++)
2002 if (this.pagetable[i] != B_PAGEPLUS)
2003 return (i - pagenum) * PAGESIZE;
2004 return (this.npages - pagenum) * PAGESIZE;
2009 * Used for sorting pools
2011 int opCmp(in Pool other)
2013 if (baseAddr < other.baseAddr)
2016 return cast(int)(baseAddr > other.baseAddr);
2021 /* ============================ SENTINEL =============================== */
2024 const size_t SENTINEL_PRE = cast(size_t) 0xF4F4F4F4F4F4F4F4UL; // 32 or 64 bits
2025 const ubyte SENTINEL_POST = 0xF5; // 8 bits
2026 const uint SENTINEL_EXTRA = 2 * size_t.sizeof + 1;
2029 size_t* sentinel_size(void *p) { return &(cast(size_t *)p)[-2]; }
2030 size_t* sentinel_pre(void *p) { return &(cast(size_t *)p)[-1]; }
2031 ubyte* sentinel_post(void *p) { return &(cast(ubyte *)p)[*sentinel_size(p)]; }
2034 void sentinel_init(void *p, size_t size)
2036 *sentinel_size(p) = size;
2037 *sentinel_pre(p) = SENTINEL_PRE;
2038 *sentinel_post(p) = SENTINEL_POST;
2042 void sentinel_Invariant(void *p)
2044 assert(*sentinel_pre(p) == SENTINEL_PRE);
2045 assert(*sentinel_post(p) == SENTINEL_POST);
2049 void *sentinel_add(void *p)
2051 return p + 2 * size_t.sizeof;
2055 void *sentinel_sub(void *p)
2057 return p - 2 * size_t.sizeof;
2062 /* ============================ C Public Interface ======================== */
2065 private int _termCleanupLevel=1;
2069 /// sets the cleanup level done by gc
2072 /// 2: fullCollect ignoring stack roots (might crash daemonThreads)
2073 /// result !=0 if the value was invalid
2074 int gc_setTermCleanupLevel(int cLevel)
2076 if (cLevel<0 || cLevel>2) return cLevel;
2077 _termCleanupLevel=cLevel;
2081 /// returns the cleanup level done by gc
2082 int gc_getTermCleanupLevel()
2084 return _termCleanupLevel;
2089 scope (exit) assert (Invariant());
2090 gc = cast(GC*) cstdlib.calloc(1, GC.sizeof);
2093 version (DigitalMars) version(OSX) {
2094 _d_osx_image_init();
2096 // NOTE: The GC must initialize the thread library
2097 // before its first collection.
2103 assert (Invariant());
2104 if (_termCleanupLevel<1) {
2106 } else if (_termCleanupLevel==2){
2107 // a more complete cleanup
2108 // NOTE: There may be daemons threads still running when this routine is
2109 // called. If so, cleaning memory out from under then is a good
2110 // way to make them crash horribly.
2111 // Often this probably doesn't matter much since the app is
2112 // supposed to be shutting down anyway, but for example tests might
2113 // crash (and be considerd failed even if the test was ok).
2114 // thus this is not the default and should be enabled by
2115 // I'm disabling cleanup for now until I can think about it some
2118 // not really a 'collect all' -- still scans static data area, roots,
2120 return locked!(void, () {
2126 // default (safe) clenup
2127 return locked!(void, () {
2135 return locked!(void, () {
2136 assert (Invariant()); scope (exit) assert (Invariant());
2137 assert (gc.disabled > 0);
2144 return locked!(void, () {
2145 assert (Invariant()); scope (exit) assert (Invariant());
2152 return locked!(void, () {
2153 assert (Invariant()); scope (exit) assert (Invariant());
2161 return locked!(void, () {
2162 assert (Invariant()); scope (exit) assert (Invariant());
2167 uint gc_getAttr(void* p)
2171 return locked!(uint, () {
2172 assert (Invariant()); scope (exit) assert (Invariant());
2173 Pool* pool = findPool(p);
2176 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
2177 return getAttr(pool, bit_i);
2181 uint gc_setAttr(void* p, uint attrs)
2185 return locked!(uint, () {
2186 assert (Invariant()); scope (exit) assert (Invariant());
2187 Pool* pool = findPool(p);
2190 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
2191 uint old_attrs = getAttr(pool, bit_i);
2192 setAttr(pool, bit_i, attrs);
2197 uint gc_clrAttr(void* p, uint attrs)
2201 return locked!(uint, () {
2202 assert (Invariant()); scope (exit) assert (Invariant());
2203 Pool* pool = findPool(p);
2206 auto bit_i = cast(size_t)(p - pool.baseAddr) / 16;
2207 uint old_attrs = getAttr(pool, bit_i);
2208 clrAttr(pool, bit_i, attrs);
2213 void* gc_malloc(size_t size, uint attrs = 0,
2214 PointerMap ptrmap = PointerMap.init)
2218 return locked!(void*, () {
2219 assert (Invariant()); scope (exit) assert (Invariant());
2220 return malloc(size, attrs, ptrmap.bits.ptr);
2224 void* gc_calloc(size_t size, uint attrs = 0,
2225 PointerMap ptrmap = PointerMap.init)
2229 return locked!(void*, () {
2230 assert (Invariant()); scope (exit) assert (Invariant());
2231 return calloc(size, attrs, ptrmap.bits.ptr);
2235 void* gc_realloc(void* p, size_t size, uint attrs = 0,
2236 PointerMap ptrmap = PointerMap.init)
2238 return locked!(void*, () {
2239 assert (Invariant()); scope (exit) assert (Invariant());
2240 return realloc(p, size, attrs, ptrmap.bits.ptr);
2244 size_t gc_extend(void* p, size_t min_size, size_t max_size)
2246 return locked!(size_t, () {
2247 assert (Invariant()); scope (exit) assert (Invariant());
2248 return extend(p, min_size, max_size);
2252 size_t gc_reserve(size_t size)
2256 return locked!(size_t, () {
2257 assert (Invariant()); scope (exit) assert (Invariant());
2258 return reserve(size);
2262 void gc_free(void* p)
2266 return locked!(void, () {
2267 assert (Invariant()); scope (exit) assert (Invariant());
2272 void* gc_addrOf(void* p)
2276 return locked!(void*, () {
2277 assert (Invariant()); scope (exit) assert (Invariant());
2278 Pool* pool = findPool(p);
2281 return pool.findBase(p);
2285 size_t gc_sizeOf(void* p)
2289 return locked!(size_t, () {
2290 assert (Invariant()); scope (exit) assert (Invariant());
2295 BlkInfo gc_query(void* p)
2298 return BlkInfo.init;
2299 return locked!(BlkInfo, () {
2300 assert (Invariant()); scope (exit) assert (Invariant());
2305 // NOTE: This routine is experimental. The stats or function name may change
2306 // before it is made officially available.
2309 return locked!(GCStats, () {
2310 assert (Invariant()); scope (exit) assert (Invariant());
2315 void gc_addRoot(void* p)
2319 return locked!(void, () {
2320 assert (Invariant()); scope (exit) assert (Invariant());
2321 if (gc.roots.append(p) is null)
2322 onOutOfMemoryError();
2326 void gc_addRange(void* p, size_t size)
2328 if (p is null || size == 0)
2330 return locked!(void, () {
2331 assert (Invariant()); scope (exit) assert (Invariant());
2332 if (gc.ranges.append(Range(p, p + size)) is null)
2333 onOutOfMemoryError();
2337 void gc_removeRoot(void* p)
2341 return locked!(void, () {
2342 assert (Invariant()); scope (exit) assert (Invariant());
2343 bool r = gc.roots.remove(p);
2348 void gc_removeRange(void* p)
2352 return locked!(void, () {
2353 assert (Invariant()); scope (exit) assert (Invariant());
2354 bool r = gc.ranges.remove(Range(p, null));
2359 void* gc_weakpointerCreate(Object r)
2361 // weakpointers do their own locking
2362 return weakpointerCreate(r);
2365 void gc_weakpointerDestroy(void* wp)
2367 // weakpointers do their own locking
2368 weakpointerDestroy(wp);
2371 Object gc_weakpointerGet(void* wp)
2373 // weakpointers do their own locking
2374 return weakpointerGet(wp);
2378 // vim: set et sw=4 sts=4 :