Debian Codegraph @

external/bdwgc/include/gc_cpp.h

raw
1 /*
2 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
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
14 #ifndef GC_CPP_H
15 #define GC_CPP_H
16
17 /****************************************************************************
18 C++ Interface to the Boehm Collector
19
20 John R. Ellis and Jesse Hull
21
22 This interface provides access to the Boehm collector. It provides
23 basic facilities similar to those described in "Safe, Efficient
24 Garbage Collection for C++", by John R. Ellis and David L. Detlefs
25 (ftp://ftp.parc.xerox.com/pub/ellis/gc).
26
27 All heap-allocated objects are either "collectible" or
28 "uncollectible". Programs must explicitly delete uncollectible
29 objects, whereas the garbage collector will automatically delete
30 collectible objects when it discovers them to be inaccessible.
31 Collectible objects may freely point at uncollectible objects and vice
32 versa.
33
34 Objects allocated with the built-in "::operator new" are uncollectible.
35
36 Objects derived from class "gc" are collectible. For example:
37
38 class A: public gc {...};
39 A* a = new A; // a is collectible.
40
41 Collectible instances of non-class types can be allocated using the GC
42 (or UseGC) placement:
43
44 typedef int A[ 10 ];
45 A* a = new (GC) A;
46
47 Uncollectible instances of classes derived from "gc" can be allocated
48 using the NoGC placement:
49
50 class A: public gc {...};
51 A* a = new (NoGC) A; // a is uncollectible.
52
53 The new(PointerFreeGC) syntax allows the allocation of collectible
54 objects that are not scanned by the collector. This useful if you
55 are allocating compressed data, bitmaps, or network packets. (In
56 the latter case, it may remove danger of unfriendly network packets
57 intentionally containing values that cause spurious memory retention.)
58
59 Both uncollectible and collectible objects can be explicitly deleted
60 with "delete", which invokes an object's destructors and frees its
61 storage immediately.
62
63 A collectible object may have a clean-up function, which will be
64 invoked when the collector discovers the object to be inaccessible.
65 An object derived from "gc_cleanup" or containing a member derived
66 from "gc_cleanup" has a default clean-up function that invokes the
67 object's destructors. Explicit clean-up functions may be specified as
68 an additional placement argument:
69
70 A* a = ::new (GC, MyCleanup) A;
71
72 An object is considered "accessible" by the collector if it can be
73 reached by a path of pointers from static variables, automatic
74 variables of active functions, or from some object with clean-up
75 enabled; pointers from an object to itself are ignored.
76
77 Thus, if objects A and B both have clean-up functions, and A points at
78 B, B is considered accessible. After A's clean-up is invoked and its
79 storage released, B will then become inaccessible and will have its
80 clean-up invoked. If A points at B and B points to A, forming a
81 cycle, then that's considered a storage leak, and neither will be
82 collectible. See the interface gc.h for low-level facilities for
83 handling such cycles of objects with clean-up.
84
85 The collector cannot guarantee that it will find all inaccessible
86 objects. In practice, it finds almost all of them.
87
88
89 Cautions:
90
91 1. Be sure the collector has been augmented with "make c++" or
92 "--enable-cplusplus".
93
94 2. If your compiler supports the new "operator new[]" syntax, then
95 add -DGC_OPERATOR_NEW_ARRAY to the Makefile.
96
97 If your compiler doesn't support "operator new[]", beware that an
98 array of type T, where T is derived from "gc", may or may not be
99 allocated as a collectible object (it depends on the compiler). Use
100 the explicit GC placement to make the array collectible. For example:
101
102 class A: public gc {...};
103 A* a1 = new A[ 10 ]; // collectible or uncollectible?
104 A* a2 = new (GC) A[ 10 ]; // collectible.
105
106 3. The destructors of collectible arrays of objects derived from
107 "gc_cleanup" will not be invoked properly. For example:
108
109 class A: public gc_cleanup {...};
110 A* a = new (GC) A[ 10 ]; // destructors not invoked correctly
111
112 Typically, only the destructor for the first element of the array will
113 be invoked when the array is garbage-collected. To get all the
114 destructors of any array executed, you must supply an explicit
115 clean-up function:
116
117 A* a = new (GC, MyCleanUp) A[ 10 ];
118
119 (Implementing clean-up of arrays correctly, portably, and in a way
120 that preserves the correct exception semantics requires a language
121 extension, e.g. the "gc" keyword.)
122
123 4. Compiler bugs (now hopefully history):
124
125 * Solaris 2's CC (SC3.0) doesn't implement t->~T() correctly, so the
126 destructors of classes derived from gc_cleanup won't be invoked.
127 You'll have to explicitly register a clean-up function with
128 new-placement syntax.
129
130 * Evidently cfront 3.0 does not allow destructors to be explicitly
131 invoked using the ANSI-conforming syntax t->~T(). If you're using
132 cfront 3.0, you'll have to comment out the class gc_cleanup, which
133 uses explicit invocation.
134
135 5. GC name conflicts:
136
137 Many other systems seem to use the identifier "GC" as an abbreviation
138 for "Graphics Context". Since version 5.0, GC placement has been replaced
139 by UseGC. GC is an alias for UseGC, unless GC_NAME_CONFLICT is defined.
140
141 ****************************************************************************/
142
143 #include "gc.h"
144
145 #ifdef GC_NAMESPACE
146 # define GC_NS_QUALIFY(T) boehmgc::T
147 #else
148 # define GC_NS_QUALIFY(T) T
149 #endif
150
151 #ifndef THINK_CPLUS
152 # define GC_cdecl GC_CALLBACK
153 #else
154 # define GC_cdecl _cdecl
155 #endif
156
157 #if !defined(GC_NO_OPERATOR_NEW_ARRAY) \
158 && !defined(_ENABLE_ARRAYNEW) /* Digimars */ \
159 && (defined(__BORLANDC__) && (__BORLANDC__ < 0x450) \
160 || (defined(__GNUC__) && !GC_GNUC_PREREQ(2, 6)) \
161 || (defined(_MSC_VER) && _MSC_VER <= 1020) \
162 || (defined(__WATCOMC__) && __WATCOMC__ < 1050))
163 # define GC_NO_OPERATOR_NEW_ARRAY
164 #endif
165
166 #if !defined(GC_NO_OPERATOR_NEW_ARRAY) && !defined(GC_OPERATOR_NEW_ARRAY)
167 # define GC_OPERATOR_NEW_ARRAY
168 #endif
169
170 #if (!defined(__BORLANDC__) || __BORLANDC__ > 0x0620) \
171 && ! defined (__sgi) && ! defined(__WATCOMC__) \
172 && (!defined(_MSC_VER) || _MSC_VER > 1020)
173 # define GC_PLACEMENT_DELETE
174 #endif
175
176 #ifdef GC_NAMESPACE
177 namespace boehmgc
178 {
179 #endif
180
181 enum GCPlacement
182 {
183 UseGC,
184 # ifndef GC_NAME_CONFLICT
185 GC = UseGC,
186 # endif
187 NoGC,
188 PointerFreeGC
189 # ifdef GC_ATOMIC_UNCOLLECTABLE
190 , PointerFreeNoGC
191 # endif
192 };
193
194 /**
195 * Instances of classes derived from "gc" will be allocated in the collected
196 * heap by default, unless an explicit NoGC placement is specified.
197 */
198 class gc
199 {
200 public:
201 inline void* operator new(size_t size);
202 inline void* operator new(size_t size, GCPlacement gcp);
203 inline void* operator new(size_t size, void* p);
204 // Must be redefined here, since the other overloadings hide
205 // the global definition.
206 inline void operator delete(void* obj);
207
208 # ifdef GC_PLACEMENT_DELETE
209 inline void operator delete(void*, GCPlacement);
210 // Called if construction fails.
211 inline void operator delete(void*, void*);
212 # endif // GC_PLACEMENT_DELETE
213
214 # ifdef GC_OPERATOR_NEW_ARRAY
215 inline void* operator new[](size_t size);
216 inline void* operator new[](size_t size, GCPlacement gcp);
217 inline void* operator new[](size_t size, void* p);
218 inline void operator delete[](void* obj);
219 # ifdef GC_PLACEMENT_DELETE
220 inline void operator delete[](void*, GCPlacement);
221 inline void operator delete[](void*, void*);
222 # endif
223 # endif // GC_OPERATOR_NEW_ARRAY
224 };
225
226 /**
227 * Instances of classes derived from "gc_cleanup" will be allocated
228 * in the collected heap by default. When the collector discovers
229 * an inaccessible object derived from "gc_cleanup" or containing
230 * a member derived from "gc_cleanup", its destructors will be invoked.
231 */
232 class gc_cleanup: virtual public gc
233 {
234 public:
235 inline gc_cleanup();
236 inline virtual ~gc_cleanup();
237
238 private:
239 inline static void GC_cdecl cleanup(void* obj, void* clientData);
240 };
241
242 extern "C" {
243 typedef void (GC_CALLBACK * GCCleanUpFunc)(void* obj, void* clientData);
244 }
245
246 #ifdef GC_NAMESPACE
247 }
248 #endif
249
250 #ifdef _MSC_VER
251 // Disable warning that "no matching operator delete found; memory will
252 // not be freed if initialization throws an exception"
253 # pragma warning(disable:4291)
254 #endif
255
256 inline void* operator new(size_t size, GC_NS_QUALIFY(GCPlacement) gcp,
257 GC_NS_QUALIFY(GCCleanUpFunc) /* cleanup */ = 0,
258 void* /* clientData */ = 0);
259 // Allocates a collectible or uncollectible object, according to the
260 // value of "gcp".
261 //
262 // For collectible objects, if "cleanup" is non-null, then when the
263 // allocated object "obj" becomes inaccessible, the collector will
264 // invoke the function "cleanup(obj, clientData)" but will not
265 // invoke the object's destructors. It is an error to explicitly
266 // delete an object allocated with a non-null "cleanup".
267 //
268 // It is an error to specify a non-null "cleanup" with NoGC or for
269 // classes derived from "gc_cleanup" or containing members derived
270 // from "gc_cleanup".
271
272 #ifdef GC_PLACEMENT_DELETE
273 inline void operator delete(void*, GC_NS_QUALIFY(GCPlacement),
274 GC_NS_QUALIFY(GCCleanUpFunc), void*);
275 #endif
276
277 #ifdef _MSC_VER
278 // The following ensures that the system default operator new[] does not
279 // get undefined, which is what seems to happen on VC++ 6 for some reason
280 // if we define a multi-argument operator new[].
281 // There seems to be no way to redirect new in this environment without
282 // including this everywhere.
283 // Inlining done to avoid mix up of new and delete operators by VC++ 9 (due
284 // to arbitrary ordering during linking).
285
286 # if _MSC_VER > 1020
287 inline void* operator new[](size_t size)
288 {
289 return GC_MALLOC_UNCOLLECTABLE(size);
290 }
291
292 inline void operator delete[](void* obj)
293 {
294 GC_FREE(obj);
295 }
296 # endif
297
298 inline void* operator new(size_t size)
299 {
300 return GC_MALLOC_UNCOLLECTABLE(size);
301 }
302
303 inline void operator delete(void* obj)
304 {
305 GC_FREE(obj);
306 }
307
308 // This new operator is used by VC++ in case of Debug builds:
309 # ifdef GC_DEBUG
310 inline void* operator new(size_t size, int /* nBlockUse */,
311 const char* szFileName, int nLine)
312 {
313 return GC_debug_malloc_uncollectable(size, szFileName, nLine);
314 }
315 # else
316 inline void* operator new(size_t size, int /* nBlockUse */,
317 const char* /* szFileName */, int /* nLine */)
318 {
319 return GC_malloc_uncollectable(size);
320 }
321 # endif /* !GC_DEBUG */
322
323 # if _MSC_VER > 1020
324 // This new operator is used by VC++ 7+ in Debug builds:
325 inline void* operator new[](size_t size, int nBlockUse,
326 const char* szFileName, int nLine)
327 {
328 return operator new(size, nBlockUse, szFileName, nLine);
329 }
330 # endif
331
332 #endif // _MSC_VER
333
334 #ifdef GC_OPERATOR_NEW_ARRAY
335 // The operator new for arrays, identical to the above.
336 inline void* operator new[](size_t size, GC_NS_QUALIFY(GCPlacement) gcp,
337 GC_NS_QUALIFY(GCCleanUpFunc) /* cleanup */ = 0,
338 void* /* clientData */ = 0);
339 #endif // GC_OPERATOR_NEW_ARRAY
340
341 /* Inline implementation */
342
343 #ifdef GC_NAMESPACE
344 namespace boehmgc
345 {
346 #endif
347
348 inline void* gc::operator new(size_t size)
349 {
350 return GC_MALLOC(size);
351 }
352
353 inline void* gc::operator new(size_t size, GCPlacement gcp)
354 {
355 switch (gcp) {
356 case UseGC:
357 return GC_MALLOC(size);
358 case PointerFreeGC:
359 return GC_MALLOC_ATOMIC(size);
360 # ifdef GC_ATOMIC_UNCOLLECTABLE
361 case PointerFreeNoGC:
362 return GC_MALLOC_ATOMIC_UNCOLLECTABLE(size);
363 # endif
364 case NoGC:
365 default:
366 return GC_MALLOC_UNCOLLECTABLE(size);
367 }
368 }
369
370 inline void* gc::operator new(size_t /* size */, void* p)
371 {
372 return p;
373 }
374
375 inline void gc::operator delete(void* obj)
376 {
377 GC_FREE(obj);
378 }
379
380 #ifdef GC_PLACEMENT_DELETE
381 inline void gc::operator delete(void*, void*) {}
382
383 inline void gc::operator delete(void* p, GCPlacement /* gcp */)
384 {
385 GC_FREE(p);
386 }
387 #endif // GC_PLACEMENT_DELETE
388
389 #ifdef GC_OPERATOR_NEW_ARRAY
390 inline void* gc::operator new[](size_t size)
391 {
392 return gc::operator new(size);
393 }
394
395 inline void* gc::operator new[](size_t size, GCPlacement gcp)
396 {
397 return gc::operator new(size, gcp);
398 }
399
400 inline void* gc::operator new[](size_t /* size */, void* p)
401 {
402 return p;
403 }
404
405 inline void gc::operator delete[](void* obj)
406 {
407 gc::operator delete(obj);
408 }
409
410 # ifdef GC_PLACEMENT_DELETE
411 inline void gc::operator delete[](void*, void*) {}
412
413 inline void gc::operator delete[](void* p, GCPlacement /* gcp */)
414 {
415 gc::operator delete(p);
416 }
417 # endif
418 #endif // GC_OPERATOR_NEW_ARRAY
419
420 inline gc_cleanup::~gc_cleanup()
421 {
422 void* base = GC_base(this);
423 if (0 == base) return; // Non-heap object.
424 GC_register_finalizer_ignore_self(base, 0, 0, 0, 0);
425 }
426
427 inline void GC_CALLBACK gc_cleanup::cleanup(void* obj, void* displ)
428 {
429 ((gc_cleanup*) ((char*) obj + (ptrdiff_t) displ))->~gc_cleanup();
430 }
431
432 inline gc_cleanup::gc_cleanup()
433 {
434 GC_finalization_proc oldProc;
435 void* oldData;
436 void* this_ptr = (void*)this;
437 void* base = GC_base(this_ptr);
438 if (base != 0) {
439 // Don't call the debug version, since this is a real base address.
440 GC_register_finalizer_ignore_self(base, (GC_finalization_proc) cleanup,
441 (void*)((char*)this_ptr - (char*)base),
442 &oldProc, &oldData);
443 if (oldProc != 0) {
444 GC_register_finalizer_ignore_self(base, oldProc, oldData, 0, 0);
445 }
446 }
447 }
448
449 #ifdef GC_NAMESPACE
450 }
451 #endif
452
453 inline void* operator new(size_t size, GC_NS_QUALIFY(GCPlacement) gcp,
454 GC_NS_QUALIFY(GCCleanUpFunc) cleanup,
455 void* clientData)
456 {
457 void* obj;
458 switch (gcp) {
459 case GC_NS_QUALIFY(UseGC):
460 obj = GC_MALLOC(size);
461 if (cleanup != 0) {
462 GC_REGISTER_FINALIZER_IGNORE_SELF(obj, cleanup, clientData, 0, 0);
463 }
464 return obj;
465 case GC_NS_QUALIFY(PointerFreeGC):
466 return GC_MALLOC_ATOMIC(size);
467 # ifdef GC_ATOMIC_UNCOLLECTABLE
468 case GC_NS_QUALIFY(PointerFreeNoGC):
469 return GC_MALLOC_ATOMIC_UNCOLLECTABLE(size);
470 # endif
471 case GC_NS_QUALIFY(NoGC):
472 default:
473 return GC_MALLOC_UNCOLLECTABLE(size);
474 }
475 }
476
477 #ifdef GC_PLACEMENT_DELETE
478 inline void operator delete(void* p, GC_NS_QUALIFY(GCPlacement) /* gcp */,
479 GC_NS_QUALIFY(GCCleanUpFunc) /* cleanup */,
480 void* /* clientData */)
481 {
482 GC_FREE(p);
483 }
484 #endif // GC_PLACEMENT_DELETE
485
486 #ifdef GC_OPERATOR_NEW_ARRAY
487 inline void* operator new[](size_t size, GC_NS_QUALIFY(GCPlacement) gcp,
488 GC_NS_QUALIFY(GCCleanUpFunc) cleanup,
489 void* clientData)
490 {
491 return ::operator new(size, gcp, cleanup, clientData);
492 }
493 #endif // GC_OPERATOR_NEW_ARRAY
494
495 #endif /* GC_CPP_H */