From 82b22e305c312b0111237e93646b1e87037c13d8 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Thu, 22 Jun 2023 16:12:09 +0300 Subject: [PATCH] Do not extend the pool on each allocation Fixes #54 --- include/boost/pool/pool.hpp | 30 +++++++++++++++--------------- test/Jamfile.v2 | 1 + test/test_bug_54.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 test/test_bug_54.cpp diff --git a/include/boost/pool/pool.hpp b/include/boost/pool/pool.hpp index 12728a7..fb2d238 100644 --- a/include/boost/pool/pool.hpp +++ b/include/boost/pool/pool.hpp @@ -295,6 +295,18 @@ class pool: protected simple_segregated_storage < typename UserAllocator::size_t //! Called if malloc/ordered_malloc needs to resize the free list. void * malloc_need_resize(); //! Called if malloc needs to resize the free list. void * ordered_malloc_need_resize(); //! Called if ordered_malloc needs to resize the free list. + void advance_next(size_type partition_size) + { + BOOST_USING_STD_MIN(); + size_type nnext_size; + if(!max_size) + nnext_size = next_size << 1; + else if(next_size*partition_size/requested_size < max_size) + nnext_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size); + else + return; + next_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(nnext_size, max_chunks()); + } protected: details::PODptr list; //!< List structure holding ordered blocks. @@ -717,11 +729,7 @@ void * pool::malloc_need_resize() } const details::PODptr node(ptr, POD_size); - BOOST_USING_STD_MIN(); - if(!max_size) - set_next_size(next_size << 1); - else if( next_size*partition_size/requested_size < max_size) - set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size)); + advance_next(partition_size); // initialize it, store().add_block(node.begin(), node.element_size(), partition_size); @@ -757,11 +765,7 @@ void * pool::ordered_malloc_need_resize() } const details::PODptr node(ptr, POD_size); - BOOST_USING_STD_MIN(); - if(!max_size) - set_next_size(next_size << 1); - else if( next_size*partition_size/requested_size < max_size) - set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size)); + advance_next(partition_size); // initialize it, // (we can use "add_block" here because we know that @@ -851,11 +855,7 @@ void * pool::ordered_malloc(const size_type n) store().add_ordered_block(node.begin() + num_chunks * partition_size, node.element_size() - num_chunks * partition_size, partition_size); - BOOST_USING_STD_MIN(); - if(!max_size) - set_next_size(next_size << 1); - else if( next_size*partition_size/requested_size < max_size) - set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size)); + advance_next(partition_size); // insert it into the list, // handle border case. diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 3ec77ea..d047278 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -36,6 +36,7 @@ test-suite pool : [ run test_bug_2696.cpp ] [ run test_bug_5526.cpp ] [ run test_bug_6701.cpp ] + [ run test_bug_54.cpp ] [ run test_threading.cpp : : : multi /boost/thread//boost_thread ] [ compile test_poisoned_macros.cpp ] ; diff --git a/test/test_bug_54.cpp b/test/test_bug_54.cpp new file mode 100644 index 0000000..88b7f6d --- /dev/null +++ b/test/test_bug_54.cpp @@ -0,0 +1,26 @@ +/* Copyright (C) 2023 Orgad Shaneh +* +* Use, modification and distribution is subject to the +* Boost Software License, Version 1.0. (See accompanying +* file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) +*/ + +// Test of bug #54 (https://github.com/boostorg/pool/issues/54) + +#include +#include + +int main() +{ + boost::pool<> pool(8, 32, 64); + // On 32 next_size reaches max_size. + // One more round to asserts that it remains 64 (max_size) + for (int i = 0; i <= 33; ++i) { + size_t expected = (i == 0) ? 32 : 64; + BOOST_ASSERT(pool.get_next_size() == expected); + void *ptr = pool.malloc(); + BOOST_ASSERT(ptr); + } + + return 0; +}