[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ extension and garbage collection

On Mon, 25 Mar 2002, Ron Stanonik wrote:
> >From wxcommon/wxGC.cxx
>   void *operator new(size_t size)
>   {
>   #ifdef USE_SENORA_GC
>     if (!cpp_objects)
>       cpp_objects = GC_new_set("C++", NULL, NULL, NULL, NULL, NULL, 0);
>     return GC_malloc_specific(size, cpp_objects);
>   #else
>     return GC_malloc(size);
>   #endif
>   }
> Shouldn't this "new" be calling GC_malloc_uncollectable?

I asked that question because mzscheme/gc/gcc_support.c defined __builtin_new
to call GC_malloc_uncollectable.  But __builtin_new wasn't getting called,
even though g++ replaces references to new with references to __builtin_new,
so that the user can override new!

  void* __builtin_new( size )
      size_t size;
      For non-gc non-array types, the compiler generates calls to
      __builtin_new, which allocates non-collected storage via
      GC_MALLOC_UNCOLLECTABLE.  This ensures that the non-collected
      storage will be part of the collector's root set, required by the
      Ellis/Detlefs semantics. */
      vfp handler = __new_handler ? __new_handler : __default_new_handler;

      while (1) {
          void* o = GC_MALLOC_UNCOLLECTABLE( size + sizeof( Descriptor ) );
          if (o != 0) return o;
          (*handler) ();}}

This caused me to learn more about g++ new.  The compiler replaces
references to new with references to __builtin_new.  But the linker
links references to __builtin_new to "void *operator new(size_t size)",
if defined.  That is, if the user wants to override new, they must
define "void *operator new(size_t size)", not __builtin_new!

Both "void *operator new" and __builtin_new are defined in mzscheme.
"void *operator new" is getting called and not protecting C++ objects
from the garbage collector.  __builtin_new looks like it would protect
C++ objects (calls GC_malloc_uncollectable), but alas isn't getting