disposeMultidimensionalArray

Destroys and then deallocates a multidimensional array, assuming it was created with makeMultidimensionalArray and the same allocator was used.

void
disposeMultidimensionalArray
(
T
Allocator
)
(
auto ref Allocator alloc
,
auto ref T[] array
)

Parameters

T

element type of an element of the multidimensional array

alloc Allocator

the allocator used for getting memory

array T[]

the multidimensional array that is to be deallocated

Examples

1 struct TestAllocator
2 {
3     import std.experimental.allocator.common : platformAlignment;
4     import std.experimental.allocator.mallocator : Mallocator;
5 
6     alias allocator = Mallocator.instance;
7 
8     private static struct ByteRange
9     {
10         void* ptr;
11         size_t length;
12     }
13 
14     private ByteRange[] _allocations;
15 
16     enum uint alignment = platformAlignment;
17 
18     void[] allocate(size_t numBytes)
19     {
20          auto ret = allocator.allocate(numBytes);
21          _allocations ~= ByteRange(ret.ptr, ret.length);
22          return ret;
23     }
24 
25     bool deallocate(void[] bytes)
26     {
27         import std.algorithm.mutation : remove;
28         import std.algorithm.searching : canFind;
29 
30         bool pred(ByteRange other)
31         { return other.ptr == bytes.ptr && other.length == bytes.length; }
32 
33         assert(_allocations.canFind!pred);
34 
35          _allocations = _allocations.remove!pred;
36          return allocator.deallocate(bytes);
37     }
38 
39     ~this()
40     {
41         assert(!_allocations.length);
42     }
43 }
44 
45 TestAllocator allocator;
46 
47 auto mArray = allocator.makeMultidimensionalArray!int(2, 3, 5, 6, 7, 2);
48 
49 allocator.disposeMultidimensionalArray(mArray);

Meta

Suggestion Box / Bug Report