dparse.rollback_allocator 0/52(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
module dparse.rollback_allocator; import core.memory : GC; //version = debug_rollback_allocator; /** * Pointer-bump allocator with rollback functionality. */ struct RollbackAllocator { public: // must be multiple of 8 enum memoryAlignment = 16u; @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); } do { import std.algorithm.comparison : min; if (first is null) allocateNode(size); // Memory align the size immutable size_t s = size & ~(cast(size_t) memoryAlignment - 1); immutable size_t s2 = s == size ? size : s + memoryAlignment; 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]; // alignment can make our size here bigger than what we actually have, so we clamp down to the used amount first.used = min(end, first.mem.length); 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 !is null` is always going to pass after the contains(point) check, it may no longer pass after deallocateNode while (first !is null && !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 is null || 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 core.exception : onOutOfMemoryError; import std.algorithm : max; import std.conv : emplace; import std.experimental.allocator.mallocator : AlignedMallocator; enum ALLOC_SIZE = 1024 * 8; ubyte[] m = cast(ubyte[]) AlignedMallocator.instance.alignedAllocate(max(size + Node.sizeof, ALLOC_SIZE), memoryAlignment); if (m is null) onOutOfMemoryError(); GC.addRange(m.ptr, m.length); 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 : AlignedMallocator; Node* next = first.next; ubyte[] mem = (cast(ubyte*) first)[0 .. Node.sizeof + first.mem.length]; version (debug_rollback_allocator) mem[] = 0; GC.removeRange(mem.ptr); AlignedMallocator.instance.deallocate(mem); first = next; } Node* first; } @("most simple usage, including memory across multiple pointers") unittest { RollbackAllocator rba; size_t[10] checkpoint; foreach (i; 0 .. 10) { checkpoint[i] = rba.setCheckpoint(); rba.allocate(4000); } foreach_reverse (i; 0 .. 10) { rba.rollback(checkpoint[i]); } } @("many allocates and frees while leaking memory") unittest { RollbackAllocator rba; foreach (i; 0 .. 10) { size_t[3] checkpoint; foreach (n; 0 .. 3) { checkpoint[n] = rba.setCheckpoint(); rba.allocate(4000); } foreach_reverse (n; 1 .. 3) { rba.rollback(checkpoint[n]); } } } @("allocating overly big") unittest { import std.stdio : stderr; RollbackAllocator rba; size_t[200] checkpoint; size_t cp; foreach (i; 1024 * 8 - 100 .. 1024 * 8 + 100) { try { checkpoint[cp++] = rba.setCheckpoint(); rba.allocate(i); } catch (Error e) { stderr.writeln("Unittest: crashed in allocating ", i, " bytes"); throw e; } } foreach_reverse (i, c; checkpoint[0 .. cp]) { try { rba.rollback(c); } catch (Error e) { stderr.writeln("Unittest: crashed in rolling back ", i, " (address ", c, ")"); throw e; } } }