From 2822004af908c914762bc2b33e7aa39fbd6a9532 Mon Sep 17 00:00:00 2001 From: Vadim Zavalishin <149502769+ni-vzavalishin@users.noreply.github.com> Date: Fri, 10 Jan 2025 16:28:08 +0100 Subject: [PATCH 1/3] Demonstrate the regressions caused by (#53) (commit 8ec1be1) by unit tests --- test/test_pool_alloc.cpp | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/test_pool_alloc.cpp b/test/test_pool_alloc.cpp index 6c36e97..1323969 100644 --- a/test/test_pool_alloc.cpp +++ b/test/test_pool_alloc.cpp @@ -294,6 +294,61 @@ void test_mem_usage() BOOST_TEST(track_alloc::ok()); } +void test_free_chunk_selection() +{ + typedef boost::pool pool_type; + + { + // Expose a regression from the commit 8ec1be1e82ba559744ecfa3c6ec13f71f9c175cc. + // Two checks will fail here. + pool_type pool(sizeof(void *), 3); + void * ptr_0 = pool.ordered_malloc(1); + void * ptr_1 = pool.ordered_malloc(1); + void * ptr_2 = pool.ordered_malloc(1); + // The blocks are expected to be allocated at subsequent locations + BOOST_TEST((char *)ptr_1 - (char *)ptr_0 == sizeof(void *)); + BOOST_TEST((char *)ptr_2 - (char *)ptr_1 == sizeof(void *)); + + pool.ordered_free(ptr_1, 1); + + void * ptr_1a = pool.ordered_malloc(1); + // Expected to reallocate the former ptr1 block + // which should be the first and only available block + BOOST_TEST(ptr_1a == ptr_1); + + pool.ordered_free(ptr_0, 1); + pool.ordered_free(ptr_1a, 1); + pool.ordered_free(ptr_2, 1); + } + + { + // Another way to expose a regression from the commit 8ec1be1e82ba559744ecfa3c6ec13f71f9c175cc. + // This time we preallocate 4 rather than 3 blocks in the pool. In this case + // the location of the ptr_2 block is as expected. The reallocation of ptr_1 + // block however still fails the location expectation. + pool_type pool(sizeof(void *), 4); + void * ptr_0 = pool.ordered_malloc(1); + void * ptr_1 = pool.ordered_malloc(1); + void * ptr_2 = pool.ordered_malloc(1); + // The blocks are expected to be allocated at subsequent locations + BOOST_TEST((char *)ptr_1 - (char *)ptr_0 == sizeof(void *)); + BOOST_TEST((char *)ptr_2 - (char *)ptr_1 == sizeof(void *)); + + pool.ordered_free(ptr_1, 1); + + void * ptr_1a = pool.ordered_malloc(1); + // Expected to reallocate the former ptr1 block + // which should be the first available block + BOOST_TEST(ptr_1a == ptr_1); + + pool.ordered_free(ptr_0, 1); + pool.ordered_free(ptr_1a, 1); + pool.ordered_free(ptr_2, 1); + } + + BOOST_TEST(track_alloc::ok()); +} + void test_void() { typedef boost::pool_allocator void_allocator; @@ -313,6 +368,7 @@ int main() test(); test_alloc(); test_mem_usage(); + test_free_chunk_selection(); test_void(); return boost::report_errors(); From 492f9a89df2296b7a23dceae26af53b2ebbeb2b7 Mon Sep 17 00:00:00 2001 From: Vadim Zavalishin <149502769+ni-vzavalishin@users.noreply.github.com> Date: Fri, 10 Jan 2025 17:29:41 +0100 Subject: [PATCH 2/3] Revert the library code change of (#53), not touching the unit tests --- include/boost/pool/simple_segregated_storage.hpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/include/boost/pool/simple_segregated_storage.hpp b/include/boost/pool/simple_segregated_storage.hpp index e0d8c42..fffd4ee 100644 --- a/include/boost/pool/simple_segregated_storage.hpp +++ b/include/boost/pool/simple_segregated_storage.hpp @@ -328,19 +328,6 @@ void * simple_segregated_storage::try_malloc_n( void * & start, size_type n, const size_type partition_size) { void * iter = nextof(start); - if (n == 1) - { - void * next = nextof(iter); - if (next != static_cast(iter) + partition_size) - { - start = iter; - return 0; - } - else - { - return iter; - } - } while (--n != 0) { void * next = nextof(iter); From 86b7987046e6692fc4241463d501eeb53dc11d98 Mon Sep 17 00:00:00 2001 From: Vadim Zavalishin <149502769+ni-vzavalishin@users.noreply.github.com> Date: Mon, 13 Jan 2025 14:29:23 +0100 Subject: [PATCH 3/3] Removed some of the unit tests (those using incorrect partition_sz parameter value) introduced with (#53) --- test/test_simple_seg_storage.cpp | 38 -------------------------------- 1 file changed, 38 deletions(-) diff --git a/test/test_simple_seg_storage.cpp b/test/test_simple_seg_storage.cpp index 2398cf1..d94be24 100644 --- a/test/test_simple_seg_storage.cpp +++ b/test/test_simple_seg_storage.cpp @@ -315,44 +315,6 @@ int main() BOOST_TEST(nchunks == 2); } - { - char* const pc = track_allocator::malloc(4 * partition_sz); - test_simp_seg_store tstore; - tstore.add_ordered_block(pc, 4 * partition_sz, partition_sz); - - void* pvret = tstore.malloc_n(1, 2 * partition_sz); - BOOST_TEST(pvret == 0); - - // There should still be two contiguous - // and one non-contiguous chunk left - std::size_t nchunks = 0; - while(!tstore.empty()) - { - tstore.malloc(); - ++nchunks; - } - BOOST_TEST(nchunks == 4); - } - - { - char* const pc = track_allocator::malloc(4 * partition_sz); - test_simp_seg_store tstore; - tstore.add_ordered_block(pc, 4 * partition_sz, partition_sz); - - void* pvret = tstore.malloc_n(2, 2 * partition_sz); - BOOST_TEST(pvret == 0); - - // There should still be two contiguous - // and one non-contiguous chunk left - std::size_t nchunks = 0; - while(!tstore.empty()) - { - tstore.malloc(); - ++nchunks; - } - BOOST_TEST(nchunks == 4); - } - { char* const pc = track_allocator::malloc(12 * partition_sz); test_simp_seg_store tstore;