New adapter: std.allocator.quantizer

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sat May 9 20:29:41 PDT 2015


On 5/9/15 3:38 PM, Timon Gehr wrote:
> Thanks! Looks good, except:
>
> 106| if (!parent.expand(b, goodAllocSize(needed) - b.length))

Let's see, this is a tad tricky. "needed" is the needed size, i.e. 
b.length + delta. We want to expand to a final size of 
goodAllocSize(needed). So we need to pass the appropriate delta to 
expand, i.e. goodAllocSize(needed) - b.length.

(recall that expand() takes the delta, not the new size)

> 142| return parent.reallocate(b, gs);

gs is precomputed at the top of the function to be goodAllocSize(s), so 
this seems to be in good shape.

> 172| return parent.alignedReallocate(b, gs, a);

Same here, the intent is to reallocate to goodAllocSize(s), which is 
precomputed in gs.

> Those should be more like:
>
> 182| parent.deallocate(b.ptr[0 .. goodAllocSize(b.length)]);
>
>
> Another point is that the documented/checked constraints on the rounding
> function are too weak. A rounding function should be monotone increasing
> and piecewise constant with one fixed point per piece.

Agreed, I see there's a bit of follow-up so I'll reply to that.

> And then, there's this, of course:
>
> size_t goodAllocSize(size_t n);
>      Returns roundingFunction(n). It is required that
> roundingFunction(n) >= n. For efficiency reasons, this is only asserted
> (checked in debug mode).
>
> Is this meant to be a complete specification of 'assert'? :o)
> What is 'debug mode'?

Good point. Fixed the docs: 
https://github.com/andralex/phobos/blob/allocator/std/experimental/allocator/quantizer.d


Andrei



More information about the Digitalmars-d mailing list