From 01bbcf7dca95035c35090124c8de6684b91b77ba Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sat, 6 Jan 2024 18:17:57 +0800 Subject: [PATCH] Fix block_pool_resource allocation and deallocation --- include/libpmr/new.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/include/libpmr/new.h b/include/libpmr/new.h index 7200987..73b3c8d 100644 --- a/include/libpmr/new.h +++ b/include/libpmr/new.h @@ -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 { return (l == 0) ? std::max(::LIBIMP::round_up(s, 8), regular_head_size) : - (l == 1) ? ::LIBIMP::round_up(s, 128) : + (l == 1) ? ::LIBIMP::round_up(s, 128 ) : (l == 2) ? ::LIBIMP::round_up(s, 1024) : (l == 3) ? ::LIBIMP::round_up(s, 8192) : (std::numeric_limits::max)(); } @@ -107,15 +107,19 @@ public: } auto &map = get_block_pool_map(); auto it = map.find(r_size); - if ((it == map.end()) || (it->second == nullptr)) LIBIMP_TRY { - // If the corresponding memory resource cannot be found, - // create a temporary general-purpose block pool to deallocate memory. - it = map.emplace(r_size, new block_pool_resource<0, 0>).first; - } LIBIMP_CATCH(...) { - // If the memory resource cannot be created, - // store the pointer directly to avoid leakage. - base_t::deallocate(p); - return; + if ((it == map.end()) || (it->second == nullptr)) { + block_pool_resource<0, 0> *bp = nullptr; + LIBIMP_TRY { + // If the corresponding memory resource cannot be found, + // create a temporary general-purpose block pool to deallocate memory. + it = map.emplace(r_size, bp = new block_pool_resource<0, 0>).first; + } LIBIMP_CATCH(...) { + // If the memory resource cannot be created, + // store the pointer directly to avoid leakage. + delete bp; + base_t::deallocate(p); + return; + } } it->second->deallocate(p); }