X-Git-Url: https://git.llucax.com/software/dgc/cdgc.git/blobdiff_plain/894bf556097247f161a48f380424ea0e8180f393..11b7fc5cdb1df190c6c434fea14a00e700721fbc:/rt/gc/cdgc/gc.d diff --git a/rt/gc/cdgc/gc.d b/rt/gc/cdgc/gc.d index f73bcb0..e7fdbd2 100644 --- a/rt/gc/cdgc/gc.d +++ b/rt/gc/cdgc/gc.d @@ -30,9 +30,7 @@ module rt.gc.cdgc.gc; /************** Debugging ***************************/ -//debug = PRINTF; // turn on printf's //debug = COLLECT_PRINTF; // turn on printf's -//debug = LOGGING; // log allocations / frees //debug = MEMSTOMP; // stomp on memory //debug = SENTINEL; // add underrun/overrrun protection //debug = PTRCHECK; // more pointer checking @@ -49,8 +47,27 @@ version = STACKGROWSDOWN; // growing the stack means subtracting from the import rt.gc.cdgc.bits: GCBits; import rt.gc.cdgc.stats: GCStats; import alloc = rt.gc.cdgc.alloc; -import libc = rt.gc.cdgc.libc; +import rt.gc.cdgc.dynarray: DynArray; +import cstdlib = tango.stdc.stdlib; +import cstring = tango.stdc.string; + +/* + * This is a small optimization that proved it's usefulness. For small chunks + * or memory memset() seems to be slower (probably because of the call) that + * simply doing a simple loop to set the memory. + */ +void memset(void* dst, int c, size_t n) +{ + // This number (32) has been determined empirically + if (n > 32) { + cstring.memset(dst, c, n); + return; + } + auto p = cast(ubyte*)(dst); + while (n-- > 0) + *p++ = c; +} version (GNU) { @@ -111,106 +128,6 @@ private alias GC gc_t; -/* ======================= Leak Detector =========================== */ - - -debug (LOGGING) -{ - struct Log - { - void* p; - size_t size; - size_t line; - char* file; - void* parent; - - void print() - { - printf(" p = %x, size = %d, parent = %x ", p, size, parent); - if (file) - { - printf("%s(%u)", file, line); - } - printf("\n"); - } - } - - - struct LogArray - { - size_t dim; - size_t allocdim; - Log *data; - - void Dtor() - { - if (data) - libc.free(data); - data = null; - } - - void reserve(size_t nentries) - { - assert(dim <= allocdim); - if (allocdim - dim < nentries) - { - allocdim = (dim + nentries) * 2; - assert(dim + nentries <= allocdim); - if (!data) - { - data = cast(Log*) libc.malloc(allocdim * Log.sizeof); - if (!data && allocdim) - onOutOfMemoryError(); - } - else - { Log *newdata; - - newdata = cast(Log*) libc.malloc(allocdim * Log.sizeof); - if (!newdata && allocdim) - onOutOfMemoryError(); - libc.memcpy(newdata, data, dim * Log.sizeof); - libc.free(data); - data = newdata; - } - } - } - - - void push(Log log) - { - reserve(1); - data[dim++] = log; - } - - void remove(size_t i) - { - libc.memmove(data + i, data + i + 1, (dim - i) * Log.sizeof); - dim--; - } - - - size_t find(void *p) - { - for (size_t i = 0; i < dim; i++) - { - if (data[i].p == p) - return i; - } - return OPFAIL; // not found - } - - - void copy(LogArray *from) - { - reserve(from.dim - dim); - assert(from.dim <= allocdim); - libc.memcpy(data, from.data, from.dim * Log.sizeof); - dim = from.dim; - } - } -} - - /* ============================ GC =============================== */ @@ -235,7 +152,7 @@ class GC void initialize() { gcLock = GCLock.classinfo; - gcx = cast(Gcx*) libc.calloc(1, Gcx.sizeof); + gcx = cast(Gcx*) cstdlib.calloc(1, Gcx.sizeof); if (!gcx) onOutOfMemoryError(); gcx.initialize(); @@ -243,17 +160,6 @@ class GC } - void Dtor() - { - if (gcx) - { - gcx.Dtor(); - libc.free(gcx); - gcx = null; - } - } - - /** * */ @@ -426,7 +332,6 @@ class GC void *p = null; Bins bin; - //debug(PRINTF) printf("GC::malloc(size = %d, gcx = %p)\n", size, gcx); assert(gcx); size += SENTINEL_EXTRA; @@ -468,10 +373,9 @@ class GC } } if (!gcx.bucket[bin] && !gcx.allocPage(bin)) - { int result; - + { gcx.newPool(1); // allocate new pool to find a new page - result = gcx.allocPage(bin); + int result = gcx.allocPage(bin); if (!result) onOutOfMemoryError(); } @@ -481,9 +385,8 @@ class GC // Return next item from free list gcx.bucket[bin] = (cast(List*)p).next; if( !(bits & BlkAttr.NO_SCAN) ) - libc.memset(p + size, 0, binsize[bin] - size); - //debug(PRINTF) printf("\tmalloc => %x\n", p); - debug (MEMSTOMP) libc.memset(p, 0xF0, size); + memset(p + size, 0, binsize[bin] - size); + debug (MEMSTOMP) memset(p, 0xF0, size); } else { @@ -494,7 +397,6 @@ class GC size -= SENTINEL_EXTRA; p = sentinel_add(p); sentinel_init(p, size); - gcx.log_malloc(p, size); if (bits) { @@ -535,9 +437,8 @@ class GC { assert(size != 0); - //debug(PRINTF) printf("calloc: %x len %d\n", p, len); void *p = mallocNoSync(size, bits); - libc.memset(p, 0, size); + memset(p, 0, size); return p; } @@ -564,8 +465,10 @@ class GC private void *reallocNoSync(void *p, size_t size, uint bits = 0) { if (!size) - { if (p) - { freeNoSync(p); + { + if (p) + { + freeNoSync(p); p = null; } } @@ -574,10 +477,10 @@ class GC p = mallocNoSync(size, bits); } else - { void *p2; + { + void *p2; size_t psize; - //debug(PRINTF) printf("GC::realloc(p = %x, size = %u)\n", p, size); version (SENTINEL) { sentinel_Invariant(p); @@ -606,8 +509,7 @@ class GC p2 = mallocNoSync(size, bits); if (psize < size) size = psize; - //debug(PRINTF) printf("\tcopying %d bytes\n",size); - libc.memcpy(p2, p, size); + cstring.memcpy(p2, p, size); p = p2; } } @@ -625,11 +527,12 @@ class GC auto pagenum = (p - pool.baseAddr) / PAGESIZE; if (newsz < psz) - { // Shrink in place + { + // Shrink in place synchronized (gcLock) { debug (MEMSTOMP) - libc.memset(p + size, 0xF2, psize - size); + memset(p + size, 0xF2, psize - size); pool.freePages(pagenum + newsz, psz - newsz); } return p; @@ -644,10 +547,10 @@ class GC if (i == pagenum + newsz) { debug (MEMSTOMP) - libc.memset(p + psize, 0xF0, + memset(p + psize, 0xF0, size - psize); - libc.memset(&pool.pagetable[pagenum + psz], - B_PAGEPLUS, newsz - psz); + memset(pool.pagetable + pagenum + + psz, B_PAGEPLUS, newsz - psz); return p; } if (i == pool.npages) @@ -686,8 +589,7 @@ class GC p2 = mallocNoSync(size, bits); if (psize < size) size = psize; - //debug(PRINTF) printf("\tcopying %d bytes\n",size); - libc.memcpy(p2, p, size); + cstring.memcpy(p2, p, size); p = p2; } } @@ -728,7 +630,6 @@ class GC } body { - //debug(PRINTF) printf("GC::extend(p = %x, minsize = %u, maxsize = %u)\n", p, minsize, maxsize); version (SENTINEL) { return 0; @@ -751,7 +652,8 @@ class GC if (i == pool.npages) break; if (pool.pagetable[i] != B_FREE) - { if (sz < minsz) + { + if (sz < minsz) return 0; break; } @@ -759,8 +661,8 @@ class GC if (sz < minsz) return 0; debug (MEMSTOMP) - libc.memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize); - libc.memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz); + memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize); + memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz); gcx.p_cache = null; gcx.size_cache = 0; return (psz + sz) * PAGESIZE; @@ -845,27 +747,25 @@ class GC bin = cast(Bins)pool.pagetable[pagenum]; if (bin == B_PAGE) // if large alloc - { size_t npages; - size_t n; - + { // Free pages - npages = 1; - n = pagenum; + size_t npages = 1; + size_t n = pagenum; while (++n < pool.npages && pool.pagetable[n] == B_PAGEPLUS) npages++; - debug (MEMSTOMP) libc.memset(p, 0xF2, npages * PAGESIZE); + debug (MEMSTOMP) memset(p, 0xF2, npages * PAGESIZE); pool.freePages(pagenum, npages); } else - { // Add to free list + { + // Add to free list List *list = cast(List*)p; - debug (MEMSTOMP) libc.memset(p, 0xF2, binsize[bin]); + debug (MEMSTOMP) memset(p, 0xF2, binsize[bin]); list.next = gcx.bucket[bin]; gcx.bucket[bin] = list; } - gcx.log_free(sentinel_add(p)); } @@ -1080,7 +980,6 @@ class GC //p = (void *)((uint *)p + 4); if (p > gcx.stackBottom) { - //debug(PRINTF) printf("setStackBottom(%x)\n", p); gcx.stackBottom = p; } } @@ -1089,7 +988,6 @@ class GC //p = (void *)((uint *)p - 4); if (p < gcx.stackBottom) { - //debug(PRINTF) printf("setStackBottom(%x)\n", p); gcx.stackBottom = cast(char*)p; } } @@ -1108,11 +1006,13 @@ class GC if (!thread_needLock()) { - gcx.addRoot(p); + if (roots.append(p) is null) + onOutOfMemoryError(); } else synchronized (gcLock) { - gcx.addRoot(p); + if (roots.append(p) is null) + onOutOfMemoryError(); } } @@ -1127,14 +1027,16 @@ class GC return; } + bool r; if (!thread_needLock()) { - gcx.removeRoot(p); + r = roots.remove(p); } else synchronized (gcLock) { - gcx.removeRoot(p); + r = roots.remove(p); } + assert (r); } @@ -1148,16 +1050,16 @@ class GC return; } - //debug(PRINTF) printf("+GC.addRange(pbot = x%x, ptop = x%x)\n", pbot, ptop); if (!thread_needLock()) { - gcx.addRange(p, p + sz); + if (ranges.append(Range(p, p+sz)) is null) + onOutOfMemoryError(); } else synchronized (gcLock) { - gcx.addRange(p, p + sz); + if (ranges.append(Range(p, p+sz)) is null) + onOutOfMemoryError(); } - //debug(PRINTF) printf("-GC.addRange()\n"); } @@ -1171,14 +1073,16 @@ class GC return; } + bool r; if (!thread_needLock()) { - gcx.removeRange(p); + r = ranges.remove(Range(p, null)); } else synchronized (gcLock) { - gcx.removeRange(p); + r = ranges.remove(Range(p, null)); } + assert (r); } @@ -1187,7 +1091,6 @@ class GC */ void fullCollect() { - debug(PRINTF) printf("GC.fullCollect()\n"); if (!thread_needLock()) { @@ -1201,13 +1104,9 @@ class GC version (none) { GCStats stats; - getStats(stats); - debug(PRINTF) printf("poolsize = %x, usedsize = %x, freelistsize = %x\n", - stats.poolsize, stats.usedsize, stats.freelistsize); } - gcx.log_collect(); } @@ -1276,12 +1175,11 @@ class GC size_t n; size_t bsize = 0; - //debug(PRINTF) printf("getStats()\n"); - libc.memset(&stats, 0, GCStats.sizeof); - - for (n = 0; n < gcx.npools; n++) - { Pool *pool = gcx.pooltable[n]; + memset(&stats, 0, GCStats.sizeof); + for (n = 0; n < pools.length; n++) + { + Pool* pool = pools[n]; psize += pool.npages * PAGESIZE; for (size_t j = 0; j < pool.npages; j++) { @@ -1297,12 +1195,8 @@ class GC for (n = 0; n < B_PAGE; n++) { - //debug(PRINTF) printf("bin %d\n", n); for (List *list = gcx.bucket[n]; list; list = list.next) - { - //debug(PRINTF) printf("\tlist %x\n", list); flsize += binsize[n]; - } } usize = bsize - flsize; @@ -1349,7 +1243,7 @@ class GC // 1. to hide the reference from the GC // 2. the GC doesn't scan delegates added by rt_attachDisposeEvent // for references - auto wp = cast(WeakPointer*)(libc.malloc(WeakPointer.sizeof)); + auto wp = cast(WeakPointer*)(cstdlib.malloc(WeakPointer.sizeof)); if (!wp) onOutOfMemoryError(); wp.reference = r; @@ -1374,7 +1268,7 @@ class GC if (wp.reference) rt_detachDisposeEvent(wp.reference, &wp.ondestroy); }); - libc.free(wp); + cstdlib.free(wp); } } @@ -1402,7 +1296,8 @@ class GC /* ============================ Gcx =============================== */ enum -{ PAGESIZE = 4096, +{ + PAGESIZE = 4096, POOLSIZE = (4096*256), } @@ -1437,6 +1332,13 @@ struct Range { void *pbot; void *ptop; + int opCmp(in Range other) + { + if (pbot < other.pbot) + return -1; + else + return cast(int)(pbot > other.pbot); + } } @@ -1444,6 +1346,13 @@ const uint binsize[B_MAX] = [ 16,32,64,128,256,512,1024,2048,4096 ]; const uint notbinsize[B_MAX] = [ ~(16u-1),~(32u-1),~(64u-1),~(128u-1),~(256u-1), ~(512u-1),~(1024u-1),~(2048u-1),~(4096u-1) ]; +DynArray!(void*) roots; + +DynArray!(Range) ranges; + +DynArray!(Pool) pools; + + /* ============================ Gcx =============================== */ @@ -1453,14 +1362,6 @@ struct Gcx void *p_cache; size_t size_cache; - size_t nroots; - size_t rootdim; - void **roots; - - size_t nranges; - size_t rangedim; - Range *ranges; - uint noStack; // !=0 means don't scan stack uint log; // turn on logging uint anychanges; @@ -1471,44 +1372,19 @@ struct Gcx byte *minAddr; // min(baseAddr) byte *maxAddr; // max(topAddr) - size_t npools; - Pool **pooltable; - List *bucket[B_MAX]; // free list for each size void initialize() - { int dummy; - + { + int dummy; (cast(byte*)this)[0 .. Gcx.sizeof] = 0; stackBottom = cast(char*)&dummy; - log_init(); //printf("gcx = %p, self = %x\n", this, self); inited = 1; } - void Dtor() - { - inited = 0; - - for (size_t i = 0; i < npools; i++) - { Pool *pool = pooltable[i]; - - pool.Dtor(); - libc.free(pool); - } - if (pooltable) - libc.free(pooltable); - - if (roots) - libc.free(roots); - - if (ranges) - libc.free(ranges); - } - - void Invariant() { } @@ -1519,41 +1395,32 @@ struct Gcx //printf("Gcx.invariant(): this = %p\n", this); size_t i; - for (i = 0; i < npools; i++) - { Pool *pool = pooltable[i]; - + for (i = 0; i < pools.length; i++) + { + Pool* pool = pools[i]; pool.Invariant(); if (i == 0) { assert(minAddr == pool.baseAddr); } - if (i + 1 < npools) + if (i + 1 < pools.length) { - assert(pool.opCmp(pooltable[i + 1]) < 0); + assert(*pool < pools[i + 1]); } - else if (i + 1 == npools) + else if (i + 1 == pools.length) { assert(maxAddr == pool.topAddr); } } - if (roots) - { - assert(rootdim != 0); - assert(nroots <= rootdim); - } + roots.Invariant(); + ranges.Invariant(); - if (ranges) + for (i = 0; i < ranges.length; i++) { - assert(rangedim != 0); - assert(nranges <= rangedim); - - for (i = 0; i < nranges; i++) - { - assert(ranges[i].pbot); - assert(ranges[i].ptop); - assert(ranges[i].pbot <= ranges[i].ptop); - } + assert(ranges[i].pbot); + assert(ranges[i].ptop); + assert(ranges[i].pbot <= ranges[i].ptop); } for (i = 0; i < B_PAGE; i++) @@ -1566,124 +1433,26 @@ struct Gcx } - /** - * - */ - void addRoot(void *p) - { - if (nroots == rootdim) - { - size_t newdim = rootdim * 2 + 16; - void** newroots; - - newroots = cast(void**) libc.malloc(newdim * newroots[0].sizeof); - if (!newroots) - onOutOfMemoryError(); - if (roots) - { libc.memcpy(newroots, roots, nroots * newroots[0].sizeof); - libc.free(roots); - } - roots = newroots; - rootdim = newdim; - } - roots[nroots] = p; - nroots++; - } - - - /** - * - */ - void removeRoot(void *p) - { - for (size_t i = nroots; i--;) - { - if (roots[i] == p) - { - nroots--; - libc.memmove(roots + i, roots + i + 1, - (nroots - i) * roots[0].sizeof); - return; - } - } - assert(0); - } - - - /** - * - */ - void addRange(void *pbot, void *ptop) - { - debug (PRINTF) printf("%x.Gcx::addRange(%x, %x), nranges = %d\n", this, - pbot, ptop, nranges); - if (nranges == rangedim) - { - size_t newdim = rangedim * 2 + 16; - Range *newranges; - - newranges = cast(Range*) libc.malloc(newdim * newranges[0].sizeof); - if (!newranges) - onOutOfMemoryError(); - if (ranges) - { libc.memcpy(newranges, ranges, nranges * newranges[0].sizeof); - libc.free(ranges); - } - ranges = newranges; - rangedim = newdim; - } - ranges[nranges].pbot = pbot; - ranges[nranges].ptop = ptop; - nranges++; - } - - - /** - * - */ - void removeRange(void *pbot) - { - debug (PRINTF) printf("%x.Gcx.removeRange(%x), nranges = %d\n", this, - pbot, nranges); - for (size_t i = nranges; i--;) - { - if (ranges[i].pbot == pbot) - { - nranges--; - libc.memmove(ranges + i, ranges + i + 1, - (nranges - i) * ranges[0].sizeof); - return; - } - } - debug(PRINTF) printf("Wrong thread\n"); - - // This is a fatal error, but ignore it. - // The problem is that we can get a Close() call on a thread - // other than the one the range was allocated on. - //assert(zero); - } - - /** * Find Pool that pointer is in. * Return null if not in a Pool. - * Assume pooltable[] is sorted. + * Assume pools is sorted. */ Pool *findPool(void *p) { if (p >= minAddr && p < maxAddr) { - if (npools == 1) + if (pools.length == 1) { - return pooltable[0]; + return pools[0]; } - for (size_t i = 0; i < npools; i++) - { Pool *pool; - - pool = pooltable[i]; + for (size_t i = 0; i < pools.length; i++) + { + Pool* pool = pools[i]; if (p < pool.topAddr) - { if (pool.baseAddr <= p) + { + if (pool.baseAddr <= p) return pool; break; } @@ -1716,7 +1485,8 @@ struct Gcx else if (bin == B_PAGEPLUS) { do - { --pn, offset -= PAGESIZE; + { + --pn, offset -= PAGESIZE; } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS); return pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1))); @@ -1793,8 +1563,10 @@ struct Gcx else if (bin == B_PAGEPLUS) { do - { --pn, offset -= PAGESIZE; - } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS); + { + --pn, offset -= PAGESIZE; + } + while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS); info.base = pool.baseAddr + (offset & (offset.max ^ (PAGESIZE-1))); @@ -1835,8 +1607,8 @@ struct Gcx * Compute bin for size. */ static Bins findBin(size_t size) - { Bins bin; - + { + Bins bin; if (size <= 256) { if (size <= 64) @@ -1879,7 +1651,7 @@ struct Gcx /** * Allocate a new pool of at least size bytes. - * Sort it into pooltable[]. + * Sort it into pools. * Mark all memory in the pool as B_FREE. * Return the actual number of bytes reserved or 0 on error. */ @@ -1903,27 +1675,22 @@ struct Gcx size_t pn; Pool* pool; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; for (pn = 0; pn < pool.npages; pn++) { if (cast(Bins)pool.pagetable[pn] != B_FREE) break; } if (pn < pool.npages) - { - n++; continue; - } pool.Dtor(); - libc.free(pool); - libc.memmove(pooltable + n, - pooltable + n + 1, - (--npools - n) * (Pool*).sizeof); - minAddr = pooltable[0].baseAddr; - maxAddr = pooltable[npools - 1].topAddr; + pools.remove_at(n); + n--; } + minAddr = pools[0].baseAddr; + maxAddr = pools[pools.length - 1].topAddr; } @@ -1948,9 +1715,9 @@ struct Gcx // This code could use some refinement when repeatedly // allocating very large arrays. - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; pn = pool.allocPages(npages); if (pn != OPFAIL) goto L1; @@ -1961,13 +1728,15 @@ struct Gcx { case 0: if (disabled) - { state = 1; + { + state = 1; continue; } // Try collecting freedpages = fullcollectshell(); - if (freedpages >= npools * ((POOLSIZE / PAGESIZE) / 4)) - { state = 1; + if (freedpages >= pools.length * ((POOLSIZE / PAGESIZE) / 4)) + { + state = 1; continue; } // Release empty pools to prevent bloat @@ -1975,7 +1744,8 @@ struct Gcx // Allocate new pool pool = newPool(npages); if (!pool) - { state = 2; + { + state = 2; continue; } pn = pool.allocPages(npages); @@ -2001,11 +1771,10 @@ struct Gcx L1: pool.pagetable[pn] = B_PAGE; if (npages > 1) - libc.memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1); + memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1); p = pool.baseAddr + pn * PAGESIZE; - libc.memset(cast(char *)p + size, 0, npages * PAGESIZE - size); - debug (MEMSTOMP) libc.memset(p, 0xF1, size); - //debug(PRINTF) printf("\tp = %x\n", p); + memset(cast(char *)p + size, 0, npages * PAGESIZE - size); + debug (MEMSTOMP) memset(p, 0xF1, size); return p; Lnomemory: @@ -2015,33 +1784,26 @@ struct Gcx /** * Allocate a new pool with at least npages in it. - * Sort it into pooltable[]. + * Sort it into pools. * Return null if failed. */ Pool *newPool(size_t npages) { - Pool* pool; - Pool** newpooltable; - size_t newnpools; - size_t i; - - //debug(PRINTF) printf("************Gcx::newPool(npages = %d)****************\n", npages); - // Minimum of POOLSIZE if (npages < POOLSIZE/PAGESIZE) npages = POOLSIZE/PAGESIZE; else if (npages > POOLSIZE/PAGESIZE) - { // Give us 150% of requested size, so there's room to extend + { + // Give us 150% of requested size, so there's room to extend auto n = npages + (npages >> 1); if (n < size_t.max/PAGESIZE) npages = n; } // Allocate successively larger pools up to 8 megs - if (npools) - { size_t n; - - n = npools; + if (pools.length) + { + size_t n = pools.length; if (n > 8) n = 8; // cap pool size at 8 megs n *= (POOLSIZE / PAGESIZE); @@ -2049,41 +1811,21 @@ struct Gcx npages = n; } - pool = cast(Pool *) libc.calloc(1, Pool.sizeof); - if (pool) + Pool p; + p.initialize(npages); + if (!p.baseAddr) { - pool.initialize(npages); - if (!pool.baseAddr) - goto Lerr; - - newnpools = npools + 1; - newpooltable = cast(Pool **) libc.realloc(pooltable, - newnpools * (Pool *).sizeof); - if (!newpooltable) - goto Lerr; - - // Sort pool into newpooltable[] - for (i = 0; i < npools; i++) - { - if (pool.opCmp(newpooltable[i]) < 0) - break; - } - libc.memmove(newpooltable + i + 1, newpooltable + i, - (npools - i) * (Pool *).sizeof); - newpooltable[i] = pool; - - pooltable = newpooltable; - npools = newnpools; + p.Dtor(); + return null; + } - minAddr = pooltable[0].baseAddr; - maxAddr = pooltable[npools - 1].topAddr; + Pool* pool = pools.insert_sorted(p); + if (pool) + { + minAddr = pools[0].baseAddr; + maxAddr = pools[pools.length - 1].topAddr; } return pool; - - Lerr: - pool.Dtor(); - libc.free(pool); - return null; } @@ -2100,10 +1842,9 @@ struct Gcx byte* p; byte* ptop; - //debug(PRINTF) printf("Gcx::allocPage(bin = %d)\n", bin); - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; pn = pool.allocPages(1); if (pn != OPFAIL) goto L1; @@ -2144,11 +1885,10 @@ struct Gcx Pool *pool; byte *p = cast(byte *)(*p1); - //if (log) debug(PRINTF) printf("\tmark %x\n", p); if (p >= minAddr && p < maxAddr) { if ((cast(size_t)p & ~(PAGESIZE-1)) == pcache) - continue; + continue; pool = findPool(p); if (pool) @@ -2158,19 +1898,16 @@ struct Gcx size_t pn = offset / PAGESIZE; Bins bin = cast(Bins)pool.pagetable[pn]; - //debug(PRINTF) printf("\t\tfound pool %x, base=%x, pn = %d, bin = %d, biti = x%x\n", pool, pool.baseAddr, pn, bin, biti); - // Adjust bit to be at start of allocated memory block if (bin <= B_PAGE) - { biti = (offset & notbinsize[bin]) >> 4; - //debug(PRINTF) printf("\t\tbiti = x%x\n", biti); - } else if (bin == B_PAGEPLUS) { do - { --pn; - } while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS); + { + --pn; + } + while (cast(Bins)pool.pagetable[pn] == B_PAGEPLUS); biti = pn * (PAGESIZE / 16); } else @@ -2182,17 +1919,14 @@ struct Gcx if (bin >= B_PAGE) // Cache B_PAGE and B_PAGEPLUS lookups pcache = cast(size_t)p & ~(PAGESIZE-1); - //debug(PRINTF) printf("\t\tmark(x%x) = %d\n", biti, pool.mark.test(biti)); if (!pool.mark.test(biti)) { - //if (log) debug(PRINTF) printf("\t\tmarking %x\n", p); pool.mark.set(biti); if (!pool.noscan.test(biti)) { pool.scan.set(biti); changes = 1; } - log_parent(sentinel_add(pool.baseAddr + biti * 16), sentinel_add(pbot)); } } } @@ -2304,9 +2038,9 @@ struct Gcx size_cache = 0; anychanges = 0; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; pool.mark.zero(); pool.scan.zero(); pool.freebits.zero(); @@ -2323,9 +2057,9 @@ struct Gcx } } - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; pool.mark.copy(&pool.freebits); } @@ -2337,14 +2071,14 @@ struct Gcx thread_scanAll( &mark, stackTop ); } - // Scan roots[] + // Scan roots debug(COLLECT_PRINTF) printf("scan roots[]\n"); - mark(roots, roots + nroots); + mark(roots.ptr, roots.ptr + roots.length); - // Scan ranges[] + // Scan ranges debug(COLLECT_PRINTF) printf("scan ranges[]\n"); //log++; - for (n = 0; n < nranges; n++) + for (n = 0; n < ranges.length; n++) { debug(COLLECT_PRINTF) printf("\t%x .. %x\n", ranges[n].pbot, ranges[n].ptop); mark(ranges[n].pbot, ranges[n].ptop); @@ -2355,18 +2089,19 @@ struct Gcx while (anychanges) { anychanges = 0; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { uint *bbase; uint *b; uint *btop; - pool = pooltable[n]; + pool = pools[n]; bbase = pool.scan.base(); btop = bbase + pool.scan.nwords; for (b = bbase; b < btop;) - { Bins bin; + { + Bins bin; size_t pn; size_t u; size_t bitm; @@ -2374,7 +2109,8 @@ struct Gcx bitm = *b; if (!bitm) - { b++; + { + b++; continue; } *b = 0; @@ -2419,27 +2155,22 @@ struct Gcx debug(COLLECT_PRINTF) printf("\tfree'ing\n"); size_t freedpages = 0; size_t freed = 0; - for (n = 0; n < npools; n++) - { size_t pn; - uint* bbase; - - pool = pooltable[n]; - bbase = pool.mark.base(); + for (n = 0; n < pools.length; n++) + { + pool = pools[n]; + uint* bbase = pool.mark.base(); + size_t pn; for (pn = 0; pn < pool.npages; pn++, bbase += PAGESIZE / (32 * 16)) { Bins bin = cast(Bins)pool.pagetable[pn]; if (bin < B_PAGE) - { byte* p; - byte* ptop; - size_t biti; - size_t bitstride; - auto size = binsize[bin]; - - p = pool.baseAddr + pn * PAGESIZE; - ptop = p + PAGESIZE; - biti = pn * (PAGESIZE/16); - bitstride = size / 16; + { + auto size = binsize[bin]; + byte* p = pool.baseAddr + pn * PAGESIZE; + byte* ptop = p + PAGESIZE; + size_t biti = pn * (PAGESIZE/16); + size_t bitstride = size / 16; version(none) // BUG: doesn't work because freebits() must also be cleared { @@ -2454,14 +2185,11 @@ struct Gcx gcx.clrBits(pool, biti, BlkAttr.ALL_BITS); List *list = cast(List *)p; - //debug(PRINTF) printf("\tcollecting %x\n", list); - log_free(sentinel_add(list)); - debug (MEMSTOMP) libc.memset(p, 0xF3, size); + debug (MEMSTOMP) memset(p, 0xF3, size); } pool.pagetable[pn] = B_FREE; freed += PAGESIZE; - //debug(PRINTF) printf("freeing entire page %d\n", pn); continue; } } @@ -2477,31 +2205,28 @@ struct Gcx clrBits(pool, biti, BlkAttr.ALL_BITS); List *list = cast(List *)p; - debug(PRINTF) printf("\tcollecting %x\n", list); - log_free(sentinel_add(list)); - debug (MEMSTOMP) libc.memset(p, 0xF3, size); + debug (MEMSTOMP) memset(p, 0xF3, size); freed += size; } } } else if (bin == B_PAGE) - { size_t biti = pn * (PAGESIZE / 16); - + { + size_t biti = pn * (PAGESIZE / 16); if (!pool.mark.test(biti)) - { byte *p = pool.baseAddr + pn * PAGESIZE; - + { + byte *p = pool.baseAddr + pn * PAGESIZE; sentinel_Invariant(sentinel_add(p)); if (pool.finals.nbits && pool.finals.testClear(biti)) rt_finalize(sentinel_add(p), false/*noStack > 0*/); clrBits(pool, biti, BlkAttr.ALL_BITS); debug(COLLECT_PRINTF) printf("\tcollecting big %x\n", p); - log_free(sentinel_add(p)); pool.pagetable[pn] = B_FREE; freedpages++; - debug (MEMSTOMP) libc.memset(p, 0xF3, PAGESIZE); + debug (MEMSTOMP) memset(p, 0xF3, PAGESIZE); while (pn + 1 < pool.npages && pool.pagetable[pn + 1] == B_PAGEPLUS) { pn++; @@ -2509,8 +2234,9 @@ struct Gcx freedpages++; debug (MEMSTOMP) - { p += PAGESIZE; - libc.memset(p, 0xF3, PAGESIZE); + { + p += PAGESIZE; + memset(p, 0xF3, PAGESIZE); } } } @@ -2524,11 +2250,10 @@ struct Gcx // Free complete pages, rebuild free list debug(COLLECT_PRINTF) printf("\tfree complete pages\n"); size_t recoveredpages = 0; - for (n = 0; n < npools; n++) - { size_t pn; - - pool = pooltable[n]; - for (pn = 0; pn < pool.npages; pn++) + for (n = 0; n < pools.length; n++) + { + pool = pools[n]; + for (size_t pn = 0; pn < pool.npages; pn++) { Bins bin = cast(Bins)pool.pagetable[pn]; size_t biti; @@ -2544,7 +2269,8 @@ struct Gcx biti = bitbase; for (biti = bitbase; biti < bittop; biti += bitstride) - { if (!pool.freebits.test(biti)) + { + if (!pool.freebits.test(biti)) goto Lnotfree; } pool.pagetable[pn] = B_FREE; @@ -2554,11 +2280,11 @@ struct Gcx Lnotfree: p = pool.baseAddr + pn * PAGESIZE; for (u = 0; u < PAGESIZE; u += size) - { biti = bitbase + u / 16; + { + biti = bitbase + u / 16; if (pool.freebits.test(biti)) - { List *list; - - list = cast(List *)(p + u); + { + List *list = cast(List *)(p + u); if (list.next != bucket[bin]) // avoid unnecessary writes list.next = bucket[bin]; bucket[bin] = list; @@ -2569,7 +2295,7 @@ struct Gcx } debug(COLLECT_PRINTF) printf("recovered pages = %d\n", recoveredpages); - debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, npools); + debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, pools.length); return freedpages + recoveredpages; } @@ -2646,138 +2372,6 @@ struct Gcx // pool.nomove.clear(biti); } - - /***** Leak Detector ******/ - - - debug (LOGGING) - { - LogArray current; - LogArray prev; - - - void log_init() - { - //debug(PRINTF) printf("+log_init()\n"); - current.reserve(1000); - prev.reserve(1000); - //debug(PRINTF) printf("-log_init()\n"); - } - - - void log_malloc(void *p, size_t size) - { - //debug(PRINTF) printf("+log_malloc(p = %x, size = %d)\n", p, size); - Log log; - - log.p = p; - log.size = size; - log.line = GC.line; - log.file = GC.file; - log.parent = null; - - GC.line = 0; - GC.file = null; - - current.push(log); - //debug(PRINTF) printf("-log_malloc()\n"); - } - - - void log_free(void *p) - { - //debug(PRINTF) printf("+log_free(%x)\n", p); - size_t i; - - i = current.find(p); - if (i == OPFAIL) - { - debug(PRINTF) printf("free'ing unallocated memory %x\n", p); - } - else - current.remove(i); - //debug(PRINTF) printf("-log_free()\n"); - } - - - void log_collect() - { - //debug(PRINTF) printf("+log_collect()\n"); - // Print everything in current that is not in prev - - debug(PRINTF) printf("New pointers this cycle: --------------------------------\n"); - size_t used = 0; - for (size_t i = 0; i < current.dim; i++) - { - size_t j; - - j = prev.find(current.data[i].p); - if (j == OPFAIL) - current.data[i].print(); - else - used++; - } - - debug(PRINTF) printf("All roots this cycle: --------------------------------\n"); - for (size_t i = 0; i < current.dim; i++) - { - void *p; - size_t j; - - p = current.data[i].p; - if (!findPool(current.data[i].parent)) - { - j = prev.find(current.data[i].p); - if (j == OPFAIL) - debug(PRINTF) printf("N"); - else - debug(PRINTF) printf(" ");; - current.data[i].print(); - } - } - - debug(PRINTF) printf("Used = %d-------------------------------------------------\n", used); - prev.copy(¤t); - - debug(PRINTF) printf("-log_collect()\n"); - } - - - void log_parent(void *p, void *parent) - { - //debug(PRINTF) printf("+log_parent()\n"); - size_t i; - - i = current.find(p); - if (i == OPFAIL) - { - debug(PRINTF) printf("parent'ing unallocated memory %x, parent = %x\n", p, parent); - Pool *pool; - pool = findPool(p); - assert(pool); - size_t offset = cast(size_t)(p - pool.baseAddr); - size_t biti; - size_t pn = offset / PAGESIZE; - Bins bin = cast(Bins)pool.pagetable[pn]; - biti = (offset & notbinsize[bin]); - debug(PRINTF) printf("\tbin = %d, offset = x%x, biti = x%x\n", bin, offset, biti); - } - else - { - current.data[i].parent = parent; - } - //debug(PRINTF) printf("-log_parent()\n"); - } - - } - else - { - void log_init() { } - void log_malloc(void *p, size_t size) { } - void log_free(void *p) { } - void log_collect() { } - void log_parent(void *p, void *parent) { } - } } @@ -2800,10 +2394,7 @@ struct Pool void initialize(size_t npages) { - size_t poolsize; - - //debug(PRINTF) printf("Pool::Pool(%u)\n", npages); - poolsize = npages * PAGESIZE; + size_t poolsize = npages * PAGESIZE; assert(poolsize >= POOLSIZE); baseAddr = cast(byte *) alloc.os_mem_map(poolsize); @@ -2812,9 +2403,6 @@ struct Pool if (!baseAddr) { - //debug(PRINTF) printf("GC fail: poolsize = x%x, errno = %d\n", poolsize, errno); - //debug(PRINTF) printf("message = '%s'\n", sys_errlist[errno]); - npages = 0; poolsize = 0; } @@ -2826,10 +2414,10 @@ struct Pool freebits.alloc(cast(size_t)poolsize / 16); noscan.alloc(cast(size_t)poolsize / 16); - pagetable = cast(ubyte*) libc.malloc(npages); + pagetable = cast(ubyte*) cstdlib.malloc(npages); if (!pagetable) onOutOfMemoryError(); - libc.memset(pagetable, B_FREE, npages); + memset(pagetable, B_FREE, npages); this.npages = npages; } @@ -2851,8 +2439,9 @@ struct Pool baseAddr = null; topAddr = null; } + // See Gcx.Dtor() for the rationale of the null check. if (pagetable) - libc.free(pagetable); + cstdlib.free(pagetable); mark.Dtor(); scan.Dtor(); @@ -2881,8 +2470,8 @@ struct Pool } for (size_t i = 0; i < npages; i++) - { Bins bin = cast(Bins)pagetable[i]; - + { + Bins bin = cast(Bins)pagetable[i]; assert(bin < B_MAX); } } @@ -2897,16 +2486,13 @@ struct Pool size_t i; size_t n2; - //debug(PRINTF) printf("Pool::allocPages(n = %d)\n", n); n2 = n; for (i = 0; i < npages; i++) { if (pagetable[i] == B_FREE) { if (--n2 == 0) - { //debug(PRINTF) printf("\texisting pn = %d\n", i - n + 1); return i - n + 1; - } } else n2 = n; @@ -2920,19 +2506,19 @@ struct Pool */ void freePages(size_t pagenum, size_t npages) { - libc.memset(&pagetable[pagenum], B_FREE, npages); + memset(&pagetable[pagenum], B_FREE, npages); } /** - * Used for sorting pooltable[] + * Used for sorting pools */ - int opCmp(Pool *p2) + int opCmp(in Pool other) { - if (baseAddr < p2.baseAddr) + if (baseAddr < other.baseAddr) return -1; else - return cast(int)(baseAddr > p2.baseAddr); + return cast(int)(baseAddr > other.baseAddr); } } @@ -3005,3 +2591,5 @@ else } } + +// vim: set et sw=4 sts=4 :