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 = LOGGING; // log allocations / frees
35 //debug = MEMSTOMP; // stomp on memory
36 //debug = SENTINEL; // add underrun/overrrun protection
37 //debug = PTRCHECK; // more pointer checking
38 //debug = PTRCHECK2; // thorough but slow pointer checking
40 /*************** Configuration *********************/
42 version = STACKGROWSDOWN; // growing the stack means subtracting from the stack pointer
43 // (use for Intel X86 CPUs)
44 // else growing the stack means adding to the stack pointer
46 /***************************************************/
48 import rt.gc.cdgc.bits: GCBits;
49 import rt.gc.cdgc.stats: GCStats;
50 import alloc = rt.gc.cdgc.alloc;
51 import libc = rt.gc.cdgc.libc;
56 // BUG: The following import will likely not work, since the gcc
57 // subdirectory is elsewhere. Instead, perhaps the functions
58 // could be declared directly or some other resolution could
60 static import gcc.builtins; // for __builtin_unwind_int
75 FINALIZE = 0b0000_0001,
76 NO_SCAN = 0b0000_0010,
77 NO_MOVE = 0b0000_0100,
78 ALL_BITS = 0b1111_1111
81 extern (C) void* rt_stackBottom();
82 extern (C) void* rt_stackTop();
84 extern (C) void rt_finalize( void* p, bool det = true );
86 alias void delegate(Object) DEvent;
87 extern (C) void rt_attachDisposeEvent(Object h, DEvent e);
88 extern (C) bool rt_detachDisposeEvent(Object h, DEvent e);
91 alias void delegate( void*, void* ) scanFn;
93 extern (C) void rt_scanStaticData( scanFn scan );
95 extern (C) bool thread_needLock();
96 extern (C) void thread_suspendAll();
97 extern (C) void thread_resumeAll();
99 extern (C) void thread_scanAll( scanFn fn, void* curStackTop = null );
101 extern (C) void onOutOfMemoryError();
105 OPFAIL = ~cast(size_t)0
113 /* ======================= Leak Detector =========================== */
128 printf(" p = %x, size = %d, parent = %x ", p, size, parent);
131 printf("%s(%u)", file, line);
151 void reserve(size_t nentries)
153 assert(dim <= allocdim);
154 if (allocdim - dim < nentries)
156 allocdim = (dim + nentries) * 2;
157 assert(dim + nentries <= allocdim);
160 data = cast(Log*) libc.malloc(allocdim * Log.sizeof);
161 if (!data && allocdim)
162 onOutOfMemoryError();
166 Log *newdata = cast(Log*) libc.malloc(
167 allocdim * Log.sizeof);
168 if (!newdata && allocdim)
169 onOutOfMemoryError();
170 libc.memcpy(newdata, data, dim * Log.sizeof);
184 void remove(size_t i)
186 libc.memmove(data + i, data + i + 1, (dim - i) * Log.sizeof);
193 for (size_t i = 0; i < dim; i++)
198 return OPFAIL; // not found
202 void copy(LogArray *from)
204 reserve(from.dim - dim);
205 assert(from.dim <= allocdim);
206 libc.memcpy(data, from.data, from.dim * Log.sizeof);
213 /* ============================ GC =============================== */
216 class GCLock { } // just a dummy so we can get a global lock
219 const uint GCVERSION = 1; // increment every time we change interface
224 // For passing to debug code
228 uint gcversion = GCVERSION;
230 Gcx *gcx; // implementation
231 static ClassInfo gcLock; // global lock
236 gcLock = GCLock.classinfo;
237 gcx = cast(Gcx*) libc.calloc(1, Gcx.sizeof);
239 onOutOfMemoryError();
241 setStackBottom(rt_stackBottom());
261 if (!thread_needLock())
263 assert(gcx.disabled > 0);
266 else synchronized (gcLock)
268 assert(gcx.disabled > 0);
279 if (!thread_needLock())
283 else synchronized (gcLock)
293 uint getAttr(void* p)
302 Pool* pool = gcx.findPool(p);
307 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
309 oldb = gcx.getBits(pool, biti);
314 if (!thread_needLock())
318 else synchronized (gcLock)
328 uint setAttr(void* p, uint mask)
337 Pool* pool = gcx.findPool(p);
342 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
344 oldb = gcx.getBits(pool, biti);
345 gcx.setBits(pool, biti, mask);
350 if (!thread_needLock())
354 else synchronized (gcLock)
364 uint clrAttr(void* p, uint mask)
373 Pool* pool = gcx.findPool(p);
378 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
380 oldb = gcx.getBits(pool, biti);
381 gcx.clrBits(pool, biti, mask);
386 if (!thread_needLock())
390 else synchronized (gcLock)
400 void *malloc(size_t size, uint bits = 0)
407 if (!thread_needLock())
409 return mallocNoSync(size, bits);
411 else synchronized (gcLock)
413 return mallocNoSync(size, bits);
421 private void *mallocNoSync(size_t size, uint bits = 0)
430 size += SENTINEL_EXTRA;
433 // Cache previous binsize lookup - Dave Fladebo.
434 static size_t lastsize = -1;
436 if (size == lastsize)
440 bin = gcx.findBin(size);
450 if (!gcx.allocPage(bin) && !gcx.disabled) // try to find a new page
452 if (!thread_needLock())
454 /* Then we haven't locked it yet. Be sure
455 * and lock for a collection, since a finalizer
456 * may start a new thread.
458 synchronized (gcLock)
460 gcx.fullcollectshell();
463 else if (!gcx.fullcollectshell()) // collect to find a new page
468 if (!gcx.bucket[bin] && !gcx.allocPage(bin))
470 gcx.newPool(1); // allocate new pool to find a new page
471 int result = gcx.allocPage(bin);
473 onOutOfMemoryError();
478 // Return next item from free list
479 gcx.bucket[bin] = (cast(List*)p).next;
480 if( !(bits & BlkAttr.NO_SCAN) )
481 libc.memset(p + size, 0, binsize[bin] - size);
482 debug (MEMSTOMP) libc.memset(p, 0xF0, size);
486 p = gcx.bigAlloc(size);
488 onOutOfMemoryError();
490 size -= SENTINEL_EXTRA;
492 sentinel_init(p, size);
493 gcx.log_malloc(p, size);
497 Pool *pool = gcx.findPool(p);
500 gcx.setBits(pool, cast(size_t)(p - pool.baseAddr) / 16, bits);
509 void *calloc(size_t size, uint bits = 0)
516 if (!thread_needLock())
518 return callocNoSync(size, bits);
520 else synchronized (gcLock)
522 return callocNoSync(size, bits);
530 private void *callocNoSync(size_t size, uint bits = 0)
534 void *p = mallocNoSync(size, bits);
535 libc.memset(p, 0, size);
543 void *realloc(void *p, size_t size, uint bits = 0)
545 if (!thread_needLock())
547 return reallocNoSync(p, size, bits);
549 else synchronized (gcLock)
551 return reallocNoSync(p, size, bits);
559 private void *reallocNoSync(void *p, size_t size, uint bits = 0)
571 p = mallocNoSync(size, bits);
580 sentinel_Invariant(p);
581 psize = *sentinel_size(p);
586 Pool *pool = gcx.findPool(p);
590 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
594 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
595 gcx.setBits(pool, biti, bits);
599 bits = gcx.getBits(pool, biti);
603 p2 = mallocNoSync(size, bits);
606 libc.memcpy(p2, p, size);
612 psize = gcx.findSize(p); // find allocated size
613 if (psize >= PAGESIZE && size >= PAGESIZE)
615 auto psz = psize / PAGESIZE;
616 auto newsz = (size + PAGESIZE - 1) / PAGESIZE;
620 auto pool = gcx.findPool(p);
621 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
626 synchronized (gcLock)
629 libc.memset(p + size, 0xF2, psize - size);
630 pool.freePages(pagenum + newsz, psz - newsz);
634 else if (pagenum + newsz <= pool.npages)
636 // Attempt to expand in place
637 synchronized (gcLock)
639 for (size_t i = pagenum + psz; 1;)
641 if (i == pagenum + newsz)
644 libc.memset(p + psize, 0xF0,
646 libc.memset(&pool.pagetable[pagenum + psz],
647 B_PAGEPLUS, newsz - psz);
650 if (i == pool.npages)
654 if (pool.pagetable[i] != B_FREE)
661 if (psize < size || // if new size is bigger
662 psize > size * 2) // or less than half
666 Pool *pool = gcx.findPool(p);
670 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
674 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
675 gcx.setBits(pool, biti, bits);
679 bits = gcx.getBits(pool, biti);
683 p2 = mallocNoSync(size, bits);
686 libc.memcpy(p2, p, size);
696 * Attempt to in-place enlarge the memory block pointed to by p by at least
697 * minbytes beyond its current capacity, up to a maximum of maxsize. This
698 * does not attempt to move the memory block (like realloc() does).
701 * 0 if could not extend p,
702 * total size of entire memory block if successful.
704 size_t extend(void* p, size_t minsize, size_t maxsize)
706 if (!thread_needLock())
708 return extendNoSync(p, minsize, maxsize);
710 else synchronized (gcLock)
712 return extendNoSync(p, minsize, maxsize);
720 private size_t extendNoSync(void* p, size_t minsize, size_t maxsize)
723 assert( minsize <= maxsize );
731 auto psize = gcx.findSize(p); // find allocated size
732 if (psize < PAGESIZE)
733 return 0; // cannot extend buckets
735 auto psz = psize / PAGESIZE;
736 auto minsz = (minsize + PAGESIZE - 1) / PAGESIZE;
737 auto maxsz = (maxsize + PAGESIZE - 1) / PAGESIZE;
739 auto pool = gcx.findPool(p);
740 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
743 for (sz = 0; sz < maxsz; sz++)
745 auto i = pagenum + psz + sz;
746 if (i == pool.npages)
748 if (pool.pagetable[i] != B_FREE)
758 libc.memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize);
759 libc.memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz);
762 return (psz + sz) * PAGESIZE;
769 size_t reserve(size_t size)
776 if (!thread_needLock())
778 return reserveNoSync(size);
780 else synchronized (gcLock)
782 return reserveNoSync(size);
790 private size_t reserveNoSync(size_t size)
795 return gcx.reserve(size);
809 if (!thread_needLock())
811 return freeNoSync(p);
813 else synchronized (gcLock)
815 return freeNoSync(p);
823 private void freeNoSync(void *p)
832 // Find which page it is in
833 pool = gcx.findPool(p);
834 if (!pool) // if not one of ours
836 sentinel_Invariant(p);
838 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
839 biti = cast(size_t)(p - pool.baseAddr) / 16;
840 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
842 bin = cast(Bins)pool.pagetable[pagenum];
843 if (bin == B_PAGE) // if large alloc
848 while (++n < pool.npages && pool.pagetable[n] == B_PAGEPLUS)
850 debug (MEMSTOMP) libc.memset(p, 0xF2, npages * PAGESIZE);
851 pool.freePages(pagenum, npages);
856 List *list = cast(List*)p;
858 debug (MEMSTOMP) libc.memset(p, 0xF2, binsize[bin]);
860 list.next = gcx.bucket[bin];
861 gcx.bucket[bin] = list;
863 gcx.log_free(sentinel_add(p));
868 * Determine the base address of the block containing p. If p is not a gc
869 * allocated pointer, return null.
871 void* addrOf(void *p)
878 if (!thread_needLock())
880 return addrOfNoSync(p);
882 else synchronized (gcLock)
884 return addrOfNoSync(p);
892 void* addrOfNoSync(void *p)
899 return gcx.findBase(p);
904 * Determine the allocated size of pointer p. If p is an interior pointer
905 * or not a gc allocated pointer, return 0.
907 size_t sizeOf(void *p)
914 if (!thread_needLock())
916 return sizeOfNoSync(p);
918 else synchronized (gcLock)
920 return sizeOfNoSync(p);
928 private size_t sizeOfNoSync(void *p)
935 size_t size = gcx.findSize(p);
937 // Check for interior pointer
939 // 1) size is a power of 2 for less than PAGESIZE values
940 // 2) base of memory pool is aligned on PAGESIZE boundary
941 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
943 return size ? size - SENTINEL_EXTRA : 0;
947 if (p == gcx.p_cache)
948 return gcx.size_cache;
950 size_t size = gcx.findSize(p);
952 // Check for interior pointer
954 // 1) size is a power of 2 for less than PAGESIZE values
955 // 2) base of memory pool is aligned on PAGESIZE boundary
956 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
961 gcx.size_cache = size;
970 * Determine the base address of the block containing p. If p is not a gc
971 * allocated pointer, return null.
973 BlkInfo query(void *p)
981 if (!thread_needLock())
983 return queryNoSync(p);
985 else synchronized (gcLock)
987 return queryNoSync(p);
995 BlkInfo queryNoSync(void *p)
999 return gcx.getInfo(p);
1004 * Verify that pointer p:
1005 * 1) belongs to this memory pool
1006 * 2) points to the start of an allocated piece of memory
1007 * 3) is not on a free list
1016 if (!thread_needLock())
1020 else synchronized (gcLock)
1030 private void checkNoSync(void *p)
1034 sentinel_Invariant(p);
1042 p = sentinel_sub(p);
1043 pool = gcx.findPool(p);
1045 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1046 bin = cast(Bins)pool.pagetable[pagenum];
1047 assert(bin <= B_PAGE);
1048 size = binsize[bin];
1049 assert((cast(size_t)p & (size - 1)) == 0);
1055 // Check that p is not on a free list
1058 for (list = gcx.bucket[bin]; list; list = list.next)
1060 assert(cast(void*)list != p);
1071 private void setStackBottom(void *p)
1073 version (STACKGROWSDOWN)
1075 //p = (void *)((uint *)p + 4);
1076 if (p > gcx.stackBottom)
1078 gcx.stackBottom = p;
1083 //p = (void *)((uint *)p - 4);
1084 if (p < gcx.stackBottom)
1086 gcx.stackBottom = cast(char*)p;
1093 * add p to list of roots
1095 void addRoot(void *p)
1102 if (!thread_needLock())
1106 else synchronized (gcLock)
1114 * remove p from list of roots
1116 void removeRoot(void *p)
1123 if (!thread_needLock())
1127 else synchronized (gcLock)
1135 * add range to scan for roots
1137 void addRange(void *p, size_t sz)
1144 if (!thread_needLock())
1146 gcx.addRange(p, p + sz);
1148 else synchronized (gcLock)
1150 gcx.addRange(p, p + sz);
1158 void removeRange(void *p)
1165 if (!thread_needLock())
1169 else synchronized (gcLock)
1177 * do full garbage collection
1182 if (!thread_needLock())
1184 gcx.fullcollectshell();
1186 else synchronized (gcLock)
1188 gcx.fullcollectshell();
1202 * do full garbage collection ignoring roots
1204 void fullCollectNoStack()
1206 if (!thread_needLock())
1209 gcx.fullcollectshell();
1212 else synchronized (gcLock)
1215 gcx.fullcollectshell();
1222 * minimize free space usage
1226 if (!thread_needLock())
1230 else synchronized (gcLock)
1238 * Retrieve statistics about garbage collection.
1239 * Useful for debugging and tuning.
1241 void getStats(out GCStats stats)
1243 if (!thread_needLock())
1245 getStatsNoSync(stats);
1247 else synchronized (gcLock)
1249 getStatsNoSync(stats);
1257 private void getStatsNoSync(out GCStats stats)
1266 libc.memset(&stats, 0, GCStats.sizeof);
1268 for (n = 0; n < gcx.npools; n++)
1270 Pool *pool = gcx.pooltable[n];
1271 psize += pool.npages * PAGESIZE;
1272 for (size_t j = 0; j < pool.npages; j++)
1274 Bins bin = cast(Bins)pool.pagetable[j];
1277 else if (bin == B_PAGE)
1279 else if (bin < B_PAGE)
1284 for (n = 0; n < B_PAGE; n++)
1286 for (List *list = gcx.bucket[n]; list; list = list.next)
1287 flsize += binsize[n];
1290 usize = bsize - flsize;
1292 stats.poolsize = psize;
1293 stats.usedsize = bsize - flsize;
1294 stats.freelistsize = flsize;
1297 /******************* weak-reference support *********************/
1299 // call locked if necessary
1300 private T locked(T)(in T delegate() code)
1302 if (thread_needLock)
1303 synchronized(gcLock) return code();
1308 private struct WeakPointer
1312 void ondestroy(Object r)
1314 assert(r is reference);
1315 // lock for memory consistency (parallel readers)
1316 // also ensures that weakpointerDestroy can be called while another
1317 // thread is freeing the reference with "delete"
1318 locked!(void)({ reference = null; });
1323 * Create a weak pointer to the given object.
1324 * Returns a pointer to an opaque struct allocated in C memory.
1326 void* weakpointerCreate( Object r )
1330 // must be allocated in C memory
1331 // 1. to hide the reference from the GC
1332 // 2. the GC doesn't scan delegates added by rt_attachDisposeEvent
1334 auto wp = cast(WeakPointer*)(libc.malloc(WeakPointer.sizeof));
1336 onOutOfMemoryError();
1338 rt_attachDisposeEvent(r, &wp.ondestroy);
1345 * Destroy a weak pointer returned by weakpointerCreate().
1346 * If null is passed, nothing happens.
1348 void weakpointerDestroy( void* p )
1352 auto wp = cast(WeakPointer*)p;
1353 // must be extra careful about the GC or parallel threads
1354 // finalizing the reference at the same time
1357 rt_detachDisposeEvent(wp.reference, &wp.ondestroy);
1364 * Query a weak pointer and return either the object passed to
1365 * weakpointerCreate, or null if it was free'd in the meantime.
1366 * If null is passed, null is returned.
1368 Object weakpointerGet( void* p )
1372 // NOTE: could avoid the lock by using Fawzi style GC counters but
1373 // that'd require core.sync.Atomic and lots of care about memory
1374 // consistency it's an optional optimization see
1375 // http://dsource.org/projects/tango/browser/trunk/user/tango/core/Lifetime.d?rev=5100#L158
1376 return locked!(Object)({
1377 return (cast(WeakPointer*)p).reference;
1384 /* ============================ Gcx =============================== */
1389 POOLSIZE = (4096*256),
1403 B_PAGE, // start of large alloc
1404 B_PAGEPLUS, // continuation of large alloc
1405 B_FREE, // free page
1426 const uint binsize[B_MAX] = [ 16,32,64,128,256,512,1024,2048,4096 ];
1427 const uint notbinsize[B_MAX] = [ ~(16u-1),~(32u-1),~(64u-1),~(128u-1),~(256u-1),
1428 ~(512u-1),~(1024u-1),~(2048u-1),~(4096u-1) ];
1430 /* ============================ Gcx =============================== */
1447 uint noStack; // !=0 means don't scan stack
1448 uint log; // turn on logging
1452 int disabled; // turn off collections if >0
1454 byte *minAddr; // min(baseAddr)
1455 byte *maxAddr; // max(topAddr)
1460 List *bucket[B_MAX]; // free list for each size
1466 (cast(byte*)this)[0 .. Gcx.sizeof] = 0;
1467 stackBottom = cast(char*)&dummy;
1469 //printf("gcx = %p, self = %x\n", this, self);
1478 for (size_t i = 0; i < npools; i++)
1480 Pool *pool = pooltable[i];
1485 libc.free(pooltable);
1495 void Invariant() { }
1502 //printf("Gcx.invariant(): this = %p\n", this);
1505 for (i = 0; i < npools; i++)
1507 Pool *pool = pooltable[i];
1511 assert(minAddr == pool.baseAddr);
1515 assert(pool.opCmp(pooltable[i + 1]) < 0);
1517 else if (i + 1 == npools)
1519 assert(maxAddr == pool.topAddr);
1525 assert(rootdim != 0);
1526 assert(nroots <= rootdim);
1531 assert(rangedim != 0);
1532 assert(nranges <= rangedim);
1534 for (i = 0; i < nranges; i++)
1536 assert(ranges[i].pbot);
1537 assert(ranges[i].ptop);
1538 assert(ranges[i].pbot <= ranges[i].ptop);
1542 for (i = 0; i < B_PAGE; i++)
1544 for (List *list = bucket[i]; list; list = list.next)
1555 void addRoot(void *p)
1557 if (nroots == rootdim)
1559 size_t newdim = rootdim * 2 + 16;
1562 newroots = cast(void**) libc.malloc(newdim * newroots[0].sizeof);
1564 onOutOfMemoryError();
1567 libc.memcpy(newroots, roots, nroots * newroots[0].sizeof);
1581 void removeRoot(void *p)
1583 for (size_t i = nroots; i--;)
1588 libc.memmove(roots + i, roots + i + 1,
1589 (nroots - i) * roots[0].sizeof);
1600 void addRange(void *pbot, void *ptop)
1602 if (nranges == rangedim)
1604 size_t newdim = rangedim * 2 + 16;
1607 newranges = cast(Range*) libc.malloc(newdim * newranges[0].sizeof);
1609 onOutOfMemoryError();
1612 libc.memcpy(newranges, ranges, nranges * newranges[0].sizeof);
1618 ranges[nranges].pbot = pbot;
1619 ranges[nranges].ptop = ptop;
1627 void removeRange(void *pbot)
1629 for (size_t i = nranges; i--;)
1631 if (ranges[i].pbot == pbot)
1634 libc.memmove(ranges + i, ranges + i + 1,
1635 (nranges - i) * ranges[0].sizeof);
1640 // This is a fatal error, but ignore it.
1641 // The problem is that we can get a Close() call on a thread
1642 // other than the one the range was allocated on.
1648 * Find Pool that pointer is in.
1649 * Return null if not in a Pool.
1650 * Assume pooltable[] is sorted.
1652 Pool *findPool(void *p)
1654 if (p >= minAddr && p < maxAddr)
1658 return pooltable[0];
1661 for (size_t i = 0; i < npools; i++)
1665 pool = pooltable[i];
1666 if (p < pool.topAddr)
1668 if (pool.baseAddr <= p)
1679 * Find base address of block containing pointer p.
1680 * Returns null if not a gc'd pointer
1682 void* findBase(void *p)
1689 size_t offset = cast(size_t)(p - pool.baseAddr);
1690 size_t pn = offset / PAGESIZE;
1691 Bins bin = cast(Bins)pool.pagetable[pn];
1693 // Adjust bit to be at start of allocated memory block
1696 return pool.baseAddr + (offset & notbinsize[bin]);
1698 else if (bin == B_PAGEPLUS)
1702 --pn, offset -= PAGESIZE;
1703 } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
1705 return pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
1709 // we are in a B_FREE page
1718 * Find size of pointer p.
1719 * Returns 0 if not a gc'd pointer
1721 size_t findSize(void *p)
1732 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1733 bin = cast(Bins)pool.pagetable[pagenum];
1734 size = binsize[bin];
1740 pt = &pool.pagetable[0];
1741 for (i = pagenum + 1; i < pool.npages; i++)
1743 if (pt[i] != B_PAGEPLUS)
1746 size = (i - pagenum) * PAGESIZE;
1756 BlkInfo getInfo(void* p)
1764 size_t offset = cast(size_t)(p - pool.baseAddr);
1765 size_t pn = offset / PAGESIZE;
1766 Bins bin = cast(Bins)pool.pagetable[pn];
1768 ////////////////////////////////////////////////////////////////////
1770 ////////////////////////////////////////////////////////////////////
1774 info.base = pool.baseAddr + (offset & notbinsize[bin]);
1776 else if (bin == B_PAGEPLUS)
1780 --pn, offset -= PAGESIZE;
1782 while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
1784 info.base = pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
1786 // fix bin for use by size calc below
1787 bin = cast(Bins)pool.pagetable[pn];
1790 ////////////////////////////////////////////////////////////////////
1792 ////////////////////////////////////////////////////////////////////
1794 info.size = binsize[bin];
1800 pt = &pool.pagetable[0];
1801 for (i = pn + 1; i < pool.npages; i++)
1803 if (pt[i] != B_PAGEPLUS)
1806 info.size = (i - pn) * PAGESIZE;
1809 ////////////////////////////////////////////////////////////////////
1811 ////////////////////////////////////////////////////////////////////
1813 info.attr = getBits(pool, cast(size_t)(offset / 16));
1820 * Compute bin for size.
1822 static Bins findBin(size_t size)
1831 else if (size <= 32)
1866 * Allocate a new pool of at least size bytes.
1867 * Sort it into pooltable[].
1868 * Mark all memory in the pool as B_FREE.
1869 * Return the actual number of bytes reserved or 0 on error.
1871 size_t reserve(size_t size)
1873 size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
1874 Pool* pool = newPool(npages);
1878 return pool.npages * PAGESIZE;
1883 * Minimizes physical memory usage by returning free pools to the OS.
1891 for (n = 0; n < npools; n++)
1893 pool = pooltable[n];
1894 for (pn = 0; pn < pool.npages; pn++)
1896 if (cast(Bins)pool.pagetable[pn] != B_FREE)
1899 if (pn < pool.npages)
1906 libc.memmove(pooltable + n,
1908 (--npools - n) * (Pool*).sizeof);
1909 minAddr = pooltable[0].baseAddr;
1910 maxAddr = pooltable[npools - 1].topAddr;
1916 * Allocate a chunk of memory that is larger than a page.
1917 * Return null if out of memory.
1919 void *bigAlloc(size_t size)
1929 npages = (size + PAGESIZE - 1) / PAGESIZE;
1933 // This code could use some refinement when repeatedly
1934 // allocating very large arrays.
1936 for (n = 0; n < npools; n++)
1938 pool = pooltable[n];
1939 pn = pool.allocPages(npages);
1954 freedpages = fullcollectshell();
1955 if (freedpages >= npools * ((POOLSIZE / PAGESIZE) / 4))
1960 // Release empty pools to prevent bloat
1962 // Allocate new pool
1963 pool = newPool(npages);
1969 pn = pool.allocPages(npages);
1970 assert(pn != OPFAIL);
1973 // Release empty pools to prevent bloat
1975 // Allocate new pool
1976 pool = newPool(npages);
1979 pn = pool.allocPages(npages);
1980 assert(pn != OPFAIL);
1990 pool.pagetable[pn] = B_PAGE;
1992 libc.memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1);
1993 p = pool.baseAddr + pn * PAGESIZE;
1994 libc.memset(cast(char *)p + size, 0, npages * PAGESIZE - size);
1995 debug (MEMSTOMP) libc.memset(p, 0xF1, size);
1999 return null; // let mallocNoSync handle the error
2004 * Allocate a new pool with at least npages in it.
2005 * Sort it into pooltable[].
2006 * Return null if failed.
2008 Pool *newPool(size_t npages)
2011 Pool** newpooltable;
2015 // Minimum of POOLSIZE
2016 if (npages < POOLSIZE/PAGESIZE)
2017 npages = POOLSIZE/PAGESIZE;
2018 else if (npages > POOLSIZE/PAGESIZE)
2020 // Give us 150% of requested size, so there's room to extend
2021 auto n = npages + (npages >> 1);
2022 if (n < size_t.max/PAGESIZE)
2026 // Allocate successively larger pools up to 8 megs
2031 n = 8; // cap pool size at 8 megs
2032 n *= (POOLSIZE / PAGESIZE);
2037 pool = cast(Pool *) libc.calloc(1, Pool.sizeof);
2040 pool.initialize(npages);
2044 newnpools = npools + 1;
2045 newpooltable = cast(Pool **) libc.realloc(pooltable,
2046 newnpools * (Pool *).sizeof);
2050 // Sort pool into newpooltable[]
2051 for (i = 0; i < npools; i++)
2053 if (pool.opCmp(newpooltable[i]) < 0)
2056 libc.memmove(newpooltable + i + 1, newpooltable + i,
2057 (npools - i) * (Pool *).sizeof);
2058 newpooltable[i] = pool;
2060 pooltable = newpooltable;
2063 minAddr = pooltable[0].baseAddr;
2064 maxAddr = pooltable[npools - 1].topAddr;
2076 * Allocate a page of bin's.
2080 int allocPage(Bins bin)
2088 for (n = 0; n < npools; n++)
2090 pool = pooltable[n];
2091 pn = pool.allocPages(1);
2098 pool.pagetable[pn] = cast(ubyte)bin;
2100 // Convert page to free list
2101 size_t size = binsize[bin];
2102 List **b = &bucket[bin];
2104 p = pool.baseAddr + pn * PAGESIZE;
2105 ptop = p + PAGESIZE;
2106 for (; p < ptop; p += size)
2108 (cast(List *)p).next = *b;
2116 * Search a range of memory values and mark any pointers into the GC pool.
2118 void mark(void *pbot, void *ptop)
2120 void **p1 = cast(void **)pbot;
2121 void **p2 = cast(void **)ptop;
2125 //printf("marking range: %p -> %p\n", pbot, ptop);
2126 for (; p1 < p2; p1++)
2129 byte *p = cast(byte *)(*p1);
2131 if (p >= minAddr && p < maxAddr)
2133 if ((cast(size_t)p & ~(PAGESIZE-1)) == pcache)
2139 size_t offset = cast(size_t)(p - pool.baseAddr);
2141 size_t pn = offset / PAGESIZE;
2142 Bins bin = cast(Bins)pool.pagetable[pn];
2144 // Adjust bit to be at start of allocated memory block
2146 biti = (offset & notbinsize[bin]) >> 4;
2147 else if (bin == B_PAGEPLUS)
2153 while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
2154 biti = pn * (PAGESIZE / 16);
2158 // Don't mark bits in B_FREE pages
2162 if (bin >= B_PAGE) // Cache B_PAGE and B_PAGEPLUS lookups
2163 pcache = cast(size_t)p & ~(PAGESIZE-1);
2165 if (!pool.mark.test(biti))
2167 pool.mark.set(biti);
2168 if (!pool.noscan.test(biti))
2170 pool.scan.set(biti);
2173 log_parent(sentinel_add(pool.baseAddr + biti * 16), sentinel_add(pbot));
2178 anychanges |= changes;
2183 * Return number of full pages free'd.
2185 size_t fullcollectshell()
2187 // The purpose of the 'shell' is to ensure all the registers
2188 // get put on the stack so they'll be scanned
2193 gcc.builtins.__builtin_unwind_init();
2200 uint eax,ecx,edx,ebx,ebp,esi,edi;
2213 else version (X86_64)
2215 ulong rax,rbx,rcx,rdx,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15;
2218 movq rax[RBP], RAX ;
2219 movq rbx[RBP], RBX ;
2220 movq rcx[RBP], RCX ;
2221 movq rdx[RBP], RDX ;
2222 movq rbp[RBP], RBP ;
2223 movq rsi[RBP], RSI ;
2224 movq rdi[RBP], RDI ;
2227 movq r10[RBP], R10 ;
2228 movq r11[RBP], R11 ;
2229 movq r12[RBP], R12 ;
2230 movq r13[RBP], R13 ;
2231 movq r14[RBP], R14 ;
2232 movq r15[RBP], R15 ;
2238 static assert( false, "Architecture not supported." );
2249 result = fullcollect(sp);
2272 size_t fullcollect(void *stackTop)
2277 debug(COLLECT_PRINTF) printf("Gcx.fullcollect()\n");
2279 thread_suspendAll();
2285 for (n = 0; n < npools; n++)
2287 pool = pooltable[n];
2290 pool.freebits.zero();
2293 // Mark each free entry, so it doesn't get scanned
2294 for (n = 0; n < B_PAGE; n++)
2296 for (List *list = bucket[n]; list; list = list.next)
2298 pool = findPool(list);
2300 pool.freebits.set(cast(size_t)(cast(byte*)list - pool.baseAddr) / 16);
2304 for (n = 0; n < npools; n++)
2306 pool = pooltable[n];
2307 pool.mark.copy(&pool.freebits);
2310 rt_scanStaticData( &mark );
2314 // Scan stacks and registers for each paused thread
2315 thread_scanAll( &mark, stackTop );
2319 debug(COLLECT_PRINTF) printf("scan roots[]\n");
2320 mark(roots, roots + nroots);
2323 debug(COLLECT_PRINTF) printf("scan ranges[]\n");
2325 for (n = 0; n < nranges; n++)
2327 debug(COLLECT_PRINTF) printf("\t%x .. %x\n", ranges[n].pbot, ranges[n].ptop);
2328 mark(ranges[n].pbot, ranges[n].ptop);
2332 debug(COLLECT_PRINTF) printf("\tscan heap\n");
2336 for (n = 0; n < npools; n++)
2342 pool = pooltable[n];
2344 bbase = pool.scan.base();
2345 btop = bbase + pool.scan.nwords;
2346 for (b = bbase; b < btop;)
2362 o = pool.baseAddr + (b - bbase) * 32 * 16;
2363 if (!(bitm & 0xFFFF))
2368 for (; bitm; o += 16, bitm >>= 1)
2373 pn = cast(size_t)(o - pool.baseAddr) / PAGESIZE;
2374 bin = cast(Bins)pool.pagetable[pn];
2377 mark(o, o + binsize[bin]);
2379 else if (bin == B_PAGE || bin == B_PAGEPLUS)
2381 if (bin == B_PAGEPLUS)
2383 while (pool.pagetable[pn - 1] != B_PAGE)
2387 while (pn + u < pool.npages && pool.pagetable[pn + u] == B_PAGEPLUS)
2389 mark(o, o + u * PAGESIZE);
2398 // Free up everything not marked
2399 debug(COLLECT_PRINTF) printf("\tfree'ing\n");
2400 size_t freedpages = 0;
2402 for (n = 0; n < npools; n++)
2404 pool = pooltable[n];
2405 uint* bbase = pool.mark.base();
2407 for (pn = 0; pn < pool.npages; pn++, bbase += PAGESIZE / (32 * 16))
2409 Bins bin = cast(Bins)pool.pagetable[pn];
2413 auto size = binsize[bin];
2414 byte* p = pool.baseAddr + pn * PAGESIZE;
2415 byte* ptop = p + PAGESIZE;
2416 size_t biti = pn * (PAGESIZE/16);
2417 size_t bitstride = size / 16;
2419 version(none) // BUG: doesn't work because freebits() must also be cleared
2421 // If free'd entire page
2422 if (bbase[0] == 0 && bbase[1] == 0 && bbase[2] == 0 && bbase[3] == 0 &&
2423 bbase[4] == 0 && bbase[5] == 0 && bbase[6] == 0 && bbase[7] == 0)
2425 for (; p < ptop; p += size, biti += bitstride)
2427 if (pool.finals.nbits && pool.finals.testClear(biti))
2428 rt_finalize(cast(List *)sentinel_add(p), false/*noStack > 0*/);
2429 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
2431 List *list = cast(List *)p;
2432 log_free(sentinel_add(list));
2434 debug (MEMSTOMP) libc.memset(p, 0xF3, size);
2436 pool.pagetable[pn] = B_FREE;
2441 for (; p < ptop; p += size, biti += bitstride)
2443 if (!pool.mark.test(biti))
2445 sentinel_Invariant(sentinel_add(p));
2447 pool.freebits.set(biti);
2448 if (pool.finals.nbits && pool.finals.testClear(biti))
2449 rt_finalize(cast(List *)sentinel_add(p), false/*noStack > 0*/);
2450 clrBits(pool, biti, BlkAttr.ALL_BITS);
2452 List *list = cast(List *)p;
2453 log_free(sentinel_add(list));
2455 debug (MEMSTOMP) libc.memset(p, 0xF3, size);
2461 else if (bin == B_PAGE)
2463 size_t biti = pn * (PAGESIZE / 16);
2464 if (!pool.mark.test(biti))
2466 byte *p = pool.baseAddr + pn * PAGESIZE;
2467 sentinel_Invariant(sentinel_add(p));
2468 if (pool.finals.nbits && pool.finals.testClear(biti))
2469 rt_finalize(sentinel_add(p), false/*noStack > 0*/);
2470 clrBits(pool, biti, BlkAttr.ALL_BITS);
2472 debug(COLLECT_PRINTF) printf("\tcollecting big %x\n", p);
2473 log_free(sentinel_add(p));
2474 pool.pagetable[pn] = B_FREE;
2476 debug (MEMSTOMP) libc.memset(p, 0xF3, PAGESIZE);
2477 while (pn + 1 < pool.npages && pool.pagetable[pn + 1] == B_PAGEPLUS)
2480 pool.pagetable[pn] = B_FREE;
2486 libc.memset(p, 0xF3, PAGESIZE);
2497 // Free complete pages, rebuild free list
2498 debug(COLLECT_PRINTF) printf("\tfree complete pages\n");
2499 size_t recoveredpages = 0;
2500 for (n = 0; n < npools; n++)
2502 pool = pooltable[n];
2503 for (size_t pn = 0; pn < pool.npages; pn++)
2505 Bins bin = cast(Bins)pool.pagetable[pn];
2511 size_t size = binsize[bin];
2512 size_t bitstride = size / 16;
2513 size_t bitbase = pn * (PAGESIZE / 16);
2514 size_t bittop = bitbase + (PAGESIZE / 16);
2518 for (biti = bitbase; biti < bittop; biti += bitstride)
2520 if (!pool.freebits.test(biti))
2523 pool.pagetable[pn] = B_FREE;
2528 p = pool.baseAddr + pn * PAGESIZE;
2529 for (u = 0; u < PAGESIZE; u += size)
2531 biti = bitbase + u / 16;
2532 if (pool.freebits.test(biti))
2534 List *list = cast(List *)(p + u);
2535 if (list.next != bucket[bin]) // avoid unnecessary writes
2536 list.next = bucket[bin];
2544 debug(COLLECT_PRINTF) printf("recovered pages = %d\n", recoveredpages);
2545 debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, npools);
2547 return freedpages + recoveredpages;
2554 uint getBits(Pool* pool, size_t biti)
2563 if (pool.finals.nbits &&
2564 pool.finals.test(biti))
2565 bits |= BlkAttr.FINALIZE;
2566 if (pool.noscan.test(biti))
2567 bits |= BlkAttr.NO_SCAN;
2568 // if (pool.nomove.nbits &&
2569 // pool.nomove.test(biti))
2570 // bits |= BlkAttr.NO_MOVE;
2578 void setBits(Pool* pool, size_t biti, uint mask)
2585 if (mask & BlkAttr.FINALIZE)
2587 if (!pool.finals.nbits)
2588 pool.finals.alloc(pool.mark.nbits);
2589 pool.finals.set(biti);
2591 if (mask & BlkAttr.NO_SCAN)
2593 pool.noscan.set(biti);
2595 // if (mask & BlkAttr.NO_MOVE)
2597 // if (!pool.nomove.nbits)
2598 // pool.nomove.alloc(pool.mark.nbits);
2599 // pool.nomove.set(biti);
2607 void clrBits(Pool* pool, size_t biti, uint mask)
2614 if (mask & BlkAttr.FINALIZE && pool.finals.nbits)
2615 pool.finals.clear(biti);
2616 if (mask & BlkAttr.NO_SCAN)
2617 pool.noscan.clear(biti);
2618 // if (mask & BlkAttr.NO_MOVE && pool.nomove.nbits)
2619 // pool.nomove.clear(biti);
2623 /***** Leak Detector ******/
2634 current.reserve(1000);
2639 void log_malloc(void *p, size_t size)
2656 void log_free(void *p)
2660 i = current.find(p);
2668 // Print everything in current that is not in prev
2670 for (size_t i = 0; i < current.dim; i++)
2674 j = prev.find(current.data[i].p);
2676 current.data[i].print();
2681 for (size_t i = 0; i < current.dim; i++)
2686 p = current.data[i].p;
2687 if (!findPool(current.data[i].parent))
2689 j = prev.find(current.data[i].p);
2690 current.data[i].print();
2694 prev.copy(¤t);
2698 void log_parent(void *p, void *parent)
2702 i = current.find(p);
2708 size_t offset = cast(size_t)(p - pool.baseAddr);
2710 size_t pn = offset / PAGESIZE;
2711 Bins bin = cast(Bins)pool.pagetable[pn];
2712 biti = (offset & notbinsize[bin]);
2715 current.data[i].parent = parent;
2722 void log_malloc(void *p, size_t size) { }
2723 void log_free(void *p) { }
2724 void log_collect() { }
2725 void log_parent(void *p, void *parent) { }
2730 /* ============================ Pool =============================== */
2737 GCBits mark; // entries already scanned, or should not be scanned
2738 GCBits scan; // entries that need to be scanned
2739 GCBits freebits; // entries that are on the free list
2740 GCBits finals; // entries that need finalizer run on them
2741 GCBits noscan; // entries that should not be scanned
2747 void initialize(size_t npages)
2749 size_t poolsize = npages * PAGESIZE;
2750 assert(poolsize >= POOLSIZE);
2751 baseAddr = cast(byte *) alloc.os_mem_map(poolsize);
2753 // Some of the code depends on page alignment of memory pools
2754 assert((cast(size_t)baseAddr & (PAGESIZE - 1)) == 0);
2762 topAddr = baseAddr + poolsize;
2764 mark.alloc(cast(size_t)poolsize / 16);
2765 scan.alloc(cast(size_t)poolsize / 16);
2766 freebits.alloc(cast(size_t)poolsize / 16);
2767 noscan.alloc(cast(size_t)poolsize / 16);
2769 pagetable = cast(ubyte*) libc.malloc(npages);
2771 onOutOfMemoryError();
2772 libc.memset(pagetable, B_FREE, npages);
2774 this.npages = npages;
2786 result = alloc.os_mem_unmap(baseAddr, npages * PAGESIZE);
2795 libc.free(pagetable);
2805 void Invariant() { }
2812 //freebits.Invariant();
2813 //finals.Invariant();
2814 //noscan.Invariant();
2818 //if (baseAddr + npages * PAGESIZE != topAddr)
2819 //printf("baseAddr = %p, npages = %d, topAddr = %p\n", baseAddr, npages, topAddr);
2820 assert(baseAddr + npages * PAGESIZE == topAddr);
2823 for (size_t i = 0; i < npages; i++)
2825 Bins bin = cast(Bins)pagetable[i];
2826 assert(bin < B_MAX);
2832 * Allocate n pages from Pool.
2833 * Returns OPFAIL on failure.
2835 size_t allocPages(size_t n)
2841 for (i = 0; i < npages; i++)
2843 if (pagetable[i] == B_FREE)
2856 * Free npages pages starting with pagenum.
2858 void freePages(size_t pagenum, size_t npages)
2860 libc.memset(&pagetable[pagenum], B_FREE, npages);
2865 * Used for sorting pooltable[]
2869 if (baseAddr < p2.baseAddr)
2872 return cast(int)(baseAddr > p2.baseAddr);
2877 /* ============================ SENTINEL =============================== */
2882 const size_t SENTINEL_PRE = cast(size_t) 0xF4F4F4F4F4F4F4F4UL; // 32 or 64 bits
2883 const ubyte SENTINEL_POST = 0xF5; // 8 bits
2884 const uint SENTINEL_EXTRA = 2 * size_t.sizeof + 1;
2887 size_t* sentinel_size(void *p) { return &(cast(size_t *)p)[-2]; }
2888 size_t* sentinel_pre(void *p) { return &(cast(size_t *)p)[-1]; }
2889 ubyte* sentinel_post(void *p) { return &(cast(ubyte *)p)[*sentinel_size(p)]; }
2892 void sentinel_init(void *p, size_t size)
2894 *sentinel_size(p) = size;
2895 *sentinel_pre(p) = SENTINEL_PRE;
2896 *sentinel_post(p) = SENTINEL_POST;
2900 void sentinel_Invariant(void *p)
2902 assert(*sentinel_pre(p) == SENTINEL_PRE);
2903 assert(*sentinel_post(p) == SENTINEL_POST);
2907 void *sentinel_add(void *p)
2909 return p + 2 * size_t.sizeof;
2913 void *sentinel_sub(void *p)
2915 return p - 2 * size_t.sizeof;
2920 const uint SENTINEL_EXTRA = 0;
2923 void sentinel_init(void *p, size_t size)
2928 void sentinel_Invariant(void *p)
2933 void *sentinel_add(void *p)
2939 void *sentinel_sub(void *p)