]> git.ipfire.org Git - thirdparty/gcc.git/blame - libphobos/libdruntime/core/internal/container/common.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / libphobos / libdruntime / core / internal / container / common.d
CommitLineData
b4c522fa
IB
1/**
2 * Common code for writing containers.
3 *
4 * Copyright: Copyright Martin Nowak 2013.
5fee5ec3 5 * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
b4c522fa
IB
6 * Authors: Martin Nowak
7 */
5fee5ec3 8module core.internal.container.common;
b4c522fa
IB
9
10import core.stdc.stdlib : malloc, realloc;
11public import core.stdc.stdlib : free;
12import core.internal.traits : dtorIsNothrow;
13nothrow:
14
15void* 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); onOutOfMemoryErrorNoGC();
22 assert(0);
23}
24
25void* xmalloc(size_t sz) nothrow @nogc
26{
27 import core.exception;
28 if (auto nptr = .malloc(sz))
29 return nptr;
30 onOutOfMemoryErrorNoGC();
31 assert(0);
32}
33
34void 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
40void destroy(T)(ref T t) if (!is(T == struct))
41{
42 t = T.init;
43}
44
45void initialize(T)(ref T t) if (is(T == struct))
46{
5fee5ec3
IB
47 import core.internal.lifetime : emplaceInitializer;
48 emplaceInitializer(t);
b4c522fa
IB
49}
50
51void initialize(T)(ref T t) if (!is(T == struct))
52{
53 t = T.init;
54}
55
5fee5ec3 56version (CoreUnittest) struct RC()
b4c522fa
IB
57{
58nothrow:
59 this(size_t* cnt) { ++*(_cnt = cnt); }
60 ~this() { if (_cnt) --*_cnt; }
61 this(this) { if (_cnt) ++*_cnt; }
62 size_t* _cnt;
63}