stdx.allocator.building_blocks.affix_allocator 0/0(100%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
/// module stdx.allocator.building_blocks.affix_allocator; /** Allocator that adds some extra data before (of type $(D Prefix)) and/or after (of type $(D Suffix)) any allocation made with its parent allocator. This is useful for uses where additional allocation-related information is needed, such as mutexes, reference counts, or walls for debugging memory corruption errors. If $(D Prefix) is not $(D void), $(D Allocator) must guarantee an alignment at least as large as $(D Prefix.alignof). Suffixes are slower to get at because of alignment rounding, so prefixes should be preferred. However, small prefixes blunt the alignment so if a large alignment with a small affix is needed, suffixes should be chosen. The following methods are defined if $(D Allocator) defines them, and forward to it: $(D deallocateAll), $(D empty), $(D owns). */ struct AffixAllocator(Allocator, Prefix, Suffix = void) { import std.algorithm.comparison : min; import std.conv : emplace; import stdx.allocator : IAllocator, theAllocator; import stdx.allocator.common : stateSize, forwardToMember, roundUpToMultipleOf, alignedAt, alignDownTo, roundUpToMultipleOf, hasStaticallyKnownAlignment; import stdx.allocator.internal : isPowerOf2; import std.traits : hasMember; import stdx.allocator.internal : Ternary; static if (hasStaticallyKnownAlignment!Allocator) { static assert( !stateSize!Prefix || Allocator.alignment >= Prefix.alignof, "AffixAllocator does not work with allocators offering a smaller" ~ " alignment than the prefix alignment."); } static assert(alignment % Suffix.alignof == 0, "This restriction could be relaxed in the future."); /** If $(D Prefix) is $(D void), the alignment is that of the parent. Otherwise, the alignment is the same as the $(D Prefix)'s alignment. */ static if (hasStaticallyKnownAlignment!Allocator) { enum uint alignment = isPowerOf2(stateSize!Prefix) ? min(stateSize!Prefix, Allocator.alignment) : (stateSize!Prefix ? Prefix.alignof : Allocator.alignment); } else static if (is(Prefix == void)) { enum uint alignment = platformAlignment; } else { enum uint alignment = Prefix.alignof; } /** If the parent allocator $(D Allocator) is stateful, an instance of it is stored as a member. Otherwise, $(D AffixAllocator) uses `Allocator.instance`. In either case, the name $(D _parent) is uniformly used for accessing the parent allocator. */ static if (stateSize!Allocator) { Allocator _parent; static if (is(Allocator == IAllocator)) { Allocator parent() { if (_parent is null) _parent = theAllocator; assert(alignment <= _parent.alignment); return _parent; } } else { alias parent = _parent; } } else { alias parent = Allocator.instance; } private template Impl() { size_t goodAllocSize(size_t s) { import stdx.allocator.common : goodAllocSize; auto a = actualAllocationSize(s); return roundUpToMultipleOf(parent.goodAllocSize(a) - stateSize!Prefix - stateSize!Suffix, this.alignment); } private size_t actualAllocationSize(size_t s) const { assert(s > 0); static if (!stateSize!Suffix) { return s + stateSize!Prefix; } else { return roundUpToMultipleOf(s + stateSize!Prefix, Suffix.alignof) + stateSize!Suffix; } } private void[] actualAllocation(void[] b) const { assert(b !is null); return (b.ptr - stateSize!Prefix) [0 .. actualAllocationSize(b.length)]; } void[] allocate(size_t bytes) { if (!bytes) return null; auto result = parent.allocate(actualAllocationSize(bytes)); if (result is null) return null; static if (stateSize!Prefix) { assert(result.ptr.alignedAt(Prefix.alignof)); emplace!Prefix(cast(Prefix*) result.ptr); } static if (stateSize!Suffix) { auto suffixP = result.ptr + result.length - Suffix.sizeof; assert(suffixP.alignedAt(Suffix.alignof)); emplace!Suffix(cast(Suffix*)(suffixP)); } return result[stateSize!Prefix .. stateSize!Prefix + bytes]; } static if (hasMember!(Allocator, "allocateAll")) void[] allocateAll() { auto result = parent.allocateAll(); if (result is null) return null; if (result.length < actualAllocationSize(1)) { deallocate(result); return null; } static if (stateSize!Prefix) { assert(result.length > stateSize!Prefix); emplace!Prefix(cast(Prefix*) result.ptr); result = result[stateSize!Prefix .. $]; } static if (stateSize!Suffix) { assert(result.length > stateSize!Suffix); // Ehm, find a properly aligned place for the suffix auto p = (result.ptr + result.length - stateSize!Suffix) .alignDownTo(Suffix.alignof); assert(p > result.ptr); emplace!Suffix(cast(Suffix*) p); result = result[0 .. p - result.ptr]; } return result; } static if (hasMember!(Allocator, "owns")) Ternary owns(void[] b) { if (b is null) return Ternary.no; return parent.owns(actualAllocation(b)); } static if (hasMember!(Allocator, "resolveInternalPointer")) Ternary resolveInternalPointer(const void* p, ref void[] result) { void[] p1; Ternary r = parent.resolveInternalPointer(p, p1); if (r != Ternary.yes || p1 is null) return r; p1 = p1[stateSize!Prefix .. $]; auto p2 = (p1.ptr + p1.length - stateSize!Suffix) .alignDownTo(Suffix.alignof); result = p1[0 .. p2 - p1.ptr]; return Ternary.yes; } static if (!stateSize!Suffix && hasMember!(Allocator, "expand")) bool expand(ref void[] b, size_t delta) { if (!b.ptr) return delta == 0; auto t = actualAllocation(b); const result = parent.expand(t, delta); if (!result) return false; b = b.ptr[0 .. b.length + delta]; return true; } static if (hasMember!(Allocator, "reallocate")) bool reallocate(ref void[] b, size_t s) { if (b is null) { b = allocate(s); return b.length == s; } auto t = actualAllocation(b); const result = parent.reallocate(t, actualAllocationSize(s)); if (!result) return false; // no harm done b = t.ptr[stateSize!Prefix .. stateSize!Prefix + s]; return true; } static if (hasMember!(Allocator, "deallocate")) bool deallocate(void[] b) { if (!b.ptr) return true; return parent.deallocate(actualAllocation(b)); } /* The following methods are defined if $(D ParentAllocator) defines them, and forward to it: $(D deallocateAll), $(D empty).*/ mixin(forwardToMember("parent", "deallocateAll", "empty")); // Computes suffix type given buffer type private template Payload2Affix(Payload, Affix) { static if (is(Payload[] : void[])) alias Payload2Affix = Affix; else static if (is(Payload[] : shared(void)[])) alias Payload2Affix = shared Affix; else static if (is(Payload[] : immutable(void)[])) alias Payload2Affix = shared Affix; else static if (is(Payload[] : const(shared(void))[])) alias Payload2Affix = shared Affix; else static if (is(Payload[] : const(void)[])) alias Payload2Affix = const Affix; else static assert(0, "Internal error for type " ~ Payload.stringof); } // Extra functions static if (stateSize!Prefix) { static auto ref prefix(T)(T[] b) { assert(b.ptr && b.ptr.alignedAt(Prefix.alignof)); return (cast(Payload2Affix!(T, Prefix)*) b.ptr)[-1]; } } static if (stateSize!Suffix) auto ref suffix(T)(T[] b) { assert(b.ptr); auto p = b.ptr - stateSize!Prefix + actualAllocationSize(b.length); assert(p && p.alignedAt(Suffix.alignof)); return (cast(Payload2Affix!(T, Suffix)*) p)[-1]; } } version (StdDdoc) { /** Standard allocator methods. Each is defined if and only if the parent allocator defines the homonym method (except for $(D goodAllocSize), which may use the global default). Also, the methods will be $(D shared) if the parent allocator defines them as such. */ size_t goodAllocSize(size_t); /// Ditto void[] allocate(size_t); /// Ditto Ternary owns(void[]); /// Ditto bool expand(ref void[] b, size_t delta); /// Ditto bool reallocate(ref void[] b, size_t s); /// Ditto bool deallocate(void[] b); /// Ditto bool deallocateAll(); /// Ditto Ternary empty(); /** The `instance` singleton is defined if and only if the parent allocator has no state and defines its own `it` object. */ static AffixAllocator instance; /** Affix access functions offering references to the affixes of a block `b` previously allocated with this allocator. `b` may not be null. They are defined if and only if the corresponding affix is not `void`. The qualifiers of the affix are not always the same as the qualifiers of the argument. This is because the affixes are not part of the data itself, but instead are just $(I associated) with the data and known to the allocator. The table below documents the type of `preffix(b)` and `affix(b)` depending on the type of `b`. $(BOOKTABLE Result of `prefix`/`suffix` depending on argument (`U` is any unqualified type, `Affix` is `Prefix` or `Suffix`), $(TR $(TH Argument$(NBSP)Type) $(TH Return) $(TH Comments)) $(TR $(TD `shared(U)[]`) $(TD `ref shared Affix`) $(TD Data is shared across threads and the affix follows suit.)) $(TR $(TD `immutable(U)[]`) $(TD `ref shared Affix`) $(TD Although the data is immutable, the allocator "knows" the underlying memory is mutable, so `immutable` is elided for the affix which is independent from the data itself. However, the result is `shared` because `immutable` is implicitly shareable so multiple threads may access and manipulate the affix for the same data.)) $(TR $(TD `const(shared(U))[]`) $(TD `ref shared Affix`) $(TD The data is always shareable across threads. Even if the data is `const`, the affix is modifiable by the same reasoning as for `immutable`.)) $(TR $(TD `const(U)[]`) $(TD `ref const Affix`) $(TD The input may have originated from `U[]` or `immutable(U)[]`, so it may be actually shared or not. Returning an unqualified affix may result in race conditions, whereas returning a `shared` affix may result in inadvertent sharing of mutable thread-local data across multiple threads. So the returned type is conservatively `ref const`.)) $(TR $(TD `U[]`) $(TD `ref Affix`) $(TD Unqualified data has unqualified affixes.)) ) Precondition: `b !is null` and `b` must have been allocated with this allocator. */ static ref auto prefix(T)(T[] b); /// Ditto ref auto suffix(T)(T[] b); } else static if (is(typeof(Allocator.instance) == shared)) { static shared AffixAllocator instance; shared { mixin Impl!(); } } else { mixin Impl!(); static if (stateSize!Allocator == 0) static __gshared AffixAllocator instance; } } /// @system unittest { import stdx.allocator.mallocator : Mallocator; // One word before and after each allocation. alias A = AffixAllocator!(Mallocator, size_t, size_t); auto b = A.instance.allocate(11); A.instance.prefix(b) = 0xCAFE_BABE; A.instance.suffix(b) = 0xDEAD_BEEF; assert(A.instance.prefix(b) == 0xCAFE_BABE && A.instance.suffix(b) == 0xDEAD_BEEF); } @system unittest { import stdx.allocator.gc_allocator : GCAllocator; import stdx.allocator : theAllocator, IAllocator; // One word before and after each allocation. auto A = AffixAllocator!(IAllocator, size_t, size_t)(theAllocator); auto a = A.allocate(11); A.prefix(a) = 0xCAFE_BABE; A.suffix(a) = 0xDEAD_BEEF; assert(A.prefix(a) == 0xCAFE_BABE && A.suffix(a) == 0xDEAD_BEEF); // One word before and after each allocation. auto B = AffixAllocator!(IAllocator, size_t, size_t)(); auto b = B.allocate(11); B.prefix(b) = 0xCAFE_BABE; B.suffix(b) = 0xDEAD_BEEF; assert(B.prefix(b) == 0xCAFE_BABE && B.suffix(b) == 0xDEAD_BEEF); } @system unittest { import stdx.allocator.building_blocks.bitmapped_block : BitmappedBlock; import stdx.allocator.common : testAllocator; testAllocator!({ auto a = AffixAllocator!(BitmappedBlock!128, ulong, ulong) (BitmappedBlock!128(new ubyte[128 * 4096])); return a; }); } @system unittest { import stdx.allocator.mallocator : Mallocator; alias A = AffixAllocator!(Mallocator, size_t); auto b = A.instance.allocate(10); A.instance.prefix(b) = 10; assert(A.instance.prefix(b) == 10); import stdx.allocator.building_blocks.null_allocator : NullAllocator; alias B = AffixAllocator!(NullAllocator, size_t); b = B.instance.allocate(100); assert(b is null); } @system unittest { import stdx.allocator; import stdx.allocator.gc_allocator; import stdx.allocator.internal : Ternary; alias MyAllocator = AffixAllocator!(GCAllocator, uint); auto a = MyAllocator.instance.makeArray!(shared int)(100); static assert(is(typeof(&MyAllocator.instance.prefix(a)) == shared(uint)*)); auto b = MyAllocator.instance.makeArray!(shared const int)(100); static assert(is(typeof(&MyAllocator.instance.prefix(b)) == shared(uint)*)); auto c = MyAllocator.instance.makeArray!(immutable int)(100); static assert(is(typeof(&MyAllocator.instance.prefix(c)) == shared(uint)*)); auto d = MyAllocator.instance.makeArray!(int)(100); static assert(is(typeof(&MyAllocator.instance.prefix(d)) == uint*)); auto e = MyAllocator.instance.makeArray!(const int)(100); static assert(is(typeof(&MyAllocator.instance.prefix(e)) == const(uint)*)); void[] p; assert(MyAllocator.instance.resolveInternalPointer(null, p) == Ternary.no); Ternary r = MyAllocator.instance.resolveInternalPointer(d.ptr, p); assert(p.ptr is d.ptr && p.length >= d.length); }