Unmanaged - a D framework on github
Lars T. Kyllingstad
public at kyllingen.net
Tue Mar 19 23:56:14 PDT 2013
On Sunday, 17 March 2013 at 15:20:04 UTC, Jakob Ovrum wrote:
> For different kinds of memory, you should simply use a
> different allocator. For example, here's a rough approximation
> of a pair of functions using malloc/free for class allocation:
>
> T alloc(T)() if(is(T == class))
> {
> enum size = __traits(classInstanceSize, T);
> auto p = enforceEx!OutOfMemoryError(malloc(size));
> return emplace!T(p[0 .. size]);
> }
I know this was just a rough example, but I just wanted to point
this out: It is not a good idea to use enforceEx() here, since
a) it uses new to allocate memory for the exception, and b) you
shouldn't try to allocate at all when you're out of memory. :)
Use core.exception.onOutOfMemory() to signal an allocation
failure instead. It throws a pre-allocated OutOfMemoryError.
Lars
More information about the Digitalmars-d-announce
mailing list