Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/new.h
213799 views
1
//===-- Libc specific custom operator new and delete ------------*- C++ -*-===//
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
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H
10
#define LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H
11
12
#include "hdr/func/aligned_alloc.h"
13
#include "hdr/func/free.h"
14
#include "hdr/func/malloc.h"
15
#include "src/__support/common.h"
16
#include "src/__support/macros/config.h"
17
#include "src/__support/macros/properties/os.h"
18
19
#include <stddef.h> // For size_t
20
21
// Defining members in the std namespace is not preferred. But, we do it here
22
// so that we can use it to define the operator new which takes std::align_val_t
23
// argument.
24
namespace std {
25
26
enum class align_val_t : size_t {};
27
28
} // namespace std
29
30
namespace LIBC_NAMESPACE_DECL {
31
32
namespace cpp {
33
template <class T> [[nodiscard]] constexpr T *launder(T *p) {
34
static_assert(__has_builtin(__builtin_launder),
35
"cpp::launder requires __builtin_launder");
36
return __builtin_launder(p);
37
}
38
} // namespace cpp
39
40
class AllocChecker {
41
bool success = false;
42
43
LIBC_INLINE AllocChecker &operator=(bool status) {
44
success = status;
45
return *this;
46
}
47
48
public:
49
LIBC_INLINE AllocChecker() = default;
50
51
LIBC_INLINE operator bool() const { return success; }
52
53
LIBC_INLINE static void *alloc(size_t s, AllocChecker &ac) {
54
void *mem = ::malloc(s);
55
ac = (mem != nullptr);
56
return mem;
57
}
58
59
LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align,
60
AllocChecker &ac) {
61
#ifdef LIBC_TARGET_OS_IS_WINDOWS
62
// std::aligned_alloc is not available on Windows because std::free on
63
// Windows cannot deallocate any over-aligned memory. Microsoft provides an
64
// alternative for std::aligned_alloc named _aligned_malloc, but it must be
65
// paired with _aligned_free instead of std::free.
66
void *mem = ::_aligned_malloc(static_cast<size_t>(align), s);
67
#else
68
void *mem = ::aligned_alloc(static_cast<size_t>(align), s);
69
#endif
70
ac = (mem != nullptr);
71
return mem;
72
}
73
};
74
75
} // namespace LIBC_NAMESPACE_DECL
76
77
LIBC_INLINE void *operator new(size_t size,
78
LIBC_NAMESPACE::AllocChecker &ac) noexcept {
79
return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);
80
}
81
82
LIBC_INLINE void *operator new(size_t size, std::align_val_t align,
83
LIBC_NAMESPACE::AllocChecker &ac) noexcept {
84
return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size, align, ac);
85
}
86
87
LIBC_INLINE void *operator new[](size_t size,
88
LIBC_NAMESPACE::AllocChecker &ac) noexcept {
89
return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);
90
}
91
92
LIBC_INLINE void *operator new[](size_t size, std::align_val_t align,
93
LIBC_NAMESPACE::AllocChecker &ac) noexcept {
94
return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size, align, ac);
95
}
96
97
LIBC_INLINE void *operator new(size_t, void *p) { return p; }
98
99
LIBC_INLINE void *operator new[](size_t, void *p) { return p; }
100
101
// The ideal situation would be to define the various flavors of operator delete
102
// inlinelike we do with operator new above. However, since we need operator
103
// delete prototypes to match those specified by the C++ standard, we cannot
104
// define them inline as the C++ standard does not allow inline definitions of
105
// replacement operator delete implementations. Note also that we assign a
106
// special linkage name to each of these replacement operator delete functions.
107
// This is because, if we do not give them a special libc internal linkage name,
108
// they will replace operator delete for the entire application. Including this
109
// header file in all libc source files where operator delete is called ensures
110
// that only libc call sites use these replacement operator delete functions.
111
112
#define DELETE_NAME(name) \
113
__asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name))
114
115
void operator delete(void *) noexcept DELETE_NAME(delete);
116
void operator delete(void *, std::align_val_t) noexcept
117
DELETE_NAME(delete_aligned);
118
void operator delete(void *, size_t) noexcept DELETE_NAME(delete_sized);
119
void operator delete(void *, size_t, std::align_val_t) noexcept
120
DELETE_NAME(delete_sized_aligned);
121
void operator delete[](void *) noexcept DELETE_NAME(delete_array);
122
void operator delete[](void *, std::align_val_t) noexcept
123
DELETE_NAME(delete_array_aligned);
124
void operator delete[](void *, size_t) noexcept DELETE_NAME(delete_array_sized);
125
void operator delete[](void *, size_t, std::align_val_t) noexcept
126
DELETE_NAME(delete_array_sized_aligned);
127
128
#undef DELETE_NAME
129
130
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H
131
132