Skip to content

Commit b8e44e7

Browse files
committed
Do not extend the pool on each allocation
Reported in Boost mailing list: https://lists.boost.org/Archives/boost/2023/06/254750.php Commit 951ca57 changed malloc_need_resize() to use set_next_size(), which sets start_size in addition to setting next_size. this causes repeated use of purge_memory() to allocate 2x size after every use. Fixes #54
1 parent 2952382 commit b8e44e7

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

include/boost/pool/pool.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,18 @@ class pool: protected simple_segregated_storage < typename UserAllocator::size_t
295295
//! Called if malloc/ordered_malloc needs to resize the free list.
296296
void * malloc_need_resize(); //! Called if malloc needs to resize the free list.
297297
void * ordered_malloc_need_resize(); //! Called if ordered_malloc needs to resize the free list.
298+
void advance_next(size_type partition_size)
299+
{
300+
BOOST_USING_STD_MIN();
301+
size_type nnext_size;
302+
if(!max_size)
303+
nnext_size = next_size << 1;
304+
else if(next_size*partition_size/requested_size < max_size)
305+
nnext_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size);
306+
else
307+
return;
308+
next_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(nnext_size, max_chunks());
309+
}
298310

299311
protected:
300312
details::PODptr<size_type> list; //!< List structure holding ordered blocks.
@@ -717,11 +729,7 @@ void * pool<UserAllocator>::malloc_need_resize()
717729
}
718730
const details::PODptr<size_type> node(ptr, POD_size);
719731

720-
BOOST_USING_STD_MIN();
721-
if(!max_size)
722-
set_next_size(next_size << 1);
723-
else if( next_size*partition_size/requested_size < max_size)
724-
set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size));
732+
advance_next(partition_size);
725733

726734
// initialize it,
727735
store().add_block(node.begin(), node.element_size(), partition_size);
@@ -757,11 +765,7 @@ void * pool<UserAllocator>::ordered_malloc_need_resize()
757765
}
758766
const details::PODptr<size_type> node(ptr, POD_size);
759767

760-
BOOST_USING_STD_MIN();
761-
if(!max_size)
762-
set_next_size(next_size << 1);
763-
else if( next_size*partition_size/requested_size < max_size)
764-
set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size));
768+
advance_next(partition_size);
765769

766770
// initialize it,
767771
// (we can use "add_block" here because we know that
@@ -851,11 +855,7 @@ void * pool<UserAllocator>::ordered_malloc(const size_type n)
851855
store().add_ordered_block(node.begin() + num_chunks * partition_size,
852856
node.element_size() - num_chunks * partition_size, partition_size);
853857

854-
BOOST_USING_STD_MIN();
855-
if(!max_size)
856-
set_next_size(next_size << 1);
857-
else if( next_size*partition_size/requested_size < max_size)
858-
set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size));
858+
advance_next(partition_size);
859859

860860
// insert it into the list,
861861
// handle border case.

test/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ test-suite pool :
3737
[ run test_bug_2696.cpp ]
3838
[ run test_bug_5526.cpp : : : <library>/boost/smart_ptr//boost_smart_ptr [ requires cxx11_noexcept ] ]
3939
[ run test_bug_6701.cpp ]
40+
[ run test_bug_54.cpp ]
4041
[ run test_threading.cpp : : : <threading>multi <library>/boost/thread//boost_thread <library>/boost/random//boost_random ]
4142
[ compile test_poisoned_macros.cpp ]
4243
;

test/test_bug_54.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Copyright (C) 2023 Orgad Shaneh
2+
*
3+
* Use, modification and distribution is subject to the
4+
* Boost Software License, Version 1.0. (See accompanying
5+
* file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
6+
*/
7+
8+
// Test of bug #54 (https://github.com/boostorg/pool/issues/54)
9+
10+
#include <boost/pool/pool.hpp>
11+
#include <boost/limits.hpp>
12+
13+
int main()
14+
{
15+
boost::pool<> pool(8, 32, 64);
16+
// On 32 next_size reaches max_size.
17+
// One more round to asserts that it remains 64 (max_size)
18+
for (int i = 0; i <= 33; ++i) {
19+
size_t expected = (i == 0) ? 32 : 64;
20+
BOOST_ASSERT(pool.get_next_size() == expected);
21+
void *ptr = pool.malloc();
22+
BOOST_ASSERT(ptr);
23+
}
24+
25+
return 0;
26+
}

0 commit comments

Comments
 (0)