vibe.internal.freelistref 46/59(77%) 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
4236
4336
4412
4512
4612
4724
480
490
5024
5148
520
530
540
5536
560
5772
580
590
600
610
620
630
6425
650
6625
670
680
6925
7025
7125
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
11025
11125
11225
1130
11425
1150
1160
1170
1180
1190
1200
1210
12275
12375
12475
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
14875
14975
15025
15150
1520
1530
15475
15575
1560
1570
1580
15926
1600
1610
1620
1630
1640
1650
1660
1670
16875
16975
17075
17175
1720
1730
1740
1750
17675
177100
1780
1790
1800
1810
1820
1830
1840
1850
1860
18730
1880
1890
19030
1910
1920
1930
19430
19560
1960
1970
19830
1990
2000
2010
20230
2030
2040
2050
2060
2070
2080
2090
21030
2110
2120
2130
2140
2150
2160
2170
21830
2190
2200
2210
2220
2230
2240
2250
2266
2270
2280
2296
2300
2310
2320
23312
2340
2350
2360
/** Utility functions for memory management Note that this module currently is a big sand box for testing allocation related stuff. Nothing here, including the interfaces, is final but rather a lot of experimentation. Copyright: © 2012-2013 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ module vibe.internal.freelistref; import vibe.internal.allocator; 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; 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 = () @trusted { return (cast(void*)ret)[0 .. ElemSlotSize]; } (); } else { //logInfo("alloc %s/%d", T.stringof, ElemSize); mem = Mallocator.instance.allocate(ElemSlotSize); static if(hasIndirections!T) () @trusted { GC.addRange(mem.ptr, ElemSlotSize); } (); } // FIXME: this emplace has issues with qualified types, but Unqual!T may result in the wrong constructor getting called. static if (INIT) internalEmplace!(Unqual!T)(mem[0 .. ElemSize], args); return () @trusted { 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); //Mallocator.instance.deallocate((cast(void*)obj)[0 .. ElemSlotSize]); } } @safe unittest { struct S {} FreeListObjectAlloc!S.alloc(); } 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) { 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) { FreeListRef ret; ret.m_object = ObjAlloc.alloc!ARGS(args); ret.refCount = 1; //logInfo("refalloc %s/%d", T.stringof, ElemSize); 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; } static if (is(T == class)) { @property inout(T) get() inout @safe nothrow { return m_object; } } else { @property ref inout(T) get() inout @safe nothrow { return *m_object; } void opAssign(T t) { *m_object = t; } } alias get this; private @property ref int refCount() const @trusted { assert(m_object !is null); 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); } } /// 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[0], T.alignof, T.stringof)); } body { enum classSize = __traits(classInstanceSize, T); auto result = () @trusted { return cast(T) chunk.ptr; } (); // Initialize the object in its pre-ctor state () @trusted { 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) @safe 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[0], T.alignof, T.stringof)); } body { return emplace(() @trusted { return cast(T*)chunk.ptr; } (), args); } private void logDebug_(ARGS...)(string msg, ARGS args) {}