From aa473160dff2ed23fdc39f780188ffb23d7a72fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Mon, 3 Jun 2024 00:12:09 +0200 Subject: [PATCH 1/4] Remove unused make_unique.hpp --- include/boost/container/detail/flat_tree.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/boost/container/detail/flat_tree.hpp b/include/boost/container/detail/flat_tree.hpp index 0fb0e35..f381229 100644 --- a/include/boost/container/detail/flat_tree.hpp +++ b/include/boost/container/detail/flat_tree.hpp @@ -43,7 +43,6 @@ #include //pair -#include #include #include #include From c7702d20eb15579699a3b6a91bfdb166b1fb9fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Mon, 3 Jun 2024 00:12:56 +0200 Subject: [PATCH 2/4] Reformat small inline functions --- include/boost/container/small_vector.hpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/include/boost/container/small_vector.hpp b/include/boost/container/small_vector.hpp index e20a8fe..41dc15b 100644 --- a/include/boost/container/small_vector.hpp +++ b/include/boost/container/small_vector.hpp @@ -415,9 +415,7 @@ class small_vector_base {} void prot_shrink_to_fit_small(const size_type small_capacity) - { - this->base_type::prot_shrink_to_fit_small(this->internal_storage(), small_capacity); - } + { this->base_type::prot_shrink_to_fit_small(this->internal_storage(), small_capacity); } using base_type::protected_set_size; @@ -667,9 +665,7 @@ class small_vector { return this->base_type::prot_swap(other, static_capacity); } inline void shrink_to_fit() - { - this->base_type::prot_shrink_to_fit_small(this->internal_capacity()); - } + { this->base_type::prot_shrink_to_fit_small(this->internal_capacity()); } }; }} From dad179ddf5c7998932388f69b4e192b98eed1730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Mon, 3 Jun 2024 00:14:00 +0200 Subject: [PATCH 3/4] Fix incorrect memory free order in prot_shrink_to_fit_small, fixing a potential memory leak --- include/boost/container/vector.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/boost/container/vector.hpp b/include/boost/container/vector.hpp index fa2b444..a6e2f49 100644 --- a/include/boost/container/vector.hpp +++ b/include/boost/container/vector.hpp @@ -2891,11 +2891,11 @@ private: ); boost::container::destroy_alloc_n(this->get_stored_allocator(), oldbuf, sz); - this->m_holder.m_start = small_buffer; - this->m_holder.set_stored_capacity(small_capacity); - if (BOOST_LIKELY(!!this->m_holder.m_start)) this->m_holder.deallocate(this->m_holder.m_start, cp); + + this->m_holder.m_start = small_buffer; + this->m_holder.set_stored_capacity(small_capacity); } else if (sz < cp) { this->priv_move_to_new_buffer(sz, alloc_version()); From aa35950ca0f8fb82e85a4de71eb77901f5f285d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Mon, 3 Jun 2024 00:14:25 +0200 Subject: [PATCH 4/4] Add shrink_to_fit tests to small_vector --- test/map_test.hpp | 2 +- test/small_vector_test.cpp | 67 +++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/test/map_test.hpp b/test/map_test.hpp index 210952b..0055f57 100644 --- a/test/map_test.hpp +++ b/test/map_test.hpp @@ -318,7 +318,7 @@ int map_test_range() ::boost::movelib::unique_ptr const pboostmultimap = ::boost::movelib::make_unique ( boost::make_move_iterator(&aux_vect3[0]) - , boost::make_move_iterator(&aux_vect3[0] + MaxElem), typename MyBoostMap::allocator_type()); + , boost::make_move_iterator(&aux_vect3[0] + MaxElem)); ::boost::movelib::unique_ptr const pstdmultimap = ::boost::movelib::make_unique (&aux_vect2[0], &aux_vect2[0] + MaxElem, typename MyStdMap::key_compare()); if(!CheckEqualContainers(*pboostmultimap, *pstdmultimap)) return 1; diff --git a/test/small_vector_test.cpp b/test/small_vector_test.cpp index 403319a..1d68699 100644 --- a/test/small_vector_test.cpp +++ b/test/small_vector_test.cpp @@ -34,6 +34,71 @@ struct alloc_propagate_base }}} //namespace boost::container::test + +bool test_small_vector_shrink_to_fit() +{ + boost::container::small_vector sm5; + boost::container::vector v; + sm5.push_back(1); + sm5.push_back(2); + sm5.push_back(3); + sm5.push_back(4); + sm5.push_back(5); + + v.push_back(1); + v.push_back(2); + v.push_back(3); + v.push_back(4); + v.push_back(5); + + if (!sm5.is_small()) + return false; + if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end())) + return false; + + //Shrinking a when internal storage is used is a no-op + sm5.shrink_to_fit(); + + if (!sm5.is_small()) + return false; + if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end())) + return false; + + //If dynamic memory is used, shrink_to_fit will move elements to the internal storage + sm5.push_back(6); + v.push_back(6); + if (sm5.is_small()) + return false; + sm5.pop_back(); + v.pop_back(); + if (sm5.is_small()) + return false; + + sm5.shrink_to_fit(); + if (!sm5.is_small()) + return false; + if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end())) + return false; + + //If dynamic memory is used, and size is zero the dynamic storage is deallocated + sm5.push_back(6); + v.push_back(6); + if (sm5.is_small()) + return false; + sm5.clear(); + v.clear(); + if (sm5.is_small()) + return false; + + sm5.shrink_to_fit(); + if (!sm5.is_small()) + return false; + if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end())) + return false; + + return true; +} + bool test_small_vector_base_test() { typedef boost::container::small_vector_base smb_t; @@ -452,7 +517,7 @@ int main() //////////////////////////////////// // Small vector base //////////////////////////////////// - if (!test_small_vector_base_test()){ + if (!test_small_vector_shrink_to_fit()){ return 1; }