1 /// 2 module std.experimental.allocator.gc_allocator; 3 import std.experimental.allocator.common; 4 5 /** 6 D's built-in garbage-collected allocator. 7 */ 8 struct GCAllocator 9 { 10 import core.memory : GC; 11 import std.typecons : Ternary; 12 @system unittest { testAllocator!(() => GCAllocator.instance); } 13 14 /** 15 The alignment is a static constant equal to $(D platformAlignment), which 16 ensures proper alignment for any D data type. 17 */ 18 enum uint alignment = platformAlignment; 19 20 /** 21 Standard allocator methods per the semantics defined above. The $(D 22 deallocate) and $(D reallocate) methods are $(D @system) because they may 23 move memory around, leaving dangling pointers in user code. 24 */ allocateGCAllocator25 pure nothrow @trusted void[] allocate(size_t bytes) shared 26 { 27 if (!bytes) return null; 28 auto p = GC.malloc(bytes); 29 return p ? p[0 .. bytes] : null; 30 } 31 32 /// Ditto expandGCAllocator33 @system bool expand(ref void[] b, size_t delta) shared 34 { 35 if (delta == 0) return true; 36 if (b is null) return false; 37 immutable curLength = GC.sizeOf(b.ptr); 38 assert(curLength != 0); // we have a valid GC pointer here 39 immutable desired = b.length + delta; 40 if (desired > curLength) // check to see if the current block can't hold the data 41 { 42 immutable sizeRequest = desired - curLength; 43 immutable newSize = GC.extend(b.ptr, sizeRequest, sizeRequest); 44 if (newSize == 0) 45 { 46 // expansion unsuccessful 47 return false; 48 } 49 assert(newSize >= desired); 50 } 51 b = b.ptr[0 .. desired]; 52 return true; 53 } 54 55 /// Ditto reallocateGCAllocator56 pure nothrow @system bool reallocate(ref void[] b, size_t newSize) shared 57 { 58 import core.exception : OutOfMemoryError; 59 try 60 { 61 auto p = cast(ubyte*) GC.realloc(b.ptr, newSize); 62 b = p[0 .. newSize]; 63 } 64 catch (OutOfMemoryError) 65 { 66 // leave the block in place, tell caller 67 return false; 68 } 69 return true; 70 } 71 72 /// Ditto 73 pure nothrow resolveInternalPointerGCAllocator74 Ternary resolveInternalPointer(const void* p, ref void[] result) shared 75 { 76 auto r = GC.addrOf(cast(void*) p); 77 if (!r) return Ternary.no; 78 result = r[0 .. GC.sizeOf(r)]; 79 return Ternary.yes; 80 } 81 82 /// Ditto deallocateGCAllocator83 pure nothrow @system bool deallocate(void[] b) shared 84 { 85 GC.free(b.ptr); 86 return true; 87 } 88 89 /// Ditto goodAllocSizeGCAllocator90 size_t goodAllocSize(size_t n) shared 91 { 92 if (n == 0) 93 return 0; 94 if (n <= 16) 95 return 16; 96 97 import core.bitop : bsr; 98 99 auto largestBit = bsr(n-1) + 1; 100 if (largestBit <= 12) // 4096 or less 101 return size_t(1) << largestBit; 102 103 // larger, we use a multiple of 4096. 104 return ((n + 4095) / 4096) * 4096; 105 } 106 107 /** 108 Returns the global instance of this allocator type. The garbage collected 109 allocator is thread-safe, therefore all of its methods and `instance` itself 110 are $(D shared). 111 */ 112 113 static shared GCAllocator instance; 114 115 // Leave it undocummented for now. collectGCAllocator116 nothrow @trusted void collect() shared 117 { 118 GC.collect(); 119 } 120 } 121 122 /// 123 @system unittest 124 { 125 auto buffer = GCAllocator.instance.allocate(1024 * 1024 * 4); 126 // deallocate upon scope's end (alternatively: leave it to collection) 127 scope(exit) GCAllocator.instance.deallocate(buffer); 128 //... 129 } 130 131 @system unittest 132 { 133 auto b = GCAllocator.instance.allocate(10_000); 134 assert(GCAllocator.instance.expand(b, 1)); 135 } 136 137 @system unittest 138 { 139 import core.memory : GC; 140 import std.typecons : Ternary; 141 142 // test allocation sizes 143 assert(GCAllocator.instance.goodAllocSize(1) == 16); 144 for (size_t s = 16; s <= 8192; s *= 2) 145 { 146 assert(GCAllocator.instance.goodAllocSize(s) == s); 147 assert(GCAllocator.instance.goodAllocSize(s - (s / 2) + 1) == s); 148 149 auto buffer = GCAllocator.instance.allocate(s); 150 scope(exit) GCAllocator.instance.deallocate(buffer); 151 152 void[] p; 153 assert(GCAllocator.instance.resolveInternalPointer(null, p) == Ternary.no); 154 Ternary r = GCAllocator.instance.resolveInternalPointer(buffer.ptr, p); 155 assert(p.ptr is buffer.ptr && p.length >= buffer.length); 156 157 assert(GC.sizeOf(buffer.ptr) == s); 158 159 auto buffer2 = GCAllocator.instance.allocate(s - (s / 2) + 1); 160 scope(exit) GCAllocator.instance.deallocate(buffer2); 161 162 assert(GC.sizeOf(buffer2.ptr) == s); 163 } 164 165 // anything above a page is simply rounded up to next page 166 assert(GCAllocator.instance.goodAllocSize(4096 * 4 + 1) == 4096 * 5); 167 } 168