Allocator troubles
Luís Marques via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jul 15 11:44:26 PDT 2016
I'm not having success trying to use the allocator API.
What am doing wrong here? (OS X, 64-bit)
void main()
{
import std.exception;
import std.experimental.allocator;
import std.experimental.allocator.building_blocks.region;
import std.stdio;
static InSituRegion!1024 ralloc;
IAllocator alloc = allocatorObject(&ralloc);
enforce(alloc.make!int !is null);
enforce(alloc.deallocateAll());
enforce(alloc.make!int !is null);
writeln("OK 1");
enforce(alloc.deallocateAll());
writeln("OK 2");
}
$ rdmd -g test.d
OK 1
Segmentation fault: 11
BTW, there doesn't seem to be any primitive like tryMake or an
allocator building block that throws instead of returning null. I
wanted to just replace my "new X" statements with a region
allocator and leave the rest unchanged, so I had to create my own
tryMake which wraps make calls with an enforcement of !is null. I
was surprised by this omission (or did I miss something?). BTW,
notice that I don't ever expect my allocator to return null
unless there is a bug in my program, since the amount of memory
needed is statically computed and carefully used, so it makes a
lot of sense for me to just throw OutOfMemoryException.
More information about the Digitalmars-d
mailing list