Commit 6d292502 authored by Matthijs Kooijman's avatar Matthijs Kooijman

Use std::size_t in new/delete

The standard dictates that `std::size_t` is used, rather than the plain
`size_t` type.

Even though these types are usually, if not always, exactly the same
type, other code might assume that `std::size_t` is actually used and thus
also available under that name after including `<new>`.

This fixes that by using the right type. One challenge is that it is
usually declared in headers that we do not have available, so this just
defines the `std::size_t` type in the `<new>` header to work around
that.
parent 1a885ce8
...@@ -31,23 +31,29 @@ namespace std { ...@@ -31,23 +31,29 @@ namespace std {
typedef void (*new_handler)(); typedef void (*new_handler)();
new_handler set_new_handler(new_handler new_p) noexcept; new_handler set_new_handler(new_handler new_p) noexcept;
new_handler get_new_handler() noexcept; new_handler get_new_handler() noexcept;
// This is normally declared in various headers that we do not have
// available, so just define it here. We could also use ::size_t
// below, but then anyone including <new> can no longer assume
// std::size_t is available.
using size_t = ::size_t;
} // namespace std } // namespace std
[[gnu::weak]] void * operator new(size_t size); [[gnu::weak]] void * operator new(std::size_t size);
[[gnu::weak]] void * operator new[](size_t size); [[gnu::weak]] void * operator new[](std::size_t size);
[[gnu::weak]] void * operator new(size_t size, const std::nothrow_t tag) noexcept; [[gnu::weak]] void * operator new(std::size_t size, const std::nothrow_t tag) noexcept;
[[gnu::weak]] void * operator new[](size_t size, const std::nothrow_t& tag) noexcept; [[gnu::weak]] void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept;
void * operator new(size_t size, void *place) noexcept; void * operator new(std::size_t size, void *place) noexcept;
void * operator new[](size_t size, void *place) noexcept; void * operator new[](std::size_t size, void *place) noexcept;
[[gnu::weak]] void operator delete(void * ptr) noexcept; [[gnu::weak]] void operator delete(void * ptr) noexcept;
[[gnu::weak]] void operator delete[](void * ptr) noexcept; [[gnu::weak]] void operator delete[](void * ptr) noexcept;
#if __cplusplus >= 201402L #if __cplusplus >= 201402L
[[gnu::weak]] void operator delete(void* ptr, size_t size) noexcept; [[gnu::weak]] void operator delete(void* ptr, std::size_t size) noexcept;
[[gnu::weak]] void operator delete[](void * ptr, size_t size) noexcept; [[gnu::weak]] void operator delete[](void * ptr, std::size_t size) noexcept;
#endif // __cplusplus >= 201402L #endif // __cplusplus >= 201402L
[[gnu::weak]] void operator delete(void* ptr, const std::nothrow_t& tag) noexcept; [[gnu::weak]] void operator delete(void* ptr, const std::nothrow_t& tag) noexcept;
......
...@@ -36,7 +36,7 @@ namespace std { ...@@ -36,7 +36,7 @@ namespace std {
const nothrow_t nothrow; const nothrow_t nothrow;
} }
static void * new_helper(size_t size) { static void * new_helper(std::size_t size) {
// Even zero-sized allocations should return a unique pointer, but // Even zero-sized allocations should return a unique pointer, but
// malloc does not guarantee this // malloc does not guarantee this
if (size == 0) if (size == 0)
...@@ -44,7 +44,7 @@ static void * new_helper(size_t size) { ...@@ -44,7 +44,7 @@ static void * new_helper(size_t size) {
return malloc(size); return malloc(size);
} }
void * operator new(size_t size) { void * operator new(std::size_t size) {
void *res = new_helper(size); void *res = new_helper(size);
#if defined(NEW_TERMINATES_ON_FAILURE) #if defined(NEW_TERMINATES_ON_FAILURE)
if (!res) if (!res)
...@@ -52,11 +52,11 @@ void * operator new(size_t size) { ...@@ -52,11 +52,11 @@ void * operator new(size_t size) {
#endif #endif
return res; return res;
} }
void * operator new[](size_t size) { void * operator new[](std::size_t size) {
return operator new(size); return operator new(size);
} }
void * operator new(size_t size, const std::nothrow_t tag) noexcept { void * operator new(std::size_t size, const std::nothrow_t tag) noexcept {
#if defined(NEW_TERMINATES_ON_FAILURE) #if defined(NEW_TERMINATES_ON_FAILURE)
// Cannot call throwing operator new as standard suggests, so call // Cannot call throwing operator new as standard suggests, so call
// new_helper directly then // new_helper directly then
...@@ -65,7 +65,7 @@ void * operator new(size_t size, const std::nothrow_t tag) noexcept { ...@@ -65,7 +65,7 @@ void * operator new(size_t size, const std::nothrow_t tag) noexcept {
return operator new(size); return operator new(size);
#endif #endif
} }
void * operator new[](size_t size, const std::nothrow_t& tag) noexcept { void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept {
#if defined(NEW_TERMINATES_ON_FAILURE) #if defined(NEW_TERMINATES_ON_FAILURE)
// Cannot call throwing operator new[] as standard suggests, so call // Cannot call throwing operator new[] as standard suggests, so call
// malloc directly then // malloc directly then
...@@ -75,12 +75,12 @@ void * operator new[](size_t size, const std::nothrow_t& tag) noexcept { ...@@ -75,12 +75,12 @@ void * operator new[](size_t size, const std::nothrow_t& tag) noexcept {
#endif #endif
} }
void * operator new(size_t size, void *place) noexcept { void * operator new(std::size_t size, void *place) noexcept {
// Nothing to do // Nothing to do
(void)size; // unused (void)size; // unused
return place; return place;
} }
void * operator new[](size_t size, void *place) noexcept { void * operator new[](std::size_t size, void *place) noexcept {
return operator new(size, place); return operator new(size, place);
} }
...@@ -92,10 +92,10 @@ void operator delete[](void * ptr) noexcept { ...@@ -92,10 +92,10 @@ void operator delete[](void * ptr) noexcept {
} }
#if __cplusplus >= 201402L #if __cplusplus >= 201402L
void operator delete(void* ptr, size_t size) noexcept { void operator delete(void* ptr, std::size_t size) noexcept {
operator delete(ptr); operator delete(ptr);
} }
void operator delete[](void * ptr, size_t size) noexcept { void operator delete[](void * ptr, std::size_t size) noexcept {
operator delete[](ptr); operator delete[](ptr);
} }
#endif // __cplusplus >= 201402L #endif // __cplusplus >= 201402L
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment