Skip to content

Commit

Permalink
Keep constraints on rounding function low
Browse files Browse the repository at this point in the history
  • Loading branch information
andralex committed May 11, 2015
1 parent aa5f103 commit 9307b9f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions std/experimental/allocator/quantizer.d
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,19 @@ struct Quantizer(ParentAllocator, alias roundingFunction)
*/
bool expand(ref void[] b, size_t delta)
{
if (!b.ptr)
{
b = allocate(delta);
return b.ptr !is null || delta == 0;
}
immutable allocated = goodAllocSize(b.length),
needed = b.length + delta;
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 >= needed && b.ptr)
if (allocated == neededAllocation)
{
// Nice!
b = b.ptr[0 .. needed];
Expand All @@ -112,7 +121,7 @@ struct Quantizer(ParentAllocator, alias roundingFunction)
// Expand to the appropriate quantum
auto original = b.ptr[0 .. allocated];
assert(goodAllocSize(needed) >= allocated);
if (!parent.expand(original, goodAllocSize(needed) - allocated))
if (!parent.expand(original, neededAllocation - allocated))
return false;
// Dial back the size
b = original.ptr[0 .. needed];
Expand Down

0 comments on commit 9307b9f

Please sign in to comment.