Skip to content

Commit 01bbcf7

Browse files
committed
Fix block_pool_resource allocation and deallocation
1 parent de80de3 commit 01bbcf7

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

include/libpmr/new.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ constexpr inline std::size_t regular_level(std::size_t s) noexcept {
3030

3131
constexpr inline std::size_t regular_sizeof_impl(std::size_t l, std::size_t s) noexcept {
3232
return (l == 0) ? std::max<std::size_t>(::LIBIMP::round_up<std::size_t>(s, 8), regular_head_size) :
33-
(l == 1) ? ::LIBIMP::round_up<std::size_t>(s, 128) :
33+
(l == 1) ? ::LIBIMP::round_up<std::size_t>(s, 128 ) :
3434
(l == 2) ? ::LIBIMP::round_up<std::size_t>(s, 1024) :
3535
(l == 3) ? ::LIBIMP::round_up<std::size_t>(s, 8192) : (std::numeric_limits<std::size_t>::max)();
3636
}
@@ -107,15 +107,19 @@ class block_pool_resource : public block_pool<BlockSize, BlockPoolExpansion>
107107
}
108108
auto &map = get_block_pool_map();
109109
auto it = map.find(r_size);
110-
if ((it == map.end()) || (it->second == nullptr)) LIBIMP_TRY {
111-
// If the corresponding memory resource cannot be found,
112-
// create a temporary general-purpose block pool to deallocate memory.
113-
it = map.emplace(r_size, new block_pool_resource<0, 0>).first;
114-
} LIBIMP_CATCH(...) {
115-
// If the memory resource cannot be created,
116-
// store the pointer directly to avoid leakage.
117-
base_t::deallocate(p);
118-
return;
110+
if ((it == map.end()) || (it->second == nullptr)) {
111+
block_pool_resource<0, 0> *bp = nullptr;
112+
LIBIMP_TRY {
113+
// If the corresponding memory resource cannot be found,
114+
// create a temporary general-purpose block pool to deallocate memory.
115+
it = map.emplace(r_size, bp = new block_pool_resource<0, 0>).first;
116+
} LIBIMP_CATCH(...) {
117+
// If the memory resource cannot be created,
118+
// store the pointer directly to avoid leakage.
119+
delete bp;
120+
base_t::deallocate(p);
121+
return;
122+
}
119123
}
120124
it->second->deallocate(p);
121125
}

0 commit comments

Comments
 (0)