diff options
author | Ivan Maidanski <ivmai@mail.ru> | 2023-04-20 09:16:47 +0300 |
---|---|---|
committer | Ivan Maidanski <ivmai@mail.ru> | 2023-04-20 13:28:49 +0300 |
commit | 06ffb93814a97d08fce62e2f3ce3135bc2473e59 (patch) | |
tree | 9d5d86d65d5de49630ad2833fe3bea1d3d4036d1 | |
parent | 906c14ec7907c28b5652ed253f7a48b259ee84e5 (diff) | |
download | bdwgc-06ffb93814a97d08fce62e2f3ce3135bc2473e59.tar.gz |
Provide global non-throwing operator new/delete in gccpp library
Note: this might require define GC_INCLUDE_NEW by client before
include gc_cpp.h on Windows hosts (to have nothrow_t declared before
the global inline operator new and delete are defined).
* gc_cpp.cc [(!_MSC_VER && !__DMC__ || GC_NO_INLINE_STD_NEW)
&& !GC_INLINE_STD_NEW && GC_OPERATOR_NEW_NOTHROW] (operator new,
operator delete): Define with nothrow_t argument.
* gc_cpp.cc [(!_MSC_VER && !__DMC__ || GC_NO_INLINE_STD_NEW)
&& !GC_INLINE_STD_NEW && GC_OPERATOR_NEW_ARRAY && !CPPCHECK
&& GC_OPERATOR_NEW_NOTHROW] (operator new[], operator delete[]):
Likewise.
* include/gc/gc_cpp.h [GC_INLINE_STD_NEW && GC_OPERATOR_NEW_ARRAY
&& GC_OPERATOR_NEW_NOTHROW] (operator new[], operator delete[]):
Likewise.
* include/gc/gc_cpp.h [GC_INLINE_STD_NEW && GC_OPERATOR_NEW_NOTHROW]
(operator new, operator delete): Likewise.
* include/gc/gc_cpp.h [!GC_OPERATOR_NEW_NOTHROW
&& !GC_NO_OPERATOR_NEW_NOTHROW && (GC_INCLUDE_NEW
&& (__cplusplus>=201103L || _MSVC_LANG>=201103L)
|| __NOTHROW_T_DEFINED)] (GC_OPERATOR_NEW_NOTHROW): Define macro;
add comment.
* include/gc/gc_cpp.h [!GC_INLINE_STD_NEW && GC_NO_INLINE_STD_NEW
&& _MSC_VER && GC_OPERATOR_NEW_ARRAY && GC_OPERATOR_NEW_NOTHROW]
(operator new[], operator delete[]): Add prototype with nothrow_t
argument (with commented out GC_ATTR_MALLOC).
* include/gc/gc_cpp.h [!GC_INLINE_STD_NEW && GC_NO_INLINE_STD_NEW
&& _MSC_VER && GC_OPERATOR_NEW_NOTHROW] (operator new,
operator delete): Likewise.
-rw-r--r-- | gc_cpp.cc | 24 | ||||
-rw-r--r-- | include/gc/gc_cpp.h | 47 |
2 files changed, 71 insertions, 0 deletions
@@ -78,6 +78,18 @@ built-in "new" and "delete". GC_FREE(obj); } +# ifdef GC_OPERATOR_NEW_NOTHROW + void* operator new(GC_SIZE_T size, const std::nothrow_t&) GC_NOEXCEPT + { + return GC_MALLOC_UNCOLLECTABLE(size); + } + + void operator delete(void* obj, const std::nothrow_t&) GC_NOEXCEPT + { + GC_FREE(obj); + } +# endif // GC_OPERATOR_NEW_NOTHROW + # if defined(GC_OPERATOR_NEW_ARRAY) && !defined(CPPCHECK) void* operator new[](GC_SIZE_T size) GC_DECL_NEW_THROW { @@ -100,6 +112,18 @@ built-in "new" and "delete". { GC_FREE(obj); } + +# ifdef GC_OPERATOR_NEW_NOTHROW + void* operator new[](GC_SIZE_T size, const std::nothrow_t&) GC_NOEXCEPT + { + return GC_MALLOC_UNCOLLECTABLE(size); + } + + void operator delete[](void* obj, const std::nothrow_t&) GC_NOEXCEPT + { + GC_FREE(obj); + } +# endif // GC_OPERATOR_NEW_NOTHROW # endif // GC_OPERATOR_NEW_ARRAY # ifdef GC_OPERATOR_SIZED_DELETE diff --git a/include/gc/gc_cpp.h b/include/gc/gc_cpp.h index d06a74d9..c8bd6fac 100644 --- a/include/gc/gc_cpp.h +++ b/include/gc/gc_cpp.h @@ -186,6 +186,16 @@ by UseGC. GC is an alias for UseGC, unless GC_NAME_CONFLICT is defined. # define GC_OPERATOR_SIZED_DELETE #endif +#if !defined(GC_OPERATOR_NEW_NOTHROW) \ + && !defined(GC_NO_OPERATOR_NEW_NOTHROW) \ + && ((defined(GC_INCLUDE_NEW) \ + && (__cplusplus >= 201103L || _MSVC_LANG >= 201103L)) \ + || defined(__NOTHROW_T_DEFINED)) + // Note: this might require defining GC_INCLUDE_NEW by client + // before include gc_cpp.h (on Windows). +# define GC_OPERATOR_NEW_NOTHROW +#endif + #if !defined(GC_NEW_DELETE_THROW_NOT_NEEDED) \ && !defined(GC_NEW_DELETE_NEED_THROW) && GC_GNUC_PREREQ(4, 2) \ && (__cplusplus < 201103L || defined(__clang__)) @@ -340,6 +350,20 @@ inline void* operator new(GC_SIZE_T, GC_NS_QUALIFY(GCPlacement), { GC_FREE(obj); } + +# ifdef GC_OPERATOR_NEW_NOTHROW + inline /* GC_ATTR_MALLOC */ void* operator new[](GC_SIZE_T size, + const std::nothrow_t&) GC_NOEXCEPT + { + return GC_MALLOC_UNCOLLECTABLE(size); + } + + inline void operator delete[](void* obj, + const std::nothrow_t&) GC_NOEXCEPT + { + GC_FREE(obj); + } +# endif // GC_OPERATOR_NEW_NOTHROW # endif // GC_OPERATOR_NEW_ARRAY inline void* operator new(GC_SIZE_T size) GC_DECL_NEW_THROW @@ -354,6 +378,19 @@ inline void* operator new(GC_SIZE_T, GC_NS_QUALIFY(GCPlacement), GC_FREE(obj); } +# ifdef GC_OPERATOR_NEW_NOTHROW + inline /* GC_ATTR_MALLOC */ void* operator new(GC_SIZE_T size, + const std::nothrow_t&) GC_NOEXCEPT + { + return GC_MALLOC_UNCOLLECTABLE(size); + } + + inline void operator delete(void* obj, const std::nothrow_t&) GC_NOEXCEPT + { + GC_FREE(obj); + } +# endif // GC_OPERATOR_NEW_NOTHROW + # ifdef GC_OPERATOR_SIZED_DELETE inline void operator delete(void* obj, GC_SIZE_T) GC_NOEXCEPT { @@ -403,6 +440,11 @@ inline void* operator new(GC_SIZE_T, GC_NS_QUALIFY(GCPlacement), # ifdef GC_OPERATOR_NEW_ARRAY void* operator new[](GC_SIZE_T) GC_DECL_NEW_THROW; void operator delete[](void*) GC_NOEXCEPT; +# ifdef GC_OPERATOR_NEW_NOTHROW + /* GC_ATTR_MALLOC */ void* operator new[](GC_SIZE_T, + const std::nothrow_t&) GC_NOEXCEPT; + void operator delete[](void*, const std::nothrow_t&) GC_NOEXCEPT; +# endif # ifdef GC_OPERATOR_SIZED_DELETE void operator delete[](void*, GC_SIZE_T) GC_NOEXCEPT; # endif @@ -413,6 +455,11 @@ inline void* operator new(GC_SIZE_T, GC_NS_QUALIFY(GCPlacement), void* operator new(GC_SIZE_T) GC_DECL_NEW_THROW; void operator delete(void*) GC_NOEXCEPT; +# ifdef GC_OPERATOR_NEW_NOTHROW + /* GC_ATTR_MALLOC */ void* operator new(GC_SIZE_T, + const std::nothrow_t&) GC_NOEXCEPT; + void operator delete(void*, const std::nothrow_t&) GC_NOEXCEPT; +# endif # ifdef GC_OPERATOR_SIZED_DELETE void operator delete(void*, GC_SIZE_T) GC_NOEXCEPT; # endif |