Merge "new.cpp: add sized operator delete." into main

This commit is contained in:
Elliott Hughes 2024-06-05 17:37:10 +00:00 committed by Gerrit Code Review
commit 3e8dd2b88c

View file

@ -16,13 +16,14 @@
#include <new>
#include <errno.h>
#include <stdlib.h>
#include <async_safe/log.h>
__attribute__((weak)) const std::nothrow_t std::nothrow = {};
// We can't throw in bionic, so we go straight to the equivalent of
// std::terminate for these two instead.
void* operator new(std::size_t size) {
void* p = malloc(size);
if (p == nullptr) {
@ -30,7 +31,6 @@ void* operator new(std::size_t size) {
}
return p;
}
void* operator new[](std::size_t size) {
void* p = malloc(size);
if (p == nullptr) {
@ -39,26 +39,21 @@ void* operator new[](std::size_t size) {
return p;
}
void operator delete(void* ptr) throw() {
free(ptr);
}
void operator delete[](void* ptr) throw() {
free(ptr);
}
// These two are the "nothrow" variants, so we just return nullptr on failure.
void* operator new(std::size_t size, const std::nothrow_t&) {
return malloc(size);
}
void* operator new[](std::size_t size, const std::nothrow_t&) {
return malloc(size);
}
void operator delete(void* ptr, const std::nothrow_t&) throw() {
free(ptr);
}
// free() can't throw anyway (except on heap corruption, which is always fatal),
// so there's no difference between the regular and "nothrow" variants here.
void operator delete(void* p) noexcept { free(p); }
void operator delete[](void* p) noexcept { free(p); }
void operator delete(void* p, const std::nothrow_t&) noexcept { free(p); }
void operator delete[](void* p, const std::nothrow_t&) noexcept { free(p); }
void operator delete[](void* ptr, const std::nothrow_t&) throw() {
free(ptr);
}
// TODO: these can use free_sized() once we have it (http://b/284321795).
void operator delete(void* p, std::size_t) noexcept { free(p); }
void operator delete[](void* p, std::size_t) noexcept { free(p); }