Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/libcxxrt/memory.cc
39476 views
1
/*
2
* Copyright 2010-2011 PathScale, Inc. All rights reserved.
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
6
*
7
* 1. Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
9
*
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
*/
26
27
/**
28
* memory.cc - Contains stub definition of C++ new/delete operators.
29
*
30
* These definitions are intended to be used for testing and are weak symbols
31
* to allow them to be replaced by definitions from a STL implementation.
32
* These versions simply wrap malloc() and free(), they do not provide a
33
* C++-specific allocator.
34
*/
35
36
#include <stddef.h>
37
#include <stdlib.h>
38
#include "stdexcept.h"
39
#include "atomic.h"
40
41
42
namespace std
43
{
44
struct nothrow_t {};
45
}
46
47
48
/// The type of the function called when allocation fails.
49
typedef void (*new_handler)();
50
/**
51
* The function to call when allocation fails. By default, there is no
52
* handler and a bad allocation exception is thrown if an allocation fails.
53
*/
54
static atomic<new_handler> new_handl{nullptr};
55
56
namespace std
57
{
58
/**
59
* Sets a function to be called when there is a failure in new.
60
*/
61
__attribute__((weak))
62
new_handler set_new_handler(new_handler handler)
63
{
64
return new_handl.exchange(handler);
65
}
66
67
__attribute__((weak))
68
new_handler get_new_handler(void)
69
{
70
return new_handl.load();
71
}
72
}
73
74
75
#if __cplusplus < 201103L
76
#define BADALLOC throw(std::bad_alloc)
77
#else
78
#define BADALLOC
79
#endif
80
81
namespace
82
{
83
/**
84
* Helper for forwarding from no-throw operators to versions that can
85
* return nullptr. Catches any exception and converts it into a nullptr
86
* return.
87
*/
88
template<void*(New)(size_t)>
89
void *noexcept_new(size_t size)
90
{
91
#if !defined(_CXXRT_NO_EXCEPTIONS)
92
try
93
{
94
return New(size);
95
} catch (...)
96
{
97
// nothrow operator new should return NULL in case of
98
// std::bad_alloc exception in new handler
99
return nullptr;
100
}
101
#else
102
return New(size);
103
#endif
104
}
105
}
106
107
108
__attribute__((weak))
109
void* operator new(size_t size) BADALLOC
110
{
111
if (0 == size)
112
{
113
size = 1;
114
}
115
void * mem = malloc(size);
116
while (0 == mem)
117
{
118
new_handler h = std::get_new_handler();
119
if (0 != h)
120
{
121
h();
122
}
123
else
124
{
125
#if !defined(_CXXRT_NO_EXCEPTIONS)
126
throw std::bad_alloc();
127
#else
128
break;
129
#endif
130
}
131
mem = malloc(size);
132
}
133
134
return mem;
135
}
136
137
138
__attribute__((weak))
139
void* operator new(size_t size, const std::nothrow_t &) _LIBCXXRT_NOEXCEPT
140
{
141
return noexcept_new<(::operator new)>(size);
142
}
143
144
145
__attribute__((weak))
146
void operator delete(void * ptr) _LIBCXXRT_NOEXCEPT
147
{
148
free(ptr);
149
}
150
151
152
__attribute__((weak))
153
void * operator new[](size_t size) BADALLOC
154
{
155
return ::operator new(size);
156
}
157
158
159
__attribute__((weak))
160
void * operator new[](size_t size, const std::nothrow_t &) _LIBCXXRT_NOEXCEPT
161
{
162
return noexcept_new<(::operator new[])>(size);
163
}
164
165
166
__attribute__((weak))
167
void operator delete[](void * ptr) _LIBCXXRT_NOEXCEPT
168
{
169
::operator delete(ptr);
170
}
171
172
// C++14 additional delete operators
173
174
#if __cplusplus >= 201402L
175
176
__attribute__((weak))
177
void operator delete(void * ptr, size_t) _LIBCXXRT_NOEXCEPT
178
{
179
::operator delete(ptr);
180
}
181
182
183
__attribute__((weak))
184
void operator delete[](void * ptr, size_t) _LIBCXXRT_NOEXCEPT
185
{
186
::operator delete(ptr);
187
}
188
189
#endif
190
191