2018-09-19 13:57:35 -04:00
|
|
|
#include <bits/functexcept.h>
|
2018-10-11 01:35:25 -04:00
|
|
|
#include <new>
|
2018-09-19 13:57:35 -04:00
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void *operator new(size_t size) {
|
2018-09-19 13:57:35 -04:00
|
|
|
return new_impl(size, false);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void *operator new[](size_t size) {
|
2018-09-19 13:57:35 -04:00
|
|
|
return new_impl(size, false);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void *operator new(size_t size, const std::nothrow_t &) noexcept {
|
2018-09-19 13:57:35 -04:00
|
|
|
return new_impl(size, true);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void *operator new[](size_t size, const std::nothrow_t &) noexcept {
|
2018-09-19 13:57:35 -04:00
|
|
|
return new_impl(size, true);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void operator delete(void *ptr) noexcept {
|
2018-09-19 13:57:35 -04:00
|
|
|
h_free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void operator delete[](void *ptr) noexcept {
|
2018-09-19 13:57:35 -04:00
|
|
|
h_free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void operator delete(void *ptr, const std::nothrow_t &) noexcept {
|
2018-09-19 13:57:35 -04:00
|
|
|
h_free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void operator delete[](void *ptr, const std::nothrow_t &) noexcept {
|
2018-09-19 13:57:35 -04:00
|
|
|
h_free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void operator delete(void *ptr, size_t size) noexcept {
|
2018-09-19 13:57:35 -04:00
|
|
|
h_free_sized(ptr, size);
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:22:17 -04:00
|
|
|
EXPORT void operator delete[](void *ptr, size_t size) noexcept {
|
2018-09-19 13:57:35 -04:00
|
|
|
h_free_sized(ptr, size);
|
|
|
|
}
|