www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Hooking the GC?

reply %u <wfunction hotmail.com> writes:
Hi,

Is there any way to add a hook to the garbage collector in D, so that I can be
immediately notified (for example) when an object is created?

(I'm aware that this could cause infinite recursion/deadlock if I try to
allocate memory, but that's all right, I'm fine with that.)

Thank you!
Jan 02 2011
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
%u <wfunction hotmail.com> wrote:

 Hi,

 Is there any way to add a hook to the garbage collector in D, so that I  
 can be
 immediately notified (for example) when an object is created?

 (I'm aware that this could cause infinite recursion/deadlock if I try to
 allocate memory, but that's all right, I'm fine with that.)

You're gonna have to add such functionality to the GC by yourself, I'm afraid. Shouldn't be too hard though, depending on what sort of info you require from the hook. -- Simen
Jan 03 2011
prev sibling next sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Mon, 03 Jan 2011 05:34:55 +0200, %u <wfunction hotmail.com> wrote:

 Is there any way to add a hook to the garbage collector in D, so that I  
 can be
 immediately notified (for example) when an object is created?

FWIW, Diamond ( http://github.com/CyberShadow/Diamond ) hooks various GC functions, but it's an ugly hack that breaks with multi-threaded programs (and the module is D1 and x86-only). A better run-time solution (not involving modifying Phobos/druntime) would need a instruction-length disassembler. -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Jan 03 2011
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 02 Jan 2011 22:34:55 -0500, %u <wfunction hotmail.com> wrote:

 Hi,

 Is there any way to add a hook to the garbage collector in D, so that I  
 can be
 immediately notified (for example) when an object is created?

 (I'm aware that this could cause infinite recursion/deadlock if I try to
 allocate memory, but that's all right, I'm fine with that.)

 Thank you!

If you want to hook the GC, you probably have to add your own code to the GC. Start with _d_newclass in druntime's src/rt/lifetime.d -Steve
Jan 03 2011
next sibling parent reply %u <wfunction hotmail.com> writes:
It doesn't have to be at run-time, I can add code to the GC too. So _d_newclass
would be *almost* what I'm looking for, except that it doesn't work for memory
allocated using GC.malloc()... so I'd have to hook in multiple places I guess.
Thanks for the info.
Jan 03 2011
parent %u <wfunction hotmail.com> writes:
 _d_newclass invokes the GC's malloc downstream. It's just that at _d_newclass,

Ohh I see, okay, thank you!
Jan 04 2011
prev sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Tue, 04 Jan 2011 01:06:39 +0200, %u <wfunction hotmail.com> wrote:

 It doesn't have to be at run-time, I can add code to the GC too. So  
 _d_newclass
 would be *almost* what I'm looking for, except that it doesn't work for  
 memory
 allocated using GC.malloc()... so I'd have to hook in multiple places I  
 guess.
 Thanks for the info.

_d_newclass invokes the GC's malloc downstream. It's just that at _d_newclass, you know the class typeinfo being allocated. -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Jan 04 2011