Memory allocation purity
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Thu May 15 05:27:43 PDT 2014
On Wed, 14 May 2014 21:11:30 -0400, Brian Schott <briancschott at gmail.com>
wrote:
> On Thursday, 15 May 2014 at 00:48:52 UTC, Walter Bright wrote:
>> On 5/14/2014 5:44 PM, Brian Schott wrote:
>>> Can we say that Mallocator failures are not recoverable?
>>
>> malloc itself does not have that property. But you could design a
>> wrapper for it that did.
>
> I'm concerned specifically with this wrapper:
> https://github.com/andralex/phobos/blob/allocator/std/allocator.d#L773
>
> We need to make these functions pure if they are going to be usable.
>
> Removing the stdlib import and adding
>
> private extern (C)
> {
> void* malloc(size_t) pure nothrow @trusted;
> void free(void*) pure nothrow @trusted;
> void* realloc(void*, size_t) pure nothrow @trusted;
> }
Be careful. The above is all correct, but as has been discussed, the
minute you start making returns immutable or parameters immutable, they
become strong-pure, which has undesirable properties for allocators. If
you wrap the above with calls to typed data, then strong-pure might be
inferred.
The compiler can specially designate things like idup to make sure they
always are considered weak-pure. But library code has to be more cautious.
-Steve
More information about the Digitalmars-d
mailing list