mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
Refactor naming convention in new and delete functions.
This commit is contained in:
parent
a0732c892c
commit
9206e674c1
@ -106,11 +106,11 @@ struct policy_cpp_new {
|
||||
template <std::size_t AllocSize>
|
||||
struct policy_pmr_new {
|
||||
static void *allocate() noexcept {
|
||||
return pmr::new$<std::array<char, AllocSize>>();
|
||||
return pmr::$new<std::array<char, AllocSize>>();
|
||||
}
|
||||
|
||||
static void deallocate(void *p) noexcept {
|
||||
pmr::delete$(static_cast<std::array<char, AllocSize> *>(p));
|
||||
pmr::$delete(static_cast<std::array<char, AllocSize> *>(p));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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_
|
||||
|
||||
@ -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 <typename T, typename... A>
|
||||
T *new$(A &&... args) noexcept {
|
||||
T *$new(A &&... args) noexcept {
|
||||
auto *res = get_regular_resource<T>();
|
||||
if (res == nullptr) return nullptr;
|
||||
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.
|
||||
/// \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 <typename T>
|
||||
void delete$(T *p) noexcept {
|
||||
void $delete(T *p) noexcept {
|
||||
if (p == nullptr) return;
|
||||
::LIBIMP::destroy(p);
|
||||
auto *res = get_regular_resource<T>();
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)());
|
||||
}
|
||||
|
||||
TEST(pmr_new, new$) {
|
||||
auto p = pmr::new$<int>();
|
||||
TEST(pmr_new, $new) {
|
||||
auto p = pmr::$new<int>();
|
||||
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$<int>((std::numeric_limits<int>::max)());
|
||||
TEST(pmr_new, $new_value) {
|
||||
auto p = pmr::$new<int>((std::numeric_limits<int>::max)());
|
||||
ASSERT_NE(p, nullptr);
|
||||
ASSERT_EQ(*p, (std::numeric_limits<int>::max)());
|
||||
pmr::delete$(p);
|
||||
pmr::$delete(p);
|
||||
}
|
||||
|
||||
namespace {
|
||||
@ -44,7 +44,7 @@ void test_new$array() {
|
||||
std::array<void *, Pts> pts;
|
||||
using T = std::array<char, N>;
|
||||
for (int i = 0; i < (int)pts.size(); ++i) {
|
||||
auto p = pmr::new$<T>();
|
||||
auto p = pmr::$new<T>();
|
||||
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<T *>(pts[i]));
|
||||
pmr::$delete(static_cast<T *>(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$<Derived>(-1);
|
||||
TEST(pmr_new, $delete_poly) {
|
||||
Base *p = pmr::$new<Derived>(-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$<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(construct_count__, (std::numeric_limits<int>::max)());
|
||||
pmr::delete$(p);
|
||||
pmr::$delete(p);
|
||||
ASSERT_EQ(construct_count__, 0);
|
||||
}
|
||||
|
||||
TEST(pmr_new, delete$poly64k) {
|
||||
Base *p = pmr::new$<Derived64K>(-1);
|
||||
TEST(pmr_new, $delete_poly64k) {
|
||||
Base *p = pmr::$new<Derived64K>(-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$<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(construct_count__, (std::numeric_limits<int>::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$<int>();
|
||||
auto p = pmr::$new<int>();
|
||||
*p = i;
|
||||
pmr::delete$(p);
|
||||
pmr::$delete(p);
|
||||
}
|
||||
std::array<void *, 10000> pts;
|
||||
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;
|
||||
std::memset(p, i, sizeof(std::array<char, 10>));
|
||||
}
|
||||
@ -158,7 +158,7 @@ TEST(pmr_new, multi_thread) {
|
||||
std::array<char, 10> tmp;
|
||||
std::memset(&tmp, i, sizeof(std::array<char, 10>));
|
||||
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]));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user