-
Notifications
You must be signed in to change notification settings - Fork 15
/
allocator.d
65 lines (54 loc) · 1.47 KB
/
allocator.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
Custom versions of std.experimental.allocator functions (unfortunately)
*/
module automem.allocator;
import automem.utils: destruct;
/**
Destroys and then deallocates (using $(D alloc)) the object pointed to by a
pointer, the class object referred to by a $(D class) or $(D interface)
reference, or an entire array. It is assumed the respective entities had been
allocated with the same allocator.
*/
void dispose(A, T)(auto ref A alloc, T* p)
{
import std.traits: hasElaborateDestructor;
static if (hasElaborateDestructor!T)
{
destruct(*p);
}
alloc.deallocate((cast(void*) p)[0 .. T.sizeof]);
}
/// Ditto
void dispose(A, T)(auto ref A alloc, T p)
if (is(T == class) || is(T == interface))
{
if (!p) return;
static if (is(T == interface))
{
version(Windows)
{
import core.sys.windows.unknwn : IUnknown;
static assert(!is(T: IUnknown), "COM interfaces can't be destroyed in "
~ __PRETTY_FUNCTION__);
}
auto ob = cast(Object) p;
}
else
alias ob = p;
auto support = (cast(void*) ob)[0 .. typeid(ob).initializer.length];
destruct(p);
alloc.deallocate(support);
}
/// Ditto
void dispose(A, T)(auto ref A alloc, T[] array)
{
import std.traits: hasElaborateDestructor;
static if (hasElaborateDestructor!(typeof(array[0])))
{
foreach (ref e; array)
{
destruct(e);
}
}
alloc.deallocate(array);
}