[D-runtime] GC to track external memory?

Michel Fortin michel.fortin at michelf.com
Sun May 22 03:38:02 PDT 2011


I'd like to add a feature to the GC allowing it to track externally-allocated memory. The simplified use case is this:

	struct S { ... }
	extern (C) S* s_create();
	extern (C) void s_destroy(S*);

	void main() {
		S* s = s_create();
		GC.track(s, S.sizeof, &s_destroy, false/*no scan*/);
		...
	}

By calling "GC.track", the GC would start tracking pointers pointing to the specified memory block during the mark phase and then call the "s_destroy" function as the finalizer when it is no longer reachable. I looked at how the GC works, and one idea that pops to mind is to add special Pool that would cover pages corresponding to those external objects, a Pool that would be created as needed when adding external blocks through "GC.track" and that would not be available for allocation.

I realize this solution makes it be impossible to track blocks at a finer granularity than aligned 16 bytes, but I can live with that constrain.

Does this approach makes sense? any concern? any better idea?

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the D-runtime mailing list