forked from GrapheneOS/hardened_malloc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
new.cc
153 lines (119 loc) · 3.71 KB
/
new.cc
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// needed with libstdc++ but not libc++
#if __has_include(<bits/functexcept.h>)
#include <bits/functexcept.h>
#endif
#include <new>
#include "h_malloc.h"
#include "util.h"
COLD static void *handle_out_of_memory(size_t size, bool nothrow) {
void *ptr = nullptr;
do {
std::new_handler handler = std::get_new_handler();
if (handler == nullptr) {
break;
}
try {
handler();
} catch (const std::bad_alloc &) {
break;
}
ptr = h_malloc(size);
} while (ptr == nullptr);
if (ptr == nullptr && !nothrow) {
std::__throw_bad_alloc();
}
return ptr;
}
static inline void *new_impl(size_t size, bool nothrow) {
void *ptr = h_malloc(size);
if (likely(ptr != nullptr)) {
return ptr;
}
return handle_out_of_memory(size, nothrow);
}
EXPORT void *operator new(size_t size) {
return new_impl(size, false);
}
EXPORT void *operator new[](size_t size) {
return new_impl(size, false);
}
EXPORT void *operator new(size_t size, const std::nothrow_t &) noexcept {
return new_impl(size, true);
}
EXPORT void *operator new[](size_t size, const std::nothrow_t &) noexcept {
return new_impl(size, true);
}
EXPORT void operator delete(void *ptr) noexcept {
h_free(ptr);
}
EXPORT void operator delete[](void *ptr) noexcept {
h_free(ptr);
}
EXPORT void operator delete(void *ptr, const std::nothrow_t &) noexcept {
h_free(ptr);
}
EXPORT void operator delete[](void *ptr, const std::nothrow_t &) noexcept {
h_free(ptr);
}
EXPORT void operator delete(void *ptr, size_t size) noexcept {
h_free_sized(ptr, size);
}
EXPORT void operator delete[](void *ptr, size_t size) noexcept {
h_free_sized(ptr, size);
}
COLD static void *handle_out_of_memory(size_t size, size_t alignment, bool nothrow) {
void *ptr = nullptr;
do {
std::new_handler handler = std::get_new_handler();
if (handler == nullptr) {
break;
}
try {
handler();
} catch (const std::bad_alloc &) {
break;
}
ptr = h_aligned_alloc(alignment, size);
} while (ptr == nullptr);
if (ptr == nullptr && !nothrow) {
std::__throw_bad_alloc();
}
return ptr;
}
static inline void *new_impl(size_t size, size_t alignment, bool nothrow) {
void *ptr = h_aligned_alloc(alignment, size);
if (likely(ptr != nullptr)) {
return ptr;
}
return handle_out_of_memory(size, alignment, nothrow);
}
EXPORT void *operator new(size_t size, std::align_val_t alignment) {
return new_impl(size, static_cast<size_t>(alignment), false);
}
EXPORT void *operator new[](size_t size, std::align_val_t alignment) {
return new_impl(size, static_cast<size_t>(alignment), false);
}
EXPORT void *operator new(size_t size, std::align_val_t alignment, const std::nothrow_t &) noexcept {
return new_impl(size, static_cast<size_t>(alignment), true);
}
EXPORT void *operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t &) noexcept {
return new_impl(size, static_cast<size_t>(alignment), true);
}
EXPORT void operator delete(void *ptr, std::align_val_t) noexcept {
h_free(ptr);
}
EXPORT void operator delete[](void *ptr, std::align_val_t) noexcept {
h_free(ptr);
}
EXPORT void operator delete(void *ptr, std::align_val_t, const std::nothrow_t &) noexcept {
h_free(ptr);
}
EXPORT void operator delete[](void *ptr, std::align_val_t, const std::nothrow_t &) noexcept {
h_free(ptr);
}
EXPORT void operator delete(void *ptr, size_t size, std::align_val_t) noexcept {
h_free_sized(ptr, size);
}
EXPORT void operator delete[](void *ptr, size_t size, std::align_val_t) noexcept {
h_free_sized(ptr, size);
}