mirror of
https://github.com/GrapheneOS/hardened_malloc.git
synced 2024-12-19 12:54:33 -05:00
4716ff5b92
This doesn't make any difference for GCC, but these aren't replaced outside of the library with Clang without explicitly exporting them.
83 lines
1.7 KiB
C++
83 lines
1.7 KiB
C++
#include <stdlib.h>
|
|
#include <new>
|
|
#include <bits/functexcept.h>
|
|
|
|
#define noreturn
|
|
|
|
extern "C" {
|
|
#include "malloc.h"
|
|
#include "util.h"
|
|
}
|
|
|
|
COLD static void *handle_out_of_memory(size_t size, bool nothrow) {
|
|
void *ptr;
|
|
|
|
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);
|
|
}
|