From: Alexander Herz Date: Sat, 20 Oct 2012 12:07:12 +0000 (+0400) Subject: Optionally put gc_cpp symbols into 'boehmgc' namespace X-Git-Tag: gc7_4_0~187 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5e360acbb258cbc08a3a3516ba7a206e62c427f9;p=gc Optionally put gc_cpp symbols into 'boehmgc' namespace * include/gc_cpp.h (GC_NAMESPACE): Recognize new macro. * include/gc_cpp.h (GC_NS_QUALIFY): New macro definition (prefixing argument with "boehmgc" namespace if GC_NAMESPACE). * include/gc_cpp.h (GCPlacement, gc, gc_cleanup, GCCleanUpFunc, gc::operator new, gc::operator delete, gc::operator new[], gc::operator delete[], gc_cleanup::~gc_cleanup, gc_cleanup::cleanup): Put into "boehmgc" namespace if GC_NAMESPACE. * include/gc_cpp.h (operator new, operator delete, operator new[]): Wrap GCPlacement, GCCleanUpFunc, UseGC, PointerFreeGC symbols into GC_NS_QUALIFY. * tests/test_cpp.cc (USE_GC, main): Wrap UseGC, GC, NoGC symbols into GC_NS_QUALIFY. * tests/test_cpp.cc (class B, class C, class D, class E): Wrap gc, gc_cleanup class names into GC_NS_QUALIFY. --- diff --git a/include/gc_cpp.h b/include/gc_cpp.h index 820dce22..6882786b 100644 --- a/include/gc_cpp.h +++ b/include/gc_cpp.h @@ -142,6 +142,12 @@ by UseGC. GC is an alias for UseGC, unless GC_NAME_CONFLICT is defined. #include "gc.h" +#ifdef GC_NAMESPACE +# define GC_NS_QUALIFY(T) boehmgc::T +#else +# define GC_NS_QUALIFY(T) T +#endif + #ifndef THINK_CPLUS # define GC_cdecl GC_CALLBACK #else @@ -168,13 +174,18 @@ by UseGC. GC is an alias for UseGC, unless GC_NAME_CONFLICT is defined. # define GC_PLACEMENT_DELETE #endif +#ifdef GC_NAMESPACE +namespace boehmgc +{ +#endif + enum GCPlacement { UseGC, # ifndef GC_NAME_CONFLICT GC=UseGC, # endif - NoGC, - PointerFreeGC + NoGC, + PointerFreeGC }; class gc { @@ -225,14 +236,18 @@ extern "C" { typedef void (GC_CALLBACK * GCCleanUpFunc)( void* obj, void* clientData ); } +#ifdef GC_NAMESPACE +} +#endif + #ifdef _MSC_VER // Disable warning that "no matching operator delete found; memory will // not be freed if initialization throws an exception" # pragma warning(disable:4291) #endif -inline void* operator new( size_t size, GCPlacement gcp, - GCCleanUpFunc cleanup = 0, +inline void* operator new( size_t size, GC_NS_QUALIFY(GCPlacement) gcp, + GC_NS_QUALIFY(GCCleanUpFunc) cleanup = 0, void* clientData = 0 ); /* Allocates a collectable or uncollected object, according to the @@ -249,7 +264,8 @@ inline void* operator new( size_t size, GCPlacement gcp, from "gc_cleanup". */ #ifdef GC_PLACEMENT_DELETE - inline void operator delete( void*, GCPlacement, GCCleanUpFunc, void * ); + inline void operator delete( void*, GC_NS_QUALIFY(GCPlacement), + GC_NS_QUALIFY(GCCleanUpFunc), void * ); #endif #ifdef _MSC_VER @@ -273,8 +289,8 @@ inline void* operator new( size_t size, GCPlacement gcp, #endif /* _MSC_VER */ #ifdef GC_OPERATOR_NEW_ARRAY - inline void* operator new[]( size_t size, GCPlacement gcp, - GCCleanUpFunc cleanup = 0, + inline void* operator new[]( size_t size, GC_NS_QUALIFY(GCPlacement) gcp, + GC_NS_QUALIFY(GCCleanUpFunc) cleanup = 0, void* clientData = 0 ); /* The operator new for arrays, identical to the above. */ #endif /* GC_OPERATOR_NEW_ARRAY */ @@ -285,6 +301,11 @@ Inline implementation ****************************************************************************/ +#ifdef GC_NAMESPACE +namespace boehmgc +{ +#endif + inline void* gc::operator new( size_t size ) { return GC_MALLOC( size ); } @@ -363,17 +384,22 @@ inline gc_cleanup::gc_cleanup() { } } -inline void* operator new( size_t size, GCPlacement gcp, - GCCleanUpFunc cleanup, void* clientData ) +#ifdef GC_NAMESPACE +} +#endif + +inline void* operator new( size_t size, GC_NS_QUALIFY(GCPlacement) gcp, + GC_NS_QUALIFY(GCCleanUpFunc) cleanup, + void* clientData ) { void* obj; - if (gcp == UseGC) { + if (gcp == GC_NS_QUALIFY(UseGC)) { obj = GC_MALLOC( size ); if (cleanup != 0) GC_REGISTER_FINALIZER_IGNORE_SELF( obj, cleanup, clientData, 0, 0 ); - } else if (gcp == PointerFreeGC) { + } else if (gcp == GC_NS_QUALIFY(PointerFreeGC)) { obj = GC_MALLOC_ATOMIC( size ); } else { obj = GC_MALLOC_UNCOLLECTABLE( size ); @@ -382,8 +408,8 @@ inline void* operator new( size_t size, GCPlacement gcp, } #ifdef GC_PLACEMENT_DELETE - inline void operator delete( void *p, GCPlacement /* gcp */, - GCCleanUpFunc /* cleanup */, + inline void operator delete( void *p, GC_NS_QUALIFY(GCPlacement) /* gcp */, + GC_NS_QUALIFY(GCCleanUpFunc) /* cleanup */, void* /* clientData */ ) { GC_FREE(p); @@ -391,8 +417,9 @@ inline void* operator new( size_t size, GCPlacement gcp, #endif /* GC_PLACEMENT_DELETE */ #ifdef GC_OPERATOR_NEW_ARRAY - inline void* operator new[]( size_t size, GCPlacement gcp, - GCCleanUpFunc cleanup, void* clientData ) + inline void* operator new[]( size_t size, GC_NS_QUALIFY(GCPlacement) gcp, + GC_NS_QUALIFY(GCCleanUpFunc) cleanup, + void* clientData ) { return ::operator new( size, gcp, cleanup, clientData ); } diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 1d47590c..0da985bd 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -58,10 +58,10 @@ extern "C" { #endif #ifdef GC_NAME_CONFLICT -# define USE_GC UseGC +# define USE_GC GC_NS_QUALIFY(UseGC) struct foo * GC; #else -# define USE_GC GC +# define USE_GC GC_NS_QUALIFY(GC) #endif #define my_assert( e ) \ @@ -85,7 +85,7 @@ class A {public: int i;}; -class B: public gc, public A {public: +class B: public GC_NS_QUALIFY(gc), public A { public: /* A collectable class. */ B( int j ): A( j ) {} @@ -98,7 +98,7 @@ class B: public gc, public A {public: int B::deleting = 0; -class C: public gc_cleanup, public A {public: +class C: public GC_NS_QUALIFY(gc_cleanup), public A { public: /* A collectable class with cleanup and virtual multiple inheritance. */ C( int levelArg ): A( levelArg ), level( levelArg ) { @@ -129,7 +129,7 @@ int C::nFreed = 0; int C::nAllocated = 0; -class D: public gc {public: +class D: public GC_NS_QUALIFY(gc) { public: /* A collectable class with a static member function to be used as an explicit clean-up function supplied to ::new. */ @@ -150,7 +150,7 @@ int D::nFreed = 0; int D::nAllocated = 0; -class E: public gc_cleanup {public: +class E: public GC_NS_QUALIFY(gc_cleanup) { public: /* A collectable class with clean-up for use by F. */ E() { @@ -244,8 +244,8 @@ int APIENTRY WinMain( HINSTANCE instance ATTR_UNUSED, GC_word as[ 1000 ]; GC_word bs[ 1000 ]; for (i = 0; i < 1000; i++) { - as[ i ] = Disguise( new (NoGC ) A( i ) ); - bs[ i ] = Disguise( new (NoGC) B( i ) );} + as[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) A(i) ); + bs[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) B(i) ); } /* Allocate a fair number of finalizable Cs, Ds, and Fs. Later we'll check to make sure they've gone away. */