@@ -25,7 +25,7 @@ LIBPMR_NAMESPACE_BEG_
25
25
class LIBIMP_EXPORT block_collector {
26
26
public:
27
27
virtual ~block_collector () noexcept = default ;
28
- virtual void deallocate (void *p) noexcept = 0;
28
+ virtual void recycle (void *p, std:: size_t bytes, std:: size_t alignment ) noexcept = 0;
29
29
};
30
30
31
31
using get_block_collector_t = block_collector *(*)() noexcept ;
@@ -60,15 +60,41 @@ constexpr inline std::size_t regular_sizeof() noexcept {
60
60
return regular_sizeof_impl (regular_head_size + sizeof (T));
61
61
}
62
62
63
+ // / \brief Use block pools to handle memory less than 64K.
64
+ template <std::size_t BlockSize, std::size_t BlockPoolExpansion>
65
+ class block_resource_base : public block_pool <BlockSize, BlockPoolExpansion> {
66
+ public:
67
+ void *allocate (std::size_t /* bytes*/ , std::size_t /* alignment*/ ) noexcept {
68
+ return block_pool<BlockSize, BlockPoolExpansion>::allocate ();
69
+ }
70
+
71
+ void deallocate (void *p, std::size_t /* bytes*/ , std::size_t /* alignment*/ ) noexcept {
72
+ block_pool<BlockSize, BlockPoolExpansion>::deallocate (p);
73
+ }
74
+ };
75
+
76
+ // / \brief Use `new`/`delete` to handle memory larger than 64K.
77
+ template <std::size_t BlockSize>
78
+ class block_resource_base <BlockSize, 0 > : public new_delete_resource {
79
+ public:
80
+ void *allocate (std::size_t bytes, std::size_t alignment) noexcept {
81
+ return new_delete_resource::allocate (regular_head_size + bytes, alignment);
82
+ }
83
+
84
+ void deallocate (void *p, std::size_t bytes, std::size_t alignment) noexcept {
85
+ new_delete_resource::deallocate (p, regular_head_size + bytes, alignment);
86
+ }
87
+ };
88
+
63
89
// / \brief Defines block pool memory resource based on block pool.
64
90
template <std::size_t BlockSize, std::size_t BlockPoolExpansion>
65
- class block_pool_resource : public block_pool <BlockSize, BlockPoolExpansion>
91
+ class block_pool_resource : public block_resource_base <BlockSize, BlockPoolExpansion>
66
92
, public block_collector {
67
93
68
- using base_t = block_pool <BlockSize, BlockPoolExpansion>;
94
+ using base_t = block_resource_base <BlockSize, BlockPoolExpansion>;
69
95
70
- void deallocate (void *p) noexcept override {
71
- base_t::deallocate (p);
96
+ void recycle (void *p, std:: size_t bytes, std:: size_t alignment ) noexcept override {
97
+ base_t::deallocate (p, bytes, alignment );
72
98
}
73
99
74
100
public:
@@ -77,22 +103,20 @@ class block_pool_resource : public block_pool<BlockSize, BlockPoolExpansion>
77
103
return &instance;
78
104
}
79
105
80
- using base_t ::base_t ;
81
-
82
- void *allocate (std::size_t /* bytes*/ , std::size_t /* alignment*/ = alignof (std::max_align_t )) noexcept {
83
- void *p = base_t::allocate ();
106
+ void *allocate (std::size_t bytes, std::size_t alignment = alignof (std::max_align_t )) noexcept {
107
+ void *p = base_t::allocate (bytes, alignment);
84
108
*static_cast <get_block_collector_t *>(p) = get;
85
109
return static_cast <::LIBIMP::byte *>(p) + regular_head_size;
86
110
}
87
111
88
- void deallocate (void *p, std::size_t /* bytes*/ , std::size_t /* alignment*/ = alignof (std::max_align_t )) noexcept {
112
+ void deallocate (void *p, std::size_t bytes, std::size_t alignment = alignof (std::max_align_t )) noexcept {
89
113
p = static_cast <::LIBIMP::byte *>(p) - regular_head_size;
90
114
auto g = *static_cast <get_block_collector_t *>(p);
91
115
if (g == get) {
92
- base_t::deallocate (p);
116
+ base_t::deallocate (p, bytes, alignment );
93
117
return ;
94
118
}
95
- g ()->deallocate (p );
119
+ g ()->recycle (p, bytes, alignment );
96
120
}
97
121
};
98
122
@@ -115,9 +139,6 @@ struct regular_resource {
115
139
}
116
140
};
117
141
118
- template <std::size_t N>
119
- struct regular_resource <N, 4 > : new_delete_resource {};
120
-
121
142
// / \brief Creates an object based on the specified type and parameters with block pool resource.
122
143
// / \note This function is thread-safe.
123
144
template <typename T, typename ... A>
@@ -136,11 +157,11 @@ void delete$(T *p) noexcept {
136
157
::LIBIMP::destroy (p);
137
158
auto *res = regular_resource<regular_sizeof<T>()>::get ();
138
159
if (res == nullptr ) return ;
139
- #if defined(LIBIMP_CC_MSVC_2015)
160
+ #if (LIBIMP_CC_MSVC > LIBIMP_CC_MSVC_2015)
161
+ res->deallocate (p, sizeof (T), alignof (T));
162
+ #else
140
163
// `alignof` of vs2015 requires that type must be able to be instantiated.
141
164
res->deallocate (p, sizeof (T));
142
- #else
143
- res->deallocate (p, sizeof (T), alignof (T));
144
165
#endif
145
166
}
146
167
0 commit comments