1 // Written in the D programming language.
2 /**
3 Source: $(PHOBOSSRC std/experimental/allocator/building_blocks/null_allocator.d)
4 */
5 module std.experimental.allocator.building_blocks.null_allocator;
6 
7 /**
8 `NullAllocator` is an emphatically empty implementation of the allocator
9 interface. Although it has no direct use, it is useful as a "terminator" in
10 composite allocators.
11 */
12 struct NullAllocator
13 {
14     import std.typecons : Ternary;
15 
16     nothrow @nogc pure @safe:
17     /**
18     `NullAllocator` advertises a relatively large _alignment equal to 64 KB.
19     This is because `NullAllocator` never actually needs to honor this
20     alignment and because composite allocators using `NullAllocator`
21     shouldn't be unnecessarily constrained.
22     */
23     enum uint alignment = 64 * 1024;
24     // /// Returns `n`.
25     //size_t goodAllocSize(size_t n) shared const
26     //{ return .goodAllocSize(this, n); }
27     /// Always returns `null`.
28     void[] allocate(size_t) shared { return null; }
29     /// Always returns `null`.
30     void[] alignedAllocate(size_t, uint) shared { return null; }
31     /// Always returns `null`.
32     void[] allocateAll() shared { return null; }
33     /**
34     These methods return `false`.
35     Precondition: $(D b is null). This is because there is no other possible
36     legitimate input.
37     */
38     bool expand(ref void[] b, size_t s) shared
39     { assert(b is null); return s == 0; }
40     /// Ditto
41     bool reallocate(ref void[] b, size_t) shared
42     { assert(b is null); return false; }
43     /// Ditto
44     bool alignedReallocate(ref void[] b, size_t, uint) shared
45     { assert(b is null); return false; }
46     /// Returns `Ternary.no`.
47     Ternary owns(const void[]) shared const { return Ternary.no; }
48     /**
49     Returns `Ternary.no`.
50     */
51     Ternary resolveInternalPointer(const void*, ref void[]) shared const
52     { return Ternary.no; }
53     /**
54     No-op.
55     Precondition: $(D b is null)
56     */
57     bool deallocate(void[] b) shared { assert(b is null); return true; }
58     /**
59     No-op.
60     */
61     bool deallocateAll() shared { return true; }
62     /**
63     Returns `Ternary.yes`.
64     */
65     Ternary empty() shared const { return Ternary.yes; }
66     /**
67     Returns the `shared` global instance of the `NullAllocator`.
68     */
69     static shared NullAllocator instance;
70 }
71 
72 nothrow @nogc pure @safe unittest
73 {
74     alias a = NullAllocator.instance;
75 
76     assert(a.alignedAllocate(100, 0) is null);
77     assert(a.allocateAll() is null);
78     auto b = a.allocate(100);
79     assert(b is null);
80     assert(a.expand(b, 0));
81     assert(!a.expand(b, 42));
82     assert(!a.reallocate(b, 42));
83     assert(!a.alignedReallocate(b, 42, 0));
84     assert(a.deallocate(b));
85     assert(a.deallocateAll());
86 
87     import std.typecons : Ternary;
88     assert(a.empty == Ternary.yes);
89     assert(a.owns(null) == Ternary.no);
90 
91     void[] p;
92     assert(a.resolveInternalPointer(null, p) == Ternary.no);
93 }