memory management and the standard library
Atila Neves
atila.neves at gmail.com
Tue Mar 19 13:07:54 UTC 2019
On Saturday, 16 March 2019 at 21:17:02 UTC, Olivier FAURE wrote:
> On Saturday, 16 March 2019 at 20:17:50 UTC, Jacob Carlborg
> wrote:
>> Instead of having a function allocate memory, pass a buffer,
>> delegate, output range or something similar to the function.
>> Then whatever you pass can decide if it should allocate using
>> the GC, malloc or something else.
>
> It would have to be a delegate or an output range, because in
> most cases the callee wouldn't know in advance how much storage
> it needs to allocate.
>
> Anyway, it would be pretty nice if there were a standardized
> way to tell a function how it should allocate its memory;
There is:
void fun(A)(auto ref A allocator);
> it would allow some nice strategies like "I know that I'm going
> to discard most of what this function allocates, so just put in
> in a continuous buffer; then deep-copy the relevant data to GC
> memory, and throw the rest away" (eg in a parser).
This can all be done with a custom allocator, or one built with
the building blocks in std.experimental.allocator.
>
> However, for that to work, you'd probably need a way to pass
> implicit arguments to a function without having to specify
> `foobar(defaultGcAllocator, x, y, z)` every time you call a
> function.
void foobar(int x, int y, int z) {
import std.experimental.allocator.gc_allocator: GCAllocator;
foobar(GCAllocator.instance, x, y, z);
}
void foobar(A)(auto ref A allocator, int x, int y, int z) {
// ...
}
Or use `theAllocator`.
More information about the Digitalmars-d
mailing list