Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/new.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--------------------------- new.cpp ----------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include <stdlib.h>
10
11
#include "new"
12
#include "include/atomic_support.h"
13
14
#if defined(_LIBCPP_ABI_MICROSOFT)
15
#   if !defined(_LIBCPP_ABI_VCRUNTIME)
16
#       include "support/runtime/new_handler_fallback.ipp"
17
#   endif
18
#elif defined(LIBCXX_BUILDING_LIBCXXABI)
19
#   include <cxxabi.h>
20
#elif defined(LIBCXXRT)
21
#   include <cxxabi.h>
22
#   include "support/runtime/new_handler_fallback.ipp"
23
#elif defined(__GLIBCXX__)
24
    // nothing to do
25
#else
26
#   include "support/runtime/new_handler_fallback.ipp"
27
#endif
28
29
namespace std
30
{
31
32
#ifndef __GLIBCXX__
33
const nothrow_t nothrow{};
34
#endif
35
36
#ifndef LIBSTDCXX
37
38
void
39
__throw_bad_alloc()
40
0
{
41
0
#ifndef _LIBCPP_NO_EXCEPTIONS
42
0
    throw bad_alloc();
43
#else
44
    _VSTD::abort();
45
#endif
46
}
47
48
#endif // !LIBSTDCXX
49
50
}  // std
51
52
#if !defined(__GLIBCXX__) &&                                                   \
53
    !defined(_LIBCPP_ABI_VCRUNTIME) &&      \
54
    !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
55
56
// Implement all new and delete operators as weak definitions
57
// in this shared library, so that they can be overridden by programs
58
// that define non-weak copies of the functions.
59
60
_LIBCPP_WEAK
61
void *
62
operator new(std::size_t size) _THROW_BAD_ALLOC
63
255M
{
64
255M
    if (size == 0)
65
18
        size = 1;
66
255M
    void* p;
67
255M
    while ((p = ::malloc(size)) == 0)
68
0
    {
69
0
        // If malloc fails and there is a new_handler,
70
0
        // call it to try free up memory.
71
0
        std::new_handler nh = std::get_new_handler();
72
0
        if (nh)
73
0
            nh();
74
0
        else
75
0
#ifndef _LIBCPP_NO_EXCEPTIONS
76
0
            throw std::bad_alloc();
77
#else
78
            break;
79
#endif
80
    }
81
255M
    return p;
82
255M
}
83
84
_LIBCPP_WEAK
85
void*
86
operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
87
15.3k
{
88
15.3k
    void* p = 0;
89
15.3k
#ifndef _LIBCPP_NO_EXCEPTIONS
90
15.3k
    try
91
15.3k
    {
92
15.3k
#endif  // _LIBCPP_NO_EXCEPTIONS
93
15.3k
        p = ::operator new(size);
94
15.3k
#ifndef _LIBCPP_NO_EXCEPTIONS
95
15.3k
    }
96
15.3k
    catch (...)
97
15.3k
    {
98
0
    }
99
15.3k
#endif  // _LIBCPP_NO_EXCEPTIONS
100
15.3k
    return p;
101
15.3k
}
102
103
_LIBCPP_WEAK
104
void*
105
operator new[](size_t size) _THROW_BAD_ALLOC
106
21.7M
{
107
21.7M
    return ::operator new(size);
108
21.7M
}
109
110
_LIBCPP_WEAK
111
void*
112
operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
113
0
{
114
0
    void* p = 0;
115
0
#ifndef _LIBCPP_NO_EXCEPTIONS
116
0
    try
117
0
    {
118
0
#endif  // _LIBCPP_NO_EXCEPTIONS
119
0
        p = ::operator new[](size);
120
0
#ifndef _LIBCPP_NO_EXCEPTIONS
121
0
    }
122
0
    catch (...)
123
0
    {
124
0
    }
125
0
#endif  // _LIBCPP_NO_EXCEPTIONS
126
0
    return p;
127
0
}
128
129
_LIBCPP_WEAK
130
void
131
operator delete(void* ptr) _NOEXCEPT
132
256M
{
133
256M
    ::free(ptr);
134
256M
}
135
136
_LIBCPP_WEAK
137
void
138
operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
139
0
{
140
0
    ::operator delete(ptr);
141
0
}
142
143
_LIBCPP_WEAK
144
void
145
operator delete(void* ptr, size_t) _NOEXCEPT
146
0
{
147
0
    ::operator delete(ptr);
148
0
}
149
150
_LIBCPP_WEAK
151
void
152
operator delete[] (void* ptr) _NOEXCEPT
153
21.7M
{
154
21.7M
    ::operator delete(ptr);
155
21.7M
}
156
157
_LIBCPP_WEAK
158
void
159
operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
160
0
{
161
0
    ::operator delete[](ptr);
162
0
}
163
164
_LIBCPP_WEAK
165
void
166
operator delete[] (void* ptr, size_t) _NOEXCEPT
167
0
{
168
0
    ::operator delete[](ptr);
169
0
}
170
171
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
172
173
_LIBCPP_WEAK
174
void *
175
operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
176
0
{
177
0
    if (size == 0)
178
0
        size = 1;
179
0
    if (static_cast<size_t>(alignment) < sizeof(void*))
180
0
      alignment = std::align_val_t(sizeof(void*));
181
0
    void* p;
182
#if defined(_LIBCPP_MSVCRT_LIKE)
183
    while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
184
#else
185
0
    while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
186
0
#endif
187
0
    {
188
0
        // If posix_memalign fails and there is a new_handler,
189
0
        // call it to try free up memory.
190
0
        std::new_handler nh = std::get_new_handler();
191
0
        if (nh)
192
0
            nh();
193
0
        else {
194
0
#ifndef _LIBCPP_NO_EXCEPTIONS
195
0
            throw std::bad_alloc();
196
#else
197
            p = nullptr; // posix_memalign doesn't initialize 'p' on failure
198
            break;
199
#endif
200
        }
201
0
    }
202
0
    return p;
203
0
}
204
205
_LIBCPP_WEAK
206
void*
207
operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
208
0
{
209
0
    void* p = 0;
210
0
#ifndef _LIBCPP_NO_EXCEPTIONS
211
0
    try
212
0
    {
213
0
#endif  // _LIBCPP_NO_EXCEPTIONS
214
0
        p = ::operator new(size, alignment);
215
0
#ifndef _LIBCPP_NO_EXCEPTIONS
216
0
    }
217
0
    catch (...)
218
0
    {
219
0
    }
220
0
#endif  // _LIBCPP_NO_EXCEPTIONS
221
0
    return p;
222
0
}
223
224
_LIBCPP_WEAK
225
void*
226
operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
227
0
{
228
0
    return ::operator new(size, alignment);
229
0
}
230
231
_LIBCPP_WEAK
232
void*
233
operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
234
0
{
235
0
    void* p = 0;
236
0
#ifndef _LIBCPP_NO_EXCEPTIONS
237
0
    try
238
0
    {
239
0
#endif  // _LIBCPP_NO_EXCEPTIONS
240
0
        p = ::operator new[](size, alignment);
241
0
#ifndef _LIBCPP_NO_EXCEPTIONS
242
0
    }
243
0
    catch (...)
244
0
    {
245
0
    }
246
0
#endif  // _LIBCPP_NO_EXCEPTIONS
247
0
    return p;
248
0
}
249
250
_LIBCPP_WEAK
251
void
252
operator delete(void* ptr, std::align_val_t) _NOEXCEPT
253
0
{
254
#if defined(_LIBCPP_MSVCRT_LIKE)
255
    ::_aligned_free(ptr);
256
#else
257
    ::free(ptr);
258
0
#endif
259
0
}
260
261
_LIBCPP_WEAK
262
void
263
operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
264
0
{
265
0
    ::operator delete(ptr, alignment);
266
0
}
267
268
_LIBCPP_WEAK
269
void
270
operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
271
0
{
272
0
    ::operator delete(ptr, alignment);
273
0
}
274
275
_LIBCPP_WEAK
276
void
277
operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
278
0
{
279
0
    ::operator delete(ptr, alignment);
280
0
}
281
282
_LIBCPP_WEAK
283
void
284
operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
285
0
{
286
0
    ::operator delete[](ptr, alignment);
287
0
}
288
289
_LIBCPP_WEAK
290
void
291
operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
292
0
{
293
0
    ::operator delete[](ptr, alignment);
294
0
}
295
296
#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
297
#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS