diff --git a/src/core/bitop.d b/src/core/bitop.d index 6e1a255428..7ae45f01ca 100644 --- a/src/core/bitop.d +++ b/src/core/bitop.d @@ -396,6 +396,32 @@ struct BitRange } } + /** + * Construct a BitRange. + * + * Params: + * bitarr = The array of bits to iterate over + * numBits = The total number of valid bits in the given bit array + * startBit = The initial start index to start searching + */ + this(const(size_t)* bitarr, size_t numBits, size_t startBit) @system + { + assert(startBit <= numBits); + bits = bitarr; + len = numBits; + idx = startBit; + if (len) + { + bits += idx / bitsPerWord; + auto curbit = idx % bitsPerWord; + // prime the first bit + cur = *bits++ ^ (size_t(1) << curbit); + if (curbit) + cur &= ~((size_t(1) << curbit) - 1); // clear skipped lower bits + popFront(); + } + } + /// Range functions size_t front() { @@ -453,6 +479,11 @@ struct BitRange bts(bitArr, 95); bts(bitArr, 78); + assert(BitRange(bitArr, 100).front() == 24); + + bts(bitArr, 0); + assert(BitRange(bitArr, 100).front() == 0); + enum sum = 48 + 24 + 95 + 78; // iterate @@ -465,7 +496,10 @@ struct BitRange } assert(testSum == sum); - assert(nBits == 4); + assert(nBits == 5); + + assert(BitRange(bitArr, 100, 50).front() == 78); + assert(BitRange(bitArr, 100, 48).front() == 48); } @system unittest diff --git a/src/gc/bits.d b/src/gc/bits.d index 21d43c4f4f..125a2c7525 100644 --- a/src/gc/bits.d +++ b/src/gc/bits.d @@ -1,12 +1,12 @@ /** * Contains a bitfield used by the GC. * - * Copyright: Copyright Digital Mars 2005 - 2013. + * Copyright: Copyright Digital Mars 2005 - 2016. * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). - * Authors: Walter Bright, David Friedman, Sean Kelly + * Authors: Walter Bright, David Friedman, Sean Kelly, Rainer Schuetze */ -/* Copyright Digital Mars 2005 - 2013. +/* Copyright Digital Mars 2005 - 2016. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE or copy at * http://www.boost.org/LICENSE_1_0.txt) @@ -19,6 +19,11 @@ import core.stdc.string; import core.stdc.stdlib; import core.exception : onOutOfMemoryError; + +// use version bitwise to disable optimizations that use word operands +// on bulk operation copyRange, setRange, clrRange, etc. +// version = bitwise; + struct GCBits { alias size_t wordtype; @@ -26,7 +31,9 @@ struct GCBits enum BITS_PER_WORD = (wordtype.sizeof * 8); enum BITS_SHIFT = (wordtype.sizeof == 8 ? 6 : 5); enum BITS_MASK = (BITS_PER_WORD - 1); + enum BITS_0 = cast(wordtype)0; enum BITS_1 = cast(wordtype)1; + enum BITS_2 = cast(wordtype)2; wordtype* data; size_t nbits; @@ -48,6 +55,7 @@ struct GCBits onOutOfMemoryError(); } + pragma(inline,true) wordtype test(size_t i) const nothrow in { @@ -58,6 +66,7 @@ struct GCBits return core.bitop.bt(data, i); } + pragma(inline,true) int set(size_t i) nothrow in { @@ -68,6 +77,7 @@ struct GCBits return core.bitop.bts(data, i); } + pragma(inline,true) int clear(size_t i) nothrow in { @@ -78,6 +88,355 @@ struct GCBits return core.bitop.btr(data, i); } + mixin template RangeVars() + { + size_t firstWord = (target >> BITS_SHIFT); + size_t firstOff = target & BITS_MASK; + size_t last = target + len - 1; + size_t lastWord = (last >> BITS_SHIFT); + size_t lastOff = last & BITS_MASK; + } + + // extract loops to allow inlining the rest + void clearWords(size_t firstWord, size_t lastWord) nothrow + { + for(size_t w = firstWord; w < lastWord; w++) + data[w] = 0; + } + + void setWords(size_t firstWord, size_t lastWord) nothrow + { + for(size_t w = firstWord; w < lastWord; w++) + data[w] = ~0; + } + + void copyWords(size_t firstWord, size_t lastWord, const(wordtype)* source) nothrow + { + for(size_t w = firstWord; w < lastWord; w++) + data[w] = source[w - firstWord]; + } + + void copyWordsShifted(size_t firstWord, size_t cntWords, size_t firstOff, const(wordtype)* source) nothrow + { + wordtype mask = ~BITS_0 << firstOff; + data[firstWord] = (data[firstWord] & ~mask) | (source[0] << firstOff); + for(size_t w = 1; w < cntWords; w++) + data[firstWord + w] = (source[w - 1] >> (BITS_PER_WORD - firstOff)) | (source[w] << firstOff); + } + + // target = the biti to start the copy to + // destlen = the number of bits to copy from source + pragma(inline,true) + void copyRange(size_t target, size_t len, const(wordtype)* source) nothrow + { + version(bitwise) + { + for (size_t i = 0; i < len; i++) + if(source[(i >> BITS_SHIFT)] & (BITS_1 << (i & BITS_MASK))) + set(target+i); + else + clear(target+i); + } + else + { + if(len > 0) + copyRangeZ(target, len, source); + } + } + + //pragma(inline,true) + void copyRangeZ(size_t target, size_t len, const(wordtype)* source) nothrow + { + mixin RangeVars!(); + + if(firstWord == lastWord) + { + wordtype mask = ((BITS_2 << (lastOff - firstOff)) - 1) << firstOff; + data[firstWord] = (data[firstWord] & ~mask) | ((source[0] << firstOff) & mask); + } + else if(firstOff == 0) + { + copyWords(firstWord, lastWord, source); + + wordtype mask = (BITS_2 << lastOff) - 1; + data[lastWord] = (data[lastWord] & ~mask) | (source[lastWord - firstWord] & mask); + } + else + { + size_t cntWords = lastWord - firstWord; + copyWordsShifted(firstWord, cntWords, firstOff, source); + + wordtype src = (source[cntWords - 1] >> (BITS_PER_WORD - firstOff)) | (source[cntWords] << firstOff); + wordtype mask = (BITS_2 << lastOff) - 1; + data[lastWord] = (data[lastWord] & ~mask) | (src & mask); + } + } + + void copyRangeRepeating(size_t target, size_t destlen, const(wordtype)* source, size_t sourcelen) nothrow + { + version(bitwise) + { + for (size_t i=0; i < destlen; i++) + { + bool b; + size_t j = i % sourcelen; + b = (source[j >> BITS_SHIFT] & (BITS_1 << (j & BITS_MASK))) != 0; + if (b) set(target+i); + else clear(target+i); + } + } + else + { + if (destlen > 4 * sourcelen && destlen > 4 * BITS_PER_WORD) + { + // precalculate the number of words where a bit pattern of the + // source length repeats on word alignment + static size_t gcd(size_t a, size_t b) + { + // euclidean algorithm + while (b != 0) + { + auto t = b; + b = a % b; + a = t; + } + return a; + } + // lowest common multiple (with BITS_PER_WORD) + static ubyte lcm(ubyte i) + { + // calc lcm(i,BITS_PER_WORD)/BITS_PER_WORD + return cast(ubyte)(BITS_PER_WORD / gcd(i, BITS_PER_WORD)); + } + static struct ut { unittest { assert(lcm(3) == BITS_PER_WORD); } } + static calcRepLength() + { + ubyte[BITS_PER_WORD] rep; + for (ubyte i = 0; i < BITS_PER_WORD; i++) + rep[i] = lcm(i); + return rep; + } + static immutable repLength = calcRepLength(); + + // make some initial copies until we have a pattern that + // repeats on word boundary + size_t rep = repLength[sourcelen & BITS_MASK]; + size_t repwords = (sourcelen * rep) >> BITS_SHIFT; + size_t alignbits = (target & BITS_MASK ? BITS_PER_WORD - (target & BITS_MASK) : 0); + size_t initbits = BITS_PER_WORD * repwords + alignbits; + + if (initbits < destlen) + { + while (initbits > sourcelen) + { + copyRange(target, sourcelen, source); + target += sourcelen; + destlen -= sourcelen; + initbits -= sourcelen; + } + copyRange(target, initbits, source); + target += initbits; + destlen -= initbits; + assert((target & BITS_MASK) == 0); + + size_t tpos = target >> BITS_SHIFT; + while (destlen >= BITS_PER_WORD) + { + data[tpos] = data[tpos - repwords]; + destlen -= BITS_PER_WORD; + tpos++; + } + + if (destlen > 0) + { + wordtype mask = (BITS_1 << destlen) - 1; + data[tpos] = (data[tpos] & ~mask) | (data[tpos - repwords] & mask); + } + return; + } + } + + while (destlen > sourcelen) + { + copyRange(target, sourcelen, source); + target += sourcelen; + destlen -= sourcelen; + } + copyRange(target, destlen, source); + } + } + + unittest + { + // simulate broken array append test case in vibe.d + GCBits bits; + bits.alloc(10000); + auto data = bits.data; + + GCBits src; + src.alloc(67); + src.data[0] = 0x4; + + bits.copyRangeRepeating(2, 10000, src.data, 67); + + foreach (i; 0 .. 10000) + if ((i - 2) % 67 == 2) + assert(bits.test(i)); + else + assert(!bits.test(i)); + } + + //pragma(inline,true) + void setRange(size_t target, size_t len) nothrow + { + version(bitwise) + { + for (size_t i = 0; i < len; i++) + set(target+i); + } + else + { + if(len > 0) + setRangeZ(target, len); + } + } + + //pragma(inline,true) + void setRangeZ(size_t target, size_t len) nothrow + { + mixin RangeVars!(); + + if(firstWord == lastWord) + { + wordtype mask = ((BITS_2 << (lastOff - firstOff)) - 1) << firstOff; + data[firstWord] |= mask; + } + else + { + data[firstWord] |= ~BITS_0 << firstOff; + setWords(firstWord + 1, lastWord); + wordtype mask = (BITS_2 << lastOff) - 1; + data[lastWord] |= mask; + } + } + + //pragma(inline,true) + void clrRange(size_t target, size_t len) nothrow + { + version(bitwise) + { + for (size_t i = 0; i < len; i++) + clear(target+i); + } + else + { + if(len > 0) + clrRangeZ(target, len); + } + } + + //pragma(inline,true) + void clrRangeZ(size_t target, size_t len) nothrow + { + mixin RangeVars!(); + if(firstWord == lastWord) + { + wordtype mask = ((BITS_2 << (lastOff - firstOff)) - 1) << firstOff; + data[firstWord] &= ~mask; + } + else + { + data[firstWord] &= ~(~BITS_0 << firstOff); + clearWords(firstWord + 1, lastWord); + wordtype mask = (BITS_2 << lastOff) - 1; + data[lastWord] &= ~mask; + } + } + + unittest + { + GCBits bits; + bits.alloc(1000); + auto data = bits.data; + + bits.setRange(0,1); + assert(data[0] == 1); + + bits.clrRange(0,1); + assert(data[0] == 0); + + bits.setRange(BITS_PER_WORD-1,1); + assert(data[0] == BITS_1 << (BITS_PER_WORD-1)); + + bits.clrRange(BITS_PER_WORD-1,1); + assert(data[0] == 0); + + bits.setRange(12,7); + assert(data[0] == 0b0111_1111_0000_0000_0000); + + bits.clrRange(14,4); + assert(data[0] == 0b0100_0011_0000_0000_0000); + + bits.clrRange(0,BITS_PER_WORD); + assert(data[0] == 0); + + bits.setRange(0,BITS_PER_WORD); + assert(data[0] == ~0); + assert(data[1] == 0); + + bits.setRange(BITS_PER_WORD,BITS_PER_WORD); + assert(data[0] == ~0); + assert(data[1] == ~0); + assert(data[2] == 0); + bits.clrRange(BITS_PER_WORD/2,BITS_PER_WORD); + assert(data[0] == (BITS_1 << (BITS_PER_WORD/2)) - 1); + assert(data[1] == ~data[0]); + assert(data[2] == 0); + + bits.setRange(8*BITS_PER_WORD+1,4*BITS_PER_WORD-2); + assert(data[8] == ~0 << 1); + assert(data[9] == ~0); + assert(data[10] == ~0); + assert(data[11] == cast(wordtype)~0 >> 1); + + bits.clrRange(9*BITS_PER_WORD+1,2*BITS_PER_WORD); + assert(data[8] == ~0 << 1); + assert(data[9] == 1); + assert(data[10] == 0); + assert(data[11] == ((cast(wordtype)~0 >> 1) & ~1)); + + wordtype[4] src = [ 0xa, 0x5, 0xaa, 0x55 ]; + + void testCopyRange(size_t start, size_t len, int repeat = 1) + { + bits.setRange(0, bits.nbits); + if (repeat > 1) + bits.copyRangeRepeating(start, repeat * len, src.ptr, len); + else + bits.copyRange(start, len, src.ptr); + foreach (i; 0 .. start) + assert(bits.test(i)); + foreach (r; 0 .. repeat) + foreach (i; 0 .. len) + assert(!bits.test(start + r*len + i) == !core.bitop.bt(src.ptr, i)); + foreach (i; start + repeat*len .. 10*BITS_PER_WORD) + assert(bits.test(i)); + } + + testCopyRange(20, 10); // short copy range within same word + testCopyRange(50, 20); // short copy range spanning two words + testCopyRange(64, 3 * BITS_PER_WORD + 3); // aligned copy range + testCopyRange(77, 2 * BITS_PER_WORD + 15); // unaligned copy range + testCopyRange(64, 127); // copy range within critical end alignment + + testCopyRange(10, 4, 5); // repeating small range within same word + testCopyRange(20, 5, 10); // repeating small range spanning two words + testCopyRange(40, 21, 7); // repeating medium range + testCopyRange(73, 2 * BITS_PER_WORD + 15, 5); // repeating multi-word range + + testCopyRange(2, 3, 166); // failed with assert + } + void zero() nothrow { memset(data, 0, nwords * wordtype.sizeof); diff --git a/src/gc/config.d b/src/gc/config.d index 1a3631bd88..0ca033294e 100644 --- a/src/gc/config.d +++ b/src/gc/config.d @@ -16,7 +16,7 @@ struct Config { bool disable; // start disabled ubyte profile; // enable profiling with summary when terminating program - string gc = "conservative"; // select gc implementation conservative|manual + string gc = "precise"; // select gc implementation conservative|precise|manual size_t initReserve; // initial reserve (MB) size_t minPoolSize = 1; // initial and minimum pool size (MB) diff --git a/src/gc/impl/conservative/gc.d b/src/gc/impl/conservative/gc.d index d9b51004a7..3ea9c99a6c 100644 --- a/src/gc/impl/conservative/gc.d +++ b/src/gc/impl/conservative/gc.d @@ -19,6 +19,7 @@ module gc.impl.conservative.gc; //debug = PRINTF; // turn on printf's //debug = COLLECT_PRINTF; // turn on printf's +//debug = MARK_PRINTF; // turn on printf's //debug = PRINTF_TO_FILE; // redirect printf's ouptut to file "gcx.log" //debug = LOGGING; // log allocations / frees //debug = MEMSTOMP; // stomp on memory @@ -63,6 +64,7 @@ debug(PRINTF_TO_FILE) { private __gshared MonoTime gcStartTick; private __gshared FILE* gcx_fh; + private __gshared bool hadNewline = false; private int printf(ARGS...)(const char* fmt, ARGS args) nothrow { @@ -76,7 +78,7 @@ debug(PRINTF_TO_FILE) { len = fprintf(gcx_fh, "before init: "); } - else + else if (hadNewline) { if (gcStartTick == MonoTime.init) gcStartTick = MonoTime.currTime; @@ -86,6 +88,8 @@ debug(PRINTF_TO_FILE) } len += fprintf(gcx_fh, fmt, args); fflush(gcx_fh); + import core.stdc.string; + hadNewline = fmt && fmt[0] && fmt[strlen(fmt) - 1] == '\n'; return len; } } @@ -249,6 +253,38 @@ debug (LOGGING) /* ============================ GC =============================== */ + +debug(PRINTF) +void printGCBits(GCBits* bits) +{ + for (size_t i = 0; i %p\n", p); return p; } @@ -609,7 +652,7 @@ class ConservativeGC : GC { void *p2; size_t psize; - //debug(PRINTF) printf("GC::realloc(p = %p, size = %zu)\n", p, size); + //debug(PRINTF) printf("GC::realloc(p = %p, size = %llu)\n", p, cast(long)size); debug (SENTINEL) { sentinel_Invariant(p); @@ -658,6 +701,8 @@ class ConservativeGC : GC auto newsz = (size + PAGESIZE - 1) / PAGESIZE; if (newsz == psz) { + if(isPrecise) + pool.setPointerBitmap(p, size, newsz * PAGESIZE, bits, ti); alloc_size = psize; return p; } @@ -693,6 +738,8 @@ class ConservativeGC : GC pool.setBits(biti, bits); } alloc_size = newsz * PAGESIZE; + if(isPrecise) + pool.setPointerBitmap(p, size, newsz * PAGESIZE, bits, ti); return p; } @@ -723,7 +770,11 @@ class ConservativeGC : GC p = p2; } else + { alloc_size = psize; + if(isPrecise) + pool.setPointerBitmap(p, size, psize, bits, ti); + } } } return p; @@ -746,7 +797,7 @@ class ConservativeGC : GC } do { - //debug(PRINTF) printf("GC::extend(p = %p, minsize = %zu, maxsize = %zu)\n", p, minsize, maxsize); + //debug(PRINTF) printf("GC::extend(p = %p, minsize = %llu, maxsize = %llu)\n", p, cast(ulong)minsize, cast(ulong)maxsize); debug (SENTINEL) { return 0; @@ -787,6 +838,12 @@ class ConservativeGC : GC lpool.updateOffsets(pagenum); lpool.freepages -= sz; gcx.usedLargePages += sz; + if (isPrecise) + { + auto biti = cast(size_t)(p - pool.baseAddr) >> pool.shiftBy; + if(!pool.noscan.test(biti)) + pool.setPointerBitmap(p, psize + sz * PAGESIZE, psize + sz * PAGESIZE, ti, BlkAttr.NONE); + } return (psz + sz) * PAGESIZE; } } @@ -1379,7 +1436,8 @@ struct Gcx roots.removeAll(); ranges.removeAll(); - toscan.reset(); + toscanConservative.reset(); + toscanPrecise.reset(); } @@ -1404,8 +1462,12 @@ struct Gcx for (size_t i = 0; i < B_PAGE; i++) { + size_t j = 0; + List* prev, pprev, ppprev; // keep a short history to inspect in the debugger for (auto list = cast(List*)bucket[i]; list; list = list.next) { + ppprev = pprev; pprev = prev; prev = list; + j++; } } } @@ -1580,9 +1642,11 @@ struct Gcx /** * */ - BlkInfo getInfo(void* p) nothrow + BlkInfo getInfo(void* p, Pool** ppool = null) nothrow { Pool* pool = findPool(p); + if(ppool) + *ppool = pool; if (pool) return pool.slGetInfo(p); return BlkInfo(); @@ -1671,14 +1735,15 @@ struct Gcx return isLowOnMem(mappedPages * PAGESIZE); } - void* alloc(size_t size, ref size_t alloc_size, uint bits) nothrow + void* alloc(size_t size, ref size_t alloc_size, uint bits, const TypeInfo ti) nothrow { - return size <= 2048 ? smallAlloc(binTable[size], alloc_size, bits) - : bigAlloc(size, alloc_size, bits); + return size <= PAGESIZE/2 ? smallAlloc(size, alloc_size, bits, ti) + : bigAlloc(size, alloc_size, bits, ti); } - void* smallAlloc(Bins bin, ref size_t alloc_size, uint bits) nothrow + void* smallAlloc(size_t size, ref size_t alloc_size, uint bits, const TypeInfo ti) nothrow { + immutable bin = binTable[size]; alloc_size = binsize[bin]; void* p; @@ -1725,6 +1790,14 @@ struct Gcx pool.setBits((p - pool.baseAddr) >> pool.shiftBy, bits); //debug(PRINTF) printf("\tmalloc => %p\n", p); debug (MEMSTOMP) memset(p, 0xF0, alloc_size); + + if (ConservativeGC.isPrecise) + { + debug(SENTINEL) + pool.setPointerBitmap(sentinel_add(p), size - SENTINEL_EXTRA, size - SENTINEL_EXTRA, bits, ti); + else + pool.setPointerBitmap(p, size, alloc_size, bits, ti); + } return p; } @@ -1808,6 +1881,14 @@ struct Gcx if (bits) pool.setBits(pn, bits); + if (ConservativeGC.isPrecise) + { + debug(SENTINEL) + pool.setPointerBitmap(sentinel_add(p), size - SENTINEL_EXTRA, size - SENTINEL_EXTRA, bits, ti); + else + pool.setPointerBitmap(p, size, alloc_size, bits, ti); + } + return p; } @@ -1890,13 +1971,15 @@ struct Gcx return null; } - static struct ScanRange + static struct ScanRange(bool precise) { void* pbot; void* ptop; + static if (precise) + size_t* ptrbase; } - static struct ToScanStack + static struct ToScanStack(T) { nothrow: @disable this(this); @@ -1904,25 +1987,28 @@ struct Gcx void reset() { _length = 0; - os_mem_unmap(_p, _cap * ScanRange.sizeof); - _p = null; + if(_p) + { + os_mem_unmap(_p, _cap * T.sizeof); + _p = null; + } _cap = 0; } - void push(ScanRange rng) + void push(T rng) { if (_length == _cap) grow(); _p[_length++] = rng; } - ScanRange pop() + T pop() in { assert(!empty); } do { return _p[--_length]; } - ref inout(ScanRange) opIndex(size_t idx) inout + ref inout(T) opIndex(size_t idx) inout in { assert(idx < _length); } do { @@ -1938,30 +2024,47 @@ struct Gcx pragma(inline, false); enum initSize = 64 * 1024; // Windows VirtualAlloc granularity - immutable ncap = _cap ? 2 * _cap : initSize / ScanRange.sizeof; - auto p = cast(ScanRange*)os_mem_map(ncap * ScanRange.sizeof); + immutable ncap = _cap ? 2 * _cap : initSize / T.sizeof; + auto p = cast(T*)os_mem_map(ncap * T.sizeof); if (p is null) onOutOfMemoryErrorNoGC(); if (_p !is null) { p[0 .. _length] = _p[0 .. _length]; - os_mem_unmap(_p, _cap * ScanRange.sizeof); + os_mem_unmap(_p, _cap * T.sizeof); } _p = p; _cap = ncap; } size_t _length; - ScanRange* _p; + T* _p; size_t _cap; } - ToScanStack toscan; + ToScanStack!(ScanRange!false) toscanConservative; + ToScanStack!(ScanRange!true) toscanPrecise; /** * Search a range of memory values and mark any pointers into the GC pool. */ - void mark(void *pbot, void *ptop) scope nothrow + void mark(bool precise)(void *pbot, void *ptop) scope nothrow { + static if(precise) + alias toscan = toscanPrecise; + else + alias toscan = toscanConservative; + + debug(MARK_PRINTF) + { + printf("marking range: [%p..%p] (%#llx)\n", pbot, ptop, cast(long)(ptop - pbot)); + + static void printSkip(void**beg, void** end) + { + for (void**q = beg; q < end; q++) + printf("\tskip %p: %p\n", q, cast(void *)(*q)); + } + } + if (pbot >= ptop) return; @@ -1971,7 +2074,20 @@ struct Gcx // limit the amount of ranges added to the toscan stack enum FANOUT_LIMIT = 32; size_t stackPos; - ScanRange[FANOUT_LIMIT] stack = void; + ScanRange!precise[FANOUT_LIMIT] stack = void; + + static if (precise) + { + import core.bitop; + enum useBitRange = false; + static if (useBitRange) + BitRange isptr = void; + size_t* isPtrBase = null; // always starting from a non-heap root + void** p1base = null; + + // align down to address where the pointer bitmap starts a new word + enum size_t p1BaseMask = ~(size_t.sizeof * GCBits.BITS_PER_WORD - 1); + } size_t pcache = 0; @@ -1983,17 +2099,40 @@ struct Gcx void* base = void; void* top = void; + void* p = void; + Pool* pool = void; - //printf("marking range: [%p..%p] (%#zx)\n", p1, p2, cast(size_t)p2 - cast(size_t)p1); for (;;) { - auto p = *p1; + static if(precise && useBitRange) if (p1base) + { + if (isptr.empty()) + { + debug(MARK_PRINTF) printSkip(p1, p2); + goto LnextRange; + } + + auto bitpos = isptr.front(); + debug(MARK_PRINTF) printSkip(p1, p1base + bitpos); + + isptr.popFront(); + p1 = p1base + bitpos; + } + + p = cast(void *)(*p1); + + debug(MARK_PRINTF) printf("\tmark %p: %p\n", p1, p); - //if (log) debug(PRINTF) printf("\tmark %p\n", p); if (cast(size_t)(p - minAddr) < memSize && (cast(size_t)p & ~cast(size_t)(PAGESIZE-1)) != pcache) { - Pool* pool = void; + static if (precise && !useBitRange) if (p1base) + { + size_t bitpos = p1 - p1base; + if (!core.bitop.bt(isPtrBase, bitpos)) + goto LnextPtr; + } + size_t low = 0; size_t high = highpool; while (true) @@ -2014,7 +2153,8 @@ struct Gcx size_t pn = offset / PAGESIZE; size_t bin = pool.pagetable[pn]; // not Bins to avoid multiple size extension instructions - //debug(PRINTF) printf("\t\tfound pool %p, base=%p, pn = %zd, bin = %d, biti = x%x\n", pool, pool.baseAddr, pn, bin, biti); + debug(MARK_PRINTF) + printf("\t\tfound pool %p, base=%p, pn = %lld, bin = %d\n", pool, pool.baseAddr, cast(long)pn, bin); // Adjust bit to be at start of allocated memory block if (bin < B_PAGE) @@ -2078,12 +2218,18 @@ struct Gcx if (++p1 < p2) continue; + LnextRange: if (stackPos) { // pop range from local stack and recurse auto next = &stack[--stackPos]; p1 = cast(void**)next.pbot; p2 = cast(void**)next.ptop; + static if(precise) + { + isPtrBase = next.ptrbase; + p1base = isPtrBase ? cast(void**)(cast(size_t)p1 & p1BaseMask) : null; + } } else if (!toscan.empty) { @@ -2091,6 +2237,11 @@ struct Gcx auto next = toscan.pop(); p1 = cast(void**)next.pbot; p2 = cast(void**)next.ptop; + static if(precise) + { + isPtrBase = next.ptrbase; + p1base = isPtrBase ? cast(void**)(cast(size_t)p1 & p1BaseMask) : null; + } } else { @@ -2098,32 +2249,77 @@ struct Gcx break; } // printf(" pop [%p..%p] (%#zx)\n", p1, p2, cast(size_t)p2 - cast(size_t)p1); - pcache = 0; - continue; + goto LcontRange; LaddRange: if (++p1 < p2) { + static if(precise && useBitRange) if (p1base && isptr.empty()) + { + debug(MARK_PRINTF) printSkip(p1, p2); + goto LendOfRange; + } + if (stackPos < stack.length) { stack[stackPos].pbot = base; stack[stackPos].ptop = top; + static if(precise) + { + auto nbase = cast(void**)(cast(size_t)base & p1BaseMask); + auto ptroff = (nbase - cast(void**)pool.baseAddr) / GCBits.BITS_PER_WORD; + stack[stackPos].ptrbase = pool.is_pointer.data + ptroff; + } stackPos++; continue; } - toscan.push(ScanRange(p1, p2)); + static if(precise) if (p1base) + { + // normalize pointers so we only have to save a single pointer + auto nbase = isPtrBase ? cast(void**)(cast(size_t)p1 & p1BaseMask) : null; + isPtrBase += (nbase - p1base) / GCBits.BITS_PER_WORD; + } + static if(precise) + toscan.push(ScanRange!precise(p1, p2, isPtrBase)); + else + toscan.push(ScanRange!precise(p1, p2)); // reverse order for depth-first-order traversal foreach_reverse (ref rng; stack) toscan.push(rng); stackPos = 0; } + LendOfRange: // continue with last found range p1 = cast(void**)base; p2 = cast(void**)top; + static if(precise) + { + assert (pool); + p1base = cast(void**)pool.baseAddr; + isPtrBase = pool.is_pointer.data; + } + + LcontRange: + static if(precise && useBitRange) if (p1base) + { + size_t p1bitpos = p1 - p1base; + size_t p2bitpos = p2 - p1base; + isptr = BitRange(isPtrBase, p2bitpos, p1bitpos); + } pcache = 0; } } + void markConservative(void *pbot, void *ptop) scope nothrow + { + mark!false(pbot, ptop); + } + + void markPrecise(void *pbot, void *ptop) scope nothrow + { + mark!true(pbot, ptop); + } + // collection step 1: prepare freebits and mark bits void prepare() nothrow { @@ -2163,20 +2359,20 @@ struct Gcx } // collection step 2: mark roots and heap - void markAll(bool nostack) nothrow + void markAll(alias markFn)(bool nostack) nothrow { if (!nostack) { debug(COLLECT_PRINTF) printf("\tscan stacks.\n"); // Scan stacks and registers for each paused thread - thread_scanAll(&mark); + thread_scanAll(&markFn); } // Scan roots[] debug(COLLECT_PRINTF) printf("\tscan roots[]\n"); foreach (root; roots) { - mark(cast(void*)&root.proot, cast(void*)(&root.proot + 1)); + markFn(cast(void*)&root.proot, cast(void*)(&root.proot + 1)); } // Scan ranges[] @@ -2185,7 +2381,7 @@ struct Gcx foreach (range; ranges) { debug(COLLECT_PRINTF) printf("\t\t%p .. %p\n", range.pbot, range.ptop); - mark(range.pbot, range.ptop); + markFn(range.pbot, range.ptop); } //log--; } @@ -2414,7 +2610,10 @@ struct Gcx start = stop; } - markAll(nostack); + if (ConservativeGC.isPrecise) + markAll!markPrecise(nostack); + else + markAll!markConservative(nostack); thread_processGCMarks(&isMarked); thread_resumeAll(); @@ -2631,6 +2830,7 @@ struct Pool GCBits appendable; // entries that are appendable GCBits nointerior; // interior pointers should be ignored. // Only implemented for large object pools. + GCBits is_pointer; // precise GC only: per-word, not per-block like the rest of them size_t npages; size_t freepages; // The number of pages not in use. ubyte* pagetable; @@ -2684,6 +2884,11 @@ struct Pool auto nbits = cast(size_t)poolsize >> shiftBy; mark.alloc(nbits); + if(ConservativeGC.isPrecise) + { + is_pointer.alloc(cast(size_t)poolsize/(void*).sizeof); + is_pointer.setRange(0, is_pointer.nbits); + } // pagetable already keeps track of what's free for the large object // pool. @@ -2741,6 +2946,8 @@ struct Pool cstdlib.free(bPageOffsets); mark.Dtor(); + if(ConservativeGC.isPrecise) + is_pointer.Dtor(); if(isLargeObject) { nointerior.Dtor(); @@ -2945,6 +3152,77 @@ struct Pool } } } + + pragma(inline,true) + void setPointerBitmap(void* p, size_t s, size_t allocSize, uint attr, const TypeInfo ti) nothrow + { + if (!(attr & BlkAttr.NO_SCAN)) + setPointerBitmap(p, s, allocSize, ti, attr); + } + + pragma(inline,false) + void setPointerBitmap(void* p, size_t s, size_t allocSize, const TypeInfo ti, uint attr) nothrow + { + size_t offset = p - baseAddr; + //debug(PRINTF) printGCBits(&pool.is_pointer); + + debug(PRINTF) + printf("Setting a pointer bitmap for %s at %p + %llu\n", debugTypeName(ti).ptr, p, cast(ulong)s); + + if (ti) + { + auto rtInfo = cast(const(size_t)*)ti.rtInfo(); + + if (rtInfo is rtinfoNoPointers) + { + debug(PRINTF) printf("\tCompiler generated rtInfo: no pointers\n"); + is_pointer.clrRange(offset/(void*).sizeof, s/(void*).sizeof); + } + else if (rtInfo is rtinfoHasPointers) + { + debug(PRINTF) printf("\tCompiler generated rtInfo: has pointers\n"); + is_pointer.setRange(offset/(void*).sizeof, s/(void*).sizeof); + } + else + { + const(size_t)* bitmap = cast (size_t*) rtInfo; + //first element of rtInfo is the size of the object the bitmap encodes + size_t element_size = * bitmap; + bitmap++; + size_t tocopy = (s < element_size ? s : element_size)/(void*).sizeof; + is_pointer.copyRange(offset/(void*).sizeof, tocopy, bitmap); + + debug(PRINTF) printf("\tSetting bitmap for new object (%s)\n\t\tat %p\t\tcopying from %p + %llu: ", + debugTypeName(ti).ptr, p, bitmap, cast(ulong)element_size); + debug(PRINTF) + for(size_t i = 0; i < element_size/((void*).sizeof); i++) + printf("%d", (bitmap[i/(8*size_t.sizeof)] >> (i%(8*size_t.sizeof))) & 1); + debug(PRINTF) printf("\n"); + + if(tocopy * (void*).sizeof < s) // better safe than sorry: if allocated more, assume pointers inside + { + debug(PRINTF) printf(" Appending %d pointer bits\n", s/(void*).sizeof - tocopy); + is_pointer.setRange(offset/(void*).sizeof + tocopy, s/(void*).sizeof - tocopy); + } + } + + if(s < allocSize) + { + offset = (offset + s + (void*).sizeof - 1) & ~((void*).sizeof - 1); + is_pointer.clrRange(offset/(void*).sizeof, (allocSize - s)/(void*).sizeof); + } + } + else + { + // limit pointers to actual size of allocation? might fail for arrays that append + // without notifying the GC + s = allocSize; + + debug(PRINTF) printf("Allocating a block without TypeInfo\n"); + is_pointer.setRange(offset/(void*).sizeof, s/(void*).sizeof); + } + //debug(PRINTF) printGCBits(&pool.is_pointer); + } } struct LargeObjectPool diff --git a/src/object.d b/src/object.d index 45634ed2fc..506f7a6fe4 100644 --- a/src/object.d +++ b/src/object.d @@ -313,7 +313,7 @@ class TypeInfo /** Return info used by the garbage collector to do precise collection. */ - @property immutable(void)* rtInfo() nothrow pure const @safe @nogc { return null; } + @property immutable(void)* rtInfo() nothrow pure const @safe @nogc { return rtinfoHasPointers; } // better safe than sorry } class TypeInfo_Enum : TypeInfo @@ -508,6 +508,8 @@ class TypeInfo_Array : TypeInfo arg2 = typeid(void*); return 0; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return RTInfo!(void[]); } } class TypeInfo_StaticArray : TypeInfo @@ -632,6 +634,9 @@ class TypeInfo_StaticArray : TypeInfo arg1 = typeid(void*); return 0; } + + // just return the rtInfo of the element, we have no generic type T to run RTInfo!T on + override @property immutable(void)* rtInfo() nothrow pure const @safe { return value.rtInfo(); } } class TypeInfo_AssociativeArray : TypeInfo @@ -758,6 +763,8 @@ class TypeInfo_Function : TypeInfo return null; } + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } + TypeInfo next; /** @@ -849,6 +856,8 @@ class TypeInfo_Delegate : TypeInfo arg2 = typeid(void*); return 0; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return RTInfo!(int delegate()); } } unittest @@ -1232,7 +1241,7 @@ class TypeInfo_Struct : TypeInfo uint m_align; - override @property immutable(void)* rtInfo() const { return m_RTInfo; } + override @property immutable(void)* rtInfo() nothrow pure const @safe { return m_RTInfo; } version (X86_64) { @@ -3382,11 +3391,25 @@ void __ctfeWrite(scope const(char)[] s) @nogc @safe pure nothrow {} * Create RTInfo for type T */ +template RTInfoImpl(size_t[] pointers) +{ + immutable size_t[pointers.length] data = pointers[]; + immutable RTInfoImpl = data.ptr; +} + template RTInfo(T) { - enum RTInfo = null; + enum RTInfo = RTInfoImpl!(__traits(getPointerBitmap, T)); } +/** +* shortcuts for the precise GC, also generated by the compiler +* used instead of the actual pointer bitmap +*/ +enum void* rtinfoNoPointers = null; +enum void* rtinfoHasPointers = cast(void*)1; + + // lhs == rhs lowers to __equals(lhs, rhs) for dynamic arrays bool __equals(T1, T2)(T1[] lhs, T2[] rhs) { diff --git a/src/rt/lifetime.d b/src/rt/lifetime.d index 51ae259189..a539ede675 100644 --- a/src/rt/lifetime.d +++ b/src/rt/lifetime.d @@ -425,7 +425,7 @@ BlkInfo __arrayAlloc(size_t arrsize, const TypeInfo ti, const TypeInfo tinext) n uint attr = (!(tinext.flags & 1) ? BlkAttr.NO_SCAN : 0) | BlkAttr.APPENDABLE; if (typeInfoSize) attr |= BlkAttr.STRUCTFINAL | BlkAttr.FINALIZE; - return GC.qalloc(padded_size, attr, ti); + return GC.qalloc(padded_size, attr, null); } BlkInfo __arrayAlloc(size_t arrsize, ref BlkInfo info, const TypeInfo ti, const TypeInfo tinext) @@ -442,7 +442,7 @@ BlkInfo __arrayAlloc(size_t arrsize, ref BlkInfo info, const TypeInfo ti, const return BlkInfo(); } - return GC.qalloc(padded_size, info.attr, ti); + return GC.qalloc(padded_size, info.attr, null); } /** diff --git a/src/rt/typeinfo/ti_byte.d b/src/rt/typeinfo/ti_byte.d index af763ca8cb..76bb6170c0 100644 --- a/src/rt/typeinfo/ti_byte.d +++ b/src/rt/typeinfo/ti_byte.d @@ -57,4 +57,6 @@ class TypeInfo_g : TypeInfo *cast(byte *)p1 = *cast(byte *)p2; *cast(byte *)p2 = t; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_cdouble.d b/src/rt/typeinfo/ti_cdouble.d index 426e585b0b..7737793c00 100644 --- a/src/rt/typeinfo/ti_cdouble.d +++ b/src/rt/typeinfo/ti_cdouble.d @@ -71,4 +71,6 @@ class TypeInfo_r : TypeInfo arg2 = typeid(double); return 0; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_cfloat.d b/src/rt/typeinfo/ti_cfloat.d index cc5b2931dd..19572bf3a7 100644 --- a/src/rt/typeinfo/ti_cfloat.d +++ b/src/rt/typeinfo/ti_cfloat.d @@ -70,4 +70,6 @@ class TypeInfo_q : TypeInfo arg1 = typeid(double); return 0; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_char.d b/src/rt/typeinfo/ti_char.d index 8b68cb7bc3..06a89e12a7 100644 --- a/src/rt/typeinfo/ti_char.d +++ b/src/rt/typeinfo/ti_char.d @@ -59,4 +59,6 @@ class TypeInfo_a : TypeInfo return (&c)[0 .. 1]; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_creal.d b/src/rt/typeinfo/ti_creal.d index 5a1579981a..4eba5d5bad 100644 --- a/src/rt/typeinfo/ti_creal.d +++ b/src/rt/typeinfo/ti_creal.d @@ -71,4 +71,6 @@ class TypeInfo_c : TypeInfo arg2 = typeid(real); return 0; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_dchar.d b/src/rt/typeinfo/ti_dchar.d index ad5afaca24..ce22043f6f 100644 --- a/src/rt/typeinfo/ti_dchar.d +++ b/src/rt/typeinfo/ti_dchar.d @@ -59,4 +59,6 @@ class TypeInfo_w : TypeInfo return (&c)[0 .. 1]; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_delegate.d b/src/rt/typeinfo/ti_delegate.d index 444e50a395..ea2fffb5bc 100644 --- a/src/rt/typeinfo/ti_delegate.d +++ b/src/rt/typeinfo/ti_delegate.d @@ -61,4 +61,6 @@ class TypeInfo_D : TypeInfo { return 1; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return RTInfo!(dg); } } diff --git a/src/rt/typeinfo/ti_double.d b/src/rt/typeinfo/ti_double.d index 421a19b22c..009ff7bba4 100644 --- a/src/rt/typeinfo/ti_double.d +++ b/src/rt/typeinfo/ti_double.d @@ -65,6 +65,8 @@ class TypeInfo_d : TypeInfo return F.alignof; } + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } + version (Windows) { } diff --git a/src/rt/typeinfo/ti_float.d b/src/rt/typeinfo/ti_float.d index 27a54bc982..9835e3c18c 100644 --- a/src/rt/typeinfo/ti_float.d +++ b/src/rt/typeinfo/ti_float.d @@ -60,6 +60,8 @@ class TypeInfo_f : TypeInfo return (&r)[0 .. 1]; } + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } + version (Windows) { } diff --git a/src/rt/typeinfo/ti_int.d b/src/rt/typeinfo/ti_int.d index 1e21d8b515..03d56bd4b9 100644 --- a/src/rt/typeinfo/ti_int.d +++ b/src/rt/typeinfo/ti_int.d @@ -61,4 +61,6 @@ class TypeInfo_i : TypeInfo *cast(int *)p1 = *cast(int *)p2; *cast(int *)p2 = t; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_long.d b/src/rt/typeinfo/ti_long.d index 6a16b1d3cc..9a105d4f51 100644 --- a/src/rt/typeinfo/ti_long.d +++ b/src/rt/typeinfo/ti_long.d @@ -68,4 +68,6 @@ class TypeInfo_l : TypeInfo { return long.alignof; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_ptr.d b/src/rt/typeinfo/ti_ptr.d index 7d4e069d90..866da95148 100644 --- a/src/rt/typeinfo/ti_ptr.d +++ b/src/rt/typeinfo/ti_ptr.d @@ -61,4 +61,6 @@ class TypeInfo_P : TypeInfo } override @property uint flags() nothrow pure const { return 1; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoHasPointers; } } diff --git a/src/rt/typeinfo/ti_real.d b/src/rt/typeinfo/ti_real.d index 642f0da0a0..b6f77a2f23 100644 --- a/src/rt/typeinfo/ti_real.d +++ b/src/rt/typeinfo/ti_real.d @@ -64,4 +64,6 @@ class TypeInfo_e : TypeInfo { return F.alignof; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_short.d b/src/rt/typeinfo/ti_short.d index 5928bc46c6..d785f69f8a 100644 --- a/src/rt/typeinfo/ti_short.d +++ b/src/rt/typeinfo/ti_short.d @@ -57,4 +57,6 @@ class TypeInfo_s : TypeInfo *cast(short *)p1 = *cast(short *)p2; *cast(short *)p2 = t; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_ubyte.d b/src/rt/typeinfo/ti_ubyte.d index 10e8a164c3..4897885fb8 100644 --- a/src/rt/typeinfo/ti_ubyte.d +++ b/src/rt/typeinfo/ti_ubyte.d @@ -57,6 +57,8 @@ class TypeInfo_h : TypeInfo *cast(ubyte *)p1 = *cast(ubyte *)p2; *cast(ubyte *)p2 = t; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } class TypeInfo_b : TypeInfo_h diff --git a/src/rt/typeinfo/ti_uint.d b/src/rt/typeinfo/ti_uint.d index 73e5cd4d6a..efc9245c19 100644 --- a/src/rt/typeinfo/ti_uint.d +++ b/src/rt/typeinfo/ti_uint.d @@ -61,4 +61,6 @@ class TypeInfo_k : TypeInfo *cast(uint *)p1 = *cast(uint *)p2; *cast(uint *)p2 = t; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_ulong.d b/src/rt/typeinfo/ti_ulong.d index ab1e4629c4..4b89f2a57f 100644 --- a/src/rt/typeinfo/ti_ulong.d +++ b/src/rt/typeinfo/ti_ulong.d @@ -68,4 +68,6 @@ class TypeInfo_m : TypeInfo { return ulong.alignof; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_ushort.d b/src/rt/typeinfo/ti_ushort.d index 152b42426a..4b07aa1b54 100644 --- a/src/rt/typeinfo/ti_ushort.d +++ b/src/rt/typeinfo/ti_ushort.d @@ -57,4 +57,6 @@ class TypeInfo_t : TypeInfo *cast(ushort *)p1 = *cast(ushort *)p2; *cast(ushort *)p2 = t; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } } diff --git a/src/rt/typeinfo/ti_wchar.d b/src/rt/typeinfo/ti_wchar.d index da27c2870a..4d5833a764 100644 --- a/src/rt/typeinfo/ti_wchar.d +++ b/src/rt/typeinfo/ti_wchar.d @@ -59,4 +59,6 @@ class TypeInfo_u : TypeInfo return (&c)[0 .. 1]; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } }