Skip to content

Improve and complete implementation of new/delete #361

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

Merged
merged 8 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
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.
  • Loading branch information
matthijskooijman committed Sep 17, 2020
commit 6d292502e138b5c73705f0df8095ded3f1df956f
22 changes: 14 additions & 8 deletions cores/arduino/new
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,29 @@ namespace std {
typedef void (*new_handler)();
new_handler set_new_handler(new_handler new_p) 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

[[gnu::weak]] void * operator new(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[](std::size_t size);

[[gnu::weak]] void * operator new(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;
[[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[](size_t size, void *place) noexcept;
void * operator new(std::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;

#if __cplusplus >= 201402L
[[gnu::weak]] void operator delete(void* ptr, 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;
[[gnu::weak]] void operator delete[](void * ptr, std::size_t size) noexcept;
#endif // __cplusplus >= 201402L

[[gnu::weak]] void operator delete(void* ptr, const std::nothrow_t& tag) noexcept;
Expand Down
18 changes: 9 additions & 9 deletions cores/arduino/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ namespace std {
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
// malloc does not guarantee this
if (size == 0)
size = 1;
return malloc(size);
}

void * operator new(size_t size) {
void * operator new(std::size_t size) {
void *res = new_helper(size);
#if defined(NEW_TERMINATES_ON_FAILURE)
if (!res)
std::terminate();
#endif
return res;
}
void * operator new[](size_t size) {
void * operator new[](std::size_t 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)
// Cannot call throwing operator new as standard suggests, so call
// new_helper directly then
Expand All @@ -65,7 +65,7 @@ void * operator new(size_t size, const std::nothrow_t tag) noexcept {
return operator new(size);
#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)
// Cannot call throwing operator new[] as standard suggests, so call
// malloc directly then
Expand All @@ -75,12 +75,12 @@ void * operator new[](size_t size, const std::nothrow_t& tag) noexcept {
#endif
}

void * operator new(size_t size, void *place) noexcept {
void * operator new(std::size_t size, void *place) noexcept {
// Nothing to do
(void)size; // unused
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);
}

Expand All @@ -92,10 +92,10 @@ void operator delete[](void * ptr) noexcept {
}

#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);
}
void operator delete[](void * ptr, size_t size) noexcept {
void operator delete[](void * ptr, std::size_t size) noexcept {
operator delete[](ptr);
}
#endif // __cplusplus >= 201402L
Expand Down