diff --git a/src/basic_string/internal/mallocator.d b/src/basic_string/internal/mallocator.d new file mode 100644 index 0000000..ab33744 --- /dev/null +++ b/src/basic_string/internal/mallocator.d @@ -0,0 +1,46 @@ +module basic_string.internal.mallocator; + + + +//mallocator: +version(D_BetterC){ + public struct Mallocator{ + import std.experimental.allocator.common : platformAlignment; + + enum uint alignment = platformAlignment; + + static void[] allocate(size_t bytes)@trusted @nogc nothrow pure{ + import core.memory : pureMalloc; + if (!bytes) return null; + auto p = pureMalloc(bytes); + return p ? p[0 .. bytes] : null; + } + + static bool deallocate(void[] b)@system @nogc nothrow pure{ + import core.memory : pureFree; + pureFree(b.ptr); + return true; + } + + static bool reallocate(ref void[] b, size_t s)@system @nogc nothrow pure{ + import core.memory : pureRealloc; + if (!s){ + // fuzzy area in the C standard, see http://goo.gl/ZpWeSE + // so just deallocate and nullify the pointer + deallocate(b); + b = null; + return true; + } + + auto p = cast(ubyte*) pureRealloc(b.ptr, s); + if (!p) return false; + b = p[0 .. s]; + return true; + } + + static Mallocator instance; + } +} +else{ + public import std.experimental.allocator.mallocator : Mallocator; +}