[BACK]Return to gc_cpp.h CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / gc / include

Annotation of OpenXM_contrib2/asir2000/gc/include/gc_cpp.h, Revision 1.4

1.1       noro        1: #ifndef GC_CPP_H
                      2: #define GC_CPP_H
                      3: /****************************************************************************
                      4: Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
                      5:
                      6: THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
                      7: OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
                      8:
                      9: Permission is hereby granted to use or copy this program for any
                     10: purpose, provided the above notices are retained on all copies.
                     11: Permission to modify the code and to distribute modified code is
                     12: granted, provided the above notices are retained, and a notice that
                     13: the code was modified is included with the above copyright notice.
                     14: ****************************************************************************
                     15:
                     16: C++ Interface to the Boehm Collector
                     17:
                     18:     John R. Ellis and Jesse Hull
                     19:
                     20: This interface provides access to the Boehm collector.  It provides
                     21: basic facilities similar to those described in "Safe, Efficient
                     22: Garbage Collection for C++", by John R. Elis and David L. Detlefs
1.2       noro       23: (ftp://ftp.parc.xerox.com/pub/ellis/gc).
1.1       noro       24:
                     25: All heap-allocated objects are either "collectable" or
                     26: "uncollectable".  Programs must explicitly delete uncollectable
                     27: objects, whereas the garbage collector will automatically delete
                     28: collectable objects when it discovers them to be inaccessible.
                     29: Collectable objects may freely point at uncollectable objects and vice
                     30: versa.
                     31:
                     32: Objects allocated with the built-in "::operator new" are uncollectable.
                     33:
                     34: Objects derived from class "gc" are collectable.  For example:
                     35:
                     36:     class A: public gc {...};
                     37:     A* a = new A;       // a is collectable.
                     38:
                     39: Collectable instances of non-class types can be allocated using the GC
1.2       noro       40: (or UseGC) placement:
1.1       noro       41:
                     42:     typedef int A[ 10 ];
                     43:     A* a = new (GC) A;
                     44:
                     45: Uncollectable instances of classes derived from "gc" can be allocated
                     46: using the NoGC placement:
                     47:
                     48:     class A: public gc {...};
                     49:     A* a = new (NoGC) A;   // a is uncollectable.
                     50:
                     51: Both uncollectable and collectable objects can be explicitly deleted
                     52: with "delete", which invokes an object's destructors and frees its
                     53: storage immediately.
                     54:
                     55: A collectable object may have a clean-up function, which will be
                     56: invoked when the collector discovers the object to be inaccessible.
                     57: An object derived from "gc_cleanup" or containing a member derived
                     58: from "gc_cleanup" has a default clean-up function that invokes the
                     59: object's destructors.  Explicit clean-up functions may be specified as
                     60: an additional placement argument:
                     61:
                     62:     A* a = ::new (GC, MyCleanup) A;
                     63:
                     64: An object is considered "accessible" by the collector if it can be
                     65: reached by a path of pointers from static variables, automatic
                     66: variables of active functions, or from some object with clean-up
                     67: enabled; pointers from an object to itself are ignored.
                     68:
                     69: Thus, if objects A and B both have clean-up functions, and A points at
                     70: B, B is considered accessible.  After A's clean-up is invoked and its
                     71: storage released, B will then become inaccessible and will have its
                     72: clean-up invoked.  If A points at B and B points to A, forming a
                     73: cycle, then that's considered a storage leak, and neither will be
                     74: collectable.  See the interface gc.h for low-level facilities for
                     75: handling such cycles of objects with clean-up.
                     76:
                     77: The collector cannot guarrantee that it will find all inaccessible
                     78: objects.  In practice, it finds almost all of them.
                     79:
                     80:
                     81: Cautions:
                     82:
                     83: 1. Be sure the collector has been augmented with "make c++".
                     84:
                     85: 2.  If your compiler supports the new "operator new[]" syntax, then
1.4     ! noro       86: add -DGC_OPERATOR_NEW_ARRAY to the Makefile.
1.1       noro       87:
                     88: If your compiler doesn't support "operator new[]", beware that an
                     89: array of type T, where T is derived from "gc", may or may not be
                     90: allocated as a collectable object (it depends on the compiler).  Use
                     91: the explicit GC placement to make the array collectable.  For example:
                     92:
                     93:     class A: public gc {...};
                     94:     A* a1 = new A[ 10 ];        // collectable or uncollectable?
                     95:     A* a2 = new (GC) A[ 10 ];   // collectable
                     96:
                     97: 3. The destructors of collectable arrays of objects derived from
                     98: "gc_cleanup" will not be invoked properly.  For example:
                     99:
                    100:     class A: public gc_cleanup {...};
                    101:     A* a = new (GC) A[ 10 ];    // destructors not invoked correctly
                    102:
                    103: Typically, only the destructor for the first element of the array will
                    104: be invoked when the array is garbage-collected.  To get all the
                    105: destructors of any array executed, you must supply an explicit
                    106: clean-up function:
                    107:
                    108:     A* a = new (GC, MyCleanUp) A[ 10 ];
                    109:
                    110: (Implementing clean-up of arrays correctly, portably, and in a way
                    111: that preserves the correct exception semantics requires a language
                    112: extension, e.g. the "gc" keyword.)
                    113:
                    114: 4. Compiler bugs:
                    115:
                    116: * Solaris 2's CC (SC3.0) doesn't implement t->~T() correctly, so the
                    117: destructors of classes derived from gc_cleanup won't be invoked.
                    118: You'll have to explicitly register a clean-up function with
                    119: new-placement syntax.
                    120:
                    121: * Evidently cfront 3.0 does not allow destructors to be explicitly
                    122: invoked using the ANSI-conforming syntax t->~T().  If you're using
                    123: cfront 3.0, you'll have to comment out the class gc_cleanup, which
                    124: uses explicit invocation.
                    125:
1.2       noro      126: 5. GC name conflicts:
                    127:
                    128: Many other systems seem to use the identifier "GC" as an abbreviation
                    129: for "Graphics Context".  Since version 5.0, GC placement has been replaced
                    130: by UseGC.  GC is an alias for UseGC, unless GC_NAME_CONFLICT is defined.
                    131:
1.1       noro      132: ****************************************************************************/
                    133:
                    134: #include "gc.h"
                    135:
                    136: #ifndef THINK_CPLUS
                    137: #define _cdecl
                    138: #endif
                    139:
1.4     ! noro      140: #if ! defined( GC_NO_OPERATOR_NEW_ARRAY ) \
        !           141:     && !defined(_ENABLE_ARRAYNEW) /* Digimars */ \
        !           142:     && (defined(__BORLANDC__) && (__BORLANDC__ < 0x450) \
        !           143:        || (defined(__GNUC__) && \
        !           144:            (__GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 6)) \
        !           145:        || (defined(__WATCOMC__) && __WATCOMC__ < 1050))
        !           146: #   define GC_NO_OPERATOR_NEW_ARRAY
        !           147: #endif
        !           148:
        !           149: #if !defined(GC_NO_OPERATOR_NEW_ARRAY) && !defined(GC_OPERATOR_NEW_ARRAY)
        !           150: #   define GC_OPERATOR_NEW_ARRAY
1.1       noro      151: #endif
                    152:
1.2       noro      153: enum GCPlacement {UseGC,
                    154: #ifndef GC_NAME_CONFLICT
                    155:                  GC=UseGC,
                    156: #endif
                    157:                   NoGC, PointerFreeGC};
1.1       noro      158:
                    159: class gc {public:
                    160:     inline void* operator new( size_t size );
                    161:     inline void* operator new( size_t size, GCPlacement gcp );
1.4     ! noro      162:     inline void* operator new( size_t size, void *p );
        !           163:        /* Must be redefined here, since the other overloadings */
        !           164:        /* hide the global definition.                          */
1.1       noro      165:     inline void operator delete( void* obj );
1.4     ! noro      166:     inline void operator delete( void*, void* );
1.1       noro      167:
1.4     ! noro      168: #ifdef GC_OPERATOR_NEW_ARRAY
1.1       noro      169:     inline void* operator new[]( size_t size );
                    170:     inline void* operator new[]( size_t size, GCPlacement gcp );
1.4     ! noro      171:     inline void* operator new[]( size_t size, void *p );
1.1       noro      172:     inline void operator delete[]( void* obj );
1.4     ! noro      173:     inline void gc::operator delete[]( void*, void* );
        !           174: #endif /* GC_OPERATOR_NEW_ARRAY */
1.1       noro      175:     };
                    176:     /*
                    177:     Instances of classes derived from "gc" will be allocated in the
                    178:     collected heap by default, unless an explicit NoGC placement is
                    179:     specified. */
                    180:
                    181: class gc_cleanup: virtual public gc {public:
                    182:     inline gc_cleanup();
                    183:     inline virtual ~gc_cleanup();
                    184: private:
                    185:     inline static void _cdecl cleanup( void* obj, void* clientData );};
                    186:     /*
                    187:     Instances of classes derived from "gc_cleanup" will be allocated
                    188:     in the collected heap by default.  When the collector discovers an
                    189:     inaccessible object derived from "gc_cleanup" or containing a
                    190:     member derived from "gc_cleanup", its destructors will be
                    191:     invoked. */
                    192:
                    193: extern "C" {typedef void (*GCCleanUpFunc)( void* obj, void* clientData );}
                    194:
1.2       noro      195: #ifdef _MSC_VER
                    196:   // Disable warning that "no matching operator delete found; memory will
                    197:   // not be freed if initialization throws an exception"
                    198: # pragma warning(disable:4291)
                    199: #endif
                    200:
1.1       noro      201: inline void* operator new(
                    202:     size_t size,
                    203:     GCPlacement gcp,
                    204:     GCCleanUpFunc cleanup = 0,
                    205:     void* clientData = 0 );
                    206:     /*
                    207:     Allocates a collectable or uncollected object, according to the
                    208:     value of "gcp".
                    209:
                    210:     For collectable objects, if "cleanup" is non-null, then when the
                    211:     allocated object "obj" becomes inaccessible, the collector will
                    212:     invoke the function "cleanup( obj, clientData )" but will not
                    213:     invoke the object's destructors.  It is an error to explicitly
                    214:     delete an object allocated with a non-null "cleanup".
                    215:
                    216:     It is an error to specify a non-null "cleanup" with NoGC or for
                    217:     classes derived from "gc_cleanup" or containing members derived
                    218:     from "gc_cleanup". */
                    219:
                    220:
1.2       noro      221: #ifdef _MSC_VER
                    222:  /** This ensures that the system default operator new[] doesn't get
                    223:   *  undefined, which is what seems to happen on VC++ 6 for some reason
                    224:   *  if we define a multi-argument operator new[].
                    225:   *  There seems to be really redirect new in this environment without
                    226:   *  including this everywhere.
                    227:   */
1.4     ! noro      228:  void *operator new[]( size_t size );
        !           229:
        !           230:  void operator delete[](void* obj);
        !           231:
        !           232:  void* operator new( size_t size);
1.2       noro      233:
1.4     ! noro      234:  void operator delete(void* obj);
1.2       noro      235:
1.4     ! noro      236:  // This new operator is used by VC++ in case of Debug builds !
        !           237:  void* operator new(  size_t size,
1.2       noro      238:                      int ,//nBlockUse,
                    239:                      const char * szFileName,
1.4     ! noro      240:                      int nLine );
        !           241: #endif /* _MSC_VER */
        !           242:
1.2       noro      243:
1.4     ! noro      244: #ifdef GC_OPERATOR_NEW_ARRAY
1.2       noro      245:
1.1       noro      246: inline void* operator new[](
                    247:     size_t size,
                    248:     GCPlacement gcp,
                    249:     GCCleanUpFunc cleanup = 0,
                    250:     void* clientData = 0 );
                    251:     /*
                    252:     The operator new for arrays, identical to the above. */
                    253:
1.4     ! noro      254: #endif /* GC_OPERATOR_NEW_ARRAY */
1.1       noro      255:
                    256: /****************************************************************************
                    257:
                    258: Inline implementation
                    259:
                    260: ****************************************************************************/
                    261:
                    262: inline void* gc::operator new( size_t size ) {
                    263:     return GC_MALLOC( size );}
                    264:
                    265: inline void* gc::operator new( size_t size, GCPlacement gcp ) {
1.2       noro      266:     if (gcp == UseGC)
1.1       noro      267:         return GC_MALLOC( size );
                    268:     else if (gcp == PointerFreeGC)
                    269:        return GC_MALLOC_ATOMIC( size );
                    270:     else
                    271:         return GC_MALLOC_UNCOLLECTABLE( size );}
                    272:
1.4     ! noro      273: inline void* gc::operator new( size_t size, void *p ) {
        !           274:     return p;}
        !           275:
1.1       noro      276: inline void gc::operator delete( void* obj ) {
                    277:     GC_FREE( obj );}
                    278:
1.4     ! noro      279: inline void gc::operator delete( void*, void* ) {}
1.1       noro      280:
1.4     ! noro      281: #ifdef GC_OPERATOR_NEW_ARRAY
1.1       noro      282:
                    283: inline void* gc::operator new[]( size_t size ) {
                    284:     return gc::operator new( size );}
                    285:
                    286: inline void* gc::operator new[]( size_t size, GCPlacement gcp ) {
                    287:     return gc::operator new( size, gcp );}
                    288:
1.4     ! noro      289: inline void* gc::operator new[]( size_t size, void *p ) {
        !           290:     return p;}
        !           291:
1.1       noro      292: inline void gc::operator delete[]( void* obj ) {
                    293:     gc::operator delete( obj );}
1.4     ! noro      294:
        !           295: inline void gc::operator delete[]( void*, void* ) {}
1.1       noro      296:
1.4     ! noro      297: #endif /* GC_OPERATOR_NEW_ARRAY */
1.1       noro      298:
                    299:
                    300: inline gc_cleanup::~gc_cleanup() {
                    301:     GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 );}
                    302:
                    303: inline void gc_cleanup::cleanup( void* obj, void* displ ) {
                    304:     ((gc_cleanup*) ((char*) obj + (ptrdiff_t) displ))->~gc_cleanup();}
                    305:
                    306: inline gc_cleanup::gc_cleanup() {
                    307:     GC_finalization_proc oldProc;
                    308:     void* oldData;
                    309:     void* base = GC_base( (void *) this );
1.2       noro      310:     if (0 != base)  {
1.4     ! noro      311:       // Don't call the debug version, since this is a real base address.
        !           312:       GC_register_finalizer_ignore_self(
1.2       noro      313:         base, (GC_finalization_proc)cleanup, (void*) ((char*) this - (char*) base),
1.1       noro      314:         &oldProc, &oldData );
1.2       noro      315:       if (0 != oldProc) {
1.4     ! noro      316:         GC_register_finalizer_ignore_self( base, oldProc, oldData, 0, 0 );}}}
1.1       noro      317:
                    318: inline void* operator new(
                    319:     size_t size,
                    320:     GCPlacement gcp,
                    321:     GCCleanUpFunc cleanup,
                    322:     void* clientData )
                    323: {
                    324:     void* obj;
                    325:
1.2       noro      326:     if (gcp == UseGC) {
1.1       noro      327:         obj = GC_MALLOC( size );
                    328:         if (cleanup != 0)
                    329:             GC_REGISTER_FINALIZER_IGNORE_SELF(
                    330:                 obj, cleanup, clientData, 0, 0 );}
                    331:     else if (gcp == PointerFreeGC) {
                    332:         obj = GC_MALLOC_ATOMIC( size );}
                    333:     else {
                    334:         obj = GC_MALLOC_UNCOLLECTABLE( size );};
                    335:     return obj;}
                    336:
                    337:
1.4     ! noro      338: #ifdef GC_OPERATOR_NEW_ARRAY
1.1       noro      339:
                    340: inline void* operator new[](
                    341:     size_t size,
                    342:     GCPlacement gcp,
                    343:     GCCleanUpFunc cleanup,
                    344:     void* clientData )
                    345: {
                    346:     return ::operator new( size, gcp, cleanup, clientData );}
                    347:
1.4     ! noro      348: #endif /* GC_OPERATOR_NEW_ARRAY */
1.1       noro      349:
                    350:
                    351: #endif /* GC_CPP_H */
                    352:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>