Merge "new.cpp: add sized operator delete." into main am: 3e8dd2b88c am: e6c408d79f

Original change: https://android-review.googlesource.com/c/platform/bionic/+/3115108

Change-Id: I7f6b2e1b288d4431e997f7588aff5eddec33d550
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Elliott Hughes 2024-06-05 18:16:00 +00:00 committed by Automerger Merge Worker
commit 2ec6d398ce

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); }