/
scoped_allocator.d
288 lines (256 loc) · 7.51 KB
/
scoped_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Written in the D programming language.
/**
Source: $(PHOBOSSRC std/experimental/allocator/building_blocks/scoped_allocator.d)
*/
module std.experimental.allocator.building_blocks.scoped_allocator;
import std.experimental.allocator.common;
/**
`ScopedAllocator` delegates all allocation requests to `ParentAllocator`.
When destroyed, the `ScopedAllocator` object automatically calls $(D
deallocate) for all memory allocated through its lifetime. (The $(D
deallocateAll) function is also implemented with the same semantics.)
`deallocate` is also supported, which is where most implementation effort
and overhead of `ScopedAllocator` go. If `deallocate` is not needed, a
simpler design combining `AllocatorList` with `Region` is recommended.
*/
struct ScopedAllocator(ParentAllocator)
{
static if (!stateSize!ParentAllocator)
{
// This test is available only for stateless allocators
@system unittest
{
testAllocator!(() => ScopedAllocator());
}
}
import std.experimental.allocator.building_blocks.affix_allocator
: AffixAllocator;
import std.traits : hasMember;
import std.typecons : Ternary;
private struct Node
{
Node* prev;
Node* next;
size_t length;
}
alias Allocator = AffixAllocator!(ParentAllocator, Node);
// state
/**
If `ParentAllocator` is stateful, `parent` is a property giving access
to an `AffixAllocator!ParentAllocator`. Otherwise, `parent` is an alias for `AffixAllocator!ParentAllocator.instance`.
*/
static if (stateSize!ParentAllocator)
{
Allocator parent;
}
else
{
alias parent = Allocator.instance;
}
private Node* root;
/**
`ScopedAllocator` is not copyable.
*/
@disable this(this);
/**
`ScopedAllocator`'s destructor releases all memory allocated during its
lifetime.
*/
~this()
{
deallocateAll;
}
/// Alignment offered
enum alignment = Allocator.alignment;
/**
Forwards to `parent.goodAllocSize` (which accounts for the management
overhead).
*/
size_t goodAllocSize(size_t n)
{
return parent.goodAllocSize(n);
}
/**
Allocates memory. For management it actually allocates extra memory from
the parent.
*/
void[] allocate(size_t n)
{
auto b = parent.allocate(n);
if (!b.ptr) return b;
Node* toInsert = & parent.prefix(b);
toInsert.prev = null;
toInsert.next = root;
toInsert.length = n;
assert(!root || !root.prev);
if (root) root.prev = toInsert;
root = toInsert;
return b;
}
/**
Forwards to $(D parent.expand(b, delta)).
*/
static if (hasMember!(Allocator, "expand"))
bool expand(ref void[] b, size_t delta)
{
auto result = parent.expand(b, delta);
if (result && b)
{
() @trusted { parent.prefix(b).length = b.length; }();
}
return result;
}
/**
Reallocates `b` to new size `s`.
*/
bool reallocate(ref void[] b, size_t s)
{
// Remove from list
if (b.ptr)
{
Node* n = & parent.prefix(b);
if (n.prev) n.prev.next = n.next;
else root = n.next;
if (n.next) n.next.prev = n.prev;
}
auto result = parent.reallocate(b, s);
// Add back to list
if (b.ptr)
{
Node* n = & parent.prefix(b);
n.prev = null;
n.next = root;
n.length = s;
if (root) root.prev = n;
root = n;
}
return result;
}
/**
Forwards to `parent.owns(b)`.
*/
static if (hasMember!(Allocator, "owns"))
Ternary owns(void[] b)
{
return parent.owns(b);
}
/**
Deallocates `b`.
*/
static if (hasMember!(Allocator, "deallocate"))
bool deallocate(void[] b)
{
// Remove from list
if (b.ptr)
{
Node* n = & parent.prefix(b);
if (n.prev) n.prev.next = n.next;
else root = n.next;
if (n.next) n.next.prev = n.prev;
}
return parent.deallocate(b);
}
/**
Deallocates all memory allocated.
*/
bool deallocateAll()
{
bool result = true;
for (auto n = root; n; )
{
void* p = n + 1;
auto length = n.length;
n = n.next;
if (!parent.deallocate(p[0 .. length]))
result = false;
}
root = null;
return result;
}
/**
Returns `Ternary.yes` if this allocator is not responsible for any memory,
`Ternary.no` otherwise. (Never returns `Ternary.unknown`.)
*/
pure nothrow @safe @nogc
Ternary empty() const
{
return Ternary(root is null);
}
}
///
@system unittest
{
import std.experimental.allocator.mallocator : Mallocator;
import std.typecons : Ternary;
ScopedAllocator!Mallocator alloc;
assert(alloc.empty == Ternary.yes);
const b = alloc.allocate(10);
assert(b.length == 10);
assert(alloc.empty == Ternary.no);
}
@system unittest
{
import std.experimental.allocator.gc_allocator : GCAllocator;
testAllocator!(() => ScopedAllocator!GCAllocator());
}
@system unittest // https://issues.dlang.org/show_bug.cgi?id=16046
{
import std.exception;
import std.experimental.allocator;
import std.experimental.allocator.mallocator;
ScopedAllocator!Mallocator alloc;
auto foo = alloc.make!int(1).enforce;
auto bar = alloc.make!int(2).enforce;
alloc.dispose(foo);
alloc.dispose(bar); // segfault here
}
@system unittest
{
import std.experimental.allocator.gc_allocator : GCAllocator;
ScopedAllocator!GCAllocator a;
assert(__traits(compiles, (() nothrow @safe @nogc => a.goodAllocSize(0))()));
// Ensure deallocate inherits from parent allocators
auto b = a.allocate(42);
assert(b.length == 42);
() nothrow @nogc { a.deallocate(b); }();
}
// Test that deallocateAll infers from parent
@system unittest
{
import std.experimental.allocator.building_blocks.region : Region;
ScopedAllocator!(Region!()) a;
a.parent.parent = Region!()(new ubyte[1024 * 64]);
auto b = a.allocate(42);
assert(b.length == 42);
assert((() pure nothrow @safe @nogc => a.expand(b, 22))());
assert(b.length == 64);
assert((() nothrow @nogc => a.reallocate(b, 100))());
assert(b.length == 100);
assert((() nothrow @nogc => a.deallocateAll())());
}
@system unittest
{
import std.experimental.allocator.building_blocks.region : Region;
import std.experimental.allocator.mallocator : Mallocator;
import std.typecons : Ternary;
auto a = Region!(Mallocator)(1024 * 64);
auto b = a.allocate(42);
assert(b.length == 42);
assert((() pure nothrow @safe @nogc => a.expand(b, 22))());
assert(b.length == 64);
assert((() pure nothrow @safe @nogc => a.owns(b))() == Ternary.yes);
assert((() nothrow @nogc => a.reallocate(b, 100))());
assert(b.length == 100);
assert((() pure nothrow @safe @nogc => a.owns(b))() == Ternary.yes);
assert((() pure nothrow @safe @nogc => a.owns(null))() == Ternary.no);
}
// Test empty
@system unittest
{
import std.experimental.allocator.mallocator : Mallocator;
import std.typecons : Ternary;
ScopedAllocator!Mallocator alloc;
assert((() pure nothrow @safe @nogc => alloc.empty)() == Ternary.yes);
const b = alloc.allocate(10);
assert((() pure nothrow @safe @nogc => alloc.empty)() == Ternary.no);
}