Fix block_pool_resource allocation and deallocation

This commit is contained in:
mutouyun 2024-01-06 18:17:57 +08:00
parent de80de3828
commit 01bbcf7dca

View File

@ -30,7 +30,7 @@ constexpr inline std::size_t regular_level(std::size_t s) noexcept {
constexpr inline std::size_t regular_sizeof_impl(std::size_t l, std::size_t s) noexcept { constexpr inline std::size_t regular_sizeof_impl(std::size_t l, std::size_t s) noexcept {
return (l == 0) ? std::max<std::size_t>(::LIBIMP::round_up<std::size_t>(s, 8), regular_head_size) : return (l == 0) ? std::max<std::size_t>(::LIBIMP::round_up<std::size_t>(s, 8), regular_head_size) :
(l == 1) ? ::LIBIMP::round_up<std::size_t>(s, 128) : (l == 1) ? ::LIBIMP::round_up<std::size_t>(s, 128 ) :
(l == 2) ? ::LIBIMP::round_up<std::size_t>(s, 1024) : (l == 2) ? ::LIBIMP::round_up<std::size_t>(s, 1024) :
(l == 3) ? ::LIBIMP::round_up<std::size_t>(s, 8192) : (std::numeric_limits<std::size_t>::max)(); (l == 3) ? ::LIBIMP::round_up<std::size_t>(s, 8192) : (std::numeric_limits<std::size_t>::max)();
} }
@ -107,15 +107,19 @@ public:
} }
auto &map = get_block_pool_map(); auto &map = get_block_pool_map();
auto it = map.find(r_size); auto it = map.find(r_size);
if ((it == map.end()) || (it->second == nullptr)) LIBIMP_TRY { if ((it == map.end()) || (it->second == nullptr)) {
// If the corresponding memory resource cannot be found, block_pool_resource<0, 0> *bp = nullptr;
// create a temporary general-purpose block pool to deallocate memory. LIBIMP_TRY {
it = map.emplace(r_size, new block_pool_resource<0, 0>).first; // If the corresponding memory resource cannot be found,
} LIBIMP_CATCH(...) { // create a temporary general-purpose block pool to deallocate memory.
// If the memory resource cannot be created, it = map.emplace(r_size, bp = new block_pool_resource<0, 0>).first;
// store the pointer directly to avoid leakage. } LIBIMP_CATCH(...) {
base_t::deallocate(p); // If the memory resource cannot be created,
return; // store the pointer directly to avoid leakage.
delete bp;
base_t::deallocate(p);
return;
}
} }
it->second->deallocate(p); it->second->deallocate(p);
} }