| 1 |
/** |
| 2 |
* Common code for writing containers. |
| 3 |
* |
| 4 |
* Copyright: Copyright Martin Nowak 2013. |
| 5 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 6 |
* Authors: Martin Nowak |
| 7 |
*/ |
| 8 |
module core.internal.container.common; |
| 9 |
|
| 10 |
import core.stdc.stdlib : malloc, realloc; |
| 11 |
public import core.stdc.stdlib : free; |
| 12 |
import core.internal.traits : dtorIsNothrow; |
| 13 |
nothrow: |
| 14 |
|
| 15 |
void* xrealloc(void* ptr, size_t sz) nothrow @nogc |
| 16 |
{ |
| 17 |
import core.exception; |
| 18 |
|
| 19 |
if (!sz) { .free(ptr); return null; } |
| 20 |
if (auto nptr = .realloc(ptr, sz)) return nptr; |
| 21 |
.free(ptr); onOutOfMemoryError(); |
| 22 |
assert(0); |
| 23 |
} |
| 24 |
|
| 25 |
void* xmalloc(size_t sz) nothrow @nogc |
| 26 |
{ |
| 27 |
import core.exception; |
| 28 |
if (auto nptr = .malloc(sz)) |
| 29 |
return nptr; |
| 30 |
onOutOfMemoryError(); |
| 31 |
assert(0); |
| 32 |
} |
| 33 |
|
| 34 |
void destroy(T)(ref T t) if (is(T == struct) && dtorIsNothrow!T) |
| 35 |
{ |
| 36 |
scope (failure) assert(0); // nothrow hack |
| 37 |
object.destroy(t); |
| 38 |
} |
| 39 |
|
| 40 |
void destroy(T)(ref T t) if (!is(T == struct)) |
| 41 |
{ |
| 42 |
t = T.init; |
| 43 |
} |
| 44 |
|
| 45 |
void initialize(T)(ref T t) if (is(T == struct)) |
| 46 |
{ |
| 47 |
import core.internal.lifetime : emplaceInitializer; |
| 48 |
emplaceInitializer(t); |
| 49 |
} |
| 50 |
|
| 51 |
void initialize(T)(ref T t) if (!is(T == struct)) |
| 52 |
{ |
| 53 |
t = T.init; |
| 54 |
} |
| 55 |
|
| 56 |
version (CoreUnittest) struct RC() |
| 57 |
{ |
| 58 |
nothrow: |
| 59 |
this(size_t* cnt) { ++*(_cnt = cnt); } |
| 60 |
~this() { if (_cnt) --*_cnt; } |
| 61 |
this(this) { if (_cnt) ++*_cnt; } |
| 62 |
size_t* _cnt; |
| 63 |
} |