Refactor naming convention in new and delete functions.

This commit is contained in:
mutouyun 2024-03-10 00:11:18 +08:00
parent a0732c892c
commit 9206e674c1
6 changed files with 38 additions and 38 deletions

View File

@ -106,11 +106,11 @@ struct policy_cpp_new {
template <std::size_t AllocSize> template <std::size_t AllocSize>
struct policy_pmr_new { struct policy_pmr_new {
static void *allocate() noexcept { static void *allocate() noexcept {
return pmr::new$<std::array<char, AllocSize>>(); return pmr::$new<std::array<char, AllocSize>>();
} }
static void deallocate(void *p) noexcept { static void deallocate(void *p) noexcept {
pmr::delete$(static_cast<std::array<char, AllocSize> *>(p)); pmr::$delete(static_cast<std::array<char, AllocSize> *>(p));
} }
}; };

View File

@ -211,9 +211,9 @@
defined(__EXCEPTIONS) || defined(_CPPUNWIND) defined(__EXCEPTIONS) || defined(_CPPUNWIND)
# define LIBIMP_TRY try # define LIBIMP_TRY try
# define LIBIMP_CATCH(...) catch (__VA_ARGS__) # define LIBIMP_CATCH(...) catch (__VA_ARGS__)
# define LIBIMP_THROW($exception, ...) throw $exception # define LIBIMP_THROW($EXCEPTION, ...) throw $EXCEPTION
#else #else
# define LIBIMP_TRY if (true) # define LIBIMP_TRY if (true)
# define LIBIMP_CATCH(...) else if (false) # define LIBIMP_CATCH(...) else if (false)
# define LIBIMP_THROW($exception, ...) return __VA_ARGS__ # define LIBIMP_THROW($EXCEPTION, ...) return __VA_ARGS__
#endif #endif

View File

@ -73,7 +73,7 @@ struct scope_exit_helper {
} // namespace detail } // namespace detail
#define LIBIMP_SCOPE_EXIT($val) \ #define LIBIMP_SCOPE_EXIT($VAL) \
LIBIMP_UNUSED auto $val = ::LIBIMP::detail::scope_exit_helper{} LIBIMP_UNUSED auto $VAL = ::LIBIMP::detail::scope_exit_helper{}
LIBIMP_NAMESPACE_END_ LIBIMP_NAMESPACE_END_

View File

@ -144,17 +144,17 @@ auto *get_regular_resource() noexcept {
/// \brief Creates an object based on the specified type and parameters with block pool resource. /// \brief Creates an object based on the specified type and parameters with block pool resource.
/// \note This function is thread-safe. /// \note This function is thread-safe.
template <typename T, typename... A> template <typename T, typename... A>
T *new$(A &&... args) noexcept { T *$new(A &&... args) noexcept {
auto *res = get_regular_resource<T>(); auto *res = get_regular_resource<T>();
if (res == nullptr) return nullptr; if (res == nullptr) return nullptr;
return ::LIBIMP::construct<T>(res->allocate(sizeof(T), alignof(T)), std::forward<A>(args)...); return ::LIBIMP::construct<T>(res->allocate(sizeof(T), alignof(T)), std::forward<A>(args)...);
} }
/// \brief Destroys object previously allocated by the `new$` and releases obtained memory area. /// \brief Destroys object previously allocated by the `$new` and releases obtained memory area.
/// \note This function is thread-safe. If the pointer type passed in is different from `new$`, /// \note This function is thread-safe. If the pointer type passed in is different from `$new`,
/// additional performance penalties may be incurred. /// additional performance penalties may be incurred.
template <typename T> template <typename T>
void delete$(T *p) noexcept { void $delete(T *p) noexcept {
if (p == nullptr) return; if (p == nullptr) return;
::LIBIMP::destroy(p); ::LIBIMP::destroy(p);
auto *res = get_regular_resource<T>(); auto *res = get_regular_resource<T>();

View File

@ -324,12 +324,12 @@ auto cvt_cstr_utf(T const *src, std::size_t slen, U *des, std::size_t dlen) noex
} // namespace } // namespace
#define LIBIMP_DEF_CVT_CSTR_($char_t, $char_u) \ #define LIBIMP_DEF_CVT_CSTR_($CHAR_T, $CHAR_U) \
template <> \ template <> \
std::size_t cvt_cstr($char_t const *src, std::size_t slen, $char_u *des, std::size_t dlen) noexcept { \ std::size_t cvt_cstr($CHAR_T const *src, std::size_t slen, $CHAR_U *des, std::size_t dlen) noexcept { \
return cvt_cstr_utf(src, slen, des, dlen); \ return cvt_cstr_utf(src, slen, des, dlen); \
} }
// #define LIBIMP_DEF_CVT_CSTR_($char_t, $char_u) // #define LIBIMP_DEF_CVT_CSTR_($CHAR_T, $CHAR_U)
LIBIMP_DEF_CVT_CSTR_(char , char) LIBIMP_DEF_CVT_CSTR_(char , char)
LIBIMP_DEF_CVT_CSTR_(char , char16_t) LIBIMP_DEF_CVT_CSTR_(char , char16_t)

View File

@ -22,19 +22,19 @@ TEST(pmr_new, regular_sizeof) {
ASSERT_EQ((pmr::regular_sizeof<std::array<char, 100000>>()), (std::numeric_limits<std::size_t>::max)()); ASSERT_EQ((pmr::regular_sizeof<std::array<char, 100000>>()), (std::numeric_limits<std::size_t>::max)());
} }
TEST(pmr_new, new$) { TEST(pmr_new, $new) {
auto p = pmr::new$<int>(); auto p = pmr::$new<int>();
ASSERT_NE(p, nullptr); ASSERT_NE(p, nullptr);
*p = -1; *p = -1;
ASSERT_EQ(*p, -1); ASSERT_EQ(*p, -1);
pmr::delete$(p); pmr::$delete(p);
} }
TEST(pmr_new, new$value) { TEST(pmr_new, $new_value) {
auto p = pmr::new$<int>((std::numeric_limits<int>::max)()); auto p = pmr::$new<int>((std::numeric_limits<int>::max)());
ASSERT_NE(p, nullptr); ASSERT_NE(p, nullptr);
ASSERT_EQ(*p, (std::numeric_limits<int>::max)()); ASSERT_EQ(*p, (std::numeric_limits<int>::max)());
pmr::delete$(p); pmr::$delete(p);
} }
namespace { namespace {
@ -44,7 +44,7 @@ void test_new$array() {
std::array<void *, Pts> pts; std::array<void *, Pts> pts;
using T = std::array<char, N>; using T = std::array<char, N>;
for (int i = 0; i < (int)pts.size(); ++i) { for (int i = 0; i < (int)pts.size(); ++i) {
auto p = pmr::new$<T>(); auto p = pmr::$new<T>();
pts[i] = p; pts[i] = p;
std::memset(p, i, sizeof(T)); std::memset(p, i, sizeof(T));
} }
@ -52,13 +52,13 @@ void test_new$array() {
T tmp; T tmp;
std::memset(&tmp, i, sizeof(T)); std::memset(&tmp, i, sizeof(T));
ASSERT_EQ(std::memcmp(pts[i], &tmp, sizeof(T)), 0); ASSERT_EQ(std::memcmp(pts[i], &tmp, sizeof(T)), 0);
pmr::delete$(static_cast<T *>(pts[i])); pmr::$delete(static_cast<T *>(pts[i]));
} }
} }
} // namespace } // namespace
TEST(pmr_new, new$array) { TEST(pmr_new, $new_array) {
test_new$array<1000, 10>(); test_new$array<1000, 10>();
test_new$array<1000, 100>(); test_new$array<1000, 100>();
test_new$array<1000, 1000>(); test_new$array<1000, 1000>();
@ -103,39 +103,39 @@ private:
} // namespace } // namespace
TEST(pmr_new, delete$poly) { TEST(pmr_new, $delete_poly) {
Base *p = pmr::new$<Derived>(-1); Base *p = pmr::$new<Derived>(-1);
ASSERT_NE(p, nullptr); ASSERT_NE(p, nullptr);
ASSERT_EQ(p->get(), -1); ASSERT_EQ(p->get(), -1);
ASSERT_EQ(construct_count__, -1); ASSERT_EQ(construct_count__, -1);
pmr::delete$(p); pmr::$delete(p);
ASSERT_EQ(construct_count__, 0); ASSERT_EQ(construct_count__, 0);
ASSERT_EQ(p, pmr::new$<Derived>((std::numeric_limits<int>::max)())); ASSERT_EQ(p, pmr::$new<Derived>((std::numeric_limits<int>::max)()));
ASSERT_EQ(p->get(), (std::numeric_limits<int>::max)()); ASSERT_EQ(p->get(), (std::numeric_limits<int>::max)());
ASSERT_EQ(construct_count__, (std::numeric_limits<int>::max)()); ASSERT_EQ(construct_count__, (std::numeric_limits<int>::max)());
pmr::delete$(p); pmr::$delete(p);
ASSERT_EQ(construct_count__, 0); ASSERT_EQ(construct_count__, 0);
} }
TEST(pmr_new, delete$poly64k) { TEST(pmr_new, $delete_poly64k) {
Base *p = pmr::new$<Derived64K>(-1); Base *p = pmr::$new<Derived64K>(-1);
ASSERT_NE(p, nullptr); ASSERT_NE(p, nullptr);
ASSERT_EQ(p->get(), -1); ASSERT_EQ(p->get(), -1);
ASSERT_EQ(construct_count__, -1); ASSERT_EQ(construct_count__, -1);
pmr::delete$(p); pmr::$delete(p);
ASSERT_EQ(construct_count__, 0); ASSERT_EQ(construct_count__, 0);
Base *q = pmr::new$<Derived64K>((std::numeric_limits<int>::max)()); Base *q = pmr::$new<Derived64K>((std::numeric_limits<int>::max)());
ASSERT_EQ(q->get(), (std::numeric_limits<int>::max)()); ASSERT_EQ(q->get(), (std::numeric_limits<int>::max)());
ASSERT_EQ(construct_count__, (std::numeric_limits<int>::max)()); ASSERT_EQ(construct_count__, (std::numeric_limits<int>::max)());
pmr::delete$(q); pmr::$delete(q);
ASSERT_EQ(construct_count__, 0); ASSERT_EQ(construct_count__, 0);
} }
TEST(pmr_new, delete$null) { TEST(pmr_new, $delete_null) {
Base *p = nullptr; Base *p = nullptr;
pmr::delete$(p); pmr::$delete(p);
SUCCEED(); SUCCEED();
} }
@ -144,13 +144,13 @@ TEST(pmr_new, multi_thread) {
for (auto &t : threads) { for (auto &t : threads) {
t = std::thread([] { t = std::thread([] {
for (int i = 0; i < 10000; ++i) { for (int i = 0; i < 10000; ++i) {
auto p = pmr::new$<int>(); auto p = pmr::$new<int>();
*p = i; *p = i;
pmr::delete$(p); pmr::$delete(p);
} }
std::array<void *, 10000> pts; std::array<void *, 10000> pts;
for (int i = 0; i < 10000; ++i) { for (int i = 0; i < 10000; ++i) {
auto p = pmr::new$<std::array<char, 10>>(); auto p = pmr::$new<std::array<char, 10>>();
pts[i] = p; pts[i] = p;
std::memset(p, i, sizeof(std::array<char, 10>)); std::memset(p, i, sizeof(std::array<char, 10>));
} }
@ -158,7 +158,7 @@ TEST(pmr_new, multi_thread) {
std::array<char, 10> tmp; std::array<char, 10> tmp;
std::memset(&tmp, i, sizeof(std::array<char, 10>)); std::memset(&tmp, i, sizeof(std::array<char, 10>));
ASSERT_EQ(std::memcmp(pts[i], &tmp, sizeof(std::array<char, 10>)), 0); ASSERT_EQ(std::memcmp(pts[i], &tmp, sizeof(std::array<char, 10>)), 0);
pmr::delete$(static_cast<std::array<char, 10> *>(pts[i])); pmr::$delete(static_cast<std::array<char, 10> *>(pts[i]));
} }
}); });
} }