@@ -26,13 +26,13 @@ namespace mem {
26
26
class LIBIPC_EXPORT block_collector {
27
27
public:
28
28
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;
30
30
};
31
31
32
32
#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 ;
34
34
#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);
36
36
#endif
37
37
38
38
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 {
60
60
}
61
61
62
62
// / \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);
66
65
}
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);
71
69
}
72
70
73
71
// / \brief Use block pools to handle memory less than 64K.
@@ -78,7 +76,7 @@ class block_resource_base : public block_pool<BlockSize, BlockPoolExpansion> {
78
76
return block_pool<BlockSize, BlockPoolExpansion>::allocate ();
79
77
}
80
78
81
- void deallocate (void *p, std:: size_t /* bytes */ , std:: size_t /* alignment */ ) noexcept {
79
+ void deallocate (void *p) noexcept {
82
80
block_pool<BlockSize, BlockPoolExpansion>::deallocate (p);
83
81
}
84
82
};
@@ -91,8 +89,8 @@ class block_resource_base<BlockSize, 0> : public new_delete_resource {
91
89
return new_delete_resource::allocate (regular_head_size + bytes, alignment);
92
90
}
93
91
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);
96
94
}
97
95
};
98
96
@@ -103,8 +101,8 @@ class block_pool_resource : public block_resource_base<BlockSize, BlockPoolExpan
103
101
104
102
using base_t = block_resource_base<BlockSize, BlockPoolExpansion>;
105
103
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);
108
106
}
109
107
110
108
public:
@@ -115,19 +113,13 @@ class block_pool_resource : public block_resource_base<BlockSize, BlockPoolExpan
115
113
116
114
template <typename T>
117
115
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 );
123
121
};
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;
131
123
}
132
124
};
133
125
@@ -147,6 +139,11 @@ auto *get_regular_resource() noexcept {
147
139
using block_poll_resource_t = block_pool_resource<N, block_pool_expansion<L>>;
148
140
return dynamic_cast <block_poll_resource_t *>(block_poll_resource_t::get ());
149
141
}
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
+ }
150
147
151
148
namespace detail_new {
152
149
@@ -171,27 +168,6 @@ struct do_allocate<void> {
171
168
}
172
169
};
173
170
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
-
195
171
} // namespace detail_new
196
172
197
173
// / \brief Creates an object based on the specified type and parameters with block pool resource.
@@ -209,9 +185,9 @@ T *$new(A &&... args) noexcept {
209
185
template <typename T>
210
186
void $delete (T *p) noexcept {
211
187
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);
215
191
}
216
192
217
193
// / \brief The destruction policy used by std::unique_ptr.
0 commit comments