Skip to content

Commit a0732c8

Browse files
committed
Refactor memory block resource retrieval in new.h
1 parent a5456d9 commit a0732c8

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

include/libpmr/new.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class block_pool_resource : public block_resource_base<BlockSize, BlockPoolExpan
125125
};
126126

127127
/// \brief Different increment levels match different chunk sizes.
128-
/// 512 means that 512 consecutive memory blocks are allocated at a time, and the block size is N.
128+
/// 512 means that 512 consecutive memory blocks are allocated at a time.
129129
template <std::size_t L>
130130
constexpr std::size_t block_pool_expansion = 0;
131131

@@ -134,20 +134,18 @@ template <> constexpr std::size_t block_pool_expansion<1> = 256;
134134
template <> constexpr std::size_t block_pool_expansion<2> = 128;
135135
template <> constexpr std::size_t block_pool_expansion<3> = 64;
136136

137-
/// \brief Match the appropriate memory block resources according to the size of the specification.
138-
template <std::size_t N, std::size_t L = regular_level(N)>
139-
struct regular_resource {
140-
static auto *get() noexcept {
141-
using block_poll_resource_t = block_pool_resource<N, block_pool_expansion<L>>;
142-
return dynamic_cast<block_poll_resource_t *>(block_poll_resource_t::get());
143-
}
144-
};
137+
/// \brief Matches the appropriate memory block resource based on the specified type.
138+
template <typename T, std::size_t N = regular_sizeof<T>(), std::size_t L = regular_level(N)>
139+
auto *get_regular_resource() noexcept {
140+
using block_poll_resource_t = block_pool_resource<N, block_pool_expansion<L>>;
141+
return dynamic_cast<block_poll_resource_t *>(block_poll_resource_t::get());
142+
}
145143

146144
/// \brief Creates an object based on the specified type and parameters with block pool resource.
147145
/// \note This function is thread-safe.
148146
template <typename T, typename... A>
149147
T *new$(A &&... args) noexcept {
150-
auto *res = regular_resource<regular_sizeof<T>()>::get();
148+
auto *res = get_regular_resource<T>();
151149
if (res == nullptr) return nullptr;
152150
return ::LIBIMP::construct<T>(res->allocate(sizeof(T), alignof(T)), std::forward<A>(args)...);
153151
}
@@ -159,7 +157,7 @@ template <typename T>
159157
void delete$(T *p) noexcept {
160158
if (p == nullptr) return;
161159
::LIBIMP::destroy(p);
162-
auto *res = regular_resource<regular_sizeof<T>()>::get();
160+
auto *res = get_regular_resource<T>();
163161
if (res == nullptr) return;
164162
#if (LIBIMP_CC_MSVC > LIBIMP_CC_MSVC_2015)
165163
res->deallocate(p, sizeof(T), alignof(T));

0 commit comments

Comments
 (0)