diff --git a/doc/circular_buffer.qbk b/doc/circular_buffer.qbk index 65f0561a..4a882c15 100644 --- a/doc/circular_buffer.qbk +++ b/doc/circular_buffer.qbk @@ -473,6 +473,14 @@ Pull requests are welcome at [@https://github.com/boostorg/circular_buffer boost [section:release Release Notes] +[h4 Boost 1.94] + +* `insert`, `rinsert`, `resize` and `rresize`, and `push_back` and `push_front` of + `circular_buffer_space_optimized`, now insert the value that `item` had at the time of the call when + `item` refers to an element of the same container, as `std::vector` does. Previously the elements were + moved, or the storage reallocated, before `item` was read, so a neighbouring value was inserted or freed + memory was read. + [h4 Boost 1.91] * Avoided a GCC 4.x warning about calling `memset` with a constant size of zero. diff --git a/include/boost/circular_buffer/base.hpp b/include/boost/circular_buffer/base.hpp index 47441544..3bb3d099 100644 --- a/include/boost/circular_buffer/base.hpp +++ b/include/boost/circular_buffer/base.hpp @@ -22,7 +22,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -33,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -918,8 +921,15 @@ private empty_value */ void resize(size_type new_size, param_value_type item = value_type()) { if (new_size > size()) { - if (new_size > capacity()) + if (new_size > capacity()) { + if (is_stored(item)) { + value_type copy(item); // set_capacity() frees the storage that item is in + set_capacity(new_size); + insert(end(), new_size - size(), copy); + return; + } set_capacity(new_size); + } insert(end(), new_size - size(), item); } else { iterator e = end(); @@ -993,8 +1003,15 @@ private empty_value */ void rresize(size_type new_size, param_value_type item = value_type()) { if (new_size > size()) { - if (new_size > capacity()) + if (new_size > capacity()) { + if (is_stored(item)) { + value_type copy(item); // set_capacity() frees the storage that item is in + set_capacity(new_size); + rinsert(begin(), new_size - size(), copy); + return; + } set_capacity(new_size); + } rinsert(begin(), new_size - size(), item); } else { rerase(begin(), end() - new_size); @@ -1670,6 +1687,10 @@ private empty_value rinsert(iterator, InputIterator, InputIterator) */ iterator insert(iterator pos, param_value_type item) { + if (is_stored(item)) { + value_type copy(item); // the elements are moved before item is read + return insert_impl(pos, copy); + } return insert_impl(pos, item); } @@ -1789,6 +1810,11 @@ private empty_value return; if (n > copy) n = copy; + if (is_stored(item)) { + value_type item_copy(item); // the elements are moved before item is read + insert_n(pos, n, cb_details::item_wrapper(item_copy)); + return; + } insert_n(pos, n, cb_details::item_wrapper(item)); } @@ -1925,6 +1951,10 @@ private empty_value insert(iterator, InputIterator, InputIterator) */ iterator rinsert(iterator pos, param_value_type item) { + if (is_stored(item)) { + value_type copy(item); // the elements are moved before item is read + return rinsert_impl(pos, copy); + } return rinsert_impl(pos, item); } @@ -2034,6 +2064,11 @@ private empty_value */ void rinsert(iterator pos, size_type n, param_value_type item) { BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator + if (n != 0 && is_stored(item)) { + value_type copy(item); // the elements are moved before item is read + rinsert_n(pos, n, cb_details::item_wrapper(copy)); + return; + } rinsert_n(pos, n, cb_details::item_wrapper(item)); } @@ -2335,6 +2370,16 @@ private empty_value m_size = 0; } +protected: + /*! INTERNAL ONLY */ + bool is_stored(param_value_type item) const BOOST_NOEXCEPT { + if (m_buff == 0) + return false; + std::less less; + const value_type* p = boost::addressof(item); + return !less(p, boost::to_address(m_buff)) && less(p, boost::to_address(m_end)); + } + private: // Helper methods diff --git a/include/boost/circular_buffer/space_optimized.hpp b/include/boost/circular_buffer/space_optimized.hpp index b413e932..8341e3c4 100644 --- a/include/boost/circular_buffer/space_optimized.hpp +++ b/include/boost/circular_buffer/space_optimized.hpp @@ -782,6 +782,12 @@ public:
pop_front() */ void push_back(param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + check_low_capacity(); + circular_buffer::push_back(copy); + return; + } check_low_capacity(); circular_buffer::push_back(item); } @@ -856,6 +862,12 @@ public:
pop_front() */ void push_front(param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + check_low_capacity(); + circular_buffer::push_front(copy); + return; + } check_low_capacity(); circular_buffer::push_front(item); } @@ -988,6 +1000,12 @@ public:
rinsert(iterator, InputIterator, InputIterator) */ iterator insert(iterator pos, param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + size_type index = pos - begin(); + check_low_capacity(); + return circular_buffer::insert(begin() + index, copy); + } size_type index = pos - begin(); check_low_capacity(); return circular_buffer::insert(begin() + index, item); @@ -1110,6 +1128,13 @@ public:
rinsert(iterator, InputIterator, InputIterator) */ void insert(iterator pos, size_type n, param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + size_type index = pos - begin(); + check_low_capacity(n); + circular_buffer::insert(begin() + index, n, copy); + return; + } size_type index = pos - begin(); check_low_capacity(n); circular_buffer::insert(begin() + index, n, item); @@ -1199,6 +1224,12 @@ public:
insert(iterator, InputIterator, InputIterator) */ iterator rinsert(iterator pos, param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + size_type index = pos - begin(); + check_low_capacity(); + return circular_buffer::rinsert(begin() + index, copy); + } size_type index = pos - begin(); check_low_capacity(); return circular_buffer::rinsert(begin() + index, item); @@ -1321,6 +1352,13 @@ public:
insert(iterator, InputIterator, InputIterator) */ void rinsert(iterator pos, size_type n, param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + size_type index = pos - begin(); + check_low_capacity(n); + circular_buffer::rinsert(begin() + index, n, copy); + return; + } size_type index = pos - begin(); check_low_capacity(n); circular_buffer::rinsert(begin() + index, n, item); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index db967f9e..8e320096 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -27,6 +27,7 @@ function(boost_circular_buffer_add_test name source) add_executable(${pname} ${source}) target_link_libraries(${pname} PRIVATE ${LINK_LIBRARIES}) target_compile_features(${pname} PRIVATE ${COMPILE_FEATURES}) + target_compile_definitions(${pname} PRIVATE BOOST_CB_ENABLE_DEBUG=0) add_test(NAME ${pname} COMMAND ${pname}) add_dependencies(tests ${pname}) endfunction() diff --git a/test/common.ipp b/test/common.ipp index e26babe5..eb1f49f8 100644 --- a/test/common.ipp +++ b/test/common.ipp @@ -2442,6 +2442,70 @@ void check_containers_exception_specifications() { } // add common tests into a test suite +// Fill with 1 2 3 4, with the first element away from the start of the storage. +template +CB& alias_fill(CB& cb) { + for (int i = 0; i < 3; ++i) + cb.push_back(0); + for (int i = 1; i <= 4; ++i) + cb.push_back(i); + while (cb.size() > 4) + cb.pop_front(); + return cb; +} + +#define CB_TEST_ALIAS(cap, expr, expected) \ + do { \ + CB_CONTAINER cb(cap); \ + alias_fill(cb); \ + cb.expr; \ + BOOST_TEST_ALL_EQ(cb.begin(), cb.end(), expected, expected + sizeof(expected) / sizeof(expected[0])); \ + } while (false) + +void insert_element_of_itself_test() { + + // Like std::vector, inserting an element of the container inserts the value it had before the call. + // (The unsigned counts select insert(pos, n, item), not the integral form of insert(pos, first, last).) + const int e1[] = {1, 3, 2, 3, 4}; + CB_TEST_ALIAS(8, insert(cb.begin() + 1, cb[2]), e1); + const int e2[] = {1, 4, 2, 3, 4}; + CB_TEST_ALIAS(8, insert(cb.begin() + 1, 1u, cb[3]), e2); + const int e3[] = {1, 3, 3, 2, 3, 4}; + CB_TEST_ALIAS(8, insert(cb.begin() + 1, 2u, cb[2]), e3); + const int e4[] = {1, 2, 1, 3, 4}; + CB_TEST_ALIAS(8, rinsert(cb.begin() + 2, cb[0]), e4); + const int e5[] = {1, 2, 3, 1, 4}; + CB_TEST_ALIAS(8, rinsert(cb.begin() + 3, 1u, cb[0]), e5); + const int e6[] = {1, 2, 2, 2, 3, 4}; + CB_TEST_ALIAS(8, rinsert(cb.begin() + 2, 2u, cb[1]), e6); + const int e7[] = {1, 2, 3, 4, 1}; + CB_TEST_ALIAS(8, push_back(cb[0]), e7); + const int e8[] = {4, 1, 2, 3, 4}; + CB_TEST_ALIAS(8, push_front(cb[3]), e8); + + // Full: the element that the item refers to may be the one that is overwritten. + const int e9[] = {2, 1, 3, 4}; + CB_TEST_ALIAS(4, insert(cb.begin() + 2, cb[0]), e9); + const int e10[] = {2, 1, 3, 4}; + CB_TEST_ALIAS(4, insert(cb.begin() + 2, 1u, cb[0]), e10); + const int e11[] = {1, 2, 4, 3}; + CB_TEST_ALIAS(4, rinsert(cb.begin() + 2, cb[3]), e11); + const int e12[] = {1, 2, 4, 3}; + CB_TEST_ALIAS(4, rinsert(cb.begin() + 2, 1u, cb[3]), e12); + const int e13[] = {2, 3, 4, 1}; + CB_TEST_ALIAS(4, push_back(cb[0]), e13); + const int e14[] = {4, 1, 2, 3}; + CB_TEST_ALIAS(4, push_front(cb[3]), e14); + + // Growing beyond the capacity reallocates before the item is read. + const int e15[] = {1, 2, 3, 4, 1, 1}; + CB_TEST_ALIAS(4, resize(6, cb[0]), e15); + const int e16[] = {4, 4, 1, 2, 3, 4}; + CB_TEST_ALIAS(4, rresize(6, cb[3]), e16); +} + +#undef CB_TEST_ALIAS + void run_common_tests() { basic_test(); @@ -2475,6 +2539,7 @@ void run_common_tests() rinsert_test(); rinsert_n_test(); rinsert_range_test(); + insert_element_of_itself_test(); erase_test(); erase_range_test(); rerase_test();