dparse.rollback_allocator 0/48(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
module dparse.rollback_allocator; //version = debug_rollback_allocator; /** * Pointer-bump allocator with rollback functionality. */ struct RollbackAllocator { public: @disable this(this); ~this() { while (first !is null) deallocateNode(); } /** * Allocates `size` bytes of memory. */ void[] allocate(const size_t size) out (arr) { assert(arr.length == size); } body { if (first is null) allocateNode(size); // Move size up to the next multiple of 8 for memory alignment purposes immutable size_t s = size & ~7UL; immutable size_t s2 = s == size ? size : s + 8; size_t fu = first.used; size_t end = fu + s2; //assert(end >= fu + size); //assert(end % 8 == 0); if (end > first.mem.length) { allocateNode(size); fu = first.used; end = fu + s2; } //assert((cast(size_t) first.mem.ptr) % 8 == 0); //assert(((cast(size_t) first.mem.ptr) + end) % 8 == 0); void[] m = first.mem[fu .. fu + size]; first.used = end; return m; } /** * Rolls back the allocator to the given checkpoint. */ void rollback(size_t point) { import std.stdio : stderr; if (point == 0) { while (first) deallocateNode(); return; } else assert(contains(point), "Attepmted to roll back to a point not in the allocator."); while (!first.contains(point)) deallocateNode(); assert(first !is null); immutable begin = point - cast(size_t) first.mem.ptr; version (debug_rollback_allocator) (cast(ubyte[]) first.mem)[begin .. $] = 0; first.used = begin; assert(cast(size_t) first.mem.ptr + first.used == point); } /** * Get a checkpoint for the allocator. */ size_t setCheckpoint() const nothrow @nogc { assert(first.used <= first.mem.length); return first is null ? 0 : cast(size_t) first.mem.ptr + first.used; } /** * Allocates a T and returns a pointer to it */ auto make(T, Args...)(auto ref Args args) { import std.algorithm.comparison : max; import std.experimental.allocator : stateSize; import std.conv : emplace; void[] mem = allocate(max(stateSize!T, 1)); if (mem.ptr is null) return null; static if (is(T == class)) return emplace!T(mem, args); else return emplace(cast(T*) mem.ptr, args); } private: // Used for debugging bool contains(size_t point) const { for (const(Node)* n = first; n !is null; n = n.next) if (n.contains(point)) return true; return false; } static struct Node { Node* next; size_t used; ubyte[] mem; bool contains(size_t p) const pure nothrow @nogc @safe { return p >= cast(size_t) mem.ptr && p <= cast(size_t) mem.ptr + mem.length; } } void allocateNode(size_t size) { import std.algorithm : max; import std.experimental.allocator.mallocator : Mallocator; import std.conv : emplace; enum ALLOC_SIZE = 1024 * 8; ubyte[] m = cast(ubyte[]) Mallocator.instance.allocate(max(size + Node.sizeof, ALLOC_SIZE)); version (debug_rollback_allocator) m[] = 0; Node* n = emplace!Node(cast(Node*) m.ptr, first, 0, m[Node.sizeof .. $]); assert((cast(size_t) n.mem.ptr) % 8 == 0, "The memoriez!"); first = n; } void deallocateNode() { assert(first !is null); import std.experimental.allocator.mallocator : Mallocator; Node* next = first.next; ubyte[] mem = (cast(ubyte*) first)[0 .. Node.sizeof + first.mem.length]; version (debug_rollback_allocator) mem[] = 0; Mallocator.instance.deallocate(mem); first = next; } Node* first; }