std.allocator needs your help

Jacob Carlborg doob at me.com
Thu Sep 26 00:46:20 PDT 2013


On 2013-09-25 20:27, Johannes Pfau wrote:

> Having the compiler rewrite
>
> "new(MallocAllocator) MyClass(a,b,c)"
> to
> "void[] __buf = MallocAllocator.it.allocate(MyClass.instancesize);"
> "__construct!MyClass(__buf, a, b, c);" //Has to handle context ptr
>
> seems like a good solution. It probably has to support both allocator
> types(for allocators without state) and allocator instances.

Here's an alternative. The compiler will lower:

new MyClass(a, b, c);

To:

auto __buf = _d_tl_new(MyClass.instancesize);

And:

new shared MyClass(a, b, c);

auto __buf = _d_global_new(MyClass.instancesize);

Then construct the class as above. To change the alloctor one just sets 
the rt.allocator.allocator variable, most likely via some function.



extern (C) void[] _d_tl_new (size_t size)
{
     return _d_new(size, rt.allocator.allocator);
}

extern (C) void[] _d_global_new (size_t size)
{
     return _d_new(size, rt.allocator.globalAllocator);
}

extern (C) void[] _d_new (size_t size, Allocator allocator)
{
     if (memory = allocator.allocate(size))
         return memory;

     onOutOfMemoryError();
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list