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 = PRINTF; // turn on printf's
34 //debug = COLLECT_PRINTF; // turn on printf's
35 //debug = LOGGING; // log allocations / frees
36 //debug = MEMSTOMP; // stomp on memory
37 //debug = SENTINEL; // add underrun/overrrun protection
38 //debug = PTRCHECK; // more pointer checking
39 //debug = PTRCHECK2; // thorough but slow pointer checking
41 /*************** Configuration *********************/
43 version = STACKGROWSDOWN; // growing the stack means subtracting from the stack pointer
44 // (use for Intel X86 CPUs)
45 // else growing the stack means adding to the stack pointer
47 /***************************************************/
57 // BUG: The following import will likely not work, since the gcc
58 // subdirectory is elsewhere. Instead, perhaps the functions
59 // could be declared directly or some other resolution could
61 import gcc.builtins; // for __builtin_unwind_init
76 FINALIZE = 0b0000_0001,
77 NO_SCAN = 0b0000_0010,
78 NO_MOVE = 0b0000_0100,
79 ALL_BITS = 0b1111_1111
82 extern (C) void* rt_stackBottom();
83 extern (C) void* rt_stackTop();
85 extern (C) void rt_finalize( void* p, bool det = true );
87 alias void delegate( void*, void* ) scanFn;
89 extern (C) void rt_scanStaticData( scanFn scan );
91 extern (C) bool thread_needLock();
92 extern (C) void thread_suspendAll();
93 extern (C) void thread_resumeAll();
95 extern (C) void thread_scanAll( scanFn fn, void* curStackTop = null );
97 extern (C) void onOutOfMemoryError();
101 OPFAIL = ~cast(size_t)0
109 /* ======================= Leak Detector =========================== */
124 printf(" p = %x, size = %d, parent = %x ", p, size, parent);
127 printf("%s(%u)", file, line);
147 void reserve(size_t nentries)
149 assert(dim <= allocdim);
150 if (allocdim - dim < nentries)
152 allocdim = (dim + nentries) * 2;
153 assert(dim + nentries <= allocdim);
156 data = cast(Log*) .malloc(allocdim * Log.sizeof);
157 if (!data && allocdim)
158 onOutOfMemoryError();
163 newdata = cast(Log*) .malloc(allocdim * Log.sizeof);
164 if (!newdata && allocdim)
165 onOutOfMemoryError();
166 .memcpy(newdata, data, dim * Log.sizeof);
180 void remove(size_t i)
182 .memmove(data + i, data + i + 1, (dim - i) * Log.sizeof);
189 for (size_t i = 0; i < dim; i++)
194 return OPFAIL; // not found
198 void copy(LogArray *from)
200 reserve(from.dim - dim);
201 assert(from.dim <= allocdim);
202 .memcpy(data, from.data, from.dim * Log.sizeof);
209 /* ============================ GC =============================== */
212 class GCLock { } // just a dummy so we can get a global lock
215 const uint GCVERSION = 1; // increment every time we change interface
220 // For passing to debug code
224 uint gcversion = GCVERSION;
226 Gcx *gcx; // implementation
227 static ClassInfo gcLock; // global lock
232 gcLock = GCLock.classinfo;
233 gcx = cast(Gcx*) .calloc(1, Gcx.sizeof);
235 onOutOfMemoryError();
237 setStackBottom(rt_stackBottom());
257 if (!thread_needLock())
259 assert(gcx.disabled > 0);
262 else synchronized (gcLock)
264 assert(gcx.disabled > 0);
275 if (!thread_needLock())
279 else synchronized (gcLock)
289 uint getAttr(void* p)
298 Pool* pool = gcx.findPool(p);
303 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
305 oldb = gcx.getBits(pool, biti);
310 if (!thread_needLock())
314 else synchronized (gcLock)
324 uint setAttr(void* p, uint mask)
333 Pool* pool = gcx.findPool(p);
338 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
340 oldb = gcx.getBits(pool, biti);
341 gcx.setBits(pool, biti, mask);
346 if (!thread_needLock())
350 else synchronized (gcLock)
360 uint clrAttr(void* p, uint mask)
369 Pool* pool = gcx.findPool(p);
374 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
376 oldb = gcx.getBits(pool, biti);
377 gcx.clrBits(pool, biti, mask);
382 if (!thread_needLock())
386 else synchronized (gcLock)
396 void *malloc(size_t size, uint bits = 0)
403 if (!thread_needLock())
405 return mallocNoSync(size, bits);
407 else synchronized (gcLock)
409 return mallocNoSync(size, bits);
417 private void *mallocNoSync(size_t size, uint bits = 0)
424 //debug(PRINTF) printf("GC::malloc(size = %d, gcx = %p)\n", size, gcx);
427 size += SENTINEL_EXTRA;
430 // Cache previous binsize lookup - Dave Fladebo.
431 static size_t lastsize = -1;
433 if (size == lastsize)
437 bin = gcx.findBin(size);
447 if (!gcx.allocPage(bin) && !gcx.disabled) // try to find a new page
449 if (!thread_needLock())
451 /* Then we haven't locked it yet. Be sure
452 * and lock for a collection, since a finalizer
453 * may start a new thread.
455 synchronized (gcLock)
457 gcx.fullcollectshell();
460 else if (!gcx.fullcollectshell()) // collect to find a new page
465 if (!gcx.bucket[bin] && !gcx.allocPage(bin))
468 gcx.newPool(1); // allocate new pool to find a new page
469 result = gcx.allocPage(bin);
471 onOutOfMemoryError();
476 // Return next item from free list
477 gcx.bucket[bin] = (cast(List*)p).next;
478 if( !(bits & BlkAttr.NO_SCAN) )
479 .memset(p + size, 0, binsize[bin] - size);
480 //debug(PRINTF) printf("\tmalloc => %x\n", p);
481 debug (MEMSTOMP) .memset(p, 0xF0, size);
485 p = gcx.bigAlloc(size);
487 onOutOfMemoryError();
489 size -= SENTINEL_EXTRA;
491 sentinel_init(p, size);
492 gcx.log_malloc(p, size);
496 Pool *pool = gcx.findPool(p);
499 gcx.setBits(pool, cast(size_t)(p - pool.baseAddr) / 16, bits);
508 void *calloc(size_t size, uint bits = 0)
515 if (!thread_needLock())
517 return callocNoSync(size, bits);
519 else synchronized (gcLock)
521 return callocNoSync(size, bits);
529 private void *callocNoSync(size_t size, uint bits = 0)
533 //debug(PRINTF) printf("calloc: %x len %d\n", p, len);
534 void *p = mallocNoSync(size, bits);
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)
569 p = mallocNoSync(size, bits);
575 //debug(PRINTF) printf("GC::realloc(p = %x, size = %u)\n", p, size);
578 sentinel_Invariant(p);
579 psize = *sentinel_size(p);
584 Pool *pool = gcx.findPool(p);
588 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
592 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
593 gcx.setBits(pool, biti, bits);
597 bits = gcx.getBits(pool, biti);
601 p2 = mallocNoSync(size, bits);
604 //debug(PRINTF) printf("\tcopying %d bytes\n",size);
605 .memcpy(p2, p, size);
611 psize = gcx.findSize(p); // find allocated size
612 if (psize >= PAGESIZE && size >= PAGESIZE)
614 auto psz = psize / PAGESIZE;
615 auto newsz = (size + PAGESIZE - 1) / PAGESIZE;
619 auto pool = gcx.findPool(p);
620 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
624 synchronized (gcLock)
626 debug (MEMSTOMP) .memset(p + size, 0xF2, psize - size);
627 pool.freePages(pagenum + newsz, psz - newsz);
631 else if (pagenum + newsz <= pool.npages)
633 // Attempt to expand in place
634 synchronized (gcLock)
636 for (size_t i = pagenum + psz; 1;)
638 if (i == pagenum + newsz)
640 debug (MEMSTOMP) .memset(p + psize, 0xF0, size - psize);
641 .memset(&pool.pagetable[pagenum + psz], B_PAGEPLUS, newsz - psz);
644 if (i == pool.npages)
648 if (pool.pagetable[i] != B_FREE)
655 if (psize < size || // if new size is bigger
656 psize > size * 2) // or less than half
660 Pool *pool = gcx.findPool(p);
664 auto biti = cast(size_t)(p - pool.baseAddr) / 16;
668 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
669 gcx.setBits(pool, biti, bits);
673 bits = gcx.getBits(pool, biti);
677 p2 = mallocNoSync(size, bits);
680 //debug(PRINTF) printf("\tcopying %d bytes\n",size);
681 .memcpy(p2, p, size);
691 * Attempt to in-place enlarge the memory block pointed to by p by at least
692 * minbytes beyond its current capacity, up to a maximum of maxsize. This
693 * does not attempt to move the memory block (like realloc() does).
696 * 0 if could not extend p,
697 * total size of entire memory block if successful.
699 size_t extend(void* p, size_t minsize, size_t maxsize)
701 if (!thread_needLock())
703 return extendNoSync(p, minsize, maxsize);
705 else synchronized (gcLock)
707 return extendNoSync(p, minsize, maxsize);
715 private size_t extendNoSync(void* p, size_t minsize, size_t maxsize)
718 assert( minsize <= maxsize );
722 //debug(PRINTF) printf("GC::extend(p = %x, minsize = %u, maxsize = %u)\n", p, minsize, maxsize);
727 auto psize = gcx.findSize(p); // find allocated size
728 if (psize < PAGESIZE)
729 return 0; // cannot extend buckets
731 auto psz = psize / PAGESIZE;
732 auto minsz = (minsize + PAGESIZE - 1) / PAGESIZE;
733 auto maxsz = (maxsize + PAGESIZE - 1) / PAGESIZE;
735 auto pool = gcx.findPool(p);
736 auto pagenum = (p - pool.baseAddr) / PAGESIZE;
739 for (sz = 0; sz < maxsz; sz++)
741 auto i = pagenum + psz + sz;
742 if (i == pool.npages)
744 if (pool.pagetable[i] != B_FREE)
752 debug (MEMSTOMP) .memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize);
753 .memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz);
756 return (psz + sz) * PAGESIZE;
763 size_t reserve(size_t size)
770 if (!thread_needLock())
772 return reserveNoSync(size);
774 else synchronized (gcLock)
776 return reserveNoSync(size);
784 private size_t reserveNoSync(size_t size)
789 return gcx.reserve(size);
803 if (!thread_needLock())
805 return freeNoSync(p);
807 else synchronized (gcLock)
809 return freeNoSync(p);
817 private void freeNoSync(void *p)
826 // Find which page it is in
827 pool = gcx.findPool(p);
828 if (!pool) // if not one of ours
830 sentinel_Invariant(p);
832 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
833 biti = cast(size_t)(p - pool.baseAddr) / 16;
834 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
836 bin = cast(Bins)pool.pagetable[pagenum];
837 if (bin == B_PAGE) // if large alloc
844 while (++n < pool.npages && pool.pagetable[n] == B_PAGEPLUS)
846 debug (MEMSTOMP) .memset(p, 0xF2, npages * PAGESIZE);
847 pool.freePages(pagenum, npages);
850 { // Add to free list
851 List *list = cast(List*)p;
853 debug (MEMSTOMP) .memset(p, 0xF2, binsize[bin]);
855 list.next = gcx.bucket[bin];
856 gcx.bucket[bin] = list;
858 gcx.log_free(sentinel_add(p));
863 * Determine the base address of the block containing p. If p is not a gc
864 * allocated pointer, return null.
866 void* addrOf(void *p)
873 if (!thread_needLock())
875 return addrOfNoSync(p);
877 else synchronized (gcLock)
879 return addrOfNoSync(p);
887 void* addrOfNoSync(void *p)
894 return gcx.findBase(p);
899 * Determine the allocated size of pointer p. If p is an interior pointer
900 * or not a gc allocated pointer, return 0.
902 size_t sizeOf(void *p)
909 if (!thread_needLock())
911 return sizeOfNoSync(p);
913 else synchronized (gcLock)
915 return sizeOfNoSync(p);
923 private size_t sizeOfNoSync(void *p)
930 size_t size = gcx.findSize(p);
932 // Check for interior pointer
934 // 1) size is a power of 2 for less than PAGESIZE values
935 // 2) base of memory pool is aligned on PAGESIZE boundary
936 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
938 return size ? size - SENTINEL_EXTRA : 0;
942 if (p == gcx.p_cache)
943 return gcx.size_cache;
945 size_t size = gcx.findSize(p);
947 // Check for interior pointer
949 // 1) size is a power of 2 for less than PAGESIZE values
950 // 2) base of memory pool is aligned on PAGESIZE boundary
951 if (cast(size_t)p & (size - 1) & (PAGESIZE - 1))
956 gcx.size_cache = size;
965 * Determine the base address of the block containing p. If p is not a gc
966 * allocated pointer, return null.
968 BlkInfo query(void *p)
976 if (!thread_needLock())
978 return queryNoSync(p);
980 else synchronized (gcLock)
982 return queryNoSync(p);
990 BlkInfo queryNoSync(void *p)
994 return gcx.getInfo(p);
999 * Verify that pointer p:
1000 * 1) belongs to this memory pool
1001 * 2) points to the start of an allocated piece of memory
1002 * 3) is not on a free list
1011 if (!thread_needLock())
1015 else synchronized (gcLock)
1025 private void checkNoSync(void *p)
1029 sentinel_Invariant(p);
1037 p = sentinel_sub(p);
1038 pool = gcx.findPool(p);
1040 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1041 bin = cast(Bins)pool.pagetable[pagenum];
1042 assert(bin <= B_PAGE);
1043 size = binsize[bin];
1044 assert((cast(size_t)p & (size - 1)) == 0);
1050 // Check that p is not on a free list
1053 for (list = gcx.bucket[bin]; list; list = list.next)
1055 assert(cast(void*)list != p);
1066 private void setStackBottom(void *p)
1068 version (STACKGROWSDOWN)
1070 //p = (void *)((uint *)p + 4);
1071 if (p > gcx.stackBottom)
1073 //debug(PRINTF) printf("setStackBottom(%x)\n", p);
1074 gcx.stackBottom = p;
1079 //p = (void *)((uint *)p - 4);
1080 if (p < gcx.stackBottom)
1082 //debug(PRINTF) printf("setStackBottom(%x)\n", p);
1083 gcx.stackBottom = cast(char*)p;
1090 * add p to list of roots
1092 void addRoot(void *p)
1099 if (!thread_needLock())
1103 else synchronized (gcLock)
1111 * remove p from list of roots
1113 void removeRoot(void *p)
1120 if (!thread_needLock())
1124 else synchronized (gcLock)
1132 * add range to scan for roots
1134 void addRange(void *p, size_t sz)
1141 //debug(PRINTF) printf("+GC.addRange(pbot = x%x, ptop = x%x)\n", pbot, ptop);
1142 if (!thread_needLock())
1144 gcx.addRange(p, p + sz);
1146 else synchronized (gcLock)
1148 gcx.addRange(p, p + sz);
1150 //debug(PRINTF) printf("-GC.addRange()\n");
1157 void removeRange(void *p)
1164 if (!thread_needLock())
1168 else synchronized (gcLock)
1176 * do full garbage collection
1180 debug(PRINTF) printf("GC.fullCollect()\n");
1182 if (!thread_needLock())
1184 gcx.fullcollectshell();
1186 else synchronized (gcLock)
1188 gcx.fullcollectshell();
1196 debug(PRINTF) printf("poolsize = %x, usedsize = %x, freelistsize = %x\n",
1197 stats.poolsize, stats.usedsize, stats.freelistsize);
1205 * do full garbage collection ignoring roots
1207 void fullCollectNoStack()
1209 if (!thread_needLock())
1212 gcx.fullcollectshell();
1215 else synchronized (gcLock)
1218 gcx.fullcollectshell();
1225 * minimize free space usage
1229 if (!thread_needLock())
1233 else synchronized (gcLock)
1241 * Retrieve statistics about garbage collection.
1242 * Useful for debugging and tuning.
1244 void getStats(out GCStats stats)
1246 if (!thread_needLock())
1248 getStatsNoSync(stats);
1250 else synchronized (gcLock)
1252 getStatsNoSync(stats);
1260 private void getStatsNoSync(out GCStats stats)
1269 //debug(PRINTF) printf("getStats()\n");
1270 .memset(&stats, 0, GCStats.sizeof);
1272 for (n = 0; n < gcx.npools; n++)
1273 { Pool *pool = gcx.pooltable[n];
1275 psize += pool.npages * PAGESIZE;
1276 for (size_t j = 0; j < pool.npages; j++)
1278 Bins bin = cast(Bins)pool.pagetable[j];
1281 else if (bin == B_PAGE)
1283 else if (bin < B_PAGE)
1288 for (n = 0; n < B_PAGE; n++)
1290 //debug(PRINTF) printf("bin %d\n", n);
1291 for (List *list = gcx.bucket[n]; list; list = list.next)
1293 //debug(PRINTF) printf("\tlist %x\n", list);
1294 flsize += binsize[n];
1298 usize = bsize - flsize;
1300 stats.poolsize = psize;
1301 stats.usedsize = bsize - flsize;
1302 stats.freelistsize = flsize;
1307 /* ============================ Gcx =============================== */
1311 POOLSIZE = (4096*256),
1325 B_PAGE, // start of large alloc
1326 B_PAGEPLUS, // continuation of large alloc
1327 B_FREE, // free page
1348 const uint binsize[B_MAX] = [ 16,32,64,128,256,512,1024,2048,4096 ];
1349 const uint notbinsize[B_MAX] = [ ~(16u-1),~(32u-1),~(64u-1),~(128u-1),~(256u-1),
1350 ~(512u-1),~(1024u-1),~(2048u-1),~(4096u-1) ];
1352 /* ============================ Gcx =============================== */
1369 uint noStack; // !=0 means don't scan stack
1370 uint log; // turn on logging
1374 int disabled; // turn off collections if >0
1376 byte *minAddr; // min(baseAddr)
1377 byte *maxAddr; // max(topAddr)
1382 List *bucket[B_MAX]; // free list for each size
1388 (cast(byte*)this)[0 .. Gcx.sizeof] = 0;
1389 stackBottom = cast(char*)&dummy;
1391 //printf("gcx = %p, self = %x\n", this, self);
1400 for (size_t i = 0; i < npools; i++)
1401 { Pool *pool = pooltable[i];
1417 void Invariant() { }
1424 //printf("Gcx.invariant(): this = %p\n", this);
1427 for (i = 0; i < npools; i++)
1428 { Pool *pool = pooltable[i];
1433 assert(minAddr == pool.baseAddr);
1437 assert(pool.opCmp(pooltable[i + 1]) < 0);
1439 else if (i + 1 == npools)
1441 assert(maxAddr == pool.topAddr);
1447 assert(rootdim != 0);
1448 assert(nroots <= rootdim);
1453 assert(rangedim != 0);
1454 assert(nranges <= rangedim);
1456 for (i = 0; i < nranges; i++)
1458 assert(ranges[i].pbot);
1459 assert(ranges[i].ptop);
1460 assert(ranges[i].pbot <= ranges[i].ptop);
1464 for (i = 0; i < B_PAGE; i++)
1466 for (List *list = bucket[i]; list; list = list.next)
1477 void addRoot(void *p)
1479 if (nroots == rootdim)
1481 size_t newdim = rootdim * 2 + 16;
1484 newroots = cast(void**) .malloc(newdim * newroots[0].sizeof);
1486 onOutOfMemoryError();
1488 { .memcpy(newroots, roots, nroots * newroots[0].sizeof);
1502 void removeRoot(void *p)
1504 for (size_t i = nroots; i--;)
1509 .memmove(roots + i, roots + i + 1, (nroots - i) * roots[0].sizeof);
1520 void addRange(void *pbot, void *ptop)
1522 debug (PRINTF) printf("%x.Gcx::addRange(%x, %x), nranges = %d\n", this,
1523 pbot, ptop, nranges);
1524 if (nranges == rangedim)
1526 size_t newdim = rangedim * 2 + 16;
1529 newranges = cast(Range*) .malloc(newdim * newranges[0].sizeof);
1531 onOutOfMemoryError();
1533 { .memcpy(newranges, ranges, nranges * newranges[0].sizeof);
1539 ranges[nranges].pbot = pbot;
1540 ranges[nranges].ptop = ptop;
1548 void removeRange(void *pbot)
1550 debug (PRINTF) printf("%x.Gcx.removeRange(%x), nranges = %d\n", this,
1552 for (size_t i = nranges; i--;)
1554 if (ranges[i].pbot == pbot)
1557 .memmove(ranges + i, ranges + i + 1, (nranges - i) * ranges[0].sizeof);
1561 debug(PRINTF) printf("Wrong thread\n");
1563 // This is a fatal error, but ignore it.
1564 // The problem is that we can get a Close() call on a thread
1565 // other than the one the range was allocated on.
1571 * Find Pool that pointer is in.
1572 * Return null if not in a Pool.
1573 * Assume pooltable[] is sorted.
1575 Pool *findPool(void *p)
1577 if (p >= minAddr && p < maxAddr)
1581 return pooltable[0];
1584 for (size_t i = 0; i < npools; i++)
1587 pool = pooltable[i];
1588 if (p < pool.topAddr)
1589 { if (pool.baseAddr <= p)
1600 * Find base address of block containing pointer p.
1601 * Returns null if not a gc'd pointer
1603 void* findBase(void *p)
1610 size_t offset = cast(size_t)(p - pool.baseAddr);
1611 size_t pn = offset / PAGESIZE;
1612 Bins bin = cast(Bins)pool.pagetable[pn];
1614 // Adjust bit to be at start of allocated memory block
1617 return pool.baseAddr + (offset & notbinsize[bin]);
1619 else if (bin == B_PAGEPLUS)
1622 { --pn, offset -= PAGESIZE;
1623 } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
1625 return pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
1629 // we are in a B_FREE page
1638 * Find size of pointer p.
1639 * Returns 0 if not a gc'd pointer
1641 size_t findSize(void *p)
1652 pagenum = cast(size_t)(p - pool.baseAddr) / PAGESIZE;
1653 bin = cast(Bins)pool.pagetable[pagenum];
1654 size = binsize[bin];
1660 pt = &pool.pagetable[0];
1661 for (i = pagenum + 1; i < pool.npages; i++)
1663 if (pt[i] != B_PAGEPLUS)
1666 size = (i - pagenum) * PAGESIZE;
1676 BlkInfo getInfo(void* p)
1684 size_t offset = cast(size_t)(p - pool.baseAddr);
1685 size_t pn = offset / PAGESIZE;
1686 Bins bin = cast(Bins)pool.pagetable[pn];
1688 ////////////////////////////////////////////////////////////////////
1690 ////////////////////////////////////////////////////////////////////
1694 info.base = pool.baseAddr + (offset & notbinsize[bin]);
1696 else if (bin == B_PAGEPLUS)
1699 { --pn, offset -= PAGESIZE;
1700 } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
1702 info.base = pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1)));
1704 // fix bin for use by size calc below
1705 bin = cast(Bins)pool.pagetable[pn];
1708 ////////////////////////////////////////////////////////////////////
1710 ////////////////////////////////////////////////////////////////////
1712 info.size = binsize[bin];
1718 pt = &pool.pagetable[0];
1719 for (i = pn + 1; i < pool.npages; i++)
1721 if (pt[i] != B_PAGEPLUS)
1724 info.size = (i - pn) * PAGESIZE;
1727 ////////////////////////////////////////////////////////////////////
1729 ////////////////////////////////////////////////////////////////////
1731 info.attr = getBits(pool, cast(size_t)(offset / 16));
1738 * Compute bin for size.
1740 static Bins findBin(size_t size)
1749 else if (size <= 32)
1784 * Allocate a new pool of at least size bytes.
1785 * Sort it into pooltable[].
1786 * Mark all memory in the pool as B_FREE.
1787 * Return the actual number of bytes reserved or 0 on error.
1789 size_t reserve(size_t size)
1791 size_t npages = (size + PAGESIZE - 1) / PAGESIZE;
1792 Pool* pool = newPool(npages);
1796 return pool.npages * PAGESIZE;
1801 * Minimizes physical memory usage by returning free pools to the OS.
1809 for (n = 0; n < npools; n++)
1811 pool = pooltable[n];
1812 for (pn = 0; pn < pool.npages; pn++)
1814 if (cast(Bins)pool.pagetable[pn] != B_FREE)
1817 if (pn < pool.npages)
1824 .memmove(pooltable + n,
1826 (--npools - n) * (Pool*).sizeof);
1827 minAddr = pooltable[0].baseAddr;
1828 maxAddr = pooltable[npools - 1].topAddr;
1834 * Allocate a chunk of memory that is larger than a page.
1835 * Return null if out of memory.
1837 void *bigAlloc(size_t size)
1847 npages = (size + PAGESIZE - 1) / PAGESIZE;
1851 // This code could use some refinement when repeatedly
1852 // allocating very large arrays.
1854 for (n = 0; n < npools; n++)
1856 pool = pooltable[n];
1857 pn = pool.allocPages(npages);
1871 freedpages = fullcollectshell();
1872 if (freedpages >= npools * ((POOLSIZE / PAGESIZE) / 4))
1876 // Release empty pools to prevent bloat
1878 // Allocate new pool
1879 pool = newPool(npages);
1884 pn = pool.allocPages(npages);
1885 assert(pn != OPFAIL);
1888 // Release empty pools to prevent bloat
1890 // Allocate new pool
1891 pool = newPool(npages);
1894 pn = pool.allocPages(npages);
1895 assert(pn != OPFAIL);
1905 pool.pagetable[pn] = B_PAGE;
1907 .memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1);
1908 p = pool.baseAddr + pn * PAGESIZE;
1909 .memset(cast(char *)p + size, 0, npages * PAGESIZE - size);
1910 debug (MEMSTOMP) .memset(p, 0xF1, size);
1911 //debug(PRINTF) printf("\tp = %x\n", p);
1915 return null; // let mallocNoSync handle the error
1920 * Allocate a new pool with at least npages in it.
1921 * Sort it into pooltable[].
1922 * Return null if failed.
1924 Pool *newPool(size_t npages)
1927 Pool** newpooltable;
1931 //debug(PRINTF) printf("************Gcx::newPool(npages = %d)****************\n", npages);
1933 // Minimum of POOLSIZE
1934 if (npages < POOLSIZE/PAGESIZE)
1935 npages = POOLSIZE/PAGESIZE;
1936 else if (npages > POOLSIZE/PAGESIZE)
1937 { // Give us 150% of requested size, so there's room to extend
1938 auto n = npages + (npages >> 1);
1939 if (n < size_t.max/PAGESIZE)
1943 // Allocate successively larger pools up to 8 megs
1949 n = 8; // cap pool size at 8 megs
1950 n *= (POOLSIZE / PAGESIZE);
1955 pool = cast(Pool *) .calloc(1, Pool.sizeof);
1958 pool.initialize(npages);
1962 newnpools = npools + 1;
1963 newpooltable = cast(Pool **) .realloc(pooltable, newnpools * (Pool *).sizeof);
1967 // Sort pool into newpooltable[]
1968 for (i = 0; i < npools; i++)
1970 if (pool.opCmp(newpooltable[i]) < 0)
1973 .memmove(newpooltable + i + 1, newpooltable + i, (npools - i) * (Pool *).sizeof);
1974 newpooltable[i] = pool;
1976 pooltable = newpooltable;
1979 minAddr = pooltable[0].baseAddr;
1980 maxAddr = pooltable[npools - 1].topAddr;
1992 * Allocate a page of bin's.
1996 int allocPage(Bins bin)
2004 //debug(PRINTF) printf("Gcx::allocPage(bin = %d)\n", bin);
2005 for (n = 0; n < npools; n++)
2007 pool = pooltable[n];
2008 pn = pool.allocPages(1);
2015 pool.pagetable[pn] = cast(ubyte)bin;
2017 // Convert page to free list
2018 size_t size = binsize[bin];
2019 List **b = &bucket[bin];
2021 p = pool.baseAddr + pn * PAGESIZE;
2022 ptop = p + PAGESIZE;
2023 for (; p < ptop; p += size)
2025 (cast(List *)p).next = *b;
2033 * Search a range of memory values and mark any pointers into the GC pool.
2035 void mark(void *pbot, void *ptop)
2037 void **p1 = cast(void **)pbot;
2038 void **p2 = cast(void **)ptop;
2042 //printf("marking range: %p -> %p\n", pbot, ptop);
2043 for (; p1 < p2; p1++)
2046 byte *p = cast(byte *)(*p1);
2048 //if (log) debug(PRINTF) printf("\tmark %x\n", p);
2049 if (p >= minAddr && p < maxAddr)
2051 if ((cast(size_t)p & ~(PAGESIZE-1)) == pcache)
2057 size_t offset = cast(size_t)(p - pool.baseAddr);
2059 size_t pn = offset / PAGESIZE;
2060 Bins bin = cast(Bins)pool.pagetable[pn];
2062 //debug(PRINTF) printf("\t\tfound pool %x, base=%x, pn = %d, bin = %d, biti = x%x\n", pool, pool.baseAddr, pn, bin, biti);
2064 // Adjust bit to be at start of allocated memory block
2067 biti = (offset & notbinsize[bin]) >> 4;
2068 //debug(PRINTF) printf("\t\tbiti = x%x\n", biti);
2070 else if (bin == B_PAGEPLUS)
2074 } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS);
2075 biti = pn * (PAGESIZE / 16);
2079 // Don't mark bits in B_FREE pages
2083 if (bin >= B_PAGE) // Cache B_PAGE and B_PAGEPLUS lookups
2084 pcache = cast(size_t)p & ~(PAGESIZE-1);
2086 //debug(PRINTF) printf("\t\tmark(x%x) = %d\n", biti, pool.mark.test(biti));
2087 if (!pool.mark.test(biti))
2089 //if (log) debug(PRINTF) printf("\t\tmarking %x\n", p);
2090 pool.mark.set(biti);
2091 if (!pool.noscan.test(biti))
2093 pool.scan.set(biti);
2096 log_parent(sentinel_add(pool.baseAddr + biti * 16), sentinel_add(pbot));
2101 anychanges |= changes;
2106 * Return number of full pages free'd.
2108 size_t fullcollectshell()
2110 // The purpose of the 'shell' is to ensure all the registers
2111 // get put on the stack so they'll be scanned
2116 __builtin_unwind_init();
2123 uint eax,ecx,edx,ebx,ebp,esi,edi;
2136 else version (X86_64)
2138 ulong rax,rbx,rcx,rdx,rbp,rsi,rdi,rsp,r8,r9,r10,r11,r12,r13,r14,r15;
2141 movq rax[RBP], RAX ;
2142 movq rbx[RBP], RBX ;
2143 movq rcx[RBP], RCX ;
2144 movq rdx[RBP], RDX ;
2145 movq rbp[RBP], RBP ;
2146 movq rsi[RBP], RSI ;
2147 movq rdi[RBP], RDI ;
2148 movq rsp[RBP], RSP ;
2151 movq r10[RBP], R10 ;
2152 movq r11[RBP], R11 ;
2153 movq r12[RBP], R12 ;
2154 movq r13[RBP], R13 ;
2155 movq r14[RBP], R14 ;
2156 movq r15[RBP], R15 ;
2161 static assert( false, "Architecture not supported." );
2172 result = fullcollect(sp);
2195 size_t fullcollect(void *stackTop)
2200 debug(COLLECT_PRINTF) printf("Gcx.fullcollect()\n");
2202 thread_suspendAll();
2208 for (n = 0; n < npools; n++)
2210 pool = pooltable[n];
2213 pool.freebits.zero();
2216 // Mark each free entry, so it doesn't get scanned
2217 for (n = 0; n < B_PAGE; n++)
2219 for (List *list = bucket[n]; list; list = list.next)
2221 pool = findPool(list);
2223 pool.freebits.set(cast(size_t)(cast(byte*)list - pool.baseAddr) / 16);
2227 for (n = 0; n < npools; n++)
2229 pool = pooltable[n];
2230 pool.mark.copy(&pool.freebits);
2233 rt_scanStaticData( &mark );
2237 // Scan stacks and registers for each paused thread
2238 thread_scanAll( &mark, stackTop );
2242 debug(COLLECT_PRINTF) printf("scan roots[]\n");
2243 mark(roots, roots + nroots);
2246 debug(COLLECT_PRINTF) printf("scan ranges[]\n");
2248 for (n = 0; n < nranges; n++)
2250 debug(COLLECT_PRINTF) printf("\t%x .. %x\n", ranges[n].pbot, ranges[n].ptop);
2251 mark(ranges[n].pbot, ranges[n].ptop);
2255 debug(COLLECT_PRINTF) printf("\tscan heap\n");
2259 for (n = 0; n < npools; n++)
2265 pool = pooltable[n];
2267 bbase = pool.scan.base();
2268 btop = bbase + pool.scan.nwords;
2269 for (b = bbase; b < btop;)
2283 o = pool.baseAddr + (b - bbase) * 32 * 16;
2284 if (!(bitm & 0xFFFF))
2289 for (; bitm; o += 16, bitm >>= 1)
2294 pn = cast(size_t)(o - pool.baseAddr) / PAGESIZE;
2295 bin = cast(Bins)pool.pagetable[pn];
2298 mark(o, o + binsize[bin]);
2300 else if (bin == B_PAGE || bin == B_PAGEPLUS)
2302 if (bin == B_PAGEPLUS)
2304 while (pool.pagetable[pn - 1] != B_PAGE)
2308 while (pn + u < pool.npages && pool.pagetable[pn + u] == B_PAGEPLUS)
2310 mark(o, o + u * PAGESIZE);
2319 // Free up everything not marked
2320 debug(COLLECT_PRINTF) printf("\tfree'ing\n");
2321 size_t freedpages = 0;
2323 for (n = 0; n < npools; n++)
2327 pool = pooltable[n];
2328 bbase = pool.mark.base();
2329 for (pn = 0; pn < pool.npages; pn++, bbase += PAGESIZE / (32 * 16))
2331 Bins bin = cast(Bins)pool.pagetable[pn];
2338 auto size = binsize[bin];
2340 p = pool.baseAddr + pn * PAGESIZE;
2341 ptop = p + PAGESIZE;
2342 biti = pn * (PAGESIZE/16);
2343 bitstride = size / 16;
2345 version(none) // BUG: doesn't work because freebits() must also be cleared
2347 // If free'd entire page
2348 if (bbase[0] == 0 && bbase[1] == 0 && bbase[2] == 0 && bbase[3] == 0 &&
2349 bbase[4] == 0 && bbase[5] == 0 && bbase[6] == 0 && bbase[7] == 0)
2351 for (; p < ptop; p += size, biti += bitstride)
2353 if (pool.finals.nbits && pool.finals.testClear(biti))
2354 rt_finalize(cast(List *)sentinel_add(p), false/*noStack > 0*/);
2355 gcx.clrBits(pool, biti, BlkAttr.ALL_BITS);
2357 List *list = cast(List *)p;
2358 //debug(PRINTF) printf("\tcollecting %x\n", list);
2359 log_free(sentinel_add(list));
2361 debug (MEMSTOMP) .memset(p, 0xF3, size);
2363 pool.pagetable[pn] = B_FREE;
2365 //debug(PRINTF) printf("freeing entire page %d\n", pn);
2369 for (; p < ptop; p += size, biti += bitstride)
2371 if (!pool.mark.test(biti))
2373 sentinel_Invariant(sentinel_add(p));
2375 pool.freebits.set(biti);
2376 if (pool.finals.nbits && pool.finals.testClear(biti))
2377 rt_finalize(cast(List *)sentinel_add(p), false/*noStack > 0*/);
2378 clrBits(pool, biti, BlkAttr.ALL_BITS);
2380 List *list = cast(List *)p;
2381 debug(PRINTF) printf("\tcollecting %x\n", list);
2382 log_free(sentinel_add(list));
2384 debug (MEMSTOMP) .memset(p, 0xF3, size);
2390 else if (bin == B_PAGE)
2391 { size_t biti = pn * (PAGESIZE / 16);
2393 if (!pool.mark.test(biti))
2394 { byte *p = pool.baseAddr + pn * PAGESIZE;
2396 sentinel_Invariant(sentinel_add(p));
2397 if (pool.finals.nbits && pool.finals.testClear(biti))
2398 rt_finalize(sentinel_add(p), false/*noStack > 0*/);
2399 clrBits(pool, biti, BlkAttr.ALL_BITS);
2401 debug(COLLECT_PRINTF) printf("\tcollecting big %x\n", p);
2402 log_free(sentinel_add(p));
2403 pool.pagetable[pn] = B_FREE;
2405 debug (MEMSTOMP) .memset(p, 0xF3, PAGESIZE);
2406 while (pn + 1 < pool.npages && pool.pagetable[pn + 1] == B_PAGEPLUS)
2409 pool.pagetable[pn] = B_FREE;
2414 .memset(p, 0xF3, PAGESIZE);
2425 // Free complete pages, rebuild free list
2426 debug(COLLECT_PRINTF) printf("\tfree complete pages\n");
2427 size_t recoveredpages = 0;
2428 for (n = 0; n < npools; n++)
2431 pool = pooltable[n];
2432 for (pn = 0; pn < pool.npages; pn++)
2434 Bins bin = cast(Bins)pool.pagetable[pn];
2440 size_t size = binsize[bin];
2441 size_t bitstride = size / 16;
2442 size_t bitbase = pn * (PAGESIZE / 16);
2443 size_t bittop = bitbase + (PAGESIZE / 16);
2447 for (biti = bitbase; biti < bittop; biti += bitstride)
2448 { if (!pool.freebits.test(biti))
2451 pool.pagetable[pn] = B_FREE;
2456 p = pool.baseAddr + pn * PAGESIZE;
2457 for (u = 0; u < PAGESIZE; u += size)
2458 { biti = bitbase + u / 16;
2459 if (pool.freebits.test(biti))
2462 list = cast(List *)(p + u);
2463 if (list.next != bucket[bin]) // avoid unnecessary writes
2464 list.next = bucket[bin];
2472 debug(COLLECT_PRINTF) printf("recovered pages = %d\n", recoveredpages);
2473 debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, npools);
2475 return freedpages + recoveredpages;
2482 uint getBits(Pool* pool, size_t biti)
2491 if (pool.finals.nbits &&
2492 pool.finals.test(biti))
2493 bits |= BlkAttr.FINALIZE;
2494 if (pool.noscan.test(biti))
2495 bits |= BlkAttr.NO_SCAN;
2496 // if (pool.nomove.nbits &&
2497 // pool.nomove.test(biti))
2498 // bits |= BlkAttr.NO_MOVE;
2506 void setBits(Pool* pool, size_t biti, uint mask)
2513 if (mask & BlkAttr.FINALIZE)
2515 if (!pool.finals.nbits)
2516 pool.finals.alloc(pool.mark.nbits);
2517 pool.finals.set(biti);
2519 if (mask & BlkAttr.NO_SCAN)
2521 pool.noscan.set(biti);
2523 // if (mask & BlkAttr.NO_MOVE)
2525 // if (!pool.nomove.nbits)
2526 // pool.nomove.alloc(pool.mark.nbits);
2527 // pool.nomove.set(biti);
2535 void clrBits(Pool* pool, size_t biti, uint mask)
2542 if (mask & BlkAttr.FINALIZE && pool.finals.nbits)
2543 pool.finals.clear(biti);
2544 if (mask & BlkAttr.NO_SCAN)
2545 pool.noscan.clear(biti);
2546 // if (mask & BlkAttr.NO_MOVE && pool.nomove.nbits)
2547 // pool.nomove.clear(biti);
2551 /***** Leak Detector ******/
2562 //debug(PRINTF) printf("+log_init()\n");
2563 current.reserve(1000);
2565 //debug(PRINTF) printf("-log_init()\n");
2569 void log_malloc(void *p, size_t size)
2571 //debug(PRINTF) printf("+log_malloc(p = %x, size = %d)\n", p, size);
2584 //debug(PRINTF) printf("-log_malloc()\n");
2588 void log_free(void *p)
2590 //debug(PRINTF) printf("+log_free(%x)\n", p);
2593 i = current.find(p);
2596 debug(PRINTF) printf("free'ing unallocated memory %x\n", p);
2600 //debug(PRINTF) printf("-log_free()\n");
2606 //debug(PRINTF) printf("+log_collect()\n");
2607 // Print everything in current that is not in prev
2609 debug(PRINTF) printf("New pointers this cycle: --------------------------------\n");
2611 for (size_t i = 0; i < current.dim; i++)
2615 j = prev.find(current.data[i].p);
2617 current.data[i].print();
2622 debug(PRINTF) printf("All roots this cycle: --------------------------------\n");
2623 for (size_t i = 0; i < current.dim; i++)
2628 p = current.data[i].p;
2629 if (!findPool(current.data[i].parent))
2631 j = prev.find(current.data[i].p);
2633 debug(PRINTF) printf("N");
2635 debug(PRINTF) printf(" ");;
2636 current.data[i].print();
2640 debug(PRINTF) printf("Used = %d-------------------------------------------------\n", used);
2641 prev.copy(¤t);
2643 debug(PRINTF) printf("-log_collect()\n");
2647 void log_parent(void *p, void *parent)
2649 //debug(PRINTF) printf("+log_parent()\n");
2652 i = current.find(p);
2655 debug(PRINTF) printf("parent'ing unallocated memory %x, parent = %x\n", p, parent);
2659 size_t offset = cast(size_t)(p - pool.baseAddr);
2661 size_t pn = offset / PAGESIZE;
2662 Bins bin = cast(Bins)pool.pagetable[pn];
2663 biti = (offset & notbinsize[bin]);
2664 debug(PRINTF) printf("\tbin = %d, offset = x%x, biti = x%x\n", bin, offset, biti);
2668 current.data[i].parent = parent;
2670 //debug(PRINTF) printf("-log_parent()\n");
2677 void log_malloc(void *p, size_t size) { }
2678 void log_free(void *p) { }
2679 void log_collect() { }
2680 void log_parent(void *p, void *parent) { }
2685 /* ============================ Pool =============================== */
2692 GCBits mark; // entries already scanned, or should not be scanned
2693 GCBits scan; // entries that need to be scanned
2694 GCBits freebits; // entries that are on the free list
2695 GCBits finals; // entries that need finalizer run on them
2696 GCBits noscan; // entries that should not be scanned
2702 void initialize(size_t npages)
2706 //debug(PRINTF) printf("Pool::Pool(%u)\n", npages);
2707 poolsize = npages * PAGESIZE;
2708 assert(poolsize >= POOLSIZE);
2709 baseAddr = cast(byte *)os_mem_map(poolsize);
2711 // Some of the code depends on page alignment of memory pools
2712 assert((cast(size_t)baseAddr & (PAGESIZE - 1)) == 0);
2716 //debug(PRINTF) printf("GC fail: poolsize = x%x, errno = %d\n", poolsize, errno);
2717 //debug(PRINTF) printf("message = '%s'\n", sys_errlist[errno]);
2723 topAddr = baseAddr + poolsize;
2725 mark.alloc(cast(size_t)poolsize / 16);
2726 scan.alloc(cast(size_t)poolsize / 16);
2727 freebits.alloc(cast(size_t)poolsize / 16);
2728 noscan.alloc(cast(size_t)poolsize / 16);
2730 pagetable = cast(ubyte*) .malloc(npages);
2732 onOutOfMemoryError();
2733 .memset(pagetable, B_FREE, npages);
2735 this.npages = npages;
2747 result = os_mem_unmap(baseAddr, npages * PAGESIZE);
2766 void Invariant() { }
2773 //freebits.Invariant();
2774 //finals.Invariant();
2775 //noscan.Invariant();
2779 //if (baseAddr + npages * PAGESIZE != topAddr)
2780 //printf("baseAddr = %p, npages = %d, topAddr = %p\n", baseAddr, npages, topAddr);
2781 assert(baseAddr + npages * PAGESIZE == topAddr);
2784 for (size_t i = 0; i < npages; i++)
2785 { Bins bin = cast(Bins)pagetable[i];
2787 assert(bin < B_MAX);
2793 * Allocate n pages from Pool.
2794 * Returns OPFAIL on failure.
2796 size_t allocPages(size_t n)
2801 //debug(PRINTF) printf("Pool::allocPages(n = %d)\n", n);
2803 for (i = 0; i < npages; i++)
2805 if (pagetable[i] == B_FREE)
2808 { //debug(PRINTF) printf("\texisting pn = %d\n", i - n + 1);
2820 * Free npages pages starting with pagenum.
2822 void freePages(size_t pagenum, size_t npages)
2824 .memset(&pagetable[pagenum], B_FREE, npages);
2829 * Used for sorting pooltable[]
2833 if (baseAddr < p2.baseAddr)
2836 return cast(int)(baseAddr > p2.baseAddr);
2841 /* ============================ SENTINEL =============================== */
2846 const size_t SENTINEL_PRE = cast(size_t) 0xF4F4F4F4F4F4F4F4UL; // 32 or 64 bits
2847 const ubyte SENTINEL_POST = 0xF5; // 8 bits
2848 const uint SENTINEL_EXTRA = 2 * size_t.sizeof + 1;
2851 size_t* sentinel_size(void *p) { return &(cast(size_t *)p)[-2]; }
2852 size_t* sentinel_pre(void *p) { return &(cast(size_t *)p)[-1]; }
2853 ubyte* sentinel_post(void *p) { return &(cast(ubyte *)p)[*sentinel_size(p)]; }
2856 void sentinel_init(void *p, size_t size)
2858 *sentinel_size(p) = size;
2859 *sentinel_pre(p) = SENTINEL_PRE;
2860 *sentinel_post(p) = SENTINEL_POST;
2864 void sentinel_Invariant(void *p)
2866 assert(*sentinel_pre(p) == SENTINEL_PRE);
2867 assert(*sentinel_post(p) == SENTINEL_POST);
2871 void *sentinel_add(void *p)
2873 return p + 2 * size_t.sizeof;
2877 void *sentinel_sub(void *p)
2879 return p - 2 * size_t.sizeof;
2884 const uint SENTINEL_EXTRA = 0;
2887 void sentinel_init(void *p, size_t size)
2892 void sentinel_Invariant(void *p)
2897 void *sentinel_add(void *p)
2903 void *sentinel_sub(void *p)