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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
// Written in the D programming language.
/**
Source: $(PHOBOSSRC std/experimental/allocator/building_blocks/quantizer.d)
*/
module std.experimental.allocator.building_blocks.quantizer;
import std.experimental.allocator.common;
/**
This allocator sits on top of `ParentAllocator` and quantizes allocation sizes,
usually from arbitrary positive numbers to a small set of round numbers (e.g.
powers of two, page sizes etc). This technique is commonly used to:
$(UL
$(LI Preallocate more memory than requested such that later on, when
reallocation is needed (e.g. to grow an array), expansion can be done quickly
in place. Reallocation to smaller sizes is also fast (in-place) when the new
size requested is within the same quantum as the existing size. Code that's
reallocation-heavy can therefore benefit from fronting a generic allocator with
a `Quantizer`. These advantages are present even if `ParentAllocator` does not
support reallocation at all.)
$(LI Improve behavior of allocators sensitive to allocation sizes, such as
`FreeList` and `FreeTree`. Rounding allocation requests up makes for smaller
free lists/trees at the cost of slack memory (internal fragmentation).)
)
The following methods are forwarded to the parent allocator if present:
`allocateAll`, `owns`, `deallocateAll`, `empty`.
Preconditions: `roundingFunction` must satisfy three constraints. These are
not enforced (save for the use of `assert`) for the sake of efficiency.
$(OL
$(LI $(D roundingFunction(n) >= n) for all `n` of type `size_t`;)
$(LI `roundingFunction` must be monotonically increasing, i.e. $(D
roundingFunction(n1) <= roundingFunction(n2)) for all $(D n1 < n2);)
$(LI `roundingFunction` must be `nothrow`, `@safe`, `@nogc` and `pure`, i.e.
always return the same value for a given `n`.)
)
*/
struct Quantizer(ParentAllocator, alias roundingFunction)
{
import std.traits : hasMember;
/**
The parent allocator. Depending on whether `ParentAllocator` holds state
or not, this is a member variable or an alias for
`ParentAllocator.instance`.
*/
static if (stateSize!ParentAllocator)
{
ParentAllocator parent;
}
else
{
alias parent = ParentAllocator.instance;
__gshared Quantizer instance;
}
/**
Returns `roundingFunction(n)`.
*/
size_t goodAllocSize(size_t n)
{
auto result = roundingFunction(n);
assert(result >= n);
return result;
}
/**
Alignment is identical to that of the parent.
*/
enum alignment = ParentAllocator.alignment;
/**
Gets a larger buffer `buf` by calling
`parent.allocate(goodAllocSize(n))`. If `buf` is `null`, returns
`null`. Otherwise, returns $(D buf[0 .. n]).
*/
void[] allocate(size_t n)
{
auto result = parent.allocate(goodAllocSize(n));
return result.ptr ? result.ptr[0 .. n] : null;
}
static if (hasMember!(ParentAllocator, "allocateZeroed"))
package(std) void[] allocateZeroed()(size_t n)
{
auto result = parent.allocateZeroed(goodAllocSize(n));
return result.ptr ? result.ptr[0 .. n] : null;
}
/**
Defined only if `parent.alignedAllocate` exists and works similarly to
`allocate` by forwarding to
$(D parent.alignedAllocate(goodAllocSize(n), a)).
*/
static if (hasMember!(ParentAllocator, "alignedAllocate"))
void[] alignedAllocate(size_t n, uint a)
{
auto result = parent.alignedAllocate(goodAllocSize(n), a);
return result.ptr ? result.ptr[0 .. n] : null;
}
/**
First checks whether there's enough slack memory preallocated for `b`
by evaluating $(D b.length + delta <= goodAllocSize(b.length)). If that's
the case, expands `b` in place. Otherwise, attempts to use
`parent.expand` appropriately if present.
*/
bool expand(ref void[] b, size_t delta)
{
if (!b || delta == 0) return delta == 0;
immutable allocated = goodAllocSize(b.length),
needed = b.length + delta,
neededAllocation = goodAllocSize(needed);
assert(b.length <= allocated);
assert(needed <= neededAllocation);
assert(allocated <= neededAllocation);
// Second test needed because expand must work for null pointers, too.
if (allocated == neededAllocation)
{
// Nice!
b = (() @trusted => b.ptr[0 .. needed])();
return true;
}
// Hail Mary
static if (hasMember!(ParentAllocator, "expand"))
{
// Expand to the appropriate quantum
auto original = (() @trusted => b.ptr[0 .. allocated])();
assert(goodAllocSize(needed) >= allocated);
if (!parent.expand(original, neededAllocation - allocated))
return false;
// Dial back the size
b = (() @trusted => original.ptr[0 .. needed])();
return true;
}
else
{
return false;
}
}
/**
Expands or shrinks allocated block to an allocated size of $(D
goodAllocSize(s)). Expansion occurs in place under the conditions required
by `expand`. Shrinking occurs in place if $(D goodAllocSize(b.length)
== goodAllocSize(s)).
*/
bool reallocate(ref void[] b, size_t s)
{
if (!b.ptr)
{
b = allocate(s);
return b.length == s;
}
if (s >= b.length && expand(b, s - b.length)) return true;
immutable toAllocate = goodAllocSize(s),
allocated = goodAllocSize(b.length);
// Are the lengths within the same quantum?
if (allocated == toAllocate)
{
// Reallocation (whether up or down) will be done in place
b = b.ptr[0 .. s];
return true;
}
// Defer to parent (or global) with quantized size
auto original = b.ptr[0 .. allocated];
if (!parent.reallocate(original, toAllocate)) return false;
b = original.ptr[0 .. s];
return true;
}
/**
Defined only if `ParentAllocator.alignedAllocate` exists. Expansion
occurs in place under the conditions required by `expand`. Shrinking
occurs in place if $(D goodAllocSize(b.length) == goodAllocSize(s)).
*/
static if (hasMember!(ParentAllocator, "alignedAllocate"))
bool alignedReallocate(ref void[] b, size_t s, uint a)
{
if (!b.ptr)
{
b = alignedAllocate(s, a);
return b.length == s;
}
if (s >= b.length && b.ptr.alignedAt(a) && expand(b, s - b.length)) return true;
immutable toAllocate = goodAllocSize(s),
allocated = goodAllocSize(b.length);
// Are the lengths within the same quantum?
if (allocated == toAllocate && b.ptr.alignedAt(a))
{
assert(b.ptr); // code above must have caught this
// Reallocation (whether up or down) will be done in place
b = b.ptr[0 .. s];
return true;
}
// Defer to parent (or global) with quantized size
auto original = b.ptr[0 .. allocated];
if (!parent.alignedReallocate(original, toAllocate, a)) return false;
b = original.ptr[0 .. s];
return true;
}
/**
Defined if `ParentAllocator.deallocate` exists and forwards to
$(D parent.deallocate(b.ptr[0 .. goodAllocSize(b.length)])).
*/
static if (hasMember!(ParentAllocator, "deallocate"))
bool deallocate(void[] b)
{
if (!b.ptr) return true;
return parent.deallocate(b.ptr[0 .. goodAllocSize(b.length)]);
}
// Forwarding methods
mixin(forwardToMember("parent",
"allocateAll", "owns", "deallocateAll", "empty"));
}
///
@system unittest
{
import std.experimental.allocator.building_blocks.free_tree : FreeTree;
import std.experimental.allocator.gc_allocator : GCAllocator;
size_t roundUpToMultipleOf(size_t s, uint base)
{
auto rem = s % base;
return rem ? s + base - rem : s;
}
// Quantize small allocations to a multiple of cache line, large ones to a
// multiple of page size
alias MyAlloc = Quantizer!(
FreeTree!GCAllocator,
n => roundUpToMultipleOf(n, n <= 16_384 ? 64 : 4096));
MyAlloc alloc;
const buf = alloc.allocate(256);
assert(buf.ptr);
}
version (StdUnittest)
@system unittest
{
import std.experimental.allocator.gc_allocator : GCAllocator;
alias MyAlloc = Quantizer!(GCAllocator,
(size_t n) => n.roundUpToMultipleOf(64));
testAllocator!(() => MyAlloc());
assert((() pure nothrow @safe @nogc => MyAlloc().goodAllocSize(1))() == 64);
auto a = MyAlloc();
auto b = a.allocate(42);
assert(b.length == 42);
// Inplace expand, since goodAllocSize is 64
assert((() @safe => a.expand(b, 22))());
//assert((() nothrow @safe => a.expand(b, 22))());
assert(b.length == 64);
// Trigger parent.expand, which may or may not succed
//() nothrow @safe { a.expand(b, 1); }();
() @safe { a.expand(b, 1); }();
assert(a.reallocate(b, 100));
assert(b.length == 100);
// Ensure deallocate inherits from parent
() nothrow @nogc { a.deallocate(b); }();
}
@system unittest
{
import std.experimental.allocator.building_blocks.region : Region;
import std.experimental.allocator.mallocator : Mallocator;
import std.typecons : Ternary;
alias Alloc = Quantizer!(Region!(Mallocator),
(size_t n) => n.roundUpToMultipleOf(64));
auto a = Alloc(Region!Mallocator(1024 * 64));
const b = a.allocate(42);
assert(b.length == 42);
// Check that owns inherits from parent, i.e. Region
assert((() pure nothrow @safe @nogc => a.owns(b))() == Ternary.yes);
assert((() pure nothrow @safe @nogc => a.owns(null))() == Ternary.no);
auto c = a.allocate(42);
assert(c.length == 42);
assert((() pure nothrow @safe @nogc => a.owns(c))() == Ternary.yes);
// Inplace expand, since goodAllocSize is 64
assert((() nothrow @safe => a.expand(c, 22))());
assert(c.length == 64);
// Trigger parent.expand
assert((() nothrow @safe => a.expand(c, 1))());
assert(c.length == 65);
// Check that reallocate inherits from parent
assert((() nothrow @nogc => a.reallocate(c, 100))());
assert(c.length == 100);
}
version (StdUnittest)
@system unittest
{
import std.experimental.allocator.building_blocks.region : Region;
import std.experimental.allocator.mallocator : Mallocator;
alias MyAlloc = Quantizer!(Region!(Mallocator),
(size_t n) => n.roundUpToMultipleOf(64));
testAllocator!(() => MyAlloc(Region!Mallocator(1024 * 64)));
auto a = MyAlloc(Region!Mallocator(1024 * 64));
void[] b;
assert((() nothrow @nogc => a.alignedReallocate(b, 42, 16))());
assert(b.length == 42);
assert(alignedAt(&b[0], 16));
}
version (StdUnittest)
@system unittest
{
import std.experimental.allocator.building_blocks.region : Region;
import std.typecons : Ternary;
alias MyAlloc = Quantizer!(Region!(),
(size_t n) => n.roundUpToMultipleOf(64));
testAllocator!(() => MyAlloc(Region!()(new ubyte[1024 * 64])));
auto a = MyAlloc(Region!()(new ubyte[1024 * 64]));
// Check that empty inherits from parent
assert((() pure nothrow @safe @nogc => a.empty)() == Ternary.yes);
auto b = a.allocate(42);
assert(b.length == 42);
assert((() pure nothrow @safe @nogc => a.empty)() == Ternary.no);
// Check that deallocateAll inherits from parent
assert((() nothrow @nogc => a.deallocateAll())());
assert((() pure nothrow @safe @nogc => a.empty)() == Ternary.yes);
}
|