vibe.internal.memory_legacy 0/252(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4520
4530
4540
4550
4560
4570
4580
4590
4600
4610
4620
4630
4640
4650
4660
4670
4680
4690
4700
4710
4720
4730
4740
4750
4760
4770
4780
4790
4800
4810
4820
4830
4840
4850
4860
4870
4880
4890
4900
4910
4920
4930
4940
4950
4960
4970
4980
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
5390
5400
5410
5420
5430
5440
5450
5460
5470
5480
5490
5500
5510
5520
5530
5540
5550
5560
5570
5580
5590
5600
5610
5620
5630
5640
5650
5660
5670
5680
5690
5700
5710
5720
5730
5740
5750
5760
5770
5780
5790
5800
5810
5820
5830
5840
5850
5860
5870
5880
5890
5900
5910
5920
5930
5940
5950
5960
5970
5980
5990
6000
6010
6020
6030
6040
6050
6060
6070
6080
6090
6100
6110
6120
6130
6140
6150
6160
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
6330
6340
6350
6360
6370
6380
6390
6400
6410
6420
6430
6440
6450
6460
6470
6480
6490
6500
6510
6520
6530
6540
6550
6560
6570
6580
6590
6600
6610
6620
6630
6640
6650
6660
6670
6680
6690
6700
6710
6720
6730
6740
6750
6760
6770
6780
6790
6800
6810
6820
6830
6840
6850
6860
6870
6880
6890
6900
6910
6920
6930
6940
6950
6960
6970
6980
6990
7000
7010
7020
7030
7040
7050
7060
7070
7080
7090
7100
7110
7120
7130
7140
7150
7160
7170
7180
7190
7200
7210
7220
7230
7240
7250
7260
7270
7280
7290
7300
7310
7320
7330
7340
7350
7360
7370
7380
7390
7400
7410
7420
7430
7440
7450
7460
7470
7480
7490
7500
7510
7520
7530
7540
7550
7560
7570
7580
7590
7600
7610
7620
7630
7640
7650
7660
7670
7680
7690
7700
7710
7720
7730
7740
7750
7760
7770
7780
7790
7800
7810
7820
7830
7840
7850
7860
7870
7880
7890
7900
7910
7920
7930
7940
7950
7960
7970
7980
7990
8000
8010
8020
8030
8040
8050
8060
8070
8080
8090
8100
8110
8120
8130
8140
8150
8160
8170
8180
8190
8200
8210
8220
8230
8240
8250
8260
8270
8280
8290
8300
8310
8320
8330
8340
8350
8360
8370
8380
8390
8400
8410
8420
8430
8440
8450
8460
8470
8480
8490
8500
8510
8520
8530
8540
8550
8560
8570
8580
8590
8600
8610
8620
8630
8640
8650
8660
8670
8680
8690
8700
8710
8720
8730
module vibe.internal.memory_legacy; import vibe.internal.meta.traits : synchronizedIsNothrow; import core.exception : OutOfMemoryError; import core.stdc.stdlib; import core.memory; import std.conv; import std.exception : enforceEx; import std.traits; import std.algorithm; Allocator defaultAllocator() @safe nothrow { version(VibeManualMemoryManagement){ return manualAllocator(); } else return () @trusted { static __gshared Allocator alloc; if (!alloc) { alloc = new GCAllocator; //alloc = new AutoFreeListAllocator(alloc); //alloc = new DebugAllocator(alloc); alloc = new LockAllocator(alloc); } return alloc; } (); } Allocator manualAllocator() @trusted nothrow { static __gshared Allocator alloc; if (!alloc) { alloc = new MallocAllocator; alloc = new AutoFreeListAllocator(alloc); //alloc = new DebugAllocator(alloc); alloc = new LockAllocator(alloc); } return alloc; } Allocator threadLocalAllocator() @safe nothrow { static Allocator alloc; if (!alloc) { version(VibeManualMemoryManagement) alloc = new MallocAllocator; else alloc = new GCAllocator; alloc = new AutoFreeListAllocator(alloc); // alloc = new DebugAllocator(alloc); } return alloc; } Allocator threadLocalManualAllocator() @safe nothrow { static Allocator alloc; if (!alloc) { alloc = new MallocAllocator; alloc = new AutoFreeListAllocator(alloc); // alloc = new DebugAllocator(alloc); } return alloc; } auto allocObject(T, bool MANAGED = true, ARGS...)(Allocator allocator, ARGS args) { auto mem = allocator.alloc(AllocSize!T); static if( MANAGED ){ static if( hasIndirections!T ) GC.addRange(mem.ptr, mem.length); return internalEmplace!T(mem, args); } else static if( is(T == class) ) return cast(T)mem.ptr; else return cast(T*)mem.ptr; } T[] allocArray(T, bool MANAGED = true)(Allocator allocator, size_t n) { auto mem = allocator.alloc(T.sizeof * n); auto ret = cast(T[])mem; static if( MANAGED ){ static if( hasIndirections!T ) GC.addRange(mem.ptr, mem.length); // TODO: use memset for class, pointers and scalars foreach (ref el; ret) { internalEmplace!T(cast(void[])((&el)[0 .. 1])); } } return ret; } void freeArray(T, bool MANAGED = true)(Allocator allocator, ref T[] array, bool call_destructors = true) { static if (MANAGED) { static if (hasIndirections!T) GC.removeRange(array.ptr); static if (hasElaborateDestructor!T) if (call_destructors) foreach_reverse (ref el; array) destroy(el); } allocator.free(cast(void[])array); array = null; } interface Allocator { nothrow: enum size_t alignment = 0x10; enum size_t alignmentMask = alignment-1; // NOTE: the contracts in this interface have two issues: // - they require an assert(false); contract in the derived class to have any effect // - there is s codegen issue that yield garbage values within the contracts defined here // For these reasons contracts need to be placed into each class individually instead void[] alloc(size_t sz); //out { assert((cast(size_t)__result.ptr & alignmentMask) == 0, "alloc() returned misaligned data."); } void[] realloc(void[] mem, size_t new_sz); /*in { assert(mem.ptr !is null, "realloc() called with null array."); assert((cast(size_t)mem.ptr & alignmentMask) == 0, "misaligned pointer passed to realloc()."); } out { assert((cast(size_t)__result.ptr & alignmentMask) == 0, "realloc() returned misaligned data."); }*/ void free(void[] mem); /*in { assert(mem.ptr !is null, "free() called with null array."); assert((cast(size_t)mem.ptr & alignmentMask) == 0, "misaligned pointer passed to free()."); }*/ } /** Simple proxy allocator protecting its base allocator with a mutex. */ class LockAllocator : Allocator { private { Allocator m_base; } this(Allocator base) nothrow @safe { m_base = base; } void[] alloc(size_t sz) { static if (!synchronizedIsNothrow) scope (failure) assert(0, "Internal error: function should be nothrow"); synchronized (this) return m_base.alloc(sz); } void[] realloc(void[] mem, size_t new_sz) in { assert(mem.ptr !is null, "realloc() called with null array."); assert((cast(size_t)mem.ptr & alignmentMask) == 0, "misaligned pointer passed to realloc()."); } body { static if (!synchronizedIsNothrow) scope (failure) assert(0, "Internal error: function should be nothrow"); synchronized(this) return m_base.realloc(mem, new_sz); } void free(void[] mem) in { assert(mem.ptr !is null, "free() called with null array."); assert((cast(size_t)mem.ptr & alignmentMask) == 0, "misaligned pointer passed to free()."); } body { static if (!synchronizedIsNothrow) scope (failure) assert(0, "Internal error: function should be nothrow"); synchronized(this) m_base.free(mem); } } final class DebugAllocator : Allocator { import vibe.utils.hashmap : HashMap; private { Allocator m_baseAlloc; HashMap!(void*, size_t) m_blocks; size_t m_bytes; size_t m_maxBytes; } this(Allocator base_allocator) nothrow @safe { import vibe.internal.utilallocator : Mallocator, allocatorObject; m_baseAlloc = base_allocator; m_blocks = HashMap!(void*, size_t)(() @trusted { return Mallocator.instance.allocatorObject; } ()); } @property size_t allocatedBlockCount() const { return m_blocks.length; } @property size_t bytesAllocated() const { return m_bytes; } @property size_t maxBytesAllocated() const { return m_maxBytes; } void[] alloc(size_t sz) { auto ret = m_baseAlloc.alloc(sz); assert(ret.length == sz, "base.alloc() returned block with wrong size."); assert(m_blocks.getNothrow(ret.ptr, size_t.max) == size_t.max, "base.alloc() returned block that is already allocated."); m_blocks[ret.ptr] = sz; m_bytes += sz; if( m_bytes > m_maxBytes ){ m_maxBytes = m_bytes; logDebug_("New allocation maximum: %d (%d blocks)", m_maxBytes, m_blocks.length); } return ret; } void[] realloc(void[] mem, size_t new_size) { auto sz = m_blocks.getNothrow(mem.ptr, size_t.max); assert(sz != size_t.max, "realloc() called with non-allocated pointer."); assert(sz == mem.length, "realloc() called with block of wrong size."); auto ret = m_baseAlloc.realloc(mem, new_size); assert(ret.length == new_size, "base.realloc() returned block with wrong size."); assert(ret.ptr is mem.ptr || m_blocks.getNothrow(ret.ptr, size_t.max) == size_t.max, "base.realloc() returned block that is already allocated."); m_bytes -= sz; m_blocks.remove(mem.ptr); m_blocks[ret.ptr] = new_size; m_bytes += new_size; return ret; } void free(void[] mem) { auto sz = m_blocks.getNothrow(mem.ptr, size_t.max); assert(sz != size_t.max, "free() called with non-allocated object."); assert(sz == mem.length, "free() called with block of wrong size."); m_baseAlloc.free(mem); m_bytes -= sz; m_blocks.remove(mem.ptr); } } final class MallocAllocator : Allocator { void[] alloc(size_t sz) { static err = new immutable OutOfMemoryError; auto ptr = .malloc(sz + Allocator.alignment); if (ptr is null) throw err; return adjustPointerAlignment(ptr)[0 .. sz]; } void[] realloc(void[] mem, size_t new_size) { size_t csz = min(mem.length, new_size); auto p = extractUnalignedPointer(mem.ptr); size_t oldmisalign = mem.ptr - p; auto pn = cast(ubyte*).realloc(p, new_size+Allocator.alignment); if (p == pn) return pn[oldmisalign .. new_size+oldmisalign]; auto pna = cast(ubyte*)adjustPointerAlignment(pn); auto newmisalign = pna - pn; // account for changed alignment after realloc (move memory back to aligned position) if (oldmisalign != newmisalign) { if (newmisalign > oldmisalign) { foreach_reverse (i; 0 .. csz) pn[i + newmisalign] = pn[i + oldmisalign]; } else { foreach (i; 0 .. csz) pn[i + newmisalign] = pn[i + oldmisalign]; } } return pna[0 .. new_size]; } void free(void[] mem) { .free(extractUnalignedPointer(mem.ptr)); } } final class GCAllocator : Allocator { void[] alloc(size_t sz) { auto mem = GC.malloc(sz+Allocator.alignment); auto alignedmem = adjustPointerAlignment(mem); assert(alignedmem - mem <= Allocator.alignment); auto ret = alignedmem[0 .. sz]; ensureValidMemory(ret); return ret; } void[] realloc(void[] mem, size_t new_size) { size_t csz = min(mem.length, new_size); auto p = extractUnalignedPointer(mem.ptr); size_t misalign = mem.ptr - p; assert(misalign <= Allocator.alignment); void[] ret; auto extended = GC.extend(p, new_size - mem.length, new_size - mem.length); if (extended) { assert(extended >= new_size+Allocator.alignment); ret = p[misalign .. new_size+misalign]; } else { ret = alloc(new_size); ret[0 .. csz] = mem[0 .. csz]; } ensureValidMemory(ret); return ret; } void free(void[] mem) { // For safety reasons, the GCAllocator should never explicitly free memory. //GC.free(extractUnalignedPointer(mem.ptr)); } } final class AutoFreeListAllocator : Allocator { import std.typetuple; private { enum minExponent = 5; enum freeListCount = 14; FreeListAlloc[freeListCount] m_freeLists; Allocator m_baseAlloc; } this(Allocator base_allocator) nothrow @safe { m_baseAlloc = base_allocator; foreach (i; iotaTuple!freeListCount) m_freeLists[i] = new FreeListAlloc(nthFreeListSize!(i), m_baseAlloc); } void[] alloc(size_t sz) { auto idx = getAllocatorIndex(sz); return idx < freeListCount ? m_freeLists[idx].alloc()[0 .. sz] : m_baseAlloc.alloc(sz); } void[] realloc(void[] data, size_t sz) { auto curidx = getAllocatorIndex(data.length); auto newidx = getAllocatorIndex(sz); if (curidx == newidx) { if (curidx == freeListCount) { // forward large blocks to the base allocator return m_baseAlloc.realloc(data, sz); } else { // just grow the slice if it still fits into the free list slot return data.ptr[0 .. sz]; } } // otherwise re-allocate manually auto newd = alloc(sz); assert(newd.ptr+sz <= data.ptr || newd.ptr >= data.ptr+data.length, "New block overlaps old one!?"); auto len = min(data.length, sz); newd[0 .. len] = data[0 .. len]; free(data); return newd; } void free(void[] data) { //logTrace("AFL free %08X(%s)", data.ptr, data.length); auto idx = getAllocatorIndex(data.length); if (idx < freeListCount) m_freeLists[idx].free(data.ptr[0 .. 1 << (idx + minExponent)]); else m_baseAlloc.free(data); } // does a CT optimized binary search for the right allocater private int getAllocatorIndex(size_t sz) @safe nothrow @nogc { //pragma(msg, getAllocatorIndexStr!(0, freeListCount)); return mixin(getAllocatorIndexStr!(0, freeListCount)); } private template getAllocatorIndexStr(int low, int high) { import std.format : format; static if (low == high) enum getAllocatorIndexStr = format("%s", low); else { enum mid = (low + high) / 2; enum getAllocatorIndexStr = "sz > nthFreeListSize!%s ? %s : %s" .format(mid, getAllocatorIndexStr!(mid+1, high), getAllocatorIndexStr!(low, mid)); } } unittest { auto a = new AutoFreeListAllocator(null); assert(a.getAllocatorIndex(0) == 0); foreach (i; iotaTuple!freeListCount) { assert(a.getAllocatorIndex(nthFreeListSize!i-1) == i); assert(a.getAllocatorIndex(nthFreeListSize!i) == i); assert(a.getAllocatorIndex(nthFreeListSize!i+1) == i+1); } assert(a.getAllocatorIndex(size_t.max) == freeListCount); } private static pure size_t nthFreeListSize(size_t i)() { return 1 << (i + minExponent); } private template iotaTuple(size_t i) { static if (i > 1) alias iotaTuple = TypeTuple!(iotaTuple!(i-1), i-1); else alias iotaTuple = TypeTuple!(0); } } final class PoolAllocator : Allocator { static struct Pool { Pool* next; void[] data; void[] remaining; } static struct Destructor { Destructor* next; void function(void*) destructor; void* object; } private { Allocator m_baseAllocator; Pool* m_freePools; Pool* m_fullPools; Destructor* m_destructors; size_t m_poolSize; } this(size_t pool_size, Allocator base) @safe nothrow { m_poolSize = pool_size; m_baseAllocator = base; } @property size_t totalSize() @safe nothrow @nogc { size_t amt = 0; for (auto p = m_fullPools; p; p = p.next) amt += p.data.length; for (auto p = m_freePools; p; p = p.next) amt += p.data.length; return amt; } @property size_t allocatedSize() @safe nothrow @nogc { size_t amt = 0; for (auto p = m_fullPools; p; p = p.next) amt += p.data.length; for (auto p = m_freePools; p; p = p.next) amt += p.data.length - p.remaining.length; return amt; } void[] alloc(size_t sz) { auto aligned_sz = alignedSize(sz); Pool* pprev = null; Pool* p = cast(Pool*)m_freePools; while( p && p.remaining.length < aligned_sz ){ pprev = p; p = p.next; } if( !p ){ auto pmem = m_baseAllocator.alloc(AllocSize!Pool); p = emplace!Pool(cast(Pool*)pmem.ptr); p.data = m_baseAllocator.alloc(max(aligned_sz, m_poolSize)); p.remaining = p.data; p.next = cast(Pool*)m_freePools; m_freePools = p; pprev = null; } auto ret = p.remaining[0 .. aligned_sz]; p.remaining = p.remaining[aligned_sz .. $]; if( !p.remaining.length ){ if( pprev ){ pprev.next = p.next; } else { m_freePools = p.next; } p.next = cast(Pool*)m_fullPools; m_fullPools = p; } return ret[0 .. sz]; } void[] realloc(void[] arr, size_t newsize) { auto aligned_sz = alignedSize(arr.length); auto aligned_newsz = alignedSize(newsize); if( aligned_newsz <= aligned_sz ) return arr[0 .. newsize]; // TODO: back up remaining auto pool = m_freePools; bool last_in_pool = pool && arr.ptr+aligned_sz == pool.remaining.ptr; if( last_in_pool && pool.remaining.length+aligned_sz >= aligned_newsz ){ pool.remaining = pool.remaining[aligned_newsz-aligned_sz .. $]; arr = arr.ptr[0 .. aligned_newsz]; assert(arr.ptr+arr.length == pool.remaining.ptr, "Last block does not align with the remaining space!?"); return arr[0 .. newsize]; } else { auto ret = alloc(newsize); assert(ret.ptr >= arr.ptr+aligned_sz || ret.ptr+ret.length <= arr.ptr, "New block overlaps old one!?"); ret[0 .. min(arr.length, newsize)] = arr[0 .. min(arr.length, newsize)]; return ret; } } void free(void[] mem) { } void freeAll() { version(VibeManualMemoryManagement){ // destroy all initialized objects for (auto d = m_destructors; d; d = d.next) d.destructor(cast(void*)d.object); m_destructors = null; // put all full Pools into the free pools list for (Pool* p = cast(Pool*)m_fullPools, pnext; p; p = pnext) { pnext = p.next; p.next = cast(Pool*)m_freePools; m_freePools = cast(Pool*)p; } // free up all pools for (Pool* p = cast(Pool*)m_freePools; p; p = p.next) p.remaining = p.data; } } void reset() { version(VibeManualMemoryManagement){ freeAll(); Pool* pnext; for (auto p = cast(Pool*)m_freePools; p; p = pnext) { pnext = p.next; m_baseAllocator.free(p.data); m_baseAllocator.free((cast(void*)p)[0 .. AllocSize!Pool]); } m_freePools = null; } } private static destroy(T)(void* ptr) { static if( is(T == class) ) .destroy(cast(T)ptr); else .destroy(*cast(T*)ptr); } } final class FreeListAlloc : Allocator { nothrow: private static struct FreeListSlot { FreeListSlot* next; } private { FreeListSlot* m_firstFree = null; size_t m_nalloc = 0; size_t m_nfree = 0; Allocator m_baseAlloc; immutable size_t m_elemSize; } this(size_t elem_size, Allocator base_allocator) @safe nothrow { assert(elem_size >= size_t.sizeof); m_elemSize = elem_size; m_baseAlloc = base_allocator; logDebug_("Create FreeListAlloc %d", m_elemSize); } @property size_t elementSize() const { return m_elemSize; } void[] alloc(size_t sz) { assert(sz == m_elemSize, "Invalid allocation size."); return alloc(); } void[] alloc() { void[] mem; if( m_firstFree ){ auto slot = m_firstFree; m_firstFree = slot.next; slot.next = null; mem = (cast(void*)slot)[0 .. m_elemSize]; debug m_nfree--; } else { mem = m_baseAlloc.alloc(m_elemSize); //logInfo("Alloc %d bytes: alloc: %d, free: %d", SZ, s_nalloc, s_nfree); } debug m_nalloc++; //logInfo("Alloc %d bytes: alloc: %d, free: %d", SZ, s_nalloc, s_nfree); return mem; } void[] realloc(void[] mem, size_t sz) { assert(mem.length == m_elemSize); assert(sz == m_elemSize); return mem; } void free(void[] mem) { assert(mem.length == m_elemSize, "Memory block passed to free has wrong size."); auto s = cast(FreeListSlot*)mem.ptr; s.next = m_firstFree; m_firstFree = s; m_nalloc--; m_nfree++; } } struct FreeListObjectAlloc(T, bool USE_GC = true, bool INIT = true, EXTRA = void) { enum ElemSize = AllocSize!T; enum ElemSlotSize = max(AllocSize!T + AllocSize!EXTRA, Slot.sizeof); static if( is(T == class) ){ alias TR = T; } else { alias TR = T*; } struct Slot { Slot* next; } private static Slot* s_firstFree; static TR alloc(ARGS...)(ARGS args) { void[] mem; if (s_firstFree !is null) { auto ret = s_firstFree; s_firstFree = s_firstFree.next; ret.next = null; mem = (cast(void*)ret)[0 .. ElemSize]; } else { //logInfo("alloc %s/%d", T.stringof, ElemSize); mem = manualAllocator().alloc(ElemSlotSize); static if( hasIndirections!T ) GC.addRange(mem.ptr, ElemSlotSize); } static if (INIT) return cast(TR)internalEmplace!(Unqual!T)(mem, args); // FIXME: this emplace has issues with qualified types, but Unqual!T may result in the wrong constructor getting called. else return cast(TR)mem.ptr; } static void free(TR obj) { static if (INIT) { scope (failure) assert(0, "You shouldn't throw in destructors"); auto objc = obj; static if (is(TR == T*)) .destroy(*objc);//typeid(T).destroy(cast(void*)obj); else .destroy(objc); } auto sl = cast(Slot*)obj; sl.next = s_firstFree; s_firstFree = sl; //static if( hasIndirections!T ) GC.removeRange(cast(void*)obj); //manualAllocator().free((cast(void*)obj)[0 .. ElemSlotSize]); } } template AllocSize(T) { static if (is(T == class)) { // workaround for a strange bug where AllocSize!SSLStream == 0: TODO: dustmite! enum dummy = T.stringof ~ __traits(classInstanceSize, T).stringof; enum AllocSize = __traits(classInstanceSize, T); } else { enum AllocSize = T.sizeof; } } struct FreeListRef(T, bool INIT = true) { @safe: alias ObjAlloc = FreeListObjectAlloc!(T, true, INIT, int); enum ElemSize = AllocSize!T; static if( is(T == class) ){ alias TR = T; } else { alias TR = T*; } private TR m_object; private size_t m_magic = 0x1EE75817; // workaround for compiler bug static FreeListRef opCall(ARGS...)(ARGS args) { //logInfo("refalloc %s/%d", T.stringof, ElemSize); FreeListRef ret; ret.m_object = () @trusted { return ObjAlloc.alloc(args); } (); ret.refCount = 1; return ret; } ~this() { //if( m_object ) logInfo("~this!%s(): %d", T.stringof, this.refCount); //if( m_object ) logInfo("ref %s destructor %d", T.stringof, refCount); //else logInfo("ref %s destructor %d", T.stringof, 0); clear(); m_magic = 0; m_object = null; } this(this) { checkInvariants(); if( m_object ){ //if( m_object ) logInfo("this!%s(this): %d", T.stringof, this.refCount); this.refCount++; } } void opAssign(FreeListRef other) { clear(); m_object = other.m_object; if( m_object ){ //logInfo("opAssign!%s(): %d", T.stringof, this.refCount); refCount++; } } void clear() { checkInvariants(); if (m_object) { if (--this.refCount == 0) () @trusted { ObjAlloc.free(m_object); } (); } m_object = null; m_magic = 0x1EE75817; } @property const(TR) get() const { checkInvariants(); return m_object; } @property TR get() { checkInvariants(); return m_object; } alias get this; private @property ref int refCount() const @trusted { auto ptr = cast(ubyte*)cast(void*)m_object; ptr += ElemSize; return *cast(int*)ptr; } private void checkInvariants() const { assert(m_magic == 0x1EE75817); assert(!m_object || refCount > 0); } } private void* extractUnalignedPointer(void* base) nothrow { ubyte misalign = *(cast(ubyte*)base-1); assert(misalign <= Allocator.alignment); return base - misalign; } private void* adjustPointerAlignment(void* base) nothrow { ubyte misalign = Allocator.alignment - (cast(size_t)base & Allocator.alignmentMask); base += misalign; *(cast(ubyte*)base-1) = misalign; return base; } unittest { void test_align(void* p, size_t adjustment) { void* pa = adjustPointerAlignment(p); assert((cast(size_t)pa & Allocator.alignmentMask) == 0, "Non-aligned pointer."); assert(*(cast(ubyte*)pa-1) == adjustment, "Invalid adjustment "~to!string(p)~": "~to!string(*(cast(ubyte*)pa-1))); void* pr = extractUnalignedPointer(pa); assert(pr == p, "Recovered base != original"); } void* ptr = .malloc(0x40); ptr += Allocator.alignment - (cast(size_t)ptr & Allocator.alignmentMask); test_align(ptr++, 0x10); test_align(ptr++, 0x0F); test_align(ptr++, 0x0E); test_align(ptr++, 0x0D); test_align(ptr++, 0x0C); test_align(ptr++, 0x0B); test_align(ptr++, 0x0A); test_align(ptr++, 0x09); test_align(ptr++, 0x08); test_align(ptr++, 0x07); test_align(ptr++, 0x06); test_align(ptr++, 0x05); test_align(ptr++, 0x04); test_align(ptr++, 0x03); test_align(ptr++, 0x02); test_align(ptr++, 0x01); test_align(ptr++, 0x10); } /// private size_t alignedSize(size_t sz) nothrow { return ((sz + Allocator.alignment - 1) / Allocator.alignment) * Allocator.alignment; } unittest { foreach( i; 0 .. 20 ){ auto ia = alignedSize(i); assert(ia >= i); assert((ia & Allocator.alignmentMask) == 0); assert(ia < i+Allocator.alignment); } } private void ensureValidMemory(void[] mem) nothrow { auto bytes = cast(ubyte[])mem; swap(bytes[0], bytes[$-1]); swap(bytes[0], bytes[$-1]); } /// See issue #14194 private T internalEmplace(T, Args...)(void[] chunk, auto ref Args args) if (is(T == class)) in { import std.string, std.format; assert(chunk.length >= T.sizeof, format("emplace: Chunk size too small: %s < %s size = %s", chunk.length, T.stringof, T.sizeof)); assert((cast(size_t) chunk.ptr) % T.alignof == 0, format("emplace: Misaligned memory block (0x%X): it must be %s-byte aligned for type %s", chunk.ptr, T.alignof, T.stringof)); } body { enum classSize = __traits(classInstanceSize, T); auto result = cast(T) chunk.ptr; // Initialize the object in its pre-ctor state static if (__VERSION__ < 2071) chunk[0 .. classSize] = typeid(T).init[]; else chunk[0 .. classSize] = typeid(T).initializer[]; // Avoid deprecation warning // Call the ctor if any static if (is(typeof(result.__ctor(args)))) { // T defines a genuine constructor accepting args // Go the classic route: write .init first, then call ctor result.__ctor(args); } else { static assert(args.length == 0 && !is(typeof(&T.__ctor)), "Don't know how to initialize an object of type " ~ T.stringof ~ " with arguments " ~ Args.stringof); } return result; } /// Dittor private auto internalEmplace(T, Args...)(void[] chunk, auto ref Args args) if (!is(T == class)) in { import std.string, std.format; assert(chunk.length >= T.sizeof, format("emplace: Chunk size too small: %s < %s size = %s", chunk.length, T.stringof, T.sizeof)); assert((cast(size_t) chunk.ptr) % T.alignof == 0, format("emplace: Misaligned memory block (0x%X): it must be %s-byte aligned for type %s", chunk.ptr, T.alignof, T.stringof)); } body { return emplace(cast(T*)chunk.ptr, args); } private void logDebug_(ARGS...)(string msg, ARGS args) {}