Skip to content

Commit

Permalink
add mallocator module
Browse files Browse the repository at this point in the history
  • Loading branch information
vitamin committed Aug 7, 2021
1 parent 8ea3128 commit d2a5771
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/basic_string/internal/mallocator.d
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit d2a5771

Please sign in to comment.