1 /* 2 License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). 3 Authors: Adam Búš 4 */ 5 module btl.internal.test_allocator; 6 7 import std.experimental.allocator.common : platformAlignment, stateSize; 8 9 import btl.traits.assume; 10 struct TestAllocator{ 11 static assert(stateSize!TestAllocator > 0); 12 13 public enum uint alignment = platformAlignment; 14 15 private static long static_count = 0; 16 private long count = 0; 17 18 private void increment()pure @safe @nogc nothrow{ 19 20 assumePure(function void(){ 21 static_count += 1; 22 })(); 23 24 count += 1; 25 } 26 27 private void decrement()pure @safe @nogc nothrow{ 28 assumePure(function void(){ 29 static_count -= 1; 30 })(); 31 count -= 1; 32 } 33 34 35 public long get_static_count()scope const nothrow @safe @nogc{ 36 return static_count; 37 } 38 public long get_count()scope const pure nothrow @safe @nogc{ 39 return count; 40 } 41 42 public void[] allocate(size_t bytes)pure @trusted @nogc nothrow{ 43 import core.memory : pureMalloc; 44 if (!bytes) return null; 45 auto p = pureMalloc(bytes); 46 47 if(p){ 48 this.increment(); 49 return p[0 .. bytes]; 50 } 51 return null; 52 } 53 54 public bool deallocate(void[] b)pure @system @nogc nothrow{ 55 import core.memory : pureFree; 56 pureFree(b.ptr); 57 this.decrement(); 58 return true; 59 } 60 61 public bool reallocate(ref void[] b, size_t s)pure @system @nogc nothrow{ 62 import core.memory : pureRealloc; 63 if (!s){ 64 // fuzzy area in the C standard, see http://goo.gl/ZpWeSE 65 // so just deallocate and nullify the pointer 66 deallocate(b); 67 b = null; 68 return true; 69 } 70 71 auto p = cast(ubyte*) pureRealloc(b.ptr, s); 72 if (!p) return false; 73 b = p[0 .. s]; 74 return true; 75 } 76 77 //static TestAllocator instance; 78 79 }