New adapter: std.allocator.quantizer

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Sun May 10 04:48:38 PDT 2015


On 05/10/2015 05:29 AM, Andrei Alexandrescu wrote:
> 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)
> ...

Yes, but 'expand' expects a _full_ block that was allocated by that 
allocator:

"bool expand(ref void[] b, size_t delta);
Post: !result || b.length == old(b).length + delta 	Expands b by delta 
bytes. If delta == 0, succeeds without changing b. If b is null, the 
call evaluates b = allocate(delta) and returns b !is null. Otherwise, *b 
must be a buffer previously allocated with the same allocator*. If 
expansion was successful, expand changes b's length to b.length + delta 
and returns true. Upon failure, the call effects no change upon the 
allocator object, leaves b unchanged, and returns false."

>> 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.
> ...

I think there has been a misunderstanding here, probably I wasn't 
specific enough. 'b' is a slice of the block that was allocated by the 
parent allocator. Its size will generally be smaller than what the 
parent allocator expects.



More information about the Digitalmars-d mailing list