Files

568 lines
17 KiB
C++
Raw Permalink Normal View History

2009-09-16 14:43:37 +00:00
/*
* Copyright (c) 1994 by Xerox Corporation. All rights reserved.
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program for any
* purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is
* granted, provided the above notices are retained, and a notice that
* the code was modified is included with the above copyright notice.
*/
2011-07-26 14:51:28 +04:00
#ifndef GC_CPP_H
#define GC_CPP_H
2009-09-16 14:43:37 +00:00
/****************************************************************************
2011-07-26 14:51:28 +04:00
C++ Interface to the Boehm Collector
2009-09-16 14:43:37 +00:00
John R. Ellis and Jesse Hull
2011-07-26 14:51:28 +04:00
This interface provides access to the Boehm collector. It provides
basic facilities similar to those described in "Safe, Efficient
2014-05-15 21:41:21 +04:00
Garbage Collection for C++", by John R. Ellis and David L. Detlefs
2011-07-26 15:36:22 +04:00
(ftp://ftp.parc.xerox.com/pub/ellis/gc).
2011-07-26 14:51:28 +04:00
2013-07-11 11:57:32 +04:00
All heap-allocated objects are either "collectible" or
"uncollectible". Programs must explicitly delete uncollectible
2011-07-26 14:51:28 +04:00
objects, whereas the garbage collector will automatically delete
2013-07-11 11:57:32 +04:00
collectible objects when it discovers them to be inaccessible.
Collectible objects may freely point at uncollectible objects and vice
2011-07-26 14:51:28 +04:00
versa.
2013-07-11 11:57:32 +04:00
Objects allocated with the built-in "::operator new" are uncollectible.
2011-07-26 14:51:28 +04:00
2013-07-11 11:57:32 +04:00
Objects derived from class "gc" are collectible. For example:
2011-07-26 14:51:28 +04:00
class A: public gc {...};
2013-07-11 11:57:32 +04:00
A* a = new A; // a is collectible.
2011-07-26 14:51:28 +04:00
2013-07-11 11:57:32 +04:00
Collectible instances of non-class types can be allocated using the GC
2011-07-26 15:36:22 +04:00
(or UseGC) placement:
2011-07-26 14:51:28 +04:00
typedef int A[ 10 ];
A* a = new (GC) A;
2013-07-11 11:57:32 +04:00
Uncollectible instances of classes derived from "gc" can be allocated
2011-07-26 14:51:28 +04:00
using the NoGC placement:
class A: public gc {...};
2013-07-11 11:57:32 +04:00
A* a = new (NoGC) A; // a is uncollectible.
2011-07-26 14:51:28 +04:00
2013-07-11 11:57:32 +04:00
The new(PointerFreeGC) syntax allows the allocation of collectible
2007-05-11 00:30:36 +00:00
objects that are not scanned by the collector. This useful if you
are allocating compressed data, bitmaps, or network packets. (In
the latter case, it may remove danger of unfriendly network packets
intentionally containing values that cause spurious memory retention.)
2013-07-11 11:57:32 +04:00
Both uncollectible and collectible objects can be explicitly deleted
2011-07-26 14:51:28 +04:00
with "delete", which invokes an object's destructors and frees its
storage immediately.
2013-07-11 11:57:32 +04:00
A collectible object may have a clean-up function, which will be
2011-07-26 14:51:28 +04:00
invoked when the collector discovers the object to be inaccessible.
An object derived from "gc_cleanup" or containing a member derived
from "gc_cleanup" has a default clean-up function that invokes the
object's destructors. Explicit clean-up functions may be specified as
an additional placement argument:
A* a = ::new (GC, MyCleanup) A;
An object is considered "accessible" by the collector if it can be
reached by a path of pointers from static variables, automatic
variables of active functions, or from some object with clean-up
enabled; pointers from an object to itself are ignored.
Thus, if objects A and B both have clean-up functions, and A points at
B, B is considered accessible. After A's clean-up is invoked and its
storage released, B will then become inaccessible and will have its
clean-up invoked. If A points at B and B points to A, forming a
cycle, then that's considered a storage leak, and neither will be
2013-07-11 11:57:32 +04:00
collectible. See the interface gc.h for low-level facilities for
2011-07-26 14:51:28 +04:00
handling such cycles of objects with clean-up.
2011-07-26 18:04:46 +04:00
The collector cannot guarantee that it will find all inaccessible
2011-07-26 14:51:28 +04:00
objects. In practice, it finds almost all of them.
Cautions:
2007-05-11 00:30:36 +00:00
1. Be sure the collector has been augmented with "make c++" or
"--enable-cplusplus".
2011-07-26 14:51:28 +04:00
2. If your compiler supports the new "operator new[]" syntax, then
2011-07-26 16:56:55 +04:00
add -DGC_OPERATOR_NEW_ARRAY to the Makefile.
2011-07-26 14:51:28 +04:00
If your compiler doesn't support "operator new[]", beware that an
array of type T, where T is derived from "gc", may or may not be
2013-07-11 11:57:32 +04:00
allocated as a collectible object (it depends on the compiler). Use
the explicit GC placement to make the array collectible. For example:
2011-07-26 14:51:28 +04:00
class A: public gc {...};
2013-07-11 11:57:32 +04:00
A* a1 = new A[ 10 ]; // collectible or uncollectible?
A* a2 = new (GC) A[ 10 ]; // collectible.
2011-07-26 14:51:28 +04:00
2013-07-11 11:57:32 +04:00
3. The destructors of collectible arrays of objects derived from
2011-07-26 14:51:28 +04:00
"gc_cleanup" will not be invoked properly. For example:
class A: public gc_cleanup {...};
A* a = new (GC) A[ 10 ]; // destructors not invoked correctly
Typically, only the destructor for the first element of the array will
be invoked when the array is garbage-collected. To get all the
destructors of any array executed, you must supply an explicit
clean-up function:
A* a = new (GC, MyCleanUp) A[ 10 ];
(Implementing clean-up of arrays correctly, portably, and in a way
that preserves the correct exception semantics requires a language
extension, e.g. the "gc" keyword.)
2007-05-11 00:30:36 +00:00
4. Compiler bugs (now hopefully history):
2011-07-26 14:51:28 +04:00
* Solaris 2's CC (SC3.0) doesn't implement t->~T() correctly, so the
destructors of classes derived from gc_cleanup won't be invoked.
You'll have to explicitly register a clean-up function with
new-placement syntax.
* Evidently cfront 3.0 does not allow destructors to be explicitly
invoked using the ANSI-conforming syntax t->~T(). If you're using
cfront 3.0, you'll have to comment out the class gc_cleanup, which
uses explicit invocation.
2011-07-26 15:36:22 +04:00
5. GC name conflicts:
Many other systems seem to use the identifier "GC" as an abbreviation
for "Graphics Context". Since version 5.0, GC placement has been replaced
by UseGC. GC is an alias for UseGC, unless GC_NAME_CONFLICT is defined.
2011-07-26 14:51:28 +04:00
****************************************************************************/
#include "gc.h"
#ifdef GC_NAMESPACE
# define GC_NS_QUALIFY(T) boehmgc::T
#else
# define GC_NS_QUALIFY(T) T
#endif
2011-07-26 14:51:28 +04:00
#ifndef THINK_CPLUS
2015-05-11 18:25:22 +03:00
# define GC_cdecl GC_CALLBACK
2011-07-26 17:08:53 +04:00
#else
2015-05-11 18:25:22 +03:00
# define GC_cdecl _cdecl
2011-07-26 14:51:28 +04:00
#endif
2015-05-11 18:25:22 +03:00
#if !defined(GC_NO_OPERATOR_NEW_ARRAY) \
2011-07-26 16:56:55 +04:00
&& !defined(_ENABLE_ARRAYNEW) /* Digimars */ \
&& (defined(__BORLANDC__) && (__BORLANDC__ < 0x450) \
|| (defined(__GNUC__) && !GC_GNUC_PREREQ(2, 6)) \
2010-01-15 07:23:05 +00:00
|| (defined(_MSC_VER) && _MSC_VER <= 1020) \
2009-09-16 14:43:37 +00:00
|| (defined(__WATCOMC__) && __WATCOMC__ < 1050))
2015-05-11 18:25:22 +03:00
# define GC_NO_OPERATOR_NEW_ARRAY
2011-07-26 16:56:55 +04:00
#endif
#if !defined(GC_NO_OPERATOR_NEW_ARRAY) && !defined(GC_OPERATOR_NEW_ARRAY)
2015-05-11 18:25:22 +03:00
# define GC_OPERATOR_NEW_ARRAY
2011-07-26 14:51:28 +04:00
#endif
2010-01-15 07:23:05 +00:00
#if (!defined(__BORLANDC__) || __BORLANDC__ > 0x0620) \
2015-05-11 18:25:22 +03:00
&& ! defined (__sgi) && ! defined(__WATCOMC__) \
&& (!defined(_MSC_VER) || _MSC_VER > 1020)
2015-05-11 18:25:22 +03:00
# define GC_PLACEMENT_DELETE
2011-07-26 17:49:27 +04:00
#endif
2018-06-08 11:34:23 +03:00
#ifndef GC_NOEXCEPT
# if defined(__DMC__) || (defined(__BORLANDC__) \
&& (defined(_RWSTD_NO_EXCEPTIONS) || defined(_RWSTD_NO_EX_SPEC))) \
|| (defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS) \
|| (defined(__WATCOMC__) && !defined(_CPPUNWIND))
# define GC_NOEXCEPT /* empty */
# ifndef GC_NEW_ABORTS_ON_OOM
# define GC_NEW_ABORTS_ON_OOM
# endif
# elif __cplusplus >= 201103L
# define GC_NOEXCEPT noexcept
# else
2018-06-08 11:34:23 +03:00
# define GC_NOEXCEPT throw()
# endif
2018-06-08 11:34:23 +03:00
#endif // !GC_NOEXCEPT
#if defined(GC_NEW_ABORTS_ON_OOM) || defined(_LIBCPP_NO_EXCEPTIONS)
# define GC_OP_NEW_OOM_CHECK(obj) \
do { if (!(obj)) GC_abort_on_oom(); } while (0)
#elif defined(GC_INCLUDE_NEW)
# include <new> // for bad_alloc
# define GC_OP_NEW_OOM_CHECK(obj) if (obj) {} else throw std::bad_alloc()
#else
// "new" header is not included, so bad_alloc cannot be thrown directly.
GC_API void GC_CALL GC_throw_bad_alloc();
# define GC_OP_NEW_OOM_CHECK(obj) if (obj) {} else GC_throw_bad_alloc()
#endif // !GC_NEW_ABORTS_ON_OOM && !GC_INCLUDE_NEW
#ifdef GC_NAMESPACE
namespace boehmgc
{
#endif
2011-01-24 17:56:37 +00:00
enum GCPlacement
{
2011-12-26 10:22:29 +04:00
UseGC,
# ifndef GC_NAME_CONFLICT
2015-05-11 18:25:22 +03:00
GC = UseGC,
2011-12-26 10:22:29 +04:00
# endif
NoGC,
PointerFreeGC
# ifdef GC_ATOMIC_UNCOLLECTABLE
, PointerFreeNoGC
# endif
2011-12-26 10:22:29 +04:00
};
2011-07-26 14:51:28 +04:00
2015-05-11 18:25:22 +03:00
/**
* Instances of classes derived from "gc" will be allocated in the collected
* heap by default, unless an explicit NoGC placement is specified.
*/
class gc
{
public:
2018-06-15 11:20:42 +03:00
inline void* operator new(size_t size);
inline void* operator new(size_t size, GCPlacement gcp);
2018-06-08 11:34:23 +03:00
inline void* operator new(size_t size, void* p) GC_NOEXCEPT;
2015-05-11 18:25:22 +03:00
// Must be redefined here, since the other overloadings hide
// the global definition.
2018-06-08 11:34:23 +03:00
inline void operator delete(void* obj) GC_NOEXCEPT;
2011-07-26 14:51:28 +04:00
2015-05-11 18:25:22 +03:00
# ifdef GC_PLACEMENT_DELETE
2018-06-08 11:34:23 +03:00
inline void operator delete(void*, GCPlacement) GC_NOEXCEPT;
2015-05-11 18:25:22 +03:00
// Called if construction fails.
2018-06-08 11:34:23 +03:00
inline void operator delete(void*, void*) GC_NOEXCEPT;
2015-05-11 18:25:22 +03:00
# endif // GC_PLACEMENT_DELETE
# ifdef GC_OPERATOR_NEW_ARRAY
2018-06-15 11:20:42 +03:00
inline void* operator new[](size_t size);
inline void* operator new[](size_t size, GCPlacement gcp);
2018-06-08 11:34:23 +03:00
inline void* operator new[](size_t size, void* p) GC_NOEXCEPT;
inline void operator delete[](void* obj) GC_NOEXCEPT;
2011-07-26 17:49:27 +04:00
# ifdef GC_PLACEMENT_DELETE
2018-06-08 11:34:23 +03:00
inline void operator delete[](void*, GCPlacement) GC_NOEXCEPT;
inline void operator delete[](void*, void*) GC_NOEXCEPT;
2011-07-26 17:28:12 +04:00
# endif
2015-05-11 18:25:22 +03:00
# endif // GC_OPERATOR_NEW_ARRAY
2011-12-26 10:22:29 +04:00
};
2011-07-26 14:51:28 +04:00
2015-05-11 18:25:22 +03:00
/**
* Instances of classes derived from "gc_cleanup" will be allocated
* in the collected heap by default. When the collector discovers
* an inaccessible object derived from "gc_cleanup" or containing
* a member derived from "gc_cleanup", its destructors will be invoked.
*/
class gc_cleanup: virtual public gc
{
public:
inline gc_cleanup();
inline virtual ~gc_cleanup();
2011-07-26 14:51:28 +04:00
private:
2015-05-11 18:25:22 +03:00
inline static void GC_cdecl cleanup(void* obj, void* clientData);
2011-12-26 10:22:29 +04:00
};
2011-07-26 14:51:28 +04:00
extern "C" {
2015-05-11 18:25:22 +03:00
typedef void (GC_CALLBACK * GCCleanUpFunc)(void* obj, void* clientData);
}
2011-07-26 14:51:28 +04:00
#ifdef GC_NAMESPACE
}
#endif
2011-07-26 16:56:55 +04:00
#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)
// TODO: "non-member operator new or delete may not be declared inline"
// warning is disabled for now.
# pragma warning(disable:4595)
2011-07-26 16:56:55 +04:00
#endif
2015-05-11 18:25:22 +03:00
inline void* operator new(size_t size, GC_NS_QUALIFY(GCPlacement) gcp,
GC_NS_QUALIFY(GCCleanUpFunc) /* cleanup */ = 0,
2018-06-15 11:20:42 +03:00
void* /* clientData */ = 0);
2015-05-11 18:25:22 +03:00
// Allocates a collectible or uncollectible object, according to the
// value of "gcp".
//
// For collectible objects, if "cleanup" is non-null, then when the
// allocated object "obj" becomes inaccessible, the collector will
// invoke the function "cleanup(obj, clientData)" but will not
// invoke the object's destructors. It is an error to explicitly
// delete an object allocated with a non-null "cleanup".
//
// It is an error to specify a non-null "cleanup" with NoGC or for
// classes derived from "gc_cleanup" or containing members derived
// from "gc_cleanup".
2011-07-26 14:51:28 +04:00
2011-04-20 21:25:18 +00:00
#ifdef GC_PLACEMENT_DELETE
2015-05-11 18:25:22 +03:00
inline void operator delete(void*, GC_NS_QUALIFY(GCPlacement),
2018-06-08 11:34:23 +03:00
GC_NS_QUALIFY(GCCleanUpFunc),
void*) GC_NOEXCEPT;
2011-04-20 21:25:18 +00:00
#endif
2011-07-26 14:51:28 +04:00
#if defined(_MSC_VER) || defined(__DMC__) \
|| ((defined(__CYGWIN32__) || defined(__CYGWIN__) \
|| defined(__MINGW32__)) && !defined(GC_BUILD) && !defined(GC_NOT_DLL))
2015-05-11 18:25:22 +03:00
// The following ensures that the system default operator new[] does not
// get undefined, which is what seems to happen on VC++ 6 for some reason
// if we define a multi-argument operator new[].
// There seems to be no way to redirect new in this environment without
// including this everywhere.
// Inlining done to avoid mix up of new and delete operators by VC++ 9 (due
// to arbitrary ordering during linking).
# ifdef GC_OPERATOR_NEW_ARRAY
2018-06-15 11:20:42 +03:00
inline void* operator new[](size_t size)
2010-07-13 15:14:53 +01:00
{
void* obj = GC_MALLOC_UNCOLLECTABLE(size);
GC_OP_NEW_OOM_CHECK(obj);
return obj;
2010-07-13 15:14:53 +01:00
}
2018-06-08 11:34:23 +03:00
inline void operator delete[](void* obj) GC_NOEXCEPT
2010-07-13 15:14:53 +01:00
{
2015-05-11 18:25:22 +03:00
GC_FREE(obj);
2010-07-13 15:14:53 +01:00
}
2015-05-11 18:25:22 +03:00
# endif
2010-07-13 15:14:53 +01:00
2018-06-15 11:20:42 +03:00
inline void* operator new(size_t size)
2015-05-11 18:25:22 +03:00
{
void* obj = GC_MALLOC_UNCOLLECTABLE(size);
GC_OP_NEW_OOM_CHECK(obj);
return obj;
2015-05-11 18:25:22 +03:00
}
2010-07-13 15:14:53 +01:00
2018-06-08 11:34:23 +03:00
inline void operator delete(void* obj) GC_NOEXCEPT
2015-05-11 18:25:22 +03:00
{
GC_FREE(obj);
}
# if __cplusplus > 201103L // C++14
inline void operator delete(void* obj, size_t size) GC_NOEXCEPT {
(void)size; // size is ignored
GC_FREE(obj);
}
# if defined(GC_OPERATOR_NEW_ARRAY)
inline void operator delete[](void* obj, size_t size) GC_NOEXCEPT {
(void)size;
GC_FREE(obj);
}
# endif
# endif // C++14
#endif
#ifdef _MSC_VER
2015-05-11 18:25:22 +03:00
// This new operator is used by VC++ in case of Debug builds:
# ifdef GC_DEBUG
inline void* operator new(size_t size, int /* nBlockUse */,
const char* szFileName, int nLine)
{
void* obj = GC_debug_malloc_uncollectable(size, szFileName, nLine);
GC_OP_NEW_OOM_CHECK(obj);
return obj;
}
# else
inline void* operator new(size_t size, int /* nBlockUse */,
const char* /* szFileName */, int /* nLine */)
{
void* obj = GC_malloc_uncollectable(size);
GC_OP_NEW_OOM_CHECK(obj);
return obj;
}
# endif /* !GC_DEBUG */
2015-05-11 18:25:22 +03:00
# ifdef GC_OPERATOR_NEW_ARRAY
// This new operator is used by VC++ 7+ in Debug builds:
inline void* operator new[](size_t size, int nBlockUse,
const char* szFileName, int nLine)
{
return operator new(size, nBlockUse, szFileName, nLine);
}
# endif
2011-07-26 16:56:55 +04:00
2015-05-11 18:25:22 +03:00
#endif // _MSC_VER
2011-07-26 15:36:22 +04:00
2011-07-26 17:07:21 +04:00
#ifdef GC_OPERATOR_NEW_ARRAY
2015-05-11 18:25:22 +03:00
// The operator new for arrays, identical to the above.
inline void* operator new[](size_t size, GC_NS_QUALIFY(GCPlacement) gcp,
GC_NS_QUALIFY(GCCleanUpFunc) /* cleanup */ = 0,
2018-06-15 11:20:42 +03:00
void* /* clientData */ = 0);
2015-05-11 18:25:22 +03:00
#endif // GC_OPERATOR_NEW_ARRAY
2011-07-26 14:51:28 +04:00
2015-05-11 18:25:22 +03:00
/* Inline implementation */
2011-07-26 14:51:28 +04:00
#ifdef GC_NAMESPACE
namespace boehmgc
{
#endif
2018-06-15 11:20:42 +03:00
inline void* gc::operator new(size_t size)
2011-01-24 17:56:37 +00:00
{
void* obj = GC_MALLOC(size);
GC_OP_NEW_OOM_CHECK(obj);
return obj;
2011-12-26 10:22:29 +04:00
}
2011-07-26 14:51:28 +04:00
2018-06-15 11:20:42 +03:00
inline void* gc::operator new(size_t size, GCPlacement gcp)
2015-05-11 18:25:22 +03:00
{
void* obj;
2015-05-11 18:25:22 +03:00
switch (gcp) {
case UseGC:
obj = GC_MALLOC(size);
break;
2015-05-11 18:25:22 +03:00
case PointerFreeGC:
obj = GC_MALLOC_ATOMIC(size);
break;
# ifdef GC_ATOMIC_UNCOLLECTABLE
case PointerFreeNoGC:
obj = GC_MALLOC_ATOMIC_UNCOLLECTABLE(size);
break;
# endif
2015-05-11 18:25:22 +03:00
case NoGC:
default:
obj = GC_MALLOC_UNCOLLECTABLE(size);
2015-05-11 18:25:22 +03:00
}
GC_OP_NEW_OOM_CHECK(obj);
return obj;
2011-12-26 10:22:29 +04:00
}
2011-07-26 17:08:53 +04:00
2018-06-08 11:34:23 +03:00
inline void* gc::operator new(size_t /* size */, void* p) GC_NOEXCEPT
2015-05-11 18:25:22 +03:00
{
return p;
}
2018-06-08 11:34:23 +03:00
inline void gc::operator delete(void* obj) GC_NOEXCEPT
2015-05-11 18:25:22 +03:00
{
GC_FREE(obj);
2011-12-26 10:22:29 +04:00
}
2009-09-16 14:43:37 +00:00
2011-07-26 17:49:27 +04:00
#ifdef GC_PLACEMENT_DELETE
2018-06-08 11:34:23 +03:00
inline void gc::operator delete(void*, void*) GC_NOEXCEPT {}
2008-02-16 06:07:00 +00:00
2018-06-08 11:34:23 +03:00
inline void gc::operator delete(void* p, GCPlacement /* gcp */) GC_NOEXCEPT
2015-05-11 18:25:22 +03:00
{
2008-02-16 06:07:00 +00:00
GC_FREE(p);
}
2015-05-11 18:25:22 +03:00
#endif // GC_PLACEMENT_DELETE
2011-07-26 14:51:28 +04:00
2011-07-26 16:56:55 +04:00
#ifdef GC_OPERATOR_NEW_ARRAY
2018-06-15 11:20:42 +03:00
inline void* gc::operator new[](size_t size)
2015-05-11 18:25:22 +03:00
{
return gc::operator new(size);
2011-12-26 10:22:29 +04:00
}
2009-09-16 14:43:37 +00:00
2015-05-11 18:25:22 +03:00
inline void* gc::operator new[](size_t size, GCPlacement gcp)
{
return gc::operator new(size, gcp);
2011-12-26 10:22:29 +04:00
}
2011-07-26 14:51:28 +04:00
2018-06-08 11:34:23 +03:00
inline void* gc::operator new[](size_t /* size */, void* p) GC_NOEXCEPT
2015-05-11 18:25:22 +03:00
{
2011-12-26 10:22:29 +04:00
return p;
}
2011-07-26 17:08:53 +04:00
2018-06-08 11:34:23 +03:00
inline void gc::operator delete[](void* obj) GC_NOEXCEPT
2015-05-11 18:25:22 +03:00
{
gc::operator delete(obj);
2011-12-26 10:22:29 +04:00
}
2011-07-26 17:08:53 +04:00
2011-04-20 21:25:18 +00:00
# ifdef GC_PLACEMENT_DELETE
2018-06-08 11:34:23 +03:00
inline void gc::operator delete[](void*, void*) GC_NOEXCEPT {}
2009-09-16 14:43:37 +00:00
2018-06-08 11:34:23 +03:00
inline void gc::operator delete[](void* p,
GCPlacement /* gcp */) GC_NOEXCEPT
2015-05-11 18:25:22 +03:00
{
2011-12-26 10:22:29 +04:00
gc::operator delete(p);
}
2011-04-20 21:25:18 +00:00
# endif
2015-05-11 18:25:22 +03:00
#endif // GC_OPERATOR_NEW_ARRAY
2011-07-26 14:51:28 +04:00
2015-05-11 18:25:22 +03:00
inline gc_cleanup::~gc_cleanup()
{
void* base = GC_base(this);
if (0 == base) return; // Non-heap object.
GC_register_finalizer_ignore_self(base, 0, 0, 0, 0);
2011-12-26 10:22:29 +04:00
}
2011-07-26 14:51:28 +04:00
2015-05-11 18:25:22 +03:00
inline void GC_CALLBACK gc_cleanup::cleanup(void* obj, void* displ)
{
((gc_cleanup*) ((char*) obj + (ptrdiff_t) displ))->~gc_cleanup();
2011-12-26 10:22:29 +04:00
}
2011-07-26 14:51:28 +04:00
2015-05-11 18:25:22 +03:00
inline gc_cleanup::gc_cleanup()
{
GC_finalization_proc oldProc;
void* oldData;
void* this_ptr = (void*)this;
void* base = GC_base(this_ptr);
2015-05-11 18:25:22 +03:00
if (base != 0) {
// Don't call the debug version, since this is a real base address.
GC_register_finalizer_ignore_self(base, (GC_finalization_proc) cleanup,
(void*)((char*)this_ptr - (char*)base),
2015-05-11 18:25:22 +03:00
&oldProc, &oldData);
if (oldProc != 0) {
GC_register_finalizer_ignore_self(base, oldProc, oldData, 0, 0);
2011-12-26 10:22:29 +04:00
}
2015-05-11 18:25:22 +03:00
}
2011-12-26 10:22:29 +04:00
}
2011-07-26 14:51:28 +04:00
#ifdef GC_NAMESPACE
}
#endif
2015-05-11 18:25:22 +03:00
inline void* operator new(size_t size, GC_NS_QUALIFY(GCPlacement) gcp,
GC_NS_QUALIFY(GCCleanUpFunc) cleanup,
2018-06-15 11:20:42 +03:00
void* clientData)
2011-07-26 14:51:28 +04:00
{
2015-05-11 18:25:22 +03:00
void* obj;
switch (gcp) {
case GC_NS_QUALIFY(UseGC):
obj = GC_MALLOC(size);
if (cleanup != 0 && obj != 0) {
2015-05-11 18:25:22 +03:00
GC_REGISTER_FINALIZER_IGNORE_SELF(obj, cleanup, clientData, 0, 0);
2011-01-24 17:56:37 +00:00
}
break;
2015-05-11 18:25:22 +03:00
case GC_NS_QUALIFY(PointerFreeGC):
obj = GC_MALLOC_ATOMIC(size);
break;
# ifdef GC_ATOMIC_UNCOLLECTABLE
case GC_NS_QUALIFY(PointerFreeNoGC):
obj = GC_MALLOC_ATOMIC_UNCOLLECTABLE(size);
break;
# endif
2015-05-11 18:25:22 +03:00
case GC_NS_QUALIFY(NoGC):
default:
obj = GC_MALLOC_UNCOLLECTABLE(size);
2015-05-11 18:25:22 +03:00
}
GC_OP_NEW_OOM_CHECK(obj);
return obj;
2011-12-26 10:22:29 +04:00
}
2009-09-16 14:43:37 +00:00
2011-04-20 21:25:18 +00:00
#ifdef GC_PLACEMENT_DELETE
2015-05-11 18:25:22 +03:00
inline void operator delete(void* p, GC_NS_QUALIFY(GCPlacement) /* gcp */,
GC_NS_QUALIFY(GCCleanUpFunc) /* cleanup */,
2018-06-08 11:34:23 +03:00
void* /* clientData */) GC_NOEXCEPT
2011-04-20 21:25:18 +00:00
{
2008-02-16 06:07:00 +00:00
GC_FREE(p);
2011-04-20 21:25:18 +00:00
}
2015-05-11 18:25:22 +03:00
#endif // GC_PLACEMENT_DELETE
2011-07-26 14:51:28 +04:00
2011-07-26 16:56:55 +04:00
#ifdef GC_OPERATOR_NEW_ARRAY
2015-05-11 18:25:22 +03:00
inline void* operator new[](size_t size, GC_NS_QUALIFY(GCPlacement) gcp,
GC_NS_QUALIFY(GCCleanUpFunc) cleanup,
2018-06-15 11:20:42 +03:00
void* clientData)
2011-04-20 21:25:18 +00:00
{
2015-05-11 18:25:22 +03:00
return ::operator new(size, gcp, cleanup, clientData);
2011-04-20 21:25:18 +00:00
}
2015-05-11 18:25:22 +03:00
#endif // GC_OPERATOR_NEW_ARRAY
2011-07-26 14:51:28 +04:00
#endif /* GC_CPP_H */