1 module Dgame.Internal.Allocator; 2 3 private import cstd = core.stdc.stdlib; 4 5 T* make(T)(ref T* p, size_t count = 1) 6 if (!is(T : U[], U)) 7 in { 8 assert(p is null); 9 assert(count > 0 && count < int.max); 10 } body { 11 p = cast(T*) cstd.calloc(count, T.sizeof); 12 return p; 13 } 14 15 T* make(T)(auto ref T value, ref T* p, size_t count = 1) 16 if (!is(T : U[], U)) 17 in { 18 assert(p is null); 19 assert(count > 0 && count < short.max); 20 } body { 21 p = cast(T*) cstd.malloc(count * T.sizeof); 22 23 for (size_t i = 0; i < count; i++) { 24 p[i] = value; 25 } 26 27 return p; 28 } 29 30 T* alloc_new(T = void)(size_t count) 31 if (!is(T : U[], U)) 32 in { 33 assert(count > 0); 34 } body { 35 T* p; 36 return make(p, count); 37 } 38 39 T* make_new(T = void)(auto ref T value, size_t count = 1) 40 if (!is(T : U[], U)) 41 in { 42 assert(count > 0); 43 } body { 44 T* p = alloc_new!(T)(count); 45 *p = value; 46 47 return p; 48 } 49 50 void unmake(T)(ref T* p) 51 if (!is(T : U[], U)) 52 { 53 static if (!is(T == void)) 54 .destroy!T(*p); 55 cstd.free(p); 56 p = null; 57 } 58 59 T* remake(T)(ref T* p, size_t cap) 60 if (!is(T : U[], U)) 61 in { 62 assert(p !is null); 63 assert(cap > 0); 64 } body { 65 p = cast(T*) cstd.realloc(p, cap * T.sizeof); 66 67 return p; 68 }