summaryrefslogtreecommitdiff
path: root/gc_cpp.cc
blob: 6e4343b42b5714809e71f3ddc85d0fc9cbf8fada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * 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.
 */

/*************************************************************************
This implementation module for gc_cpp.h provides an implementation of
the global operators "new" and "delete" that calls the Boehm
allocator.  All objects allocated by this implementation will be
uncollectible but part of the root set of the collector.

You should ensure (using implementation-dependent techniques) that the
linker finds this module before the library that defines the default
built-in "new" and "delete".
**************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifndef GC_BUILD
# define GC_BUILD
#endif

#define GC_DONT_INCL_WINDOWS_H
#include "gc.h"

#include <new> // for bad_alloc, precedes include of gc_cpp.h

#include "gc_cpp.h" // for GC_OPERATOR_NEW_ARRAY

#if !(defined(_MSC_VER) || defined(__DMC__)) || defined(GC_NO_INLINE_STD_NEW)

#if defined(GC_NEW_ABORTS_ON_OOM) || defined(_LIBCPP_NO_EXCEPTIONS)
# define GC_ALLOCATOR_THROW_OR_ABORT() GC_abort_on_oom()
#else
// Use bad_alloc() directly instead of GC_throw_bad_alloc() call.
# define GC_ALLOCATOR_THROW_OR_ABORT() throw std::bad_alloc()
#endif

# if !defined(GC_NEW_DELETE_THROW_NOT_NEEDED) \
    && !defined(GC_NEW_DELETE_NEED_THROW) && GC_GNUC_PREREQ(4, 2) \
    && (__cplusplus < 201103L || defined(__clang__))
#   define GC_NEW_DELETE_NEED_THROW
# endif

# ifdef GC_NEW_DELETE_NEED_THROW
#   if __cplusplus >= 201703L || _MSVC_LANG >= 201703L
      // The "dynamic exception" syntax had been deprecated in C++11
      // and was removed in C++17.
#     define GC_DECL_NEW_THROW noexcept(false)
#   else
#     define GC_DECL_NEW_THROW throw(std::bad_alloc)
#   endif
# else
#   define GC_DECL_NEW_THROW /* empty */
# endif

  void* operator new(size_t size) GC_DECL_NEW_THROW {
    void* obj = GC_MALLOC_UNCOLLECTABLE(size);
    if (0 == obj)
      GC_ALLOCATOR_THROW_OR_ABORT();
    return obj;
  }

# ifdef _MSC_VER
    // This new operator is used by VC++ in case of Debug builds.
    void* operator new(size_t size, int /* nBlockUse */,
                       const char* szFileName, int nLine)
    {
#     ifdef GC_DEBUG
        void* obj = GC_debug_malloc_uncollectable(size, szFileName, nLine);
#     else
        void* obj = GC_MALLOC_UNCOLLECTABLE(size);
        (void)szFileName; (void)nLine;
#     endif
      if (0 == obj)
        GC_ALLOCATOR_THROW_OR_ABORT();
      return obj;
    }
# endif // _MSC_VER

  void operator delete(void* obj) GC_NOEXCEPT {
    GC_FREE(obj);
  }

# if defined(GC_OPERATOR_NEW_ARRAY) && !defined(CPPCHECK)
    void* operator new[](size_t size) GC_DECL_NEW_THROW {
      void* obj = GC_MALLOC_UNCOLLECTABLE(size);
      if (0 == obj)
        GC_ALLOCATOR_THROW_OR_ABORT();
      return obj;
    }

#   ifdef _MSC_VER
      // This new operator is used by VC++ 7+ in Debug builds.
      void* operator new[](size_t size, int nBlockUse,
                           const char* szFileName, int nLine)
      {
        return operator new(size, nBlockUse, szFileName, nLine);
      }
#   endif // _MSC_VER

    void operator delete[](void* obj) GC_NOEXCEPT {
      GC_FREE(obj);
    }
# endif // GC_OPERATOR_NEW_ARRAY

# if __cplusplus >= 201402L || _MSVC_LANG >= 201402L // C++14
    void operator delete(void* obj, size_t size) GC_NOEXCEPT {
      (void)size; // size is ignored
      GC_FREE(obj);
    }

#   if defined(GC_OPERATOR_NEW_ARRAY) && !defined(CPPCHECK)
      void operator delete[](void* obj, size_t size) GC_NOEXCEPT {
        (void)size;
        GC_FREE(obj);
      }
#   endif
# endif // C++14

#endif // !_MSC_VER && !__DMC__ || GC_NO_INLINE_STD_NEW