std.allocator ready for some abuse

Namespace rswhite4 at googlemail.com
Fri Oct 25 01:27:50 PDT 2013


>> You mena something like this?
>> ----
>> use(Mallocator) {
>>    int[] arr;
>>    arr ~= 42;
>> }
>> ----
>> Or did I understand you wrong?
>
> It depends on how Mallocator is related to integer array (which 
> again boils down to allocator design). If it is appropriate, 
> then yes.
>
>>
>> Whats about the virtual property idea, that every array has 
>> internal an allocator? Wouldn't it be easier to implement such 
>> a thing?
>
> Please provide example.

Something like that: 
http://forum.dlang.org/thread/l4btsk$5u8$1@digitalmars.com?page=3#post-pfoxyfzyjxqcqwnvgnpi:40forum.dlang.org

Every array has an internal allocator property which can be reset:
----
int[] arr;
arr.allocator = Mallocator;
----

or

----
int[] arr;
arr.useAllocator(Mallocator);
----

But maybe a design without some alias notation would be more 
preferable:
----
{
     ScopeAllocator m;
     int[] arr;
     arr.useAllocator(m);

     arr ~= 42; /// Use m.allocate
} /// end of scope: ScopeAllocator collects all remaining memory.
----

And:
----
int[] arr;
assert(arr is null);
{
     ScopeAllocator m;
     arr.useAllocator(m);

     arr ~= 42; /// Use m.allocate
} /// end of scope: ScopeAllocator collects all remaining memory.
assert(arr is null);
----



More information about the Digitalmars-d mailing list