www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - GC.BlkAttr.FINALIZE

reply "Namespace" <rswhite4 googlemail.com> writes:
Is it my misunderstanding or does FINALIZE not work?

----
import std.stdio;
import core.memory : GC;

void main() {
	int id = 2;

	struct Foo {
		~this() {
			id--;
			writeln("DTor");
		}
	}

	Foo* fs = cast(Foo*) GC.malloc(2 * Foo.sizeof, 
GC.BlkAttr.FINALIZE);

	writeln("end of main: ", id);
}
----

id is still 2. I know that the GC is incapable with arrays of 
structs but if FINALIZE means the same thing I expect, it should 
work. Otherwise the docs should be updated.
Feb 28 2014
parent reply "Namespace" <rswhite4 googlemail.com> writes:
Oh man I need sleep. Of course it is 2 at the end of the scope. 
But what is important: I never see "DTor". So the Data is not 
finalized.
Feb 28 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 28 Feb 2014 18:26:29 -0500, Namespace <rswhite4 googlemail.com>  
wrote:

 Oh man I need sleep. Of course it is 2 at the end of the scope. But what  
 is important: I never see "DTor". So the Data is not finalized.
FINALIZE expects the layout of the block to be an Object. You aren't implementing the correct internals. Specifically, you need a vtable, which will point at the dtor. I'm surprised you don't get a segfault. We need a precise GC to get struct dtors to work, the alternative would be extremely hackish. -Steve
Feb 28 2014
next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Friday, 28 February 2014 at 23:30:46 UTC, Steven Schveighoffer 
wrote:
 On Fri, 28 Feb 2014 18:26:29 -0500, Namespace 
 <rswhite4 googlemail.com> wrote:

 Oh man I need sleep. Of course it is 2 at the end of the 
 scope. But what is important: I never see "DTor". So the Data 
 is not finalized.
FINALIZE expects the layout of the block to be an Object. You aren't implementing the correct internals. Specifically, you need a vtable, which will point at the dtor. I'm surprised you don't get a segfault. We need a precise GC to get struct dtors to work, the alternative would be extremely hackish. -Steve
Ah! That's not in the docs. :P I thougth we got already some implementations. What is the status on this?
Feb 28 2014
prev sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
 We need a precise GC to get struct dtors to work, the 
 alternative would be extremely hackish.
Since I expect/fear that such a implementation is not done in the next 12 months: what would be the alternative?
Feb 28 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 28 Feb 2014 18:45:50 -0500, Namespace <rswhite4 googlemail.com>  
wrote:

 We need a precise GC to get struct dtors to work, the alternative would  
 be extremely hackish.
Since I expect/fear that such a implementation is not done in the next 12 months: what would be the alternative?
Examine the GC code, and figure out a way to hook the finalizer. Look for FINALIZE. Another crude but effective option would be to wrap the struct in a class. May not actually be that bad, depending on the size of the struct (blocks come in chunks of 16, 32, 64, etc.). -Steve
Feb 28 2014
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 1 March 2014 at 01:28:06 UTC, Steven Schveighoffer 
wrote:
 On Fri, 28 Feb 2014 18:45:50 -0500, Namespace 
 <rswhite4 googlemail.com> wrote:

 We need a precise GC to get struct dtors to work, the 
 alternative would be extremely hackish.
Since I expect/fear that such a implementation is not done in the next 12 months: what would be the alternative?
Examine the GC code, and figure out a way to hook the finalizer. Look for FINALIZE. Another crude but effective option would be to wrap the struct in a class. May not actually be that bad, depending on the size of the struct (blocks come in chunks of 16, 32, 64, etc.). -Steve
I tried to enable the printf's in rt/lifetime.d (e.g.: https://github.com/D-Programming-Language/druntime/blob/e47a00bff935c3f079bb567a6ec97663ba384487/src/ t/lifetime.d#L1125) to see what happens if I/the GC delete an array. But I see no output, no matter what I try to allocate/deallocate.
Mar 01 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 01 Mar 2014 09:57:44 -0500, Namespace <rswhite4 googlemail.com>  
wrote:

 I tried to enable the printf's in rt/lifetime.d (e.g.:  
 https://github.com/D-Programming-Language/druntime/blob/e47a00bff935c3f079bb567a6ec97663ba384487/src/
t/lifetime.d#L1125)  
 to see what happens if I/the GC delete an array. But I see no output, no  
 matter what I try to allocate/deallocate.
Enabling debugging code in druntime is really hard. I remember struggling with that when I was changing the array append code. It does work when you get it right... -Steve
Mar 02 2014
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 2 March 2014 at 20:21:51 UTC, Steven Schveighoffer 
wrote:
 On Sat, 01 Mar 2014 09:57:44 -0500, Namespace 
 <rswhite4 googlemail.com> wrote:

 I tried to enable the printf's in rt/lifetime.d (e.g.: 
 https://github.com/D-Programming-Language/druntime/blob/e47a00bff935c3f079bb567a6ec97663ba384487/src/
t/lifetime.d#L1125) 
 to see what happens if I/the GC delete an array. But I see no 
 output, no matter what I try to allocate/deallocate.
Enabling debugging code in druntime is really hard. I remember struggling with that when I was changing the array append code. It does work when you get it right... -Steve
Great... :D I have no idea how. :P Should I recompile phobos also? Or try something special...?
Mar 02 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 02 Mar 2014 15:44:47 -0500, Namespace <rswhite4 googlemail.com>  
wrote:

 On Sunday, 2 March 2014 at 20:21:51 UTC, Steven Schveighoffer wrote:
 On Sat, 01 Mar 2014 09:57:44 -0500, Namespace <rswhite4 googlemail.com>  
 wrote:

 I tried to enable the printf's in rt/lifetime.d (e.g.:  
 https://github.com/D-Programming-Language/druntime/blob/e47a00bff935c3f079bb567a6ec97663ba384487/src/
t/lifetime.d#L1125)  
 to see what happens if I/the GC delete an array. But I see no output,  
 no matter what I try to allocate/deallocate.
Enabling debugging code in druntime is really hard. I remember struggling with that when I was changing the array append code. It does work when you get it right... -Steve
Great... :D I have no idea how. :P Should I recompile phobos also? Or try something special...?
You HAVE to recompile phobos, because druntime is statically included inside it ;) -Steve
Mar 02 2014
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 2 March 2014 at 20:45:42 UTC, Steven Schveighoffer 
wrote:
 On Sun, 02 Mar 2014 15:44:47 -0500, Namespace 
 <rswhite4 googlemail.com> wrote:

 On Sunday, 2 March 2014 at 20:21:51 UTC, Steven Schveighoffer 
 wrote:
 On Sat, 01 Mar 2014 09:57:44 -0500, Namespace 
 <rswhite4 googlemail.com> wrote:

 I tried to enable the printf's in rt/lifetime.d (e.g.: 
 https://github.com/D-Programming-Language/druntime/blob/e47a00bff935c3f079bb567a6ec97663ba384487/src/
t/lifetime.d#L1125) 
 to see what happens if I/the GC delete an array. But I see 
 no output, no matter what I try to allocate/deallocate.
Enabling debugging code in druntime is really hard. I remember struggling with that when I was changing the array append code. It does work when you get it right... -Steve
Great... :D I have no idea how. :P Should I recompile phobos also? Or try something special...?
You HAVE to recompile phobos, because druntime is statically included inside it ;) -Steve
Ahh.. I thought it is enough to recompile druntime... :)
Mar 02 2014
parent reply "Namespace" <rswhite4 googlemail.com> writes:
It's working now, I get all my debug infos I need.
But: I have absolutly no idea how to get the correct TypeInfo in 
rt_finalize2. The ClassInfo is identified by casting the void* 
pointer to a void** and then cast this void** to ClassInfo*. But 
for other TypeInfos this weird trick doesn't work. How can I 
extract the correct TypeInfo from this void**? :P
Mar 02 2014
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Monday, 3 March 2014 at 02:44:09 UTC, Namespace wrote:
 It's working now, I get all my debug infos I need.
 But: I have absolutly no idea how to get the correct TypeInfo 
 in rt_finalize2. The ClassInfo is identified by casting the 
 void* pointer to a void** and then cast this void** to 
 ClassInfo*. But for other TypeInfos this weird trick doesn't 
 work. How can I extract the correct TypeInfo from this void**? 
 :P
I get the real TypeInfo (not sure if this was intended this way) and in my test base it works! But in druntime I get the weird error: src\rt\lifetime.d(1256): Error: variable a1 used before set ---- if (gc_getAttr(p) & BlkAttr.APPENDABLE) { void[] a1 = *cast(void[]*) ppv; //printf("len = %d\n", a1.length); void* p_a1 = &a1; void* tp = p_a1 + 12; TypeInfo* ti = cast(TypeInfo*) tp; import std.string : toStringz; string s = ti.toString(); // line 1256 printf("Type = %s\n", toStringz(s)); return; } ---- Any call to ti will invoke the error. Any idea why?
Mar 03 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 03 Mar 2014 12:03:12 -0500, Namespace <rswhite4 googlemail.com>  
wrote:

 On Monday, 3 March 2014 at 02:44:09 UTC, Namespace wrote:
 It's working now, I get all my debug infos I need.
 But: I have absolutly no idea how to get the correct TypeInfo in  
 rt_finalize2. The ClassInfo is identified by casting the void* pointer  
 to a void** and then cast this void** to ClassInfo*. But for other  
 TypeInfos this weird trick doesn't work. How can I extract the correct  
 TypeInfo from this void**? :P
I get the real TypeInfo (not sure if this was intended this way) and in my test base it works! But in druntime I get the weird error: src\rt\lifetime.d(1256): Error: variable a1 used before set ---- if (gc_getAttr(p) & BlkAttr.APPENDABLE) { void[] a1 = *cast(void[]*) ppv; //printf("len = %d\n", a1.length); void* p_a1 = &a1; void* tp = p_a1 + 12; TypeInfo* ti = cast(TypeInfo*) tp; import std.string : toStringz; string s = ti.toString(); // line 1256 printf("Type = %s\n", toStringz(s)); return; } ---- Any call to ti will invoke the error. Any idea why?
Not really any clue. But if this is in druntime, it should not be importing anything from std. -Steve
Mar 03 2014
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Monday, 3 March 2014 at 18:36:56 UTC, Steven Schveighoffer 
wrote:
 On Mon, 03 Mar 2014 12:03:12 -0500, Namespace 
 <rswhite4 googlemail.com> wrote:

 On Monday, 3 March 2014 at 02:44:09 UTC, Namespace wrote:
 It's working now, I get all my debug infos I need.
 But: I have absolutly no idea how to get the correct TypeInfo 
 in rt_finalize2. The ClassInfo is identified by casting the 
 void* pointer to a void** and then cast this void** to 
 ClassInfo*. But for other TypeInfos this weird trick doesn't 
 work. How can I extract the correct TypeInfo from this 
 void**? :P
I get the real TypeInfo (not sure if this was intended this way) and in my test base it works! But in druntime I get the weird error: src\rt\lifetime.d(1256): Error: variable a1 used before set ---- if (gc_getAttr(p) & BlkAttr.APPENDABLE) { void[] a1 = *cast(void[]*) ppv; //printf("len = %d\n", a1.length); void* p_a1 = &a1; void* tp = p_a1 + 12; TypeInfo* ti = cast(TypeInfo*) tp; import std.string : toStringz; string s = ti.toString(); // line 1256 printf("Type = %s\n", toStringz(s)); return; } ---- Any call to ti will invoke the error. Any idea why?
Not really any clue. But if this is in druntime, it should not be importing anything from std. -Steve
Even without I get this weird error. Is this a bug?
Mar 03 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 03 Mar 2014 15:34:18 -0500, Namespace <rswhite4 googlemail.com>  
wrote:

 On Monday, 3 March 2014 at 18:36:56 UTC, Steven Schveighoffer wrote:
 On Mon, 03 Mar 2014 12:03:12 -0500, Namespace <rswhite4 googlemail.com>  
 wrote:

 On Monday, 3 March 2014 at 02:44:09 UTC, Namespace wrote:
 It's working now, I get all my debug infos I need.
 But: I have absolutly no idea how to get the correct TypeInfo in  
 rt_finalize2. The ClassInfo is identified by casting the void*  
 pointer to a void** and then cast this void** to ClassInfo*. But for  
 other TypeInfos this weird trick doesn't work. How can I extract the  
 correct TypeInfo from this void**? :P
I get the real TypeInfo (not sure if this was intended this way) and in my test base it works! But in druntime I get the weird error: src\rt\lifetime.d(1256): Error: variable a1 used before set ---- if (gc_getAttr(p) & BlkAttr.APPENDABLE) { void[] a1 = *cast(void[]*) ppv; //printf("len = %d\n", a1.length); void* p_a1 = &a1; void* tp = p_a1 + 12; TypeInfo* ti = cast(TypeInfo*) tp; import std.string : toStringz; string s = ti.toString(); // line 1256 printf("Type = %s\n", toStringz(s)); return; } ---- Any call to ti will invoke the error. Any idea why?
Not really any clue. But if this is in druntime, it should not be importing anything from std.
Even without I get this weird error. Is this a bug?
It sounds suspicious. Clearly, you are not accessing a1 there, and clearly a1 is set before being used. Any chance to reduce it? -Steve
Mar 03 2014
parent "Namespace" <rswhite4 googlemail.com> writes:
 It sounds suspicious. Clearly, you are not accessing a1 there, 
 and clearly a1 is set before being used.

 Any chance to reduce it?

 -Steve
I don't know. But I know that my little trick doesn't work in lifetime.d although it works locally. Here what I tried: ---- import std.stdio; struct Foo { int id; } void main() { Foo[] fs; fs ~= Foo(45); fs ~= Foo(46); fs ~= Foo(47); fs ~= Foo(48); auto ti = typeid(fs); writeln("&ti = ", &ti); void* p = fs.ptr; void[] arr = *cast(void[]*) p; void* tp = cast(void*)((&arr) - cast(void*) 8); TypeInfo ti2 = *cast(TypeInfo*) tp; writeln(ti, " versus ", ti2); } ---- Since rt_finalize and the GC have the same pointer as 'fs.ptr' I cast it to an array struct. From the pointer of this struct I subtract 8 (8 because of try and error until the pointer was equal the real TypeInfo pointer). This I can cast to the TypeInfo: it works. But not in lifetime.d. Was a try, but I have no idea why it doesn't work (the pointers aren't equal). An idea or alternative?
Mar 03 2014