From 9206e674c12e00f062fb9ee400d5bc6c1b2a58db Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 10 Mar 2024 00:11:18 +0800 Subject: [PATCH] Refactor naming convention in new and delete functions. --- benchmark/benchmark_new.cpp | 4 +-- include/libimp/detect_plat.h | 4 +-- include/libimp/scope_exit.h | 4 +-- include/libpmr/new.h | 8 +++--- src/libimp/codecvt.cpp | 6 ++--- test/pmr/test_pmr_new.cpp | 50 ++++++++++++++++++------------------ 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/benchmark/benchmark_new.cpp b/benchmark/benchmark_new.cpp index 7dc1c67..edef459 100644 --- a/benchmark/benchmark_new.cpp +++ b/benchmark/benchmark_new.cpp @@ -106,11 +106,11 @@ struct policy_cpp_new { template struct policy_pmr_new { static void *allocate() noexcept { - return pmr::new$>(); + return pmr::$new>(); } static void deallocate(void *p) noexcept { - pmr::delete$(static_cast *>(p)); + pmr::$delete(static_cast *>(p)); } }; diff --git a/include/libimp/detect_plat.h b/include/libimp/detect_plat.h index 26e8c18..1bc12dc 100644 --- a/include/libimp/detect_plat.h +++ b/include/libimp/detect_plat.h @@ -211,9 +211,9 @@ defined(__EXCEPTIONS) || defined(_CPPUNWIND) # define LIBIMP_TRY try # define LIBIMP_CATCH(...) catch (__VA_ARGS__) -# define LIBIMP_THROW($exception, ...) throw $exception +# define LIBIMP_THROW($EXCEPTION, ...) throw $EXCEPTION #else # define LIBIMP_TRY if (true) # define LIBIMP_CATCH(...) else if (false) -# define LIBIMP_THROW($exception, ...) return __VA_ARGS__ +# define LIBIMP_THROW($EXCEPTION, ...) return __VA_ARGS__ #endif diff --git a/include/libimp/scope_exit.h b/include/libimp/scope_exit.h index 382c44a..7604311 100644 --- a/include/libimp/scope_exit.h +++ b/include/libimp/scope_exit.h @@ -73,7 +73,7 @@ struct scope_exit_helper { } // namespace detail -#define LIBIMP_SCOPE_EXIT($val) \ - LIBIMP_UNUSED auto $val = ::LIBIMP::detail::scope_exit_helper{} +#define LIBIMP_SCOPE_EXIT($VAL) \ + LIBIMP_UNUSED auto $VAL = ::LIBIMP::detail::scope_exit_helper{} LIBIMP_NAMESPACE_END_ diff --git a/include/libpmr/new.h b/include/libpmr/new.h index c160a51..d3a70e1 100644 --- a/include/libpmr/new.h +++ b/include/libpmr/new.h @@ -144,17 +144,17 @@ auto *get_regular_resource() noexcept { /// \brief Creates an object based on the specified type and parameters with block pool resource. /// \note This function is thread-safe. template -T *new$(A &&... args) noexcept { +T *$new(A &&... args) noexcept { auto *res = get_regular_resource(); if (res == nullptr) return nullptr; return ::LIBIMP::construct(res->allocate(sizeof(T), alignof(T)), std::forward(args)...); } -/// \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$`, +/// \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`, /// additional performance penalties may be incurred. template -void delete$(T *p) noexcept { +void $delete(T *p) noexcept { if (p == nullptr) return; ::LIBIMP::destroy(p); auto *res = get_regular_resource(); diff --git a/src/libimp/codecvt.cpp b/src/libimp/codecvt.cpp index 84d8a88..6de3e0a 100644 --- a/src/libimp/codecvt.cpp +++ b/src/libimp/codecvt.cpp @@ -324,12 +324,12 @@ auto cvt_cstr_utf(T const *src, std::size_t slen, U *des, std::size_t dlen) noex } // namespace -#define LIBIMP_DEF_CVT_CSTR_($char_t, $char_u) \ +#define LIBIMP_DEF_CVT_CSTR_($CHAR_T, $CHAR_U) \ 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); \ } -// #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 , char16_t) diff --git a/test/pmr/test_pmr_new.cpp b/test/pmr/test_pmr_new.cpp index 5095751..e3b1c69 100644 --- a/test/pmr/test_pmr_new.cpp +++ b/test/pmr/test_pmr_new.cpp @@ -22,19 +22,19 @@ TEST(pmr_new, regular_sizeof) { ASSERT_EQ((pmr::regular_sizeof>()), (std::numeric_limits::max)()); } -TEST(pmr_new, new$) { - auto p = pmr::new$(); +TEST(pmr_new, $new) { + auto p = pmr::$new(); ASSERT_NE(p, nullptr); *p = -1; ASSERT_EQ(*p, -1); - pmr::delete$(p); + pmr::$delete(p); } -TEST(pmr_new, new$value) { - auto p = pmr::new$((std::numeric_limits::max)()); +TEST(pmr_new, $new_value) { + auto p = pmr::$new((std::numeric_limits::max)()); ASSERT_NE(p, nullptr); ASSERT_EQ(*p, (std::numeric_limits::max)()); - pmr::delete$(p); + pmr::$delete(p); } namespace { @@ -44,7 +44,7 @@ void test_new$array() { std::array pts; using T = std::array; for (int i = 0; i < (int)pts.size(); ++i) { - auto p = pmr::new$(); + auto p = pmr::$new(); pts[i] = p; std::memset(p, i, sizeof(T)); } @@ -52,13 +52,13 @@ void test_new$array() { T tmp; std::memset(&tmp, i, sizeof(T)); ASSERT_EQ(std::memcmp(pts[i], &tmp, sizeof(T)), 0); - pmr::delete$(static_cast(pts[i])); + pmr::$delete(static_cast(pts[i])); } } } // namespace -TEST(pmr_new, new$array) { +TEST(pmr_new, $new_array) { test_new$array<1000, 10>(); test_new$array<1000, 100>(); test_new$array<1000, 1000>(); @@ -103,39 +103,39 @@ private: } // namespace -TEST(pmr_new, delete$poly) { - Base *p = pmr::new$(-1); +TEST(pmr_new, $delete_poly) { + Base *p = pmr::$new(-1); ASSERT_NE(p, nullptr); ASSERT_EQ(p->get(), -1); ASSERT_EQ(construct_count__, -1); - pmr::delete$(p); + pmr::$delete(p); ASSERT_EQ(construct_count__, 0); - ASSERT_EQ(p, pmr::new$((std::numeric_limits::max)())); + ASSERT_EQ(p, pmr::$new((std::numeric_limits::max)())); ASSERT_EQ(p->get(), (std::numeric_limits::max)()); ASSERT_EQ(construct_count__, (std::numeric_limits::max)()); - pmr::delete$(p); + pmr::$delete(p); ASSERT_EQ(construct_count__, 0); } -TEST(pmr_new, delete$poly64k) { - Base *p = pmr::new$(-1); +TEST(pmr_new, $delete_poly64k) { + Base *p = pmr::$new(-1); ASSERT_NE(p, nullptr); ASSERT_EQ(p->get(), -1); ASSERT_EQ(construct_count__, -1); - pmr::delete$(p); + pmr::$delete(p); ASSERT_EQ(construct_count__, 0); - Base *q = pmr::new$((std::numeric_limits::max)()); + Base *q = pmr::$new((std::numeric_limits::max)()); ASSERT_EQ(q->get(), (std::numeric_limits::max)()); ASSERT_EQ(construct_count__, (std::numeric_limits::max)()); - pmr::delete$(q); + pmr::$delete(q); ASSERT_EQ(construct_count__, 0); } -TEST(pmr_new, delete$null) { +TEST(pmr_new, $delete_null) { Base *p = nullptr; - pmr::delete$(p); + pmr::$delete(p); SUCCEED(); } @@ -144,13 +144,13 @@ TEST(pmr_new, multi_thread) { for (auto &t : threads) { t = std::thread([] { for (int i = 0; i < 10000; ++i) { - auto p = pmr::new$(); + auto p = pmr::$new(); *p = i; - pmr::delete$(p); + pmr::$delete(p); } std::array pts; for (int i = 0; i < 10000; ++i) { - auto p = pmr::new$>(); + auto p = pmr::$new>(); pts[i] = p; std::memset(p, i, sizeof(std::array)); } @@ -158,7 +158,7 @@ TEST(pmr_new, multi_thread) { std::array tmp; std::memset(&tmp, i, sizeof(std::array)); ASSERT_EQ(std::memcmp(pts[i], &tmp, sizeof(std::array)), 0); - pmr::delete$(static_cast *>(pts[i])); + pmr::$delete(static_cast *>(pts[i])); } }); }