std.allocator needs your help

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Sep 24 09:05:28 PDT 2013


On 9/24/13 2:49 AM, Robert wrote:
> On Mon, 2013-09-23 at 10:53 -0700, Andrei Alexandrescu wrote:
>> void deallocate(void[] buffer);
>>
>> This is because the size of D objects is naturally known: classes
>> have
>> it in the classinfo, slices store it, and the few cases of using bald
>> pointers for allocation are irrelevant and unrecommended.
>
>
> One concern though: void[] is just a slice, who guarantees that the size
> matches the one that was allocated?
>
> Is the allocator assumed to handle this correctly:
>
>     auto arr = allocate(100);
>     auto arr2 = arr[50..100];
>     arr = arr[0..50];
>     deallocate(arr);
>     deallocate(arr2);
>
> ?
>
> Or is the user simply obliged to use the originally returned range?

Initially I thought the user must pass the exact slice returned by 
allocate(). Then I thought we can relax the requirement to any length 
between the _requested_ size in allocate and the _received_ length of 
the slice. For example:

auto arr = a.allocate(11);

Most allocators will allocate more than 11 bytes. Say the returned arr 
has length 64. Then either a.deallocate(arr) or a.deallocate(arr[0 .. 
11]) would work. In fact a.deallocate(arr[0 .. n]) would work for any n 
between 11 and 64 inclusive. This is because the allocator will apply 
whatever rounding mechanism it has applied to the size when allocating, 
to the deallocated size.

If the size passed for deallocation is smaller than the size requested, 
most allocators won't handle that properly. Interior pointers are also 
not handled by most allocators.


Andrei



More information about the Digitalmars-d mailing list