Manually allocate delegate?

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 12 03:18:59 PDT 2015


On Sunday, 12 July 2015 at 09:03:25 UTC, Tofu Ninja wrote:
> On Sunday, 12 July 2015 at 08:47:37 UTC, ketmar wrote:
>> On Sun, 12 Jul 2015 08:38:00 +0000, Tofu Ninja wrote:
>>
>>> Is it even possible?
>>
>> what do you mean?
>
> Sorry, thought the title was enough.
>
> The context for a delegate(assuming not a method delegate) is 
> allocated by the GC. Is there any way to allocate the context 
> manually.

You can copy a delegate in a GC-free chunk but so far i think 
that the simple fact to get a delegate with "&" will allocate 
from the GC.

By the way i'd be interested to see the runtime function that 
creates a delegate.
i see nothing in druntime.

---
import std.stdio, std.c.stdlib, std.c.string;

class Foo {
     void bar(){writeln("bang");}
}

void main(string[] args) {
     auto foo = new Foo;
     auto dg0 = &foo.bar;
     auto dg1 = *cast(void delegate()*) malloc(size_t.sizeof * 2);
     memmove(cast(void*)&dg1, cast(void*)&dg0, size_t.sizeof * 2);
     dg1();
}
---


More information about the Digitalmars-d-learn mailing list