stdx.allocator.gc_allocator 5/35(14%) 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
2719
2819
2938
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
855
865
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
/// module stdx.allocator.gc_allocator; import stdx.allocator.common; /** D's built-in garbage-collected allocator. */ struct GCAllocator { import core.memory : GC; import std.typecons : Ternary; @system unittest { testAllocator!(() => GCAllocator.instance); } /** The alignment is a static constant equal to $(D platformAlignment), which ensures proper alignment for any D data type. */ enum uint alignment = platformAlignment; /** Standard allocator methods per the semantics defined above. The $(D deallocate) and $(D reallocate) methods are $(D @system) because they may move memory around, leaving dangling pointers in user code. */ pure nothrow @trusted void[] allocate(size_t bytes) shared { if (!bytes) return null; auto p = GC.malloc(bytes); return p ? p[0 .. bytes] : null; } /// Ditto @system bool expand(ref void[] b, size_t delta) shared { if (delta == 0) return true; if (b is null) return false; immutable curLength = GC.sizeOf(b.ptr); assert(curLength != 0); // we have a valid GC pointer here immutable desired = b.length + delta; if (desired > curLength) // check to see if the current block can't hold the data { immutable sizeRequest = desired - curLength; immutable newSize = GC.extend(b.ptr, sizeRequest, sizeRequest); if (newSize == 0) { // expansion unsuccessful return false; } assert(newSize >= desired); } b = b.ptr[0 .. desired]; return true; } /// Ditto pure nothrow @system bool reallocate(ref void[] b, size_t newSize) shared { import core.exception : OutOfMemoryError; try { auto p = cast(ubyte*) GC.realloc(b.ptr, newSize); b = p[0 .. newSize]; } catch (OutOfMemoryError) { // leave the block in place, tell caller return false; } return true; } /// Ditto pure nothrow Ternary resolveInternalPointer(const void* p, ref void[] result) shared { auto r = GC.addrOf(cast(void*) p); if (!r) return Ternary.no; result = r[0 .. GC.sizeOf(r)]; return Ternary.yes; } /// Ditto pure nothrow @system bool deallocate(void[] b) shared { GC.free(b.ptr); return true; } /// Ditto size_t goodAllocSize(size_t n) shared { if (n == 0) return 0; if (n <= 16) return 16; import core.bitop : bsr; auto largestBit = bsr(n-1) + 1; if (largestBit <= 12) // 4096 or less return size_t(1) << largestBit; // larger, we use a multiple of 4096. return ((n + 4095) / 4096) * 4096; } /** Returns the global instance of this allocator type. The garbage collected allocator is thread-safe, therefore all of its methods and `instance` itself are $(D shared). */ static shared GCAllocator instance; // Leave it undocummented for now. nothrow @trusted void collect() shared { GC.collect(); } } /// @system unittest { auto buffer = GCAllocator.instance.allocate(1024 * 1024 * 4); // deallocate upon scope's end (alternatively: leave it to collection) scope(exit) GCAllocator.instance.deallocate(buffer); //... } @system unittest { auto b = GCAllocator.instance.allocate(10_000); assert(GCAllocator.instance.expand(b, 1)); } @system unittest { import core.memory : GC; import std.typecons : Ternary; // test allocation sizes assert(GCAllocator.instance.goodAllocSize(1) == 16); for (size_t s = 16; s <= 8192; s *= 2) { assert(GCAllocator.instance.goodAllocSize(s) == s); assert(GCAllocator.instance.goodAllocSize(s - (s / 2) + 1) == s); auto buffer = GCAllocator.instance.allocate(s); scope(exit) GCAllocator.instance.deallocate(buffer); void[] p; assert(GCAllocator.instance.resolveInternalPointer(null, p) == Ternary.no); Ternary r = GCAllocator.instance.resolveInternalPointer(buffer.ptr, p); assert(p.ptr is buffer.ptr && p.length >= buffer.length); assert(GC.sizeOf(buffer.ptr) == s); auto buffer2 = GCAllocator.instance.allocate(s - (s / 2) + 1); scope(exit) GCAllocator.instance.deallocate(buffer2); assert(GC.sizeOf(buffer2.ptr) == s); } // anything above a page is simply rounded up to next page assert(GCAllocator.instance.goodAllocSize(4096 * 4 + 1) == 4096 * 5); }