Skip to content

Commit

Permalink
add thunks for operator new and operator delete
Browse files Browse the repository at this point in the history
Reviewed By: Orvid

Differential Revision: D46865108

fbshipit-source-id: 6f25d1216e971a5dc0552ad2fbd3017ac0e76d85
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jun 23, 2023
1 parent c82d4fb commit 96d31e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions folly/lang/Thunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <exception>

#include <folly/Utility.h>
#include <folly/lang/New.h>

namespace folly {
namespace detail {
Expand Down Expand Up @@ -73,6 +74,19 @@ struct thunk {
return ::new (dst) T(static_cast<T&&>(*reinterpret_cast<T*>(src)));
}

template <std::size_t Size, std::size_t Align>
static void* operator_new() {
return folly::operator_new(Size, align_val_t(Align));
}
template <std::size_t Size, std::size_t Align>
static void* operator_new_nx() {
return folly::operator_new(Size, align_val_t(Align), std::nothrow);
}
template <std::size_t Size, std::size_t Align>
static void operator_delete(void* const ptr) noexcept {
return folly::operator_delete(ptr, Size, align_val_t(Align));
}

template <typename... A>
static void noop(A...) noexcept {}

Expand Down
9 changes: 9 additions & 0 deletions folly/lang/test/ThunkTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ TEST_F(ThunkTest, ctor_dtor) {
}
}

TEST_F(ThunkTest, operator_new_delete) {
auto ptr = thunk::operator_new<64, 32>();
EXPECT_NE(nullptr, ptr);
thunk::operator_delete<64, 32>(ptr);
ptr = thunk::operator_new_nx<64, 32>();
EXPECT_NE(nullptr, ptr);
thunk::operator_delete<64, 32>(ptr);
}

} // namespace folly::detail

0 comments on commit 96d31e8

Please sign in to comment.