2 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to copy this code for any purpose,
8 * provided the above notices are retained on all copies.
11 /*************************************************************************
12 This implementation module for gc_c++.h provides an implementation of
13 the global operators "new" and "delete" that calls the Boehm
14 allocator. All objects allocated by this implementation will be
15 uncollectible but part of the root set of the collector.
17 You should ensure (using implementation-dependent techniques) that the
18 linker finds this module before the library that defines the default
19 built-in "new" and "delete".
20 **************************************************************************/
30 #define GC_DONT_INCL_WINDOWS_H
33 #include <new> // for bad_alloc, precedes include of gc_cpp.h
35 #include "gc_cpp.h" // for GC_OPERATOR_NEW_ARRAY, GC_NOEXCEPT
37 #if defined(GC_NEW_ABORTS_ON_OOM) || defined(_LIBCPP_NO_EXCEPTIONS)
38 # define GC_ALLOCATOR_THROW_OR_ABORT() GC_abort_on_oom()
40 # define GC_ALLOCATOR_THROW_OR_ABORT() throw std::bad_alloc()
43 GC_API void GC_CALL GC_throw_bad_alloc() {
44 GC_ALLOCATOR_THROW_OR_ABORT();
47 #if !defined(_MSC_VER) && !defined(__DMC__)
49 # if !defined(GC_NEW_DELETE_THROW_NOT_NEEDED) \
50 && !defined(GC_NEW_DELETE_NEED_THROW) && GC_GNUC_PREREQ(4, 2) \
51 && (__cplusplus < 201103L || defined(__clang__))
52 # define GC_NEW_DELETE_NEED_THROW
55 # ifdef GC_NEW_DELETE_NEED_THROW
56 # if __cplusplus < 201703L
57 # define GC_DECL_NEW_THROW throw(std::bad_alloc)
59 // The "dynamic exception" syntax was deprecated in C++11
60 // and removed in C++17.
61 # define GC_DECL_NEW_THROW noexcept(false)
64 # define GC_DECL_NEW_THROW /* empty */
67 void* operator new(size_t size) GC_DECL_NEW_THROW {
68 void* obj = GC_MALLOC_UNCOLLECTABLE(size);
70 GC_ALLOCATOR_THROW_OR_ABORT();
74 void operator delete(void* obj) GC_NOEXCEPT {
78 # if defined(GC_OPERATOR_NEW_ARRAY) && !defined(CPPCHECK)
79 void* operator new[](size_t size) GC_DECL_NEW_THROW {
80 void* obj = GC_MALLOC_UNCOLLECTABLE(size);
82 GC_ALLOCATOR_THROW_OR_ABORT();
86 void operator delete[](void* obj) GC_NOEXCEPT {
89 # endif // GC_OPERATOR_NEW_ARRAY
91 # if __cplusplus > 201103L // C++14
92 void operator delete(void* obj, size_t size) GC_NOEXCEPT {
93 (void)size; // size is ignored
97 # if defined(GC_OPERATOR_NEW_ARRAY) && !defined(CPPCHECK)
98 void operator delete[](void* obj, size_t size) GC_NOEXCEPT {