DMD 1.001 release

Tomas Lindquist Olsen tomas at famolsen.dk
Wed Jan 24 00:53:29 PST 2007


Walter Bright wrote:

> Tomas Lindquist Olsen wrote:
> 
> > which also makes me think about how to handle this case properly:
> > 
> > void[] dst = new void[1024];
> > dst.length = dst.length + 1024;
> > dst.length = dst.length + 1024;
> > dst.length = dst.length + 1024;
> > dst.length = dst.length + 1024;
> > dst.length = dst.length + 1024;
> > 
> > assuming one of these calls have to relocate how would I be sure
> > that I inform the GC properly?
> 
> Inform it with the last one.
> 
> > A call to the GC in the end would obviously do, but what if you
> > want to make sure the GC doesn't scan the array while it's still
> > being resized?
> 
> Depends on what data you put in it.
> 

Let me clarify what I mean. Consider this code:

import std.gc;

void test()
{
	void[] buf = new void[1024];
	std.gc.hasNoPointers(ret.ptr);
	// invoking gc will not scan buf. ok
	buf.length = buf.length*2;
	// could invoking gc now potentially scan buf?
}

I'm thinking how to make sure a void[] is _never_ scanned.
A somewhat realistic case for this would be (again) the std.file.read
function.
If we append some data to this buffer will the GC be smart enough to
know it still holds no pointers? That is even with the reallocation and
potential relocation.
Just putting the hasNoPointers in the end when no more resizing will
happen is not good enough as the GC could easily be invoked in between
and 'buf' would be scanned.

I guess you could be unlucky and the GC could be triggered (by another
thread) after 'new void[1024]' but before
'std.gc.hasNoPointers(buf.ptr)', in which case there is not much to do.
Is a ubyte[] safe from this problem? (if it even exists)

> > While I'm at it I have been wondering why we can't have:
> > 
> > void[1024] sa;
> > 
> > when
> > 
> > void[] da = new void[1024];
> > 
> > is perfectly fine.
> 
> You're probably right.

Given the new meaning of void arrays, I definitely think it should be
reconsidered.



More information about the Digitalmars-d-announce mailing list