diff --git a/src/core/bitop.d b/src/core/bitop.d index 0daee55c7e..fc4d7ef008 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/core/memory.d b/src/core/memory.d index 4e3ee5affb..0be671af88 100644 --- a/src/core/memory.d +++ b/src/core/memory.d @@ -96,7 +96,7 @@ * happens and there is insufficient _memory available.) * ) * - * Copyright: Copyright Sean Kelly 2005 - 2015. + * Copyright: Copyright Sean Kelly 2005 - 2016. * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) * Authors: Sean Kelly, Alex Rønne Petersen * Source: $(DRUNTIMESRC core/_memory.d) @@ -126,6 +126,7 @@ private extern (C) size_t gc_extend( void* p, size_t mx, size_t sz, const TypeInfo = null ) pure nothrow; extern (C) size_t gc_reserve( size_t sz ) nothrow; extern (C) void gc_free( void* p ) pure nothrow @nogc; + extern (C) bool gc_emplace( void *p, size_t len, const TypeInfo ti ) pure nothrow; extern (C) void* gc_addrOf( void* p ) pure nothrow @nogc; extern (C) size_t gc_sizeOf( void* p ) pure nothrow @nogc; @@ -260,6 +261,10 @@ struct GC NO_INTERIOR = 0b0001_0000, STRUCTFINAL = 0b0010_0000, // the block has a finalizer for (an array of) structs + + // additional info for allocating with type info + NO_RTINFO = 0b0100_0000, // do not copy RTInfo in malloc/realloc + REP_RTINFO = 0b1000_0000, // repeat RTInfo if allocation is larger than type info } @@ -680,6 +685,22 @@ struct GC return gc_stats(); } + /** + * Describe the memory at the given address range for precise collection + * + * Params: + * p = A pointer to the root or the interior of a valid memory block + * len = Length of the memory range + * ti = Type info describing the memory usage + * + * Returns: + * true if p points to GC managed memory + */ + static bool emplace( void *p, size_t len, const TypeInfo ti ) pure nothrow + { + return gc_emplace( p, len, ti ); + } + /** * Adds an internal root pointing to the GC memory block referenced by p. * As a result, the block referenced by p itself and any blocks accessible diff --git a/src/gc/bits.d b/src/gc/bits.d index 21d43c4f4f..10b8d26f88 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..4306c694cd 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) @@ -36,7 +36,7 @@ struct Config string s = "GC options are specified as whitespace separated assignments: disable:0|1 - start disabled (%d) profile:0|1|2 - enable profiling with summary when terminating program (%d) - gc:conservative|manual - select gc implementation (default = conservative) + gc:conservative|precise|manual - select gc implementation (default = conservative) initReserve:N - initial memory to reserve in MB (%lld) minPoolSize:N - initial and minimum pool size in MB (%lld) diff --git a/src/gc/gcinterface.d b/src/gc/gcinterface.d index c162041994..58df1e02a3 100644 --- a/src/gc/gcinterface.d +++ b/src/gc/gcinterface.d @@ -148,6 +148,11 @@ interface GC */ core.memory.GC.Stats stats() nothrow; + /** + * Tell the GC the type of the memory range + */ + bool emplace(void *p, size_t len, const(TypeInfo) ti) nothrow; + /** * add p to list of roots */ diff --git a/src/gc/impl/conservative/gc.d b/src/gc/impl/conservative/gc.d index fa1d900808..96bf40a001 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; } } @@ -115,10 +119,12 @@ __gshared long numFrees; __gshared long numReallocs; __gshared long numExtends; __gshared long numOthers; +__gshared long numEmplaces; __gshared long mallocTime; // using ticks instead of MonoTime for better performance __gshared long freeTime; __gshared long reallocTime; __gshared long extendTime; +__gshared long emplaceTime; __gshared long otherTime; __gshared long lockTime; @@ -249,6 +255,38 @@ debug (LOGGING) /* ============================ GC =============================== */ + +debug(PRINTF) +void printGCBits(GCBits* bits) +{ + for (size_t i = 0; i %p\n", p); return p; } @@ -609,7 +654,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 +703,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 +740,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 +772,11 @@ class ConservativeGC : GC p = p2; } else + { alloc_size = psize; + if (isPrecise) + pool.setPointerBitmap(p, size, psize, bits, ti); + } } } return p; @@ -746,7 +799,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 +840,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.REP_RTINFO); + } return (psz + sz) * PAGESIZE; } } @@ -1052,6 +1111,43 @@ class ConservativeGC : GC } + /** + * Tell the GC the type of the memory range + */ + bool emplace(void *p, size_t len, const(TypeInfo) ti) nothrow + { + if (!isPrecise) + return false; + if (!p) + return false; + + return runLocked!(emplaceNoSync, emplaceTime, numEmplaces)(p, len, ti); + } + + + // + // + // + private bool emplaceNoSync(void *p, size_t len, const(TypeInfo) ti) nothrow + { + debug(PRINTF) printf("Emplacing %s at %p + %d\n", debugTypeName(ti).ptr, cast(size_t) p, len); + assert(p); + + sentinel_Invariant(p); + Pool* pool; + BlkInfo info = gcx.getInfo(p, &pool); + if (!info.base) + return false; + + debug(SENTINEL) + size_t allocSize = len; + else + size_t allocSize = info.size - (p - info.base); + pool.setPointerBitmap(p, len, allocSize, ti, BlkAttr.REP_RTINFO); + return true; + } + + void addRoot(void *p) nothrow @nogc { if (!p) @@ -1352,10 +1448,11 @@ struct Gcx printf("\trealloc: %llu calls, %lld ms\n", cast(ulong)numReallocs, toDuration(reallocTime).total!"msecs"); printf("\tfree: %llu calls, %lld ms\n", cast(ulong)numFrees, toDuration(freeTime).total!"msecs"); printf("\textend: %llu calls, %lld ms\n", cast(ulong)numExtends, toDuration(extendTime).total!"msecs"); + printf("\templace: %llu calls, %lld ms\n", cast(ulong)numEmplaces, toDuration(emplaceTime).total!"msecs"); printf("\tother: %llu calls, %lld ms\n", cast(ulong)numOthers, toDuration(otherTime).total!"msecs"); printf("\tlock time: %lld ms\n", toDuration(lockTime).total!"msecs"); - long apiTime = mallocTime + reallocTime + freeTime + extendTime + otherTime + lockTime; + long apiTime = mallocTime + reallocTime + freeTime + extendTime + emplaceTime + otherTime + lockTime; printf("\tGC API: %lld ms\n", toDuration(apiTime).total!"msecs"); sprintf(apitxt.ptr, " API%5ld ms", toDuration(apiTime).total!"msecs"); } @@ -1379,7 +1476,8 @@ struct Gcx roots.removeAll(); ranges.removeAll(); - toscan.reset(); + toscanConservative.reset(); + toscanPrecise.reset(); } @@ -1404,8 +1502,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 +1682,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 +1775,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 +1830,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 +1921,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 +2011,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 +2027,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 +2064,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 +2114,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 +2139,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 +2193,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 +2258,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 +2277,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 +2289,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 +2399,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 +2421,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--; } @@ -2422,7 +2658,10 @@ struct Gcx start = stop; } - markAll(nostack); + if (ConservativeGC.isPrecise) + markAll!markPrecise(nostack); + else + markAll!markConservative(nostack); thread_processGCMarks(&isMarked); thread_resumeAll(); @@ -2639,6 +2878,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; @@ -2692,6 +2932,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. @@ -2749,6 +2994,8 @@ struct Pool cstdlib.free(bPageOffsets); mark.Dtor(); + if (ConservativeGC.isPrecise) + is_pointer.Dtor(); if (isLargeObject) { nointerior.Dtor(); @@ -2953,6 +3200,89 @@ 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 | BlkAttr.NO_RTINFO))) + 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) + { + if (attr & BlkAttr.REP_RTINFO) + s = allocSize; + + 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; + if (attr & BlkAttr.REP_RTINFO) + { + tocopy = s/(void*).sizeof; + is_pointer.copyRangeRepeating(offset/(void*).sizeof, tocopy, bitmap, element_size/(void*).sizeof); + } + else + { + 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/gc/impl/manual/gc.d b/src/gc/impl/manual/gc.d index 3bcde758e3..660201171f 100644 --- a/src/gc/impl/manual/gc.d +++ b/src/gc/impl/manual/gc.d @@ -163,6 +163,11 @@ class ManualGC : GC cstdlib.free(p); } + bool emplace(void *p, size_t len, const(TypeInfo) ti) nothrow + { + return false; + } + /** * Determine the base address of the block containing p. If p is not a gc * allocated pointer, return null. diff --git a/src/gc/impl/proto/gc.d b/src/gc/impl/proto/gc.d index e23e38f7f9..632a499bf3 100644 --- a/src/gc/impl/proto/gc.d +++ b/src/gc/impl/proto/gc.d @@ -140,6 +140,11 @@ class ProtoGC : GC if (p) assert(false, "Invalid memory deallocation"); } + bool emplace(void *p, size_t len, const(TypeInfo) ti) nothrow + { + return false; + } + void* addrOf(void* p) nothrow @nogc { return null; diff --git a/src/gc/proxy.d b/src/gc/proxy.d index fe74db8e09..af33de7ea2 100644 --- a/src/gc/proxy.d +++ b/src/gc/proxy.d @@ -36,13 +36,17 @@ private extern (C) { + void gc_config() + { + config.initialize(); + } + void gc_init() { instanceLock.lock(); if (!isInstanceInit) { auto protoInstance = instance; - config.initialize(); ManualGC.initialize(instance); ConservativeGC.initialize(instance); @@ -169,6 +173,11 @@ extern (C) return instance.free( p ); } + bool gc_emplace( void* p, size_t len, const TypeInfo ti ) nothrow + { + return instance.emplace( p, len, ti ); + } + void* gc_addrOf( void* p ) nothrow @nogc { return instance.addrOf( p ); diff --git a/src/object.d b/src/object.d index 075d98bcc9..0729246cb8 100644 --- a/src/object.d +++ b/src/object.d @@ -1141,7 +1141,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 @@ -1337,6 +1337,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 @@ -1461,6 +1463,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 @@ -1587,6 +1592,8 @@ class TypeInfo_Function : TypeInfo return null; } + override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } + TypeInfo next; /** @@ -1678,6 +1685,8 @@ class TypeInfo_Delegate : TypeInfo arg2 = typeid(void*); return 0; } + + override @property immutable(void)* rtInfo() nothrow pure const @safe { return RTInfo!(int delegate()); } } /** @@ -2034,7 +2043,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) { @@ -3786,11 +3795,24 @@ 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; + // Compiler hook into the runtime implementation of array (vector) operations. template _arrayOp(Args...) { diff --git a/src/rt/aaA.d b/src/rt/aaA.d index 5362b159a0..2e9bceb785 100644 --- a/src/rt/aaA.d +++ b/src/rt/aaA.d @@ -200,6 +200,14 @@ Bucket[] allocBuckets(size_t dim) @trusted pure nothrow // Entry //------------------------------------------------------------------------------ +immutable bool gc_precise; + +extern(C) void aaa_init() @nogc +{ + import gc.config; + cast() gc_precise = (config.gc == "precise"); +} + private void* allocEntry(in Impl* aa, in void* pkey) { import rt.lifetime : _d_newitemU; @@ -250,12 +258,14 @@ TypeInfo_Struct fakeEntryTI(const TypeInfo keyti, const TypeInfo valti) auto kti = unqualify(keyti); auto vti = unqualify(valti); - if (!hasDtor(kti) && !hasDtor(vti)) + + bool entryHasDtor = hasDtor(kti) || hasDtor(vti); + if (!gc_precise && !entryHasDtor) return null; // save kti and vti after type info for struct enum sizeti = __traits(classInstanceSize, TypeInfo_Struct); - void* p = GC.malloc(sizeti + 2 * (void*).sizeof); + void* p = GC.malloc(sizeti + (gc_precise ? 4 : 2) * (void*).sizeof); import core.stdc.string : memcpy; memcpy(p, typeid(TypeInfo_Struct).initializer().ptr, sizeti); @@ -268,23 +278,86 @@ TypeInfo_Struct fakeEntryTI(const TypeInfo keyti, const TypeInfo valti) static immutable tiName = __MODULE__ ~ ".Entry!(...)"; ti.name = tiName; + if (gc_precise) + { + auto rtinfoData = cast(size_t[2]*) (extra + 2); + ti.m_RTInfo = rtinfoEntry(keyti, valti, *rtinfoData); + ti.m_flags = ti.m_RTInfo is rtinfoNoPointers ? cast(TypeInfo_Struct.StructFlags)0 : TypeInfo_Struct.StructFlags.hasPointers; + } + else + { + ti.m_RTInfo = null; + ti.m_flags = cast(TypeInfo_Struct.StructFlags)(keyti.flags | valti.flags) & TypeInfo_Struct.StructFlags.hasPointers; + } // we don't expect the Entry objects to be used outside of this module, so we have control // over the non-usage of the callback methods and other entries and can keep these null // xtoHash, xopEquals, xopCmp, xtoString and xpostblit - ti.m_RTInfo = null; immutable entrySize = talign(kti.tsize, vti.talign) + vti.tsize; ti.m_init = (cast(ubyte*) null)[0 .. entrySize]; // init length, but not ptr - // xdtor needs to be built from the dtors of key and value for the GC - ti.xdtorti = &entryDtor; + if (entryHasDtor) + { + // xdtor needs to be built from the dtors of key and value for the GC + ti.xdtorti = &entryDtor; + ti.m_flags |= TypeInfo_Struct.StructFlags.isDynamicType; + } - ti.m_flags = TypeInfo_Struct.StructFlags.isDynamicType; - ti.m_flags |= (keyti.flags | valti.flags) & TypeInfo_Struct.StructFlags.hasPointers; ti.m_align = cast(uint) max(kti.talign, vti.talign); return ti; } +// build type info with appropriate RTInfo at runtime +immutable(void)* rtinfoEntry(const TypeInfo keyti, const TypeInfo valti, return ref size_t[2] rtinfoData) +{ + static bool isNoClass(const TypeInfo ti) { return ti && typeid(ti) !is typeid(TypeInfo_Class); } + + immutable(size_t)* keyinfo = cast(immutable(size_t)*) (isNoClass(keyti) ? keyti.rtInfo : rtinfoHasPointers); + immutable(size_t)* valinfo = cast(immutable(size_t)*) (isNoClass(valti) ? valti.rtInfo : rtinfoHasPointers); + + enum maxSupportedSize = 8 * (void*).sizeof * (void*).sizeof; + + size_t rtinfosize = 0; + size_t valuesize = valti.tsize(); + + size_t valbits; + if (valinfo is rtinfoNoPointers) + valbits = 0; + else if (rtinfosize + valuesize > maxSupportedSize) + return rtinfoHasPointers; + else if (valinfo is rtinfoHasPointers) + valbits = (1 << (valuesize / (void*).sizeof)) - 1; + else + valbits = valinfo[1]; + + if (valbits != 0) + rtinfosize += valuesize; + + size_t keybits; + size_t keysize = keyti.tsize; + if (keyinfo is rtinfoNoPointers) + keybits = 0; + else if (rtinfosize + keysize > maxSupportedSize) + return rtinfoHasPointers; + else if (keyinfo is rtinfoHasPointers) + keybits = (1 << (keysize / (void*).sizeof)) - 1; + else + keybits = keyinfo[1]; + + if (valbits == 0 && keybits == 0) + return rtinfoNoPointers; + + if (valbits != 0 || keybits != 0) + rtinfosize += keysize; + + rtinfoData[0] = rtinfosize; + + size_t valshift = (keysize + (void*).sizeof - 1) / (void*).sizeof; + rtinfoData[1] = keybits | (valbits << valshift); + + return cast(immutable(void)*) rtinfoData.ptr; +} + //============================================================================== // Helper functions //------------------------------------------------------------------------------ diff --git a/src/rt/dmain2.d b/src/rt/dmain2.d index 1261ecba96..7946d41694 100644 --- a/src/rt/dmain2.d +++ b/src/rt/dmain2.d @@ -58,11 +58,12 @@ extern (C) void _d_monitor_staticctor(); extern (C) void _d_monitor_staticdtor(); extern (C) void _d_critical_init(); extern (C) void _d_critical_term(); -extern (C) void gc_init(); +extern (C) void gc_config(); extern (C) void gc_term(); extern (C) void thread_init() @nogc; extern (C) void thread_term() @nogc; -extern (C) void lifetime_init(); +extern (C) void lifetime_init() @nogc; +extern (C) void aaa_init() @nogc; extern (C) void rt_moduleCtor(); extern (C) void rt_moduleTlsCtor(); extern (C) void rt_moduleDtor(); @@ -196,10 +197,12 @@ extern (C) int rt_init() // this initializes mono time before anything else to allow usage // in other druntime systems. _d_initMonoTime(); + gc_config(); thread_init(); // TODO: fixme - calls GC.addRange -> Initializes GC initStaticDataGC(); lifetime_init(); + aaa_init(); rt_moduleCtor(); rt_moduleTlsCtor(); return 1; diff --git a/src/rt/lifetime.d b/src/rt/lifetime.d index 2f5c99a2bf..7458790477 100644 --- a/src/rt/lifetime.d +++ b/src/rt/lifetime.d @@ -40,8 +40,9 @@ private } private immutable bool callStructDtorsDuringGC; +private immutable bool gc_precise; -extern (C) void lifetime_init() +extern (C) void lifetime_init() @nogc { // this is run before static ctors, so it is safe to modify immutables import rt.config; @@ -50,6 +51,9 @@ extern (C) void lifetime_init() cast() callStructDtorsDuringGC = s[0] == '1' || s[0] == 'y' || s[0] == 'Y'; else cast() callStructDtorsDuringGC = true; + + import gc.config; + cast() gc_precise = (config.gc == "precise"); } /** @@ -425,7 +429,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_emplace(padded_size, attr, ti, tinext); } BlkInfo __arrayAlloc(size_t arrsize, ref BlkInfo info, const TypeInfo ti, const TypeInfo tinext) @@ -442,7 +446,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_emplace(padded_size, info.attr, ti, tinext); } /** @@ -726,6 +730,53 @@ void __doPostblit(void *ptr, size_t len, const TypeInfo ti) } } +BlkInfo gc_qalloc_emplace(size_t sz, uint ba, const TypeInfo ti, const TypeInfo tinext) nothrow pure +{ + if (gc_precise && !(ba & BlkAttr.NO_SCAN)) + { + // an array of classes is in fact an array of pointers + const(TypeInfo) tielem = typeid(tinext) is typeid(TypeInfo_Class) ? typeid(void*) : tinext; + + if (sz <= PAGESIZE / 2) + return GC.qalloc(sz, ba | BlkAttr.REP_RTINFO, tielem); + + // for large arrays, we have to emplace the type info pointer bitmap at offset LARGEPAD + BlkInfo info = GC.qalloc(sz, ba | BlkAttr.NO_RTINFO, tielem); + if (info.base) + { + void* arr = __arrayStart(info); + GC.emplace(arr, info.base + info.size - arr, tielem); + } + return info; + } + else + return GC.qalloc(sz, ba, ti); +} + +BlkInfo gc_qalloc_emplace(size_t sz, const TypeInfo ti, const TypeInfo tinext) nothrow pure +{ + uint attr = !(tinext.flags & 1) ? BlkAttr.NO_SCAN | BlkAttr.APPENDABLE : BlkAttr.APPENDABLE; + return gc_qalloc_emplace(sz, attr, ti, tinext); +} + +size_t gc_extend_emplace(void* p, size_t mx, size_t sz, size_t oldsz, const TypeInfo ti, const TypeInfo tinext) +{ + if (!gc_precise) + return GC.extend(p, mx, sz, ti); + + // only called on large pages, so passing a type info to GC.extend will emplace RTInfo at the wrong location. + // safer to use "null" to temporarily switch to conservative scanning (if not NOSCAN set) until we have + // a mechanism not to change exisiting pointer info at all. + size_t newsz = GC.extend(p, mx, sz, null); + if (newsz >= PAGESIZE) + { + // an array of classes is in fact an array of pointers + const(TypeInfo) tielem = typeid(tinext) is typeid(TypeInfo_Class) ? typeid(void*) : tinext; + void* arr = p + LARGEPREFIX; + GC.emplace(arr, newsz - LARGEPAD, tielem); + } + return newsz; +} /** * set the array capacity. If the array capacity isn't currently large enough @@ -844,7 +895,7 @@ Lcontinue: if (info.size >= PAGESIZE && curcapacity != 0) { auto extendsize = reqsize + offset + LARGEPAD - info.size; - auto u = GC.extend(info.base, extendsize, extendsize); + auto u = gc_extend_emplace(info.base, extendsize, extendsize, info.size, ti, tinext); if (u) { // extend worked, save the new current allocated size @@ -1550,7 +1601,7 @@ do { // not enough space, try extending auto extendsize = newsize + offset + LARGEPAD - info.size; - auto u = GC.extend(info.base, extendsize, extendsize); + auto u = gc_extend_emplace(info.base, extendsize, extendsize, info.size, ti, tinext); if (u) { // extend worked, now try setting the length @@ -1769,7 +1820,7 @@ do { // not enough space, try extending auto extendsize = newsize + offset + LARGEPAD - info.size; - auto u = GC.extend(info.base, extendsize, extendsize); + auto u = gc_extend_emplace(info.base, extendsize, extendsize, info.size, ti, tinext); if (u) { // extend worked, now try setting the length @@ -1988,7 +2039,7 @@ byte[] _d_arrayappendcTX(const TypeInfo ti, ref byte[] px, size_t n) { // not enough space, try extending auto extendoffset = offset + LARGEPAD - info.size; - auto u = GC.extend(info.base, newsize + extendoffset, newcap + extendoffset); + auto u = gc_extend_emplace(info.base, newsize + extendoffset, newcap + extendoffset, info.size, ti, tinext); if (u) { // extend worked, now try setting the length diff --git a/src/rt/typeinfo/ti_byte.d b/src/rt/typeinfo/ti_byte.d index 2cc3f98388..c35bc82e94 100644 --- a/src/rt/typeinfo/ti_byte.d +++ b/src/rt/typeinfo/ti_byte.d @@ -58,4 +58,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 939edda579..ce5764acec 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 6c856310d6..8c29453e30 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 c804711af2..9633bb9eac 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 5ed045d1ac..a236f86437 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 0051dfc4ac..7329946199 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 fa39593442..d712be08a9 100644 --- a/src/rt/typeinfo/ti_delegate.d +++ b/src/rt/typeinfo/ti_delegate.d @@ -60,4 +60,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 f9a95a6fff..e371bc652f 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 790d759fc0..e161addae2 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 a4c47d3af6..d3ff4540d1 100644 --- a/src/rt/typeinfo/ti_int.d +++ b/src/rt/typeinfo/ti_int.d @@ -62,4 +62,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 3f8ba8e655..bc7e79fb53 100644 --- a/src/rt/typeinfo/ti_long.d +++ b/src/rt/typeinfo/ti_long.d @@ -70,4 +70,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 453c945d6e..c27d907dc5 100644 --- a/src/rt/typeinfo/ti_ptr.d +++ b/src/rt/typeinfo/ti_ptr.d @@ -62,4 +62,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 6ca240caeb..19839adf7c 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 da445c2744..4a06c6ed35 100644 --- a/src/rt/typeinfo/ti_short.d +++ b/src/rt/typeinfo/ti_short.d @@ -58,4 +58,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 09232fd1c3..860592fdf8 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 8f8e42fa91..3ae043277d 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 7382e487ad..6b067f2c49 100644 --- a/src/rt/typeinfo/ti_ulong.d +++ b/src/rt/typeinfo/ti_ulong.d @@ -70,4 +70,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 3a95eb4646..f90b4ab972 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 a1ba04196f..7d278abaac 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; } }