New adapter: std.allocator.quantizer

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Sun May 10 05:02:02 PDT 2015


On 05/10/2015 05:38 AM, Andrei Alexandrescu wrote:
> On 5/9/15 3:54 PM, Timon Gehr wrote:
>> On 05/10/2015 12:38 AM, Timon Gehr wrote:
>>> monotone increasing and piecewise constant with one fixed point per
>>> piece.
>>
>> (Note that monotone increasing is implied by piecewise constant with one
>> fixed point per piece, so it does not necessarily need to be documented
>> separately.)
>
> I think the only requirements are (a) roundingFunction is pure, (b)
> roundingFunction(n) >= n. E.g. the identity function works, although
> it's not terribly useful.
>
> These could be enforced by Quantizer, but it doesn't feel right. A
> designer who is at the same time sophisticated enough to need Quantizer
> yet naïve enough to choose a lousy one is quite unlikely. On the other
> hand, I can imagine stuff like this could be useful to some:
>
> __gshared uint SMALL_ALLOC = 64;
> ... configure it via an application-level flag...
> alias MyAlloc = Quantizer!(
>      FreeTree!GCAllocator,
>      n => n.roundUpToMultipleOf(n <= SMALL_ALLOC ? 64 : 4096));
>
> That's technically not pure but works, and might be appreciated.
>
>
> Andrei
>

size_t brokenRoundingFunction(size_t siz){
     if(siz==10) return 40;
     if(siz==20) return 30;
     return siz;
}

alias BrokenAlloc = Quantizer!(
     SomeAllocatorThatReliesOnProvidedBlockSize,
     brokenRoundingFunction
);

void main(){
     BrokenAlloc borked;
     // allocate buffer of size 40, slice 10 bytes off it:
     auto b=borked.allocate(10);
     // awesome, can expand in-place since 20 <= 40:
     borked.expand(a,10);
     // oops, now we try to deallocate a block of size 30
     // from the parent, even though the allocated size
     // is 40:
     borked.deallocate(a);
}

(Untested, but it should wreak some havoc.)


More information about the Digitalmars-d mailing list