From 52fe4d058e7a4f73202c9e45c8dfa3ff0fe62e6c Mon Sep 17 00:00:00 2001 From: Lei Mao Date: Tue, 13 Dec 2022 19:30:44 -0800 Subject: [PATCH 1/7] Fix the Broken `malloc_n` Behavior --- include/boost/pool/simple_segregated_storage.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/pool/simple_segregated_storage.hpp b/include/boost/pool/simple_segregated_storage.hpp index fffd4ee6..bc7398fc 100644 --- a/include/boost/pool/simple_segregated_storage.hpp +++ b/include/boost/pool/simple_segregated_storage.hpp @@ -328,7 +328,7 @@ void * simple_segregated_storage::try_malloc_n( void * & start, size_type n, const size_type partition_size) { void * iter = nextof(start); - while (--n != 0) + while (n-- != 0) { void * next = nextof(iter); if (next != static_cast(iter) + partition_size) From e08ec193ff0f2564c643eb1e47f05f211982abfe Mon Sep 17 00:00:00 2001 From: Lei Mao Date: Tue, 13 Dec 2022 21:58:07 -0800 Subject: [PATCH 2/7] Fix the Broken `malloc_n` Behavior --- include/boost/pool/simple_segregated_storage.hpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/include/boost/pool/simple_segregated_storage.hpp b/include/boost/pool/simple_segregated_storage.hpp index bc7398fc..e0d8c42b 100644 --- a/include/boost/pool/simple_segregated_storage.hpp +++ b/include/boost/pool/simple_segregated_storage.hpp @@ -328,7 +328,20 @@ void * simple_segregated_storage::try_malloc_n( void * & start, size_type n, const size_type partition_size) { void * iter = nextof(start); - while (n-- != 0) + 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); if (next != static_cast(iter) + partition_size) From cf9b5c4a820e4221cbcf50d652b53742777de1e9 Mon Sep 17 00:00:00 2001 From: Lei Mao Date: Tue, 13 Dec 2022 22:03:24 -0800 Subject: [PATCH 3/7] Fix the Broken `malloc_n` Behavior Added Unit Tests --- test/test_simple_seg_storage.cpp | 76 ++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/test/test_simple_seg_storage.cpp b/test/test_simple_seg_storage.cpp index 67a33e00..2398cf1e 100644 --- a/test/test_simple_seg_storage.cpp +++ b/test/test_simple_seg_storage.cpp @@ -277,6 +277,82 @@ int main() BOOST_TEST(nchunks == 3); } + { + 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, partition_sz); + BOOST_TEST(pvret == pc); + + // 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 == 3); + } + + { + 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, partition_sz); + BOOST_TEST(pvret == pc); + + // 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 == 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; From 14e0c1163ba1a0c83b2fde28c0ec76bafcd7d696 Mon Sep 17 00:00:00 2001 From: Lei Mao Date: Wed, 14 Dec 2022 09:32:10 -0800 Subject: [PATCH 4/7] Fix the Broken `malloc_n` Behavior Add more unit tests. --- test/test_pool_alloc.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_pool_alloc.cpp b/test/test_pool_alloc.cpp index b0b50eab..f8f3ebf9 100644 --- a/test/test_pool_alloc.cpp +++ b/test/test_pool_alloc.cpp @@ -263,6 +263,19 @@ void test_mem_usage() // This will clean up the memory leak from (*B*) } + { + pool_type pool(sizeof(int), 2); + void * ptr_0 = pool.malloc(); + void * ptr_1 = pool.malloc(); + void * ptr_2 = pool.malloc(); + void * ptr_3 = pool.malloc(); + pool.free(ptr_2); + pool.free(ptr_3); + BOOST_TEST(pool.release_memory()); + pool.free(ptr_0); + pool.free(ptr_1); + } + BOOST_TEST(track_alloc::ok()); } From ce5c800803108cec13568d28375d6018bdc07bc3 Mon Sep 17 00:00:00 2001 From: Lei Mao Date: Wed, 14 Dec 2022 09:33:32 -0800 Subject: [PATCH 5/7] Fix the Broken `malloc_n` Behavior --- test/test_pool_alloc.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_pool_alloc.cpp b/test/test_pool_alloc.cpp index f8f3ebf9..83463ef5 100644 --- a/test/test_pool_alloc.cpp +++ b/test/test_pool_alloc.cpp @@ -274,6 +274,7 @@ void test_mem_usage() BOOST_TEST(pool.release_memory()); pool.free(ptr_0); pool.free(ptr_1); + BOOST_TEST(pool.release_memory()); } BOOST_TEST(track_alloc::ok()); From 2bae19d7e6235e7caa3e655a6158f68b733cfe3f Mon Sep 17 00:00:00 2001 From: Lei Mao Date: Wed, 14 Dec 2022 19:19:52 -0800 Subject: [PATCH 6/7] Fix the Broken `malloc_n` Behavior --- test/test_pool_alloc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_pool_alloc.cpp b/test/test_pool_alloc.cpp index 83463ef5..26fa38ed 100644 --- a/test/test_pool_alloc.cpp +++ b/test/test_pool_alloc.cpp @@ -269,11 +269,11 @@ void test_mem_usage() void * ptr_1 = pool.malloc(); void * ptr_2 = pool.malloc(); void * ptr_3 = pool.malloc(); - pool.free(ptr_2); - pool.free(ptr_3); + pool.ordered_free(ptr_2); + pool.ordered_free(ptr_3); BOOST_TEST(pool.release_memory()); - pool.free(ptr_0); - pool.free(ptr_1); + pool.ordered_free(ptr_0); + pool.ordered_free(ptr_1); BOOST_TEST(pool.release_memory()); } From 6ec9801c5c6d008dec4b720201f9ecc0d71c397a Mon Sep 17 00:00:00 2001 From: Lei Mao Date: Sun, 18 Dec 2022 20:15:13 -0800 Subject: [PATCH 7/7] Fix the Broken `malloc_n` Behavior --- test/test_pool_alloc.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test_pool_alloc.cpp b/test/test_pool_alloc.cpp index 26fa38ed..6c36e97a 100644 --- a/test/test_pool_alloc.cpp +++ b/test/test_pool_alloc.cpp @@ -277,6 +277,20 @@ void test_mem_usage() BOOST_TEST(pool.release_memory()); } + { + pool_type pool(sizeof(int), 2); + void * ptr_0 = pool.malloc(); + void * ptr_1 = pool.malloc(); + void * ptr_2 = pool.malloc(); + void * ptr_3 = pool.malloc(); + pool.ordered_free(ptr_0); + pool.ordered_free(ptr_1); + BOOST_TEST(pool.release_memory()); + pool.ordered_free(ptr_2); + pool.ordered_free(ptr_3); + BOOST_TEST(pool.release_memory()); + } + BOOST_TEST(track_alloc::ok()); }