-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vitamin
committed
Aug 7, 2021
1 parent
8ea3128
commit d2a5771
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |