Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compliance of new() operator overrides #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/libc_override.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,17 @@ void* (*__MALLOC_HOOK_VOLATILE __memalign_hook)(size_t, size_t, const void*) =

} // extern "C"

inline constexpr size_t size_for_cpp_new(size_t size) {
// The C++ spec requires new() operators to always
// return distinct non-null pointers.
//
// Generic `malloc` implementations may return NULL for zero-sized
// allocations, so to facilitate C++ `new` we request a small size.
return size == 0 ? 1 : size;
}

void* operator new(size_t size) noexcept(false) {
void* res = bench::malloc(size);
void* res = bench::malloc(size_for_cpp_new(size));
if (res == nullptr) {
throw std::bad_alloc();
}
Expand All @@ -111,10 +120,10 @@ void operator delete(void* p) noexcept {
bench::free(p);
}
void operator delete(void* p, size_t size) noexcept {
bench::free(p, size);
bench::free(p, size_for_cpp_new(size));
}
void* operator new[](size_t size) noexcept(false) {
void* res = bench::malloc(size);
void* res = bench::malloc(size_for_cpp_new(size));
if (res == nullptr) {
throw std::bad_alloc();
}
Expand All @@ -124,13 +133,13 @@ void operator delete[](void* p) noexcept {
bench::free(p);
}
void operator delete[](void* p, size_t size) noexcept {
bench::free(p, size);
bench::free(p, size_for_cpp_new(size));
}
void* operator new(size_t size, const std::nothrow_t&) noexcept {
return bench::malloc(size);
return bench::malloc(size_for_cpp_new(size));
}
void* operator new[](size_t size, const std::nothrow_t&) noexcept {
return bench::malloc(size);
return bench::malloc(size_for_cpp_new(size));
}
void operator delete(void* p, const std::nothrow_t&) noexcept {
bench::free(p);
Expand All @@ -140,48 +149,50 @@ void operator delete[](void* p, const std::nothrow_t&) noexcept {
}

void* operator new(size_t size, std::align_val_t alignment) noexcept(false) {
void* res = bench::malloc(static_cast<size_t>(alignment), size);
void* res =
bench::malloc(size_for_cpp_new(size), static_cast<size_t>(alignment));
if (res == nullptr) {
throw std::bad_alloc();
}
return res;
}
void* operator new(size_t size, std::align_val_t alignment,
const std::nothrow_t&) noexcept {
return bench::malloc(static_cast<size_t>(alignment), size);
return bench::malloc(size_for_cpp_new(size), static_cast<size_t>(alignment));
}
void operator delete(void* p, std::align_val_t alignment) noexcept {
bench::free(p, /*size=*/0, static_cast<size_t>(alignment));
bench::free(p, size_for_cpp_new(0), static_cast<size_t>(alignment));
}
void operator delete(void* p, std::align_val_t alignment,
const std::nothrow_t&) noexcept {
bench::free(p, /*size=*/0, static_cast<size_t>(alignment));
bench::free(p, size_for_cpp_new(0), static_cast<size_t>(alignment));
}
void operator delete(void* p, size_t size,
std::align_val_t alignment) noexcept {
bench::free(p, size, static_cast<size_t>(alignment));
bench::free(p, size_for_cpp_new(size), static_cast<size_t>(alignment));
}
void* operator new[](size_t size, std::align_val_t alignment) noexcept(false) {
void* res = bench::malloc(static_cast<size_t>(alignment), size);
void* res =
bench::malloc(size_for_cpp_new(size), static_cast<size_t>(alignment));
if (res == nullptr) {
throw std::bad_alloc();
}
return res;
}
void* operator new[](size_t size, std::align_val_t alignment,
const std::nothrow_t&) noexcept {
return bench::malloc(static_cast<size_t>(alignment), size);
return bench::malloc(size_for_cpp_new(size), static_cast<size_t>(alignment));
}
void operator delete[](void* p, std::align_val_t alignment) noexcept {
bench::free(p, /*size=*/0, static_cast<size_t>(alignment));
bench::free(p, size_for_cpp_new(0), static_cast<size_t>(alignment));
}
void operator delete[](void* p, std::align_val_t alignment,
const std::nothrow_t&) noexcept {
bench::free(p, /*size=*/0, static_cast<size_t>(alignment));
bench::free(p, size_for_cpp_new(0), static_cast<size_t>(alignment));
}
void operator delete[](void* p, size_t size,
std::align_val_t alignment) noexcept {
bench::free(p, size, static_cast<size_t>(alignment));
bench::free(p, size_for_cpp_new(size), static_cast<size_t>(alignment));
}

extern "C" {
Expand Down