dparse.stack_buffer 0/30(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
module dparse.stack_buffer; import core.memory : GC; import std.traits; //version = debug_stack_allocator; struct StackBuffer { bool put(T)(T t) { import std.experimental.allocator.mallocator : Mallocator; static if (is(T == class) || isPointer!T) if (t is null) return false; if (_length == 0) arr = stackSpace[]; static if (is(T == class)) static assert(T.sizeof == size_t.sizeof); static if (hasIndirections!T) while (_length % size_t.sizeof != 0) put(ubyte(1)); if (arr.ptr != stackSpace.ptr) { if (_length + T.sizeof > arr.length) { size_t newLength = arr.length << 1; while (_length + T.sizeof > newLength) newLength <<= 1; auto oldPtr = arr.ptr; Mallocator.instance.reallocate(arr, newLength); GC.removeRange(oldPtr); GC.addRange(arr.ptr, arr.length); version (debug_stack_allocator) (cast(ubyte[]) arr)[_length .. $] = 0; } } else if (_length + T.sizeof > stackSpace.length) { size_t newLength = stackSpace.length << 1; while (_length + T.sizeof > newLength) newLength <<= 1; arr = Mallocator.instance.allocate(newLength); GC.addRange(arr.ptr, arr.length); version (debug_stack_allocator) (cast(ubyte[]) arr)[] = 0; arr[0 .. stackSpace.length] = stackSpace[]; } arr[_length .. _length + T.sizeof] = (cast(void*) &t)[0 .. T.sizeof]; _length += T.sizeof; return true; } ~this() { import std.experimental.allocator.mallocator:Mallocator; version (debug_stack_allocator) (cast(ubyte[]) arr)[] = 0; if (arr.ptr !is stackSpace.ptr) { GC.removeRange(arr.ptr); Mallocator.instance.deallocate(arr); } } void[] opSlice() { return arr[0 .. _length]; } @disable this(this); uint length() const pure nothrow @nogc @safe @property { return _length; } alias opDollar = length; private: void[8 * 16] stackSpace; void[] arr; uint _length; } unittest { StackBuffer sb; ubyte[80] u; sb.put(u); assert(sb.length == 80); static struct S { void[100] u; } S s; sb.put(s); class B { int b; } class D : B { double d; } B b = new B; sb.put(b); B d = new D; sb.put(d); }