Skip to content

Commit b20e11d

Browse files
committed
Simplify the implementation of memory allocation management
1 parent 238d12e commit b20e11d

File tree

1 file changed

+27
-51
lines changed

1 file changed

+27
-51
lines changed

include/libipc/mem/new.h

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ namespace mem {
2626
class LIBIPC_EXPORT block_collector {
2727
public:
2828
virtual ~block_collector() noexcept = default;
29-
virtual void recycle(void *p, std::size_t bytes, std::size_t alignment) noexcept = 0;
29+
virtual void recycle(void *p) noexcept = 0;
3030
};
3131

3232
#if defined(LIBIPC_CPP_17)
33-
using recycle_t = void (*)(void *p, void *o, std::size_t bytes, std::size_t alignment) noexcept;
33+
using recycle_t = void (*)(void *p, void *o) noexcept;
3434
#else
35-
using recycle_t = void (*)(void *p, void *o, std::size_t bytes, std::size_t alignment);
35+
using recycle_t = void (*)(void *p, void *o);
3636
#endif
3737

3838
static constexpr std::size_t regular_head_size
@@ -60,14 +60,12 @@ constexpr inline std::size_t regular_sizeof_impl(std::size_t s) noexcept {
6060
}
6161

6262
/// \brief Calculates the appropriate memory block size based on the specific type.
63-
template <typename T>
64-
constexpr inline std::size_t regular_sizeof() noexcept {
65-
return regular_sizeof_impl(regular_head_size + sizeof(T));
63+
constexpr inline std::size_t regular_sizeof(std::size_t s) noexcept {
64+
return regular_sizeof_impl(regular_head_size + s);
6665
}
67-
68-
template <>
69-
constexpr inline std::size_t regular_sizeof<void>() noexcept {
70-
return (std::numeric_limits<std::size_t>::max)();
66+
template <typename T, std::size_t S = sizeof(T)>
67+
constexpr inline std::size_t regular_sizeof() noexcept {
68+
return regular_sizeof(S);
7169
}
7270

7371
/// \brief Use block pools to handle memory less than 64K.
@@ -78,7 +76,7 @@ class block_resource_base : public block_pool<BlockSize, BlockPoolExpansion> {
7876
return block_pool<BlockSize, BlockPoolExpansion>::allocate();
7977
}
8078

81-
void deallocate(void *p, std::size_t /*bytes*/, std::size_t /*alignment*/) noexcept {
79+
void deallocate(void *p) noexcept {
8280
block_pool<BlockSize, BlockPoolExpansion>::deallocate(p);
8381
}
8482
};
@@ -91,8 +89,8 @@ class block_resource_base<BlockSize, 0> : public new_delete_resource {
9189
return new_delete_resource::allocate(regular_head_size + bytes, alignment);
9290
}
9391

94-
void deallocate(void *p, std::size_t bytes, std::size_t alignment) noexcept {
95-
new_delete_resource::deallocate(p, regular_head_size + bytes, alignment);
92+
void deallocate(void *p) noexcept {
93+
new_delete_resource::deallocate(p, regular_head_size);
9694
}
9795
};
9896

@@ -103,8 +101,8 @@ class block_pool_resource : public block_resource_base<BlockSize, BlockPoolExpan
103101

104102
using base_t = block_resource_base<BlockSize, BlockPoolExpansion>;
105103

106-
void recycle(void *p, std::size_t bytes, std::size_t alignment) noexcept override {
107-
base_t::deallocate(p, bytes, alignment);
104+
void recycle(void *p) noexcept override {
105+
base_t::deallocate(p);
108106
}
109107

110108
public:
@@ -115,19 +113,13 @@ class block_pool_resource : public block_resource_base<BlockSize, BlockPoolExpan
115113

116114
template <typename T>
117115
void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept {
118-
void *p = base_t::allocate(bytes, alignment);
119-
*static_cast<recycle_t *>(p)
120-
= [](void *p, void *o, std::size_t bytes, std::size_t alignment) noexcept {
121-
std::ignore = destroy(static_cast<T *>(o));
122-
get()->recycle(p, bytes, alignment);
116+
void *b = base_t::allocate(bytes, alignment);
117+
*static_cast<recycle_t *>(b)
118+
= [](void *b, void *p) noexcept {
119+
std::ignore = destroy(static_cast<T *>(p));
120+
get()->recycle(b);
123121
};
124-
return static_cast<byte *>(p) + regular_head_size;
125-
}
126-
127-
void deallocate(void *p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept {
128-
void *b = static_cast<byte *>(p) - regular_head_size;
129-
auto *r = static_cast<recycle_t *>(b);
130-
(*r)(b, p, bytes, alignment);
122+
return static_cast<byte *>(b) + regular_head_size;
131123
}
132124
};
133125

@@ -147,6 +139,11 @@ auto *get_regular_resource() noexcept {
147139
using block_poll_resource_t = block_pool_resource<N, block_pool_expansion<L>>;
148140
return dynamic_cast<block_poll_resource_t *>(block_poll_resource_t::get());
149141
}
142+
template <typename T, std::enable_if_t<std::is_void<T>::value, bool> = true>
143+
auto *get_regular_resource() noexcept {
144+
using block_poll_resource_t = block_pool_resource<0, 0>;
145+
return dynamic_cast<block_poll_resource_t *>(block_poll_resource_t::get());
146+
}
150147

151148
namespace detail_new {
152149

@@ -171,27 +168,6 @@ struct do_allocate<void> {
171168
}
172169
};
173170

174-
template <typename T>
175-
struct do_deallocate {
176-
template <typename R>
177-
static void apply(R *res, T *p) noexcept {
178-
#if (LIBIPC_CC_MSVC > LIBIPC_CC_MSVC_2015)
179-
res->deallocate(p, sizeof(T), alignof(T));
180-
#else
181-
// `alignof` of vs2015 requires that type must be able to be instantiated.
182-
res->deallocate(p, sizeof(T));
183-
#endif
184-
}
185-
};
186-
187-
template <>
188-
struct do_deallocate<void> {
189-
template <typename R>
190-
static void apply(R *res, void *p) noexcept {
191-
res->deallocate(p, 0);
192-
}
193-
};
194-
195171
} // namespace detail_new
196172

197173
/// \brief Creates an object based on the specified type and parameters with block pool resource.
@@ -209,9 +185,9 @@ T *$new(A &&... args) noexcept {
209185
template <typename T>
210186
void $delete(T *p) noexcept {
211187
if (p == nullptr) return;
212-
auto *res = get_regular_resource<T>();
213-
if (res == nullptr) return;
214-
detail_new::do_deallocate<T>::apply(res, p);
188+
auto *b = reinterpret_cast<byte *>(p) - regular_head_size;
189+
auto *r = reinterpret_cast<recycle_t *>(b);
190+
(*r)(b, p);
215191
}
216192

217193
/// \brief The destruction policy used by std::unique_ptr.

0 commit comments

Comments
 (0)