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 copy this code for any purpose,
8  * provided the above notices are retained on all copies.
9  */
10 
11 /*************************************************************************
12 This implementation module for gc_cpp.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.
16 
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 **************************************************************************/
21 
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25 
26 #ifndef GC_BUILD
27 # define GC_BUILD
28 #endif
29 
30 #define GC_DONT_INCL_WINDOWS_H
31 #include "gc.h"
32 
33 #include <new> // for bad_alloc, precedes include of gc_cpp.h
34 
35 #include "gc_cpp.h" // for GC_OPERATOR_NEW_ARRAY, GC_NOEXCEPT
36 
37 #if defined(GC_NEW_ABORTS_ON_OOM) || defined(_LIBCPP_NO_EXCEPTIONS)
38 # define GC_ALLOCATOR_THROW_OR_ABORT() GC_abort_on_oom()
39 #else
40 # define GC_ALLOCATOR_THROW_OR_ABORT() throw std::bad_alloc()
41 #endif
42 
GC_throw_bad_alloc()43 GC_API void GC_CALL GC_throw_bad_alloc() {
44   GC_ALLOCATOR_THROW_OR_ABORT();
45 }
46 
47 #if !(defined(_MSC_VER) || defined(__DMC__)) || defined(GC_NO_INLINE_STD_NEW)
48 
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
53 # endif
54 
55 # ifdef GC_NEW_DELETE_NEED_THROW
56 #   if __cplusplus >= 201703L || _MSVC_LANG >= 201703L
57       // The "dynamic exception" syntax had been deprecated in C++11
58       // and was removed in C++17.
59 #     define GC_DECL_NEW_THROW noexcept(false)
60 #   else
61 #     define GC_DECL_NEW_THROW throw(std::bad_alloc)
62 #   endif
63 # else
64 #   define GC_DECL_NEW_THROW /* empty */
65 # endif
66 
operator new(size_t size)67   void* operator new(size_t size) GC_DECL_NEW_THROW {
68     void* obj = GC_MALLOC_UNCOLLECTABLE(size);
69     if (0 == obj)
70       GC_ALLOCATOR_THROW_OR_ABORT();
71     return obj;
72   }
73 
74 # ifdef _MSC_VER
75     // This new operator is used by VC++ in case of Debug builds.
operator new(size_t size,int,const char * szFileName,int nLine)76     void* operator new(size_t size, int /* nBlockUse */,
77                        const char* szFileName, int nLine)
78     {
79 #     ifdef GC_DEBUG
80         void* obj = GC_debug_malloc_uncollectable(size, szFileName, nLine);
81 #     else
82         void* obj = GC_MALLOC_UNCOLLECTABLE(size);
83         (void)szFileName; (void)nLine;
84 #     endif
85       if (0 == obj)
86         GC_ALLOCATOR_THROW_OR_ABORT();
87       return obj;
88     }
89 # endif // _MSC_VER
90 
operator delete(void * obj)91   void operator delete(void* obj) GC_NOEXCEPT {
92     GC_FREE(obj);
93   }
94 
95 # if defined(GC_OPERATOR_NEW_ARRAY) && !defined(CPPCHECK)
operator new[](size_t size)96     void* operator new[](size_t size) GC_DECL_NEW_THROW {
97       void* obj = GC_MALLOC_UNCOLLECTABLE(size);
98       if (0 == obj)
99         GC_ALLOCATOR_THROW_OR_ABORT();
100       return obj;
101     }
102 
103 #   ifdef _MSC_VER
104       // This new operator is used by VC++ 7+ in Debug builds.
operator new[](size_t size,int nBlockUse,const char * szFileName,int nLine)105       void* operator new[](size_t size, int nBlockUse,
106                            const char* szFileName, int nLine)
107       {
108         return operator new(size, nBlockUse, szFileName, nLine);
109       }
110 #   endif // _MSC_VER
111 
operator delete[](void * obj)112     void operator delete[](void* obj) GC_NOEXCEPT {
113       GC_FREE(obj);
114     }
115 # endif // GC_OPERATOR_NEW_ARRAY
116 
117 # if __cplusplus >= 201402L || _MSVC_LANG >= 201402L // C++14
operator delete(void * obj,size_t size)118     void operator delete(void* obj, size_t size) GC_NOEXCEPT {
119       (void)size; // size is ignored
120       GC_FREE(obj);
121     }
122 
123 #   if defined(GC_OPERATOR_NEW_ARRAY) && !defined(CPPCHECK)
operator delete[](void * obj,size_t size)124       void operator delete[](void* obj, size_t size) GC_NOEXCEPT {
125         (void)size;
126         GC_FREE(obj);
127       }
128 #   endif
129 # endif // C++14
130 
131 #endif // !_MSC_VER && !__DMC__ || GC_NO_INLINE_STD_NEW
132