Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/circular_buffer.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 45 additions & 2 deletions include/boost/circular_buffer/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <boost/concept_check.hpp>
#include <boost/limits.hpp>
#include <boost/core/allocator_access.hpp>
#include <boost/core/addressof.hpp>
#include <boost/core/empty_value.hpp>
#include <boost/core/pointer_traits.hpp>
#include <boost/type_traits/is_stateless.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_scalar.hpp>
Expand All @@ -33,6 +35,7 @@
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/move.hpp>
#include <algorithm>
#include <functional>
#include <iterator>
#include <utility>
#include <deque>
Expand Down Expand Up @@ -918,8 +921,14 @@ private empty_value<Alloc>
*/
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
resize(new_size, copy);
return;
}
set_capacity(new_size);
}
insert(end(), new_size - size(), item);
} else {
iterator e = end();
Expand Down Expand Up @@ -993,8 +1002,14 @@ private empty_value<Alloc>
*/
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
rresize(new_size, copy);
return;
}
set_capacity(new_size);
}
rinsert(begin(), new_size - size(), item);
} else {
rerase(begin(), end() - new_size);
Expand Down Expand Up @@ -1670,6 +1685,10 @@ private empty_value<Alloc>
<code>rinsert(iterator, InputIterator, InputIterator)</code>
*/
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(pos, copy);
}
return insert_impl<param_value_type>(pos, item);
}

Expand Down Expand Up @@ -1789,6 +1808,11 @@ private empty_value<Alloc>
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<const_pointer, param_value_type>(item_copy));
return;
}
insert_n(pos, n, cb_details::item_wrapper<const_pointer, param_value_type>(item));
}

Expand Down Expand Up @@ -1925,6 +1949,10 @@ private empty_value<Alloc>
<code>insert(iterator, InputIterator, InputIterator)</code>
*/
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(pos, copy);
}
return rinsert_impl<param_value_type>(pos, item);
}

Expand Down Expand Up @@ -2034,6 +2062,11 @@ private empty_value<Alloc>
*/
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<const_pointer, param_value_type>(copy));
return;
}
rinsert_n(pos, n, cb_details::item_wrapper<const_pointer, param_value_type>(item));
}

Expand Down Expand Up @@ -2335,6 +2368,16 @@ private empty_value<Alloc>
m_size = 0;
}

protected:
/*! INTERNAL ONLY */
bool is_stored(param_value_type item) const BOOST_NOEXCEPT {
if (m_buff == 0)
return false;
std::less<const value_type*> 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

Expand Down
28 changes: 28 additions & 0 deletions include/boost/circular_buffer/space_optimized.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ public:<br>
<code>pop_front()</code>
*/
void push_back(param_value_type item) {
if (circular_buffer<T, Alloc>::is_stored(item)) {
value_type copy(item); // check_low_capacity() may free the storage that item is in
push_back(copy);
return;
}
check_low_capacity();
circular_buffer<T, Alloc>::push_back(item);
}
Expand Down Expand Up @@ -856,6 +861,11 @@ public:<br>
<code>pop_front()</code>
*/
void push_front(param_value_type item) {
if (circular_buffer<T, Alloc>::is_stored(item)) {
value_type copy(item); // check_low_capacity() may free the storage that item is in
push_front(copy);
return;
}
check_low_capacity();
circular_buffer<T, Alloc>::push_front(item);
}
Expand Down Expand Up @@ -988,6 +998,10 @@ public:<br>
<code>rinsert(iterator, InputIterator, InputIterator)</code>
*/
iterator insert(iterator pos, param_value_type item) {
if (circular_buffer<T, Alloc>::is_stored(item)) {
value_type copy(item); // check_low_capacity() may free the storage that item is in
return insert(pos, copy);
}
size_type index = pos - begin();
check_low_capacity();
return circular_buffer<T, Alloc>::insert(begin() + index, item);
Expand Down Expand Up @@ -1110,6 +1124,11 @@ public:<br>
<code>rinsert(iterator, InputIterator, InputIterator)</code>
*/
void insert(iterator pos, size_type n, param_value_type item) {
if (circular_buffer<T, Alloc>::is_stored(item)) {
value_type copy(item); // check_low_capacity() may free the storage that item is in
insert(pos, n, copy);
return;
}
size_type index = pos - begin();
check_low_capacity(n);
circular_buffer<T, Alloc>::insert(begin() + index, n, item);
Expand Down Expand Up @@ -1199,6 +1218,10 @@ public:<br>
<code>insert(iterator, InputIterator, InputIterator)</code>
*/
iterator rinsert(iterator pos, param_value_type item) {
if (circular_buffer<T, Alloc>::is_stored(item)) {
value_type copy(item); // check_low_capacity() may free the storage that item is in
return rinsert(pos, copy);
}
size_type index = pos - begin();
check_low_capacity();
return circular_buffer<T, Alloc>::rinsert(begin() + index, item);
Expand Down Expand Up @@ -1321,6 +1344,11 @@ public:<br>
<code>insert(iterator, InputIterator, InputIterator)</code>
*/
void rinsert(iterator pos, size_type n, param_value_type item) {
if (circular_buffer<T, Alloc>::is_stored(item)) {
value_type copy(item); // check_low_capacity() may free the storage that item is in
rinsert(pos, n, copy);
return;
}
size_type index = pos - begin();
check_low_capacity(n);
circular_buffer<T, Alloc>::rinsert(begin() + index, n, item);
Expand Down
64 changes: 64 additions & 0 deletions test/common.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,69 @@ 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 <class CB>
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<int> cb(cap); \
alias_fill(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();
Expand Down Expand Up @@ -2475,6 +2538,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();
Expand Down
22 changes: 22 additions & 0 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@
#if !defined(BOOST_NO_EXCEPTIONS)
#include <exception>
#endif
#if defined(_MSC_VER) && defined(_DEBUG)
#include <crtdbg.h>
#include <stdlib.h>
#endif

#if defined(_MSC_VER) && defined(_DEBUG)
namespace {

// Report CRT asserts and errors on stderr instead of in a dialog, which hangs an unattended
// test until it times out and hides the message.
struct cb_no_crt_dialogs {
cb_no_crt_dialogs() {
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
_set_abort_behavior(0, _CALL_REPORTFAULT);
}
} cb_no_crt_dialogs_instance;

} // namespace
#endif

// Integer (substitute for int) - more appropriate for testing
class MyInteger {
Expand Down
Loading