www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Less known std.gc

reply bearophile <bearophileHUGS lycos.com> writes:
The std.gc module of Phobos contains various things that I don't undertand:

void addRoot(void* p);
void removeRoot(void* p);
void addRange(void* pbot, void* ptop);
void removeRange(void* pbot);
void setTypeInfo(TypeInfo ti, void* p);
void* getGCHandle();
void setGCHandle(void* p);

Maybe you can point me to some more info about their usage and usefulness, or
some code that uses them. (I think the docs of Phobos can gain some usage
examples of those functions).

Bye and thank you,
bearophile
Nov 19 2007
next sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
bearophile wrote:
 The std.gc module of Phobos contains various things that I don't undertand:
 
 void addRoot(void* p);
 void removeRoot(void* p);
 void addRange(void* pbot, void* ptop);
 void removeRange(void* pbot);
 void setTypeInfo(TypeInfo ti, void* p);
 void* getGCHandle();
 void setGCHandle(void* p);
 
 Maybe you can point me to some more info about their usage and usefulness, or
some code that uses them. (I think the docs of Phobos can gain some usage
examples of those functions).
 
 Bye and thank you,
 bearophile

Hi, some of these functions are documented in Tango's GC, which are probably identical: http://www.dsource.org/projects/tango/docs/current/tango.core.Memory.html There is also an example of overriding new for classes that contain some of these functions: http://www.digitalmars.com/d/memory.html#newdelete These links are about the first four functions only. These are useful to let the GC know whether some parts of memory should be scanned or not. I don't know about the other functions.
Nov 19 2007
prev sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
bearophile wrote:
 The std.gc module of Phobos contains various things that I don't undertand:
 
 void addRoot(void* p);
 void removeRoot(void* p);
 void addRange(void* pbot, void* ptop);
 void removeRange(void* pbot);
 void setTypeInfo(TypeInfo ti, void* p);
 void* getGCHandle();
 void setGCHandle(void* p);
 
 Maybe you can point me to some more info about their usage and usefulness, or
some code that uses them. (I think the docs of Phobos can gain some usage
examples of those functions).
 
 Bye and thank you,
 bearophile

There's nothing in any of that that allows you to allocate memory and then say, "Let the garbage collector handle this from now on." I guess I have to start writing code to delete the objects that I malloc'd; I have an embarrassing amount of memory leaks now.
Nov 19 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Christopher Wright" <dhasenan gmail.com> wrote in message 
news:fhs891$277v$1 digitalmars.com...

 There's nothing in any of that that allows you to allocate memory and then 
 say, "Let the garbage collector handle this from now on." I guess I have 
 to start writing code to delete the objects that I malloc'd; I have an 
 embarrassing amount of memory leaks now.

Oh yes there is :) std.gc.alloc, std.gc.realloc, and std.gc.extend all allow you to allocate arbitrary blocks of GC'ed memory. Also if you allocate memory with alloc (and possibly realloc?) you can flag the block as containing or not containing pointers using std.gc.hasPointers(ptr) and std.gc.hasNoPointers(ptr).
Nov 19 2007
parent reply Christopher Wright <dhasenan gmail.com> writes:
Jarrett Billingsley wrote:
 "Christopher Wright" <dhasenan gmail.com> wrote in message 
 news:fhs891$277v$1 digitalmars.com...
 
 There's nothing in any of that that allows you to allocate memory and then 
 say, "Let the garbage collector handle this from now on." I guess I have 
 to start writing code to delete the objects that I malloc'd; I have an 
 embarrassing amount of memory leaks now.

Oh yes there is :) std.gc.alloc, std.gc.realloc, and std.gc.extend all allow you to allocate arbitrary blocks of GC'ed memory. Also if you allocate memory with alloc (and possibly realloc?) you can flag the block as containing or not containing pointers using std.gc.hasPointers(ptr) and std.gc.hasNoPointers(ptr).

My mistake; thank you. That's why I didn't see a 'free' entry in std.gc. Is there any way to hook up a destructor, besides allocating memory with a constructor? I ask purely out of academic interest; I allocate objects manually in some cases, but I don't need to use destructors with them, currently.
Nov 19 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Christopher Wright" <dhasenan gmail.com> wrote in message 
news:fhsko3$2or4$2 digitalmars.com...

 My mistake; thank you. That's why I didn't see a 'free' entry in std.gc.

 Is there any way to hook up a destructor, besides allocating memory with a 
 constructor? I ask purely out of academic interest; I allocate objects 
 manually in some cases, but I don't need to use destructors with them, 
 currently.

Oddly, there is a "setFinalizer" function in the internal GC implementation, but for some reason there is no public function in std.gc that corresponds to it. Maybe it's experimental?
Nov 19 2007