Skip to content

Commit

Permalink
Do not extend the pool on each allocation
Browse files Browse the repository at this point in the history
Fixes #54
  • Loading branch information
orgads committed Jun 25, 2023
1 parent 8ec1be1 commit 82b22e3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
30 changes: 15 additions & 15 deletions include/boost/pool/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_type> list; //!< List structure holding ordered blocks.
Expand Down Expand Up @@ -717,11 +729,7 @@ void * pool<UserAllocator>::malloc_need_resize()
}
const details::PODptr<size_type> 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);
Expand Down Expand Up @@ -757,11 +765,7 @@ void * pool<UserAllocator>::ordered_malloc_need_resize()
}
const details::PODptr<size_type> 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
Expand Down Expand Up @@ -851,11 +855,7 @@ void * pool<UserAllocator>::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.
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 : : : <threading>multi <library>/boost/thread//boost_thread ]
[ compile test_poisoned_macros.cpp ]
;
Expand Down
26 changes: 26 additions & 0 deletions test/test_bug_54.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/pool/pool.hpp>
#include <boost/limits.hpp>

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;
}

0 comments on commit 82b22e3

Please sign in to comment.