]> granicus.if.org Git - gc/blob - gc_cpp.cc
Update ChangeLog file (v7.6 changes)
[gc] / gc_cpp.cc
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_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.
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
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__)
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
57 #     define GC_DECL_NEW_THROW throw(std::bad_alloc)
58 #   else
59       // The "dynamic exception" syntax was deprecated in C++11
60       // and removed in C++17.
61 #     define GC_DECL_NEW_THROW noexcept(false)
62 #   endif
63 # else
64 #   define GC_DECL_NEW_THROW /* empty */
65 # endif
66
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   void operator delete(void* obj) GC_NOEXCEPT {
75     GC_FREE(obj);
76   }
77
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);
81       if (0 == obj)
82         GC_ALLOCATOR_THROW_OR_ABORT();
83       return obj;
84     }
85
86     void operator delete[](void* obj) GC_NOEXCEPT {
87       GC_FREE(obj);
88     }
89 # endif // GC_OPERATOR_NEW_ARRAY
90
91 # if __cplusplus > 201103L // C++14
92     void operator delete(void* obj, size_t size) GC_NOEXCEPT {
93       (void)size; // size is ignored
94       GC_FREE(obj);
95     }
96
97 #   if defined(GC_OPERATOR_NEW_ARRAY) && !defined(CPPCHECK)
98       void operator delete[](void* obj, size_t size) GC_NOEXCEPT {
99         (void)size;
100         GC_FREE(obj);
101       }
102 #   endif
103 # endif // C++14
104
105 #endif // !_MSC_VER