1 /* 2 License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). 3 Authors: Adam Búš 4 */ 5 module btl.internal.allocator; 6 7 8 public template isStatelessAllocator(T){ 9 import std.experimental.allocator.common : stateSize; 10 11 enum isStatelessAllocator = (stateSize!T == 0); 12 } 13 14 //alias to stateless allocator instance 15 public template statelessAllcoator(T){ 16 import std.experimental.allocator.common : stateSize; 17 import std.traits : hasStaticMember; 18 19 static assert(isStatelessAllocator!T); 20 21 static if(hasStaticMember!(T, "instance")) 22 alias statelessAllcoator = T.instance; 23 else 24 enum T statelessAllcoator = T.init; 25 } 26 27 28 //NullAllocator: 29 version(D_BetterC){ 30 public struct NullAllocator{ 31 import std.experimental.allocator.common : platformAlignment; 32 33 enum uint alignment = platformAlignment; 34 35 static void[] allocate(size_t bytes)@trusted @nogc nothrow pure{ 36 return null; 37 } 38 39 static bool deallocate(void[] b)@system @nogc nothrow pure{ 40 return false; 41 } 42 43 static bool reallocate(ref void[] b, size_t s)@system @nogc nothrow pure{ 44 return false; 45 } 46 47 static NullAllocator instance; 48 } 49 } 50 else{ 51 public import std.experimental.allocator.building_blocks.null_allocator : NullAllocator; 52 } 53 54 //Mallocator: 55 version(D_BetterC){ 56 public struct Mallocator{ 57 import std.experimental.allocator.common : platformAlignment; 58 59 enum uint alignment = platformAlignment; 60 61 static void[] allocate(size_t bytes)@trusted @nogc nothrow pure{ 62 import core.memory : pureMalloc; 63 if (!bytes) return null; 64 auto p = pureMalloc(bytes); 65 return p ? p[0 .. bytes] : null; 66 } 67 68 static bool deallocate(void[] b)@system @nogc nothrow pure{ 69 import core.memory : pureFree; 70 pureFree(b.ptr); 71 return true; 72 } 73 74 static bool reallocate(ref void[] b, size_t s)@system @nogc nothrow pure{ 75 import core.memory : pureRealloc; 76 if (!s){ 77 // fuzzy area in the C standard, see http://goo.gl/ZpWeSE 78 // so just deallocate and nullify the pointer 79 deallocate(b); 80 b = null; 81 return true; 82 } 83 84 auto p = cast(ubyte*) pureRealloc(b.ptr, s); 85 if (!p) return false; 86 b = p[0 .. s]; 87 return true; 88 } 89 90 static Mallocator instance; 91 } 92 } 93 else{ 94 public import std.experimental.allocator.mallocator : Mallocator; 95 } 96 97