Jump to page: 1 2
Thread overview
Manually allocate delegate?
Jul 12, 2015
Tofu Ninja
Jul 12, 2015
ketmar
Jul 12, 2015
Tofu Ninja
Jul 12, 2015
Baz
Jul 12, 2015
Tofu Ninja
Jul 12, 2015
Baz
Jul 12, 2015
Tofu Ninja
Jul 12, 2015
ketmar
Jul 12, 2015
Tofu Ninja
Jul 12, 2015
Adam D. Ruppe
Jul 13, 2015
ketmar
Nov 21, 2015
userABCabc123
Nov 21, 2015
Tofu Ninja
Jul 12, 2015
Adam D. Ruppe
Jul 13, 2015
Tofu Ninja
Nov 21, 2015
Jack Applegame
July 12, 2015
Is it even possible?
July 12, 2015
On Sun, 12 Jul 2015 08:38:00 +0000, Tofu Ninja wrote:

> Is it even possible?

what do you mean?

July 12, 2015
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.
July 12, 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();
}
---
July 12, 2015
On Sunday, 12 July 2015 at 10:19:02 UTC, Baz wrote:
> 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();
> }
> ---

That is not manually allocating a delegate context, and & in that instance does not even allocate. For delegates to class methods, the context is just the "this" pointer of the object, so the context in that code is just foo, which you still allocated on the gc. The delegate itself (the function pointer and the context pointer) is just allocated on the stack.

What I am talking about is the context that implicitly gets allocated when you make a delegate to a nested function.

void main(string[] args)
{
	auto d = bar();
	d();
}


auto bar()
{
	int x = 5;
	
	void foo()
	{
		writeln(x);
	}
	
	auto d = &foo; // <-- allocates
	return d;
}


July 12, 2015
On Sunday, 12 July 2015 at 10:39:44 UTC, Tofu Ninja wrote:
> On Sunday, 12 July 2015 at 10:19:02 UTC, Baz wrote:
>> [...]
>
> That is not manually allocating a delegate context, and & in that instance does not even allocate. For delegates to class methods, the context is just the "this" pointer of the object, so the context in that code is just foo, which you still allocated on the gc. The delegate itself (the function pointer and the context pointer) is just allocated on the stack.
>
> What I am talking about is the context that implicitly gets allocated when you make a delegate to a nested function.
>
> void main(string[] args)
> {
> 	auto d = bar();
> 	d();
> }
>
>
> auto bar()
> {
> 	int x = 5;
> 	
> 	void foo()
> 	{
> 		writeln(x);
> 	}
> 	
> 	auto d = &foo; // <-- allocates
> 	return d;
> }

At least now your Question is clearer and understandable...but sorry goodbye. I don't feel good vibes here. See ya ^^.

July 12, 2015
On Sun, 12 Jul 2015 09:03:24 +0000, 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.

nope. there is no way to overload context allocation function, afaik. at least without patching druntime, and i still don't know what one have to patch. ;-)

July 12, 2015
On Sunday, 12 July 2015 at 11:22:41 UTC, Baz wrote:
> At least now your Question is clearer and understandable...but sorry goodbye. I don't feel good vibes here. See ya ^^.

Sorry if I came off as rude, didn't mean to... >.>
July 12, 2015
On Sunday, 12 July 2015 at 11:42:09 UTC, ketmar wrote:
> On Sun, 12 Jul 2015 09:03:24 +0000, 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.
>
> nope. there is no way to overload context allocation function, afaik. at least without patching druntime, and i still don't know what one have to patch. ;-)

Hmmmmm.... with allocators being added, this seems like something that needs to be fixed. Maybe when allocators do get added then the implicit allocations for the context could be done with the global allocator, that and other implicit allocations like array resizing. Guess I will just wait and see how it plays out.
July 12, 2015
On Sunday, 12 July 2015 at 11:42:09 UTC, ketmar wrote:
> nope. there is no way to overload context allocation function, afaik. at least without patching druntime, and i still don't know what one have to patch. ;-)

_d_allocmemory
« First   ‹ Prev
1 2