2023-06-10 23:47:17 +05:30
|
|
|
// needed with libstdc++ but not libc++
|
|
|
|
#if __has_include(<bits/functexcept.h>)
|
2018-09-19 23:27:35 +05:30
|
|
|
#include <bits/functexcept.h>
|
2023-06-10 23:47:17 +05:30
|
|
|
#endif
|
|
|
|
|
2018-10-11 11:05:25 +05:30
|
|
|
#include <new>
|
2018-09-19 23:27:35 +05:30
|
|
|
|
2018-12-05 12:51:28 +05:30
|
|
|
#include "h_malloc.h"
|
2018-09-19 23:27:35 +05:30
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
COLD static void *handle_out_of_memory(size_t size, bool nothrow) {
|
2018-10-15 03:29:43 +05:30
|
|
|
void *ptr = nullptr;
|
2018-09-19 23:27:35 +05:30
|
|
|
|
|
|
|
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 11:52:17 +05:30
|
|
|
EXPORT void *operator new(size_t size) {
|
2018-09-19 23:27:35 +05:30
|
|
|
return new_impl(size, false);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void *operator new[](size_t size) {
|
2018-09-19 23:27:35 +05:30
|
|
|
return new_impl(size, false);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void *operator new(size_t size, const std::nothrow_t &) noexcept {
|
2018-09-19 23:27:35 +05:30
|
|
|
return new_impl(size, true);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void *operator new[](size_t size, const std::nothrow_t &) noexcept {
|
2018-09-19 23:27:35 +05:30
|
|
|
return new_impl(size, true);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void operator delete(void *ptr) noexcept {
|
2018-09-19 23:27:35 +05:30
|
|
|
h_free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void operator delete[](void *ptr) noexcept {
|
2018-09-19 23:27:35 +05:30
|
|
|
h_free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void operator delete(void *ptr, const std::nothrow_t &) noexcept {
|
2018-09-19 23:27:35 +05:30
|
|
|
h_free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void operator delete[](void *ptr, const std::nothrow_t &) noexcept {
|
2018-09-19 23:27:35 +05:30
|
|
|
h_free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void operator delete(void *ptr, size_t size) noexcept {
|
2018-09-19 23:27:35 +05:30
|
|
|
h_free_sized(ptr, size);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:52:17 +05:30
|
|
|
EXPORT void operator delete[](void *ptr, size_t size) noexcept {
|
2018-09-19 23:27:35 +05:30
|
|
|
h_free_sized(ptr, size);
|
|
|
|
}
|
2019-03-20 22:48:27 +05:30
|
|
|
|
|
|
|
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);
|
|
|
|
}
|