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;
47 import rt.gc.cdgc.dynarray: 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
91 FINALIZE = 0b0000_0001,
92 NO_SCAN = 0b0000_0010,
93 NO_MOVE = 0b0000_0100,
94 ALL_BITS = 0b1111_1111
97 extern (C) void* rt_stackBottom();
98 extern (C) void* rt_stackTop();
100 extern (C) void rt_finalize( void* p, bool det = true );
102 alias void delegate(Object) DEvent;
103 extern (C) void rt_attachDisposeEvent(Object h, DEvent e);
104 extern (C) bool rt_detachDisposeEvent(Object h, DEvent e);
107 alias void delegate( void*, void* ) scanFn;
109 extern (C) void rt_scanStaticData( scanFn scan );
111 extern (C) bool thread_needLock();
112 extern (C) void thread_suspendAll();
113 extern (C) void thread_resumeAll();
115 extern (C) void thread_scanAll( scanFn fn, void* curStackTop = null );
117 extern (C) void onOutOfMemoryError();
121 OPFAIL = ~cast(size_t)0
129 /* ============================ GC =============================== */
132 class GCLock { } // just a dummy so we can get a global lock
135 const uint GCVERSION = 1; // increment every time we change interface
140 // For passing to debug code
144 uint gcversion = GCVERSION;
146 Gcx *gcx; // implementation
147 static ClassInfo gcLock; // global lock
152 opts.parse(cstdlib.getenv("D_GC_OPTS"));
153 gcLock = GCLock.classinfo;
154 gcx = cast(Gcx*) cstdlib.calloc(1, Gcx.sizeof);
156 onOutOfMemoryError();
158 setStackBottom(rt_stackBottom());
167 if (!thread_needLock())
169 assert(gcx.disabled > 0);
172 else synchronized (gcLock)
174 assert(gcx.disabled > 0);
185 if (!thread_needLock())
189 else synchronized (gcLock)
199 uint getAttr(void* p)
208 Pool* pool = gcx.findPool(p);
213 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
215 oldb = gcx.getBits(pool, biti);
220 if (!thread_needLock())
224 else synchronized (gcLock)
234 uint setAttr(void* p, uint mask)
243 Pool* pool = gcx.findPool(p);
248 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
250 oldb = gcx.getBits(pool, biti);
251 gcx.setBits(pool, biti, mask);
256 if (!thread_needLock())
260 else synchronized (gcLock)
270 uint clrAttr(void* p, uint mask)
279 Pool* pool = gcx.findPool(p);
284 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
286 oldb = gcx.getBits(pool, biti);
287 gcx.clrBits(pool, biti, mask);
292 if (!thread_needLock())
296 else synchronized (gcLock)
306 void *malloc(size_t size, uint bits = 0)
313 if (!thread_needLock())
315 return mallocNoSync(size, bits);
317 else synchronized (gcLock)
319 return mallocNoSync(size, bits);
327 private void *mallocNoSync(size_t size, uint bits = 0)
336 if (opts.options.sentinel)
337 size += SENTINEL_EXTRA;
340 // Cache previous binsize lookup - Dave Fladebo.
341 static size_t lastsize = -1;
343 if (size == lastsize)
347 bin = gcx.findBin(size);
357 if (!gcx.allocPage(bin) && !gcx.disabled) // try to find a new page
359 if (!thread_needLock())
361 /* Then we haven't locked it yet. Be sure
362 * and lock for a collection, since a finalizer
363 * may start a new thread.
365 synchronized (gcLock)
367 gcx.fullcollectshell();
370 else if (!gcx.fullcollectshell()) // collect to find a new page
375 if (!gcx.bucket[bin] && !gcx.allocPage(bin))
377 gcx.newPool(1); // allocate new pool to find a new page
378 int result = gcx.allocPage(bin);
380 onOutOfMemoryError();
385 // Return next item from free list
386 gcx.bucket[bin] = (cast(List*)p).next;
387 if( !(bits & BlkAttr.NO_SCAN) )
388 memset(p + size, 0, binsize[bin] - size);
389 if (opts.options.mem_stomp)
390 memset(p, 0xF0, size);
394 p = gcx.bigAlloc(size);
396 onOutOfMemoryError();
398 if (opts.options.sentinel) {
399 size -= SENTINEL_EXTRA;
401 sentinel_init(p, size);
406 Pool *pool = gcx.findPool(p);
409 gcx.setBits(pool, cast(size_t)(p - pool.baseAddr) / 16, bits);
418 void *calloc(size_t size, uint bits = 0)
425 if (!thread_needLock())
427 return callocNoSync(size, bits);
429 else synchronized (gcLock)
431 return callocNoSync(size, bits);
439 private void *callocNoSync(size_t size, uint bits = 0)
443 void *p = mallocNoSync(size, bits);
452 void *realloc(void *p, size_t size, uint bits = 0)
454 if (!thread_needLock())
456 return reallocNoSync(p, size, bits);
458 else synchronized (gcLock)
460 return reallocNoSync(p, size, bits);
468 private void *reallocNoSync(void *p, size_t size, uint bits = 0)
480 p = mallocNoSync(size, bits);
487 if (opts.options.sentinel)
489 sentinel_Invariant(p);
490 psize = *sentinel_size(p);
495 Pool *pool = gcx.findPool(p);
499 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
503 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
504 gcx.setBits(pool, biti, bits);
508 bits = gcx.getBits(pool, biti);
512 p2 = mallocNoSync(size, bits);
515 cstring.memcpy(p2, p, size);
521 psize = gcx.findSize(p); // find allocated size
522 if (psize >= PAGESIZE && size >= PAGESIZE)
524 auto psz = psize / PAGESIZE;
525 auto newsz = (size + PAGESIZE - 1) / PAGESIZE;
529 auto pool = gcx.findPool(p);
530 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
535 synchronized (gcLock)
537 if (opts.options.mem_stomp)
538 memset(p + size, 0xF2, psize - size);
539 pool.freePages(pagenum + newsz, psz - newsz);
543 else if (pagenum + newsz <= pool.npages)
545 // Attempt to expand in place
546 synchronized (gcLock)
548 for (size_t i = pagenum + psz; 1;)
550 if (i == pagenum + newsz)
552 if (opts.options.mem_stomp)
553 memset(p + psize, 0xF0, size - psize);
554 memset(pool.pagetable + pagenum +
555 psz, B_PAGEPLUS, newsz - psz);
558 if (i == pool.npages)
562 if (pool.pagetable[i] != B_FREE)
569 if (psize < size || // if new size is bigger
570 psize > size * 2) // or less than half
574 Pool *pool = gcx.findPool(p);
578 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
582 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
583 gcx.setBits(pool, biti, bits);
587 bits = gcx.getBits(pool, biti);
591 p2 = mallocNoSync(size, bits);
594 cstring.memcpy(p2, p, size);
604 * Attempt to in-place enlarge the memory block pointed to by p by at least
605 * minbytes beyond its current capacity, up to a maximum of maxsize. This
606 * does not attempt to move the memory block (like realloc() does).
609 * 0 if could not extend p,
610 * total size of entire memory block if successful.
612 size_t extend(void* p, size_t minsize, size_t maxsize)
614 if (!thread_needLock())
616 return extendNoSync(p, minsize, maxsize);
618 else synchronized (gcLock)
620 return extendNoSync(p, minsize, maxsize);
628 private size_t extendNoSync(void* p, size_t minsize, size_t maxsize)
631 assert( minsize <= maxsize );
635 if (opts.options.sentinel)
639 auto psize = gcx.findSize(p); // find allocated size
640 if (psize < PAGESIZE)
641 return 0; // cannot extend buckets
643 auto psz = psize / PAGESIZE;
644 auto minsz = (minsize + PAGESIZE - 1) / PAGESIZE;
645 auto maxsz = (maxsize + PAGESIZE - 1) / PAGESIZE;
647 auto pool = gcx.findPool(p);
648 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
651 for (sz = 0; sz < maxsz; sz++)
653 auto i = pagenum + psz + sz;
654 if (i == pool.npages)
656 if (pool.pagetable[i] != B_FREE)
665 if (opts.options.mem_stomp)
666 memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize);
667 memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz);
670 return (psz + sz) * PAGESIZE;
677 size_t reserve(size_t size)
684 if (!thread_needLock())
686 return reserveNoSync(size);
688 else synchronized (gcLock)
690 return reserveNoSync(size);
698 private size_t reserveNoSync(size_t size)
703 return gcx.reserve(size);
717 if (!thread_needLock())
719 return freeNoSync(p);
721 else synchronized (gcLock)
723 return freeNoSync(p);
731 private void freeNoSync(void *p)
740 // Find which page it is in
741 pool = gcx.findPool(p);
742 if (!pool) // if not one of ours
744 if (opts.options.sentinel) {
745 sentinel_Invariant(p);
748 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
749 biti = cast(size_t)(p - pool.baseAddr) / 16;
750 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
752 bin = cast(Bins)pool.pagetable[pagenum];
753 if (bin == B_PAGE) // if large alloc
758 while (++n < pool.npages && pool.pagetable[n] == B_PAGEPLUS)
760 if (opts.options.mem_stomp)
761 memset(p, 0xF2, npages * PAGESIZE);
762 pool.freePages(pagenum, npages);
767 List *list = cast(List*)p;
769 if (opts.options.mem_stomp)
770 memset(p, 0xF2, binsize[bin]);
772 list.next = gcx.bucket[bin];
773 gcx.bucket[bin] = list;
779 * Determine the base address of the block containing p. If p is not a gc
780 * allocated pointer, return null.
782 void* addrOf(void *p)
789 if (!thread_needLock())
791 return addrOfNoSync(p);
793 else synchronized (gcLock)
795 return addrOfNoSync(p);
803 void* addrOfNoSync(void *p)
810 return gcx.findBase(p);
815 * Determine the allocated size of pointer p. If p is an interior pointer
816 * or not a gc allocated pointer, return 0.
818 size_t sizeOf(void *p)
825 if (!thread_needLock())
827 return sizeOfNoSync(p);
829 else synchronized (gcLock)
831 return sizeOfNoSync(p);
839 private size_t sizeOfNoSync(void *p)
843 if (opts.options.sentinel)
846 size_t size = gcx.findSize(p);
848 // Check for interior pointer
850 // 1) size is a power of 2 for less than PAGESIZE values
851 // 2) base of memory pool is aligned on PAGESIZE boundary
852 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
854 return size ? size - SENTINEL_EXTRA : 0;
858 if (p == gcx.p_cache)
859 return gcx.size_cache;
861 size_t size = gcx.findSize(p);
863 // Check for interior pointer
865 // 1) size is a power of 2 for less than PAGESIZE values
866 // 2) base of memory pool is aligned on PAGESIZE boundary
867 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
872 gcx.size_cache = size;
881 * Determine the base address of the block containing p. If p is not a gc
882 * allocated pointer, return null.
884 BlkInfo query(void *p)
892 if (!thread_needLock())
894 return queryNoSync(p);
896 else synchronized (gcLock)
898 return queryNoSync(p);
906 BlkInfo queryNoSync(void *p)
910 return gcx.getInfo(p);
915 * Verify that pointer p:
916 * 1) belongs to this memory pool
917 * 2) points to the start of an allocated piece of memory
918 * 3) is not on a free list
927 if (!thread_needLock())
931 else synchronized (gcLock)
941 private void checkNoSync(void *p)
945 if (opts.options.sentinel)
946 sentinel_Invariant(p);
954 if (opts.options.sentinel)
956 pool = gcx.findPool(p);
958 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
959 bin = cast(Bins)pool.pagetable[pagenum];
960 assert(bin <= B_PAGE);
962 assert((cast(size_t)p & (size - 1)) == 0);
968 // Check that p is not on a free list
971 for (list = gcx.bucket[bin]; list; list = list.next)
973 assert(cast(void*)list != p);
984 private void setStackBottom(void *p)
986 version (STACKGROWSDOWN)
988 //p = (void *)((uint *)p + 4);
989 if (p > gcx.stackBottom)
996 //p = (void *)((uint *)p - 4);
997 if (p < gcx.stackBottom)
999 gcx.stackBottom = cast(char*)p;
1006 * add p to list of roots
1008 void addRoot(void *p)
1015 if (!thread_needLock())
1017 if (roots.append(p) is null)
1018 onOutOfMemoryError();
1020 else synchronized (gcLock)
1022 if (roots.append(p) is null)
1023 onOutOfMemoryError();
1029 * remove p from list of roots
1031 void removeRoot(void *p)
1039 if (!thread_needLock())
1041 r = roots.remove(p);
1043 else synchronized (gcLock)
1045 r = roots.remove(p);
1052 * add range to scan for roots
1054 void addRange(void *p, size_t sz)
1061 if (!thread_needLock())
1063 if (ranges.append(Range(p, p+sz)) is null)
1064 onOutOfMemoryError();
1066 else synchronized (gcLock)
1068 if (ranges.append(Range(p, p+sz)) is null)
1069 onOutOfMemoryError();
1077 void removeRange(void *p)
1085 if (!thread_needLock())
1087 r = ranges.remove(Range(p, null));
1089 else synchronized (gcLock)
1091 r = ranges.remove(Range(p, null));
1098 * do full garbage collection
1103 if (!thread_needLock())
1105 gcx.fullcollectshell();
1107 else synchronized (gcLock)
1109 gcx.fullcollectshell();
1122 * do full garbage collection ignoring roots
1124 void fullCollectNoStack()
1126 if (!thread_needLock())
1129 gcx.fullcollectshell();
1132 else synchronized (gcLock)
1135 gcx.fullcollectshell();
1142 * minimize free space usage
1146 if (!thread_needLock())
1150 else synchronized (gcLock)
1158 * Retrieve statistics about garbage collection.
1159 * Useful for debugging and tuning.
1161 void getStats(out GCStats stats)
1163 if (!thread_needLock())
1165 getStatsNoSync(stats);
1167 else synchronized (gcLock)
1169 getStatsNoSync(stats);
1177 private void getStatsNoSync(out GCStats stats)
1186 memset(&stats, 0, GCStats.sizeof);
1188 for (n = 0; n < pools.length; n++)
1190 Pool* pool = pools[n];
1191 psize += pool.npages * PAGESIZE;
1192 for (size_t j = 0; j < pool.npages; j++)
1194 Bins bin = cast(Bins)pool.pagetable[j];
1197 else if (bin == B_PAGE)
1199 else if (bin < B_PAGE)
1204 for (n = 0; n < B_PAGE; n++)
1206 for (List *list = gcx.bucket[n]; list; list = list.next)
1207 flsize += binsize[n];
1210 usize = bsize - flsize;
1212 stats.poolsize = psize;
1213 stats.usedsize = bsize - flsize;
1214 stats.freelistsize = flsize;
1217 /******************* weak-reference support *********************/
1219 // call locked if necessary
1220 private T locked(T)(in T delegate() code)
1222 if (thread_needLock)
1223 synchronized(gcLock) return code();
1228 private struct WeakPointer
1232 void ondestroy(Object r)
1234 assert(r is reference);
1235 // lock for memory consistency (parallel readers)
1236 // also ensures that weakpointerDestroy can be called while another
1237 // thread is freeing the reference with "delete"
1238 locked!(void)({ reference = null; });
1243 * Create a weak pointer to the given object.
1244 * Returns a pointer to an opaque struct allocated in C memory.
1246 void* weakpointerCreate( Object r )
1250 // must be allocated in C memory
1251 // 1. to hide the reference from the GC
1252 // 2. the GC doesn't scan delegates added by rt_attachDisposeEvent
1254 auto wp = cast(WeakPointer*)(cstdlib.malloc(WeakPointer.sizeof));
1256 onOutOfMemoryError();
1258 rt_attachDisposeEvent(r, &wp.ondestroy);
1265 * Destroy a weak pointer returned by weakpointerCreate().
1266 * If null is passed, nothing happens.
1268 void weakpointerDestroy( void* p )
1272 auto wp = cast(WeakPointer*)p;
1273 // must be extra careful about the GC or parallel threads
1274 // finalizing the reference at the same time
1277 rt_detachDisposeEvent(wp.reference, &wp.ondestroy);
1284 * Query a weak pointer and return either the object passed to
1285 * weakpointerCreate, or null if it was free'd in the meantime.
1286 * If null is passed, null is returned.
1288 Object weakpointerGet( void* p )
1292 // NOTE: could avoid the lock by using Fawzi style GC counters but
1293 // that'd require core.sync.Atomic and lots of care about memory
1294 // consistency it's an optional optimization see
1295 // http://dsource.org/projects/tango/browser/trunk/user/tango/core/Lifetime.d?rev=5100#L158
1296 return locked!(Object)({
1297 return (cast(WeakPointer*)p).reference;
1304 /* ============================ Gcx =============================== */
1309 POOLSIZE = (4096*256),
1323 B_PAGE, // start of large alloc
1324 B_PAGEPLUS, // continuation of large alloc
1325 B_FREE, // free page
1343 int opCmp(in Range other)
1345 if (pbot < other.pbot)
1348 return cast(int)(pbot > other.pbot);
1353 const uint binsize[B_MAX] = [ 16,32,64,128,256,512,1024,2048,4096 ];
1354 const uint notbinsize[B_MAX] = [ ~(16u-1),~(32u-1),~(64u-1),~(128u-1),~(256u-1),
1355 ~(512u-1),~(1024u-1),~(2048u-1),~(4096u-1) ];
1357 DynArray!(void*) roots;
1359 DynArray!(Range) ranges;
1361 DynArray!(Pool) pools;
1364 /* ============================ Gcx =============================== */
1373 uint noStack; // !=0 means don't scan stack
1374 uint log; // turn on logging
1378 int disabled; // turn off collections if >0
1380 byte *minAddr; // min(baseAddr)
1381 byte *maxAddr; // max(topAddr)
1383 List *bucket[B_MAX]; // free list for each size
1389 (cast(byte*)this)[0 .. Gcx.sizeof] = 0;
1390 stackBottom = cast(char*)&dummy;
1391 //printf("gcx = %p, self = %x\n", this, self);
1396 void Invariant() { }
1403 //printf("Gcx.invariant(): this = %p\n", this);
1406 for (i = 0; i < pools.length; i++)
1408 Pool* pool = pools[i];
1412 assert(minAddr == pool.baseAddr);
1414 if (i + 1 < pools.length)
1416 assert(*pool < pools[i + 1]);
1418 else if (i + 1 == pools.length)
1420 assert(maxAddr == pool.topAddr);
1427 for (i = 0; i < ranges.length; i++)
1429 assert(ranges[i].pbot);
1430 assert(ranges[i].ptop);
1431 assert(ranges[i].pbot <= ranges[i].ptop);
1434 for (i = 0; i < B_PAGE; i++)
1436 for (List *list = bucket[i]; list; list = list.next)
1445 * Find Pool that pointer is in.
1446 * Return null if not in a Pool.
1447 * Assume pools is sorted.
1449 Pool *findPool(void *p)
1451 if (p >= minAddr && p < maxAddr)
1453 if (pools.length == 1)
1458 for (size_t i = 0; i < pools.length; i++)
1460 Pool* pool = pools[i];
1461 if (p < pool.topAddr)
1463 if (pool.baseAddr <= p)
1474 * Find base address of block containing pointer p.
1475 * Returns null if not a gc'd pointer
1477 void* findBase(void *p)
1484 size_t offset = cast(size_t)(p - pool.baseAddr);
1485 size_t pn = offset / PAGESIZE;
1486 Bins bin = cast(Bins)pool.pagetable[pn];
1488 // Adjust bit to be at start of allocated memory block
1491 return pool.baseAddr + (offset & notbinsize[bin]);
1493 else if (bin == B_PAGEPLUS)
1497 --pn, offset -= PAGESIZE;
1498 } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
1500 return pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
1504 // we are in a B_FREE page
1513 * Find size of pointer p.
1514 * Returns 0 if not a gc'd pointer
1516 size_t findSize(void *p)
1527 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1528 bin = cast(Bins)pool.pagetable[pagenum];
1529 size = binsize[bin];
1535 pt = &pool.pagetable[0];
1536 for (i = pagenum + 1; i < pool.npages; i++)
1538 if (pt[i] != B_PAGEPLUS)
1541 size = (i - pagenum) * PAGESIZE;
1551 BlkInfo getInfo(void* p)
1559 size_t offset = cast(size_t)(p - pool.baseAddr);
1560 size_t pn = offset / PAGESIZE;
1561 Bins bin = cast(Bins)pool.pagetable[pn];
1563 ////////////////////////////////////////////////////////////////////
1565 ////////////////////////////////////////////////////////////////////
1569 info.base = pool.baseAddr + (offset & notbinsize[bin]);
1571 else if (bin == B_PAGEPLUS)
1575 --pn, offset -= PAGESIZE;
1577 while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
1579 info.base = pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
1581 // fix bin for use by size calc below
1582 bin = cast(Bins)pool.pagetable[pn];
1585 ////////////////////////////////////////////////////////////////////
1587 ////////////////////////////////////////////////////////////////////
1589 info.size = binsize[bin];
1595 pt = &pool.pagetable[0];
1596 for (i = pn + 1; i < pool.npages; i++)
1598 if (pt[i] != B_PAGEPLUS)
1601 info.size = (i - pn) * PAGESIZE;
1604 ////////////////////////////////////////////////////////////////////
1606 ////////////////////////////////////////////////////////////////////
1608 info.attr = getBits(pool, cast(size_t)(offset / 16));
1615 * Compute bin for size.
1617 static Bins findBin(size_t size)
1626 else if (size <= 32)
1661 * Allocate a new pool of at least size bytes.
1662 * Sort it into pools.
1663 * Mark all memory in the pool as B_FREE.
1664 * Return the actual number of bytes reserved or 0 on error.
1666 size_t reserve(size_t size)
1668 size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
1669 Pool* pool = newPool(npages);
1673 return pool.npages * PAGESIZE;
1678 * Minimizes physical memory usage by returning free pools to the OS.
1686 for (n = 0; n < pools.length; n++)
1689 for (pn = 0; pn < pool.npages; pn++)
1691 if (cast(Bins)pool.pagetable[pn] != B_FREE)
1694 if (pn < pool.npages)
1700 minAddr = pools[0].baseAddr;
1701 maxAddr = pools[pools.length - 1].topAddr;
1706 * Allocate a chunk of memory that is larger than a page.
1707 * Return null if out of memory.
1709 void *bigAlloc(size_t size)
1719 npages = (size + PAGESIZE - 1) / PAGESIZE;
1723 // This code could use some refinement when repeatedly
1724 // allocating very large arrays.
1726 for (n = 0; n < pools.length; n++)
1729 pn = pool.allocPages(npages);
1744 freedpages = fullcollectshell();
1745 if (freedpages >= pools.length * ((POOLSIZE / PAGESIZE) / 4))
1750 // Release empty pools to prevent bloat
1752 // Allocate new pool
1753 pool = newPool(npages);
1759 pn = pool.allocPages(npages);
1760 assert(pn != OPFAIL);
1763 // Release empty pools to prevent bloat
1765 // Allocate new pool
1766 pool = newPool(npages);
1769 pn = pool.allocPages(npages);
1770 assert(pn != OPFAIL);
1780 pool.pagetable[pn] = B_PAGE;
1782 memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1);
1783 p = pool.baseAddr + pn * PAGESIZE;
1784 memset(cast(char *)p + size, 0, npages * PAGESIZE - size);
1785 if (opts.options.mem_stomp)
1786 memset(p, 0xF1, size);
1790 return null; // let mallocNoSync handle the error
1795 * Allocate a new pool with at least npages in it.
1796 * Sort it into pools.
1797 * Return null if failed.
1799 Pool *newPool(size_t npages)
1801 // Minimum of POOLSIZE
1802 if (npages < POOLSIZE/PAGESIZE)
1803 npages = POOLSIZE/PAGESIZE;
1804 else if (npages > POOLSIZE/PAGESIZE)
1806 // Give us 150% of requested size, so there's room to extend
1807 auto n = npages + (npages >> 1);
1808 if (n < size_t.max/PAGESIZE)
1812 // Allocate successively larger pools up to 8 megs
1815 size_t n = pools.length;
1817 n = 8; // cap pool size at 8 megs
1818 n *= (POOLSIZE / PAGESIZE);
1824 p.initialize(npages);
1831 Pool* pool = pools.insert_sorted(p);
1834 minAddr = pools[0].baseAddr;
1835 maxAddr = pools[pools.length - 1].topAddr;
1842 * Allocate a page of bin's.
1846 int allocPage(Bins bin)
1854 for (n = 0; n < pools.length; n++)
1857 pn = pool.allocPages(1);
1864 pool.pagetable[pn] = cast(ubyte)bin;
1866 // Convert page to free list
1867 size_t size = binsize[bin];
1868 List **b = &bucket[bin];
1870 p = pool.baseAddr + pn * PAGESIZE;
1871 ptop = p + PAGESIZE;
1872 for (; p < ptop; p += size)
1874 (cast(List *)p).next = *b;
1882 * Search a range of memory values and mark any pointers into the GC pool.
1884 void mark(void *pbot, void *ptop)
1886 void **p1 = cast(void **)pbot;
1887 void **p2 = cast(void **)ptop;
1891 //printf("marking range: %p -> %p\n", pbot, ptop);
1892 for (; p1 < p2; p1++)
1895 byte *p = cast(byte *)(*p1);
1897 if (p >= minAddr && p < maxAddr)
1899 if ((cast(size_t)p & ~(PAGESIZE-1)) == pcache)
1905 size_t offset = cast(size_t)(p - pool.baseAddr);
1907 size_t pn = offset / PAGESIZE;
1908 Bins bin = cast(Bins)pool.pagetable[pn];
1910 // Adjust bit to be at start of allocated memory block
1912 biti = (offset & notbinsize[bin]) >> 4;
1913 else if (bin == B_PAGEPLUS)
1919 while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
1920 biti = pn * (PAGESIZE / 16);
1924 // Don't mark bits in B_FREE pages
1928 if (bin >= B_PAGE) // Cache B_PAGE and B_PAGEPLUS lookups
1929 pcache = cast(size_t)p & ~(PAGESIZE-1);
1931 if (!pool.mark.test(biti))
1933 pool.mark.set(biti);
1934 if (!pool.noscan.test(biti))
1936 pool.scan.set(biti);
1943 anychanges |= changes;
1948 * Return number of full pages free'd.
1950 size_t fullcollectshell()
1952 // The purpose of the 'shell' is to ensure all the registers
1953 // get put on the stack so they'll be scanned
1958 gcc.builtins.__builtin_unwind_init();
1965 uint eax,ecx,edx,ebx,ebp,esi,edi;
1978 else version (X86_64)
1980 ulong rax,rbx,rcx,rdx,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15;
1983 movq rax[RBP], RAX ;
1984 movq rbx[RBP], RBX ;
1985 movq rcx[RBP], RCX ;
1986 movq rdx[RBP], RDX ;
1987 movq rbp[RBP], RBP ;
1988 movq rsi[RBP], RSI ;
1989 movq rdi[RBP], RDI ;
1992 movq r10[RBP], R10 ;
1993 movq r11[RBP], R11 ;
1994 movq r12[RBP], R12 ;
1995 movq r13[RBP], R13 ;
1996 movq r14[RBP], R14 ;
1997 movq r15[RBP], R15 ;
2003 static assert( false, "Architecture not supported." );
2014 result = fullcollect(sp);
2037 size_t fullcollect(void *stackTop)
2042 debug(COLLECT_PRINTF) printf("Gcx.fullcollect()\n");
2044 thread_suspendAll();
2050 for (n = 0; n < pools.length; n++)
2055 pool.freebits.zero();
2058 // Mark each free entry, so it doesn't get scanned
2059 for (n = 0; n < B_PAGE; n++)
2061 for (List *list = bucket[n]; list; list = list.next)
2063 pool = findPool(list);
2065 pool.freebits.set(cast(size_t)(cast(byte*)list - pool.baseAddr) / 16);
2069 for (n = 0; n < pools.length; n++)
2072 pool.mark.copy(&pool.freebits);
2075 rt_scanStaticData( &mark );
2079 // Scan stacks and registers for each paused thread
2080 thread_scanAll( &mark, stackTop );
2084 debug(COLLECT_PRINTF) printf("scan roots[]\n");
2085 mark(roots.ptr, roots.ptr + roots.length);
2088 debug(COLLECT_PRINTF) printf("scan ranges[]\n");
2090 for (n = 0; n < ranges.length; n++)
2092 debug(COLLECT_PRINTF) printf("\t%x .. %x\n", ranges[n].pbot, ranges[n].ptop);
2093 mark(ranges[n].pbot, ranges[n].ptop);
2097 debug(COLLECT_PRINTF) printf("\tscan heap\n");
2101 for (n = 0; n < pools.length; n++)
2109 bbase = pool.scan.base();
2110 btop = bbase + pool.scan.nwords;
2111 for (b = bbase; b < btop;)
2127 o = pool.baseAddr + (b - bbase) * 32 * 16;
2128 if (!(bitm & 0xFFFF))
2133 for (; bitm; o += 16, bitm >>= 1)
2138 pn = cast(size_t)(o - pool.baseAddr) / PAGESIZE;
2139 bin = cast(Bins)pool.pagetable[pn];
2142 mark(o, o + binsize[bin]);
2144 else if (bin == B_PAGE || bin == B_PAGEPLUS)
2146 if (bin == B_PAGEPLUS)
2148 while (pool.pagetable[pn - 1] != B_PAGE)
2152 while (pn + u < pool.npages && pool.pagetable[pn + u] == B_PAGEPLUS)
2154 mark(o, o + u * PAGESIZE);
2163 // Free up everything not marked
2164 debug(COLLECT_PRINTF) printf("\tfree'ing\n");
2165 size_t freedpages = 0;
2167 for (n = 0; n < pools.length; n++)
2170 uint* bbase = pool.mark.base();
2172 for (pn = 0; pn < pool.npages; pn++, bbase += PAGESIZE / (32 * 16))
2174 Bins bin = cast(Bins)pool.pagetable[pn];
2178 auto size = binsize[bin];
2179 byte* p = pool.baseAddr + pn * PAGESIZE;
2180 byte* ptop = p + PAGESIZE;
2181 size_t biti = pn * (PAGESIZE/16);
2182 size_t bitstride = size / 16;
2184 version(none) // BUG: doesn't work because freebits() must also be cleared
2186 // If free'd entire page
2187 if (bbase[0] == 0 && bbase[1] == 0 && bbase[2] == 0 && bbase[3] == 0 &&
2188 bbase[4] == 0 && bbase[5] == 0 && bbase[6] == 0 && bbase[7] == 0)
2190 for (; p < ptop; p += size, biti += bitstride)
2192 if (pool.finals.nbits && pool.finals.testClear(biti)) {
2193 if (opts.options.sentinel)
2194 rt_finalize(cast(List *)sentinel_add(p), false/*noStack > 0*/);
2196 rt_finalize(cast(List *)p, false/*noStack > 0*/);
2198 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
2200 List *list = cast(List *)p;
2202 if (opts.options.mem_stomp)
2203 memset(p, 0xF3, size);
2205 pool.pagetable[pn] = B_FREE;
2210 for (; p < ptop; p += size, biti += bitstride)
2212 if (!pool.mark.test(biti))
2214 if (opts.options.sentinel)
2215 sentinel_Invariant(sentinel_add(p));
2217 pool.freebits.set(biti);
2218 if (pool.finals.nbits && pool.finals.testClear(biti)) {
2219 if (opts.options.sentinel)
2220 rt_finalize(cast(List *)sentinel_add(p), false/*noStack > 0*/);
2222 rt_finalize(cast(List *)p, false/*noStack > 0*/);
2224 clrBits(pool, biti, BlkAttr.ALL_BITS);
2226 List *list = cast(List *)p;
2228 if (opts.options.mem_stomp)
2229 memset(p, 0xF3, size);
2235 else if (bin == B_PAGE)
2237 size_t biti = pn * (PAGESIZE / 16);
2238 if (!pool.mark.test(biti))
2240 byte *p = pool.baseAddr + pn * PAGESIZE;
2241 if (opts.options.sentinel)
2242 sentinel_Invariant(sentinel_add(p));
2243 if (pool.finals.nbits && pool.finals.testClear(biti)) {
2244 if (opts.options.sentinel)
2245 rt_finalize(sentinel_add(p), false/*noStack > 0*/);
2247 rt_finalize(p, false/*noStack > 0*/);
2249 clrBits(pool, biti, BlkAttr.ALL_BITS);
2251 debug(COLLECT_PRINTF) printf("\tcollecting big %x\n", p);
2252 pool.pagetable[pn] = B_FREE;
2254 if (opts.options.mem_stomp)
2255 memset(p, 0xF3, PAGESIZE);
2256 while (pn + 1 < pool.npages && pool.pagetable[pn + 1] == B_PAGEPLUS)
2259 pool.pagetable[pn] = B_FREE;
2262 if (opts.options.mem_stomp)
2265 memset(p, 0xF3, PAGESIZE);
2276 // Free complete pages, rebuild free list
2277 debug(COLLECT_PRINTF) printf("\tfree complete pages\n");
2278 size_t recoveredpages = 0;
2279 for (n = 0; n < pools.length; n++)
2282 for (size_t pn = 0; pn < pool.npages; pn++)
2284 Bins bin = cast(Bins)pool.pagetable[pn];
2290 size_t size = binsize[bin];
2291 size_t bitstride = size / 16;
2292 size_t bitbase = pn * (PAGESIZE / 16);
2293 size_t bittop = bitbase + (PAGESIZE / 16);
2297 for (biti = bitbase; biti < bittop; biti += bitstride)
2299 if (!pool.freebits.test(biti))
2302 pool.pagetable[pn] = B_FREE;
2307 p = pool.baseAddr + pn * PAGESIZE;
2308 for (u = 0; u < PAGESIZE; u += size)
2310 biti = bitbase + u / 16;
2311 if (pool.freebits.test(biti))
2313 List *list = cast(List *)(p + u);
2314 if (list.next != bucket[bin]) // avoid unnecessary writes
2315 list.next = bucket[bin];
2323 debug(COLLECT_PRINTF) printf("recovered pages = %d\n", recoveredpages);
2324 debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, pools.length);
2326 return freedpages + recoveredpages;
2333 uint getBits(Pool* pool, size_t biti)
2342 if (pool.finals.nbits &&
2343 pool.finals.test(biti))
2344 bits |= BlkAttr.FINALIZE;
2345 if (pool.noscan.test(biti))
2346 bits |= BlkAttr.NO_SCAN;
2347 // if (pool.nomove.nbits &&
2348 // pool.nomove.test(biti))
2349 // bits |= BlkAttr.NO_MOVE;
2357 void setBits(Pool* pool, size_t biti, uint mask)
2364 if (mask & BlkAttr.FINALIZE)
2366 if (!pool.finals.nbits)
2367 pool.finals.alloc(pool.mark.nbits);
2368 pool.finals.set(biti);
2370 if (mask & BlkAttr.NO_SCAN)
2372 pool.noscan.set(biti);
2374 // if (mask & BlkAttr.NO_MOVE)
2376 // if (!pool.nomove.nbits)
2377 // pool.nomove.alloc(pool.mark.nbits);
2378 // pool.nomove.set(biti);
2386 void clrBits(Pool* pool, size_t biti, uint mask)
2393 if (mask & BlkAttr.FINALIZE && pool.finals.nbits)
2394 pool.finals.clear(biti);
2395 if (mask & BlkAttr.NO_SCAN)
2396 pool.noscan.clear(biti);
2397 // if (mask & BlkAttr.NO_MOVE && pool.nomove.nbits)
2398 // pool.nomove.clear(biti);
2404 /* ============================ Pool =============================== */
2411 GCBits mark; // entries already scanned, or should not be scanned
2412 GCBits scan; // entries that need to be scanned
2413 GCBits freebits; // entries that are on the free list
2414 GCBits finals; // entries that need finalizer run on them
2415 GCBits noscan; // entries that should not be scanned
2421 void initialize(size_t npages)
2423 size_t poolsize = npages * PAGESIZE;
2424 assert(poolsize >= POOLSIZE);
2425 baseAddr = cast(byte *) alloc.os_mem_map(poolsize);
2427 // Some of the code depends on page alignment of memory pools
2428 assert((cast(size_t)baseAddr & (PAGESIZE - 1)) == 0);
2436 topAddr = baseAddr + poolsize;
2438 mark.alloc(cast(size_t)poolsize / 16);
2439 scan.alloc(cast(size_t)poolsize / 16);
2440 freebits.alloc(cast(size_t)poolsize / 16);
2441 noscan.alloc(cast(size_t)poolsize / 16);
2443 pagetable = cast(ubyte*) cstdlib.malloc(npages);
2445 onOutOfMemoryError();
2446 memset(pagetable, B_FREE, npages);
2448 this.npages = npages;
2460 result = alloc.os_mem_unmap(baseAddr, npages * PAGESIZE);
2468 // See Gcx.Dtor() for the rationale of the null check.
2470 cstdlib.free(pagetable);
2480 void Invariant() { }
2487 //freebits.Invariant();
2488 //finals.Invariant();
2489 //noscan.Invariant();
2493 //if (baseAddr + npages * PAGESIZE != topAddr)
2494 //printf("baseAddr = %p, npages = %d, topAddr = %p\n", baseAddr, npages, topAddr);
2495 assert(baseAddr + npages * PAGESIZE == topAddr);
2498 for (size_t i = 0; i < npages; i++)
2500 Bins bin = cast(Bins)pagetable[i];
2501 assert(bin < B_MAX);
2507 * Allocate n pages from Pool.
2508 * Returns OPFAIL on failure.
2510 size_t allocPages(size_t n)
2516 for (i = 0; i < npages; i++)
2518 if (pagetable[i] == B_FREE)
2531 * Free npages pages starting with pagenum.
2533 void freePages(size_t pagenum, size_t npages)
2535 memset(&pagetable[pagenum], B_FREE, npages);
2540 * Used for sorting pools
2542 int opCmp(in Pool other)
2544 if (baseAddr < other.baseAddr)
2547 return cast(int)(baseAddr > other.baseAddr);
2552 /* ============================ SENTINEL =============================== */
2555 const size_t SENTINEL_PRE = cast(size_t) 0xF4F4F4F4F4F4F4F4UL; // 32 or 64 bits
2556 const ubyte SENTINEL_POST = 0xF5; // 8 bits
2557 const uint SENTINEL_EXTRA = 2 * size_t.sizeof + 1;
2560 size_t* sentinel_size(void *p) { return &(cast(size_t *)p)[-2]; }
2561 size_t* sentinel_pre(void *p) { return &(cast(size_t *)p)[-1]; }
2562 ubyte* sentinel_post(void *p) { return &(cast(ubyte *)p)[*sentinel_size(p)]; }
2565 void sentinel_init(void *p, size_t size)
2567 *sentinel_size(p) = size;
2568 *sentinel_pre(p) = SENTINEL_PRE;
2569 *sentinel_post(p) = SENTINEL_POST;
2573 void sentinel_Invariant(void *p)
2575 assert(*sentinel_pre(p) == SENTINEL_PRE);
2576 assert(*sentinel_post(p) == SENTINEL_POST);
2580 void *sentinel_add(void *p)
2582 return p + 2 * size_t.sizeof;
2586 void *sentinel_sub(void *p)
2588 return p - 2 * size_t.sizeof;
2592 // vim: set et sw=4 sts=4 :