gc_cpp.h 12 KB

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