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
28 changes: 18 additions & 10 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ build.jam b2 project and dependency list
- **Debug mode**: `BOOST_CB_ENABLE_DEBUG=1` makes iterators derive from
`debug_iterator_base` and register with the container, so invalidated
iterators are caught by `BOOST_CB_ASSERT`. It is off by default. Some
behaviour differs between modes: for example, `constant_erase_test` and
`soft_iterator_invalidation` only make sense with debug off.
behaviour differs between modes. For example, `soft_iterator_invalidation`
only makes sense with debug off, and debug mode overwrites erased memory, so
`constant_erase_test` skips its checks of erased memory when debug is on.
For checks like these, `test.hpp` provides `CB_TEST_DEBUG_ONLY(expr)` and
`CB_TEST_DEBUG_EXCLUDE(expr)`.
- **Internal macros**: `BOOST_CB_ASSERT`, `BOOST_CB_IS_CONVERTIBLE` and
`BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS` are defined in
`circular_buffer.hpp` and `#undef`'d at its end. As a result, the
Expand All @@ -81,9 +84,16 @@ build.jam b2 project and dependency list
- **Exceptions** use `BOOST_TRY` / `BOOST_CATCH(...)` / `BOOST_CATCH_END` /
`BOOST_RETHROW` and `boost::throw_exception`. The code must build with
`BOOST_NO_EXCEPTIONS`.
- **Scalar fast paths**: `erase_begin(n)`, `erase_end(n)` and `clear()` take
constant time for scalar `T`, because they dispatch on `is_scalar<value_type>`
and no destructors run. Preserve these complexity
- **Trivially destructible fast paths**: `erase_begin(n)`, `erase_end(n)`,
`clear()` and the destructor take constant time when `T` is trivially
destructible and the allocator doesn't need `destroy()` calls. They dispatch
on the private `trivial_erase` type: `has_trivial_destructor<value_type>`
and `circular_buffer_trivial_destroy<Alloc>` (in `circular_buffer_fwd.hpp`).
The latter is true only for the default allocator
(`circular_buffer_default_allocator<T>::type`), and users specialize it to
opt in for other allocators. The fast path skips the allocator's `destroy()`
as well as the destructor, so it is opt-in for any non-default allocator.
Debug mode always takes the slow path. Preserve these complexity
guarantees, since they are documented.

## Conventions
Expand Down Expand Up @@ -172,15 +182,13 @@ copy):
INC="-Iinclude $(for d in $BOOST/libs/*/include; do case $d in *circular_buffer*) ;; *) printf -- '-isystem %s ' $d;; esac; done)"
for std in c++03 c++11; do
for dbg in 0 1; do
for t in base_test space_optimized_test; do
for t in base_test space_optimized_test constant_erase_test; do
g++ -std=$std -Wall -Wextra -DBOOST_CB_ENABLE_DEBUG=$dbg $INC test/$t.cpp -o /tmp/$t && /tmp/$t || echo "FAIL $t $std dbg=$dbg"
done
done
done
# debug-off only:
for t in soft_iterator_invalidation constant_erase_test; do
g++ -std=c++11 $INC test/$t.cpp -o /tmp/$t && /tmp/$t || echo "FAIL $t"
done
g++ -std=c++11 $INC test/soft_iterator_invalidation.cpp -o /tmp/sii && /tmp/sii || echo "FAIL soft_iterator_invalidation"
```

`bounded_buffer_comparison.cpp` and `circular_buffer_bound_example.cpp` need
Expand All @@ -200,7 +208,7 @@ to `boost-root/doc/html/circular_buffer.html`.
| Stage | Tool | Source |
|---|---|---|
| Narrative | Quickbook 1.6 | `doc/circular_buffer.qbk` |
| API reference | Doxygen, then BoostBook | `/*! */` comments in `circular_buffer.hpp`, `base.hpp`, `space_optimized.hpp` (emitted as `autodoc.xml`) |
| API reference | Doxygen, then BoostBook | `/*! */` comments in `circular_buffer.hpp`, `circular_buffer_fwd.hpp`, `base.hpp`, `space_optimized.hpp` (emitted as `autodoc.xml`) |
| Index | AutoIndex | `doc/circular_buffer.idx` |
| Rendering | BoostBook XSL (xsltproc; FOP for PDF) | the combined DocBook |

Expand Down
11 changes: 11 additions & 0 deletions doc/circular_buffer.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,17 @@ Pull requests are welcome at [@https://github.com/boostorg/circular_buffer boost

[section:release Release Notes]

[h4 Boost 1.94]

* `erase_begin(size_type)`, `erase_end(size_type)`, `clear()` and the destructor now have constant complexity for
all trivially destructible types, not only scalars (`has_trivial_destructor` instead of `is_scalar`).
* The constant-time path skips the allocator's `destroy()` as well as the destructor, so it is now only used with
the default allocator. With any other allocator each element is destroyed through the allocator, so an allocator
with its own `destroy()` sees every call; for scalars this makes these operations linear where they were constant.
Specialize `boost::circular_buffer_trivial_destroy<Alloc>` to derive from `boost::true_type` to use the
constant-time path with an allocator that does not need `destroy()` calls for trivially destructible elements.
* Added `boost::circular_buffer_default_allocator<T>::type`, the allocator used when none is specified.

[h4 Boost 1.91]

* Avoided a GCC 4.x warning about calling `memset` with a constant size of zero.
Expand Down
4 changes: 4 additions & 0 deletions doc/jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ doxygen autodoc
:
# List all the files individually (RECURSIVE=NO ).
[ glob ../include/boost/circular_buffer.hpp ]
[ glob ../include/boost/circular_buffer_fwd.hpp ]
[ glob ../include/boost/circular_buffer/base.hpp ]
[ glob ../include/boost/circular_buffer/space_optimized.hpp ]

Expand Down Expand Up @@ -65,12 +66,15 @@ doxygen autodoc
\"BOOST_NOEXCEPT_IF(T)=noexcept(T)\" \\
\"BOOST_ASSERT(expr)=\" \\
\"BOOST_RV_REF(T)=T &&\" \\
\"BOOST_CB_DEFAULT_ALLOCATOR(T):=std::allocator<T>\" \\
\"ASSERT(x)=assert(x)\" \\
\"__cplusplus \""
# BOOST_PREVENT_MACRO_SUBSTITUTION, will not be replaced by ,
# BOOST_STATIC_CONSTANT will be replaced by "static x const y",
# BOOST_DEDUCED_TYPENAME will be replaced by "typename",
# BOOST_CONSTEXPR will be replaced by "constexpr".
# BOOST_CB_DEFAULT_ALLOCATOR(T) will be replaced by "std::allocator<T>" (":=" so the #define and #undef in
# circular_buffer_fwd.hpp do not override it).
<doxygen:param>EXCLUDE_SYMBOLS=*_throws
# <doxygen:param>IMAGE_PATH="../images" # for circular_buffer.svg
# See autodoxywarnings.log to check this is correct.
Expand Down
60 changes: 38 additions & 22 deletions include/boost/circular_buffer/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
#endif

#include <boost/config.hpp>
#include <boost/circular_buffer_fwd.hpp>
#include <boost/concept_check.hpp>
#include <boost/limits.hpp>
#include <boost/core/allocator_access.hpp>
#include <boost/core/empty_value.hpp>
#include <boost/type_traits/is_stateless.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_scalar.hpp>
#include <boost/type_traits/is_nothrow_move_constructible.hpp>
#include <boost/type_traits/is_nothrow_move_assignable.hpp>
#include <boost/type_traits/is_copy_constructible.hpp>
#include <boost/type_traits/has_trivial_destructor.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/conditional.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/move.hpp>
Expand Down Expand Up @@ -1189,7 +1191,8 @@ private empty_value<Alloc>
Invalidates all iterators pointing to the <code>circular_buffer</code> (including iterators equal to
<code>end()</code>).
\par Complexity
Constant (in the size of the <code>circular_buffer</code>) for scalar types; linear for other types.
Constant (in the size of the <code>circular_buffer</code>) for trivially destructible types with the
default allocator (see <code>circular_buffer_trivial_destroy</code>); linear otherwise.
\sa <code>clear()</code>
*/
~circular_buffer() BOOST_NOEXCEPT {
Expand Down Expand Up @@ -2251,24 +2254,27 @@ private empty_value<Alloc>
return iterator(this, last.m_it);
}

//! Remove first <code>n</code> elements (with constant complexity for scalar types).
//! Remove first <code>n</code> elements (with constant complexity for trivially destructible types).
/*!
\pre <code>n \<= size()</code>
\post The <code>n</code> elements at the beginning of the <code>circular_buffer</code> will be removed.
\param n The number of elements to be removed.
\throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
\par Exception Safety
Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything. (I.e. no throw in
case of scalars.)
case of trivially destructible types.)
\par Iterator Invalidation
Invalidates iterators pointing to the first <code>n</code> erased elements.
\par Complexity
Constant (in <code>n</code>) for scalar types; linear for other types.
\note This method has been specially designed for types which do not require an explicit destructruction (e.g.
integer, float or a pointer). For these scalar types a call to a destructor is not required which makes
it possible to implement the "erase from beginning" operation with a constant complexity. For non-sacalar
types the complexity is linear (hence the explicit destruction is needed) and the implementation is
actually equivalent to
Constant (in <code>n</code>) for trivially destructible types with the default allocator (see
<code>circular_buffer_trivial_destroy</code>); linear otherwise.
\note This method has been specially designed for types which do not require an explicit destruction (e.g.
integer, float, a pointer or a class with a trivial destructor). For these trivially destructible types
a call to a destructor is not required which makes it possible to implement the "erase from beginning"
operation with a constant complexity, without calling <code>destroy()</code> on the allocator. This
applies to the default allocator, and to any other allocator for which
<code>circular_buffer_trivial_destroy</code> is specialized. In all other cases the complexity is linear
(hence the explicit destruction is needed) and the implementation is actually equivalent to
<code>\link circular_buffer::rerase(iterator, iterator) rerase(begin(), begin() + n)\endlink</code>.
\sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
<code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
Expand All @@ -2279,28 +2285,31 @@ private empty_value<Alloc>
#if BOOST_CB_ENABLE_DEBUG
erase_begin(n, false_type());
#else
erase_begin(n, is_scalar<value_type>());
erase_begin(n, trivial_erase());
#endif
}

//! Remove last <code>n</code> elements (with constant complexity for scalar types).
//! Remove last <code>n</code> elements (with constant complexity for trivially destructible types).
/*!
\pre <code>n \<= size()</code>
\post The <code>n</code> elements at the end of the <code>circular_buffer</code> will be removed.
\param n The number of elements to be removed.
\throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
\par Exception Safety
Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything. (I.e. no throw in
case of scalars.)
case of trivially destructible types.)
\par Iterator Invalidation
Invalidates iterators pointing to the last <code>n</code> erased elements.
\par Complexity
Constant (in <code>n</code>) for scalar types; linear for other types.
\note This method has been specially designed for types which do not require an explicit destructruction (e.g.
integer, float or a pointer). For these scalar types a call to a destructor is not required which makes
it possible to implement the "erase from end" operation with a constant complexity. For non-sacalar
types the complexity is linear (hence the explicit destruction is needed) and the implementation is
actually equivalent to
Constant (in <code>n</code>) for trivially destructible types with the default allocator (see
<code>circular_buffer_trivial_destroy</code>); linear otherwise.
\note This method has been specially designed for types which do not require an explicit destruction (e.g.
integer, float, a pointer or a class with a trivial destructor). For these trivially destructible types
a call to a destructor is not required which makes it possible to implement the "erase from end"
operation with a constant complexity, without calling <code>destroy()</code> on the allocator. This
applies to the default allocator, and to any other allocator for which
<code>circular_buffer_trivial_destroy</code> is specialized. In all other cases the complexity is linear
(hence the explicit destruction is needed) and the implementation is actually equivalent to
<code>\link circular_buffer::erase(iterator, iterator) erase(end() - n, end())\endlink</code>.
\sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
<code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
Expand All @@ -2311,7 +2320,7 @@ private empty_value<Alloc>
#if BOOST_CB_ENABLE_DEBUG
erase_end(n, false_type());
#else
erase_end(n, is_scalar<value_type>());
erase_end(n, trivial_erase());
#endif
}

Expand All @@ -2325,7 +2334,8 @@ private empty_value<Alloc>
Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
<code>end()</code>).
\par Complexity
Constant (in the size of the <code>circular_buffer</code>) for scalar types; linear for other types.
Constant (in the size of the <code>circular_buffer</code>) for trivially destructible types with the
default allocator (see <code>circular_buffer_trivial_destroy</code>); linear otherwise.
\sa <code>~circular_buffer()</code>, <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
<code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
<code>erase_begin(size_type)</code>, <code>erase_end(size_type)</code>
Expand All @@ -2338,6 +2348,12 @@ private empty_value<Alloc>
private:
// Helper methods

/*! INTERNAL ONLY */
// Whether erasing can skip both the destructor and the allocator's destroy() (see
// circular_buffer_trivial_destroy). Selects the constant-time erase_begin(), erase_end() and destroy_content().
typedef integral_constant<bool,
has_trivial_destructor<value_type>::value && circular_buffer_trivial_destroy<Alloc>::value> trivial_erase;

/*! INTERNAL ONLY */
void check_position(size_type index) const {
if (index >= size())
Expand Down Expand Up @@ -2462,7 +2478,7 @@ private empty_value<Alloc>
#if BOOST_CB_ENABLE_DEBUG
destroy_content(false_type());
#else
destroy_content(is_scalar<value_type>());
destroy_content(trivial_erase());
#endif
}

Expand Down
39 changes: 35 additions & 4 deletions include/boost/circular_buffer_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#endif

#include <boost/config.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/is_same.hpp>
#if !defined(BOOST_NO_STD_ALLOCATOR)
#include <memory>
#else
Expand All @@ -30,14 +32,43 @@ namespace boost {
#define BOOST_CB_DEFAULT_ALLOCATOR(T) BOOST_DEDUCED_TYPENAME std::vector<T>::allocator_type
#endif

template <class T, class Alloc = BOOST_CB_DEFAULT_ALLOCATOR(T)>
//! The default allocator of <code>circular_buffer</code> and <code>circular_buffer_space_optimized</code>.
template <class T>
struct circular_buffer_default_allocator {
//! The allocator used for elements of type <code>T</code> when none is specified.
typedef BOOST_CB_DEFAULT_ALLOCATOR(T) type;
};

#undef BOOST_CB_DEFAULT_ALLOCATOR

//! Whether elements with a trivial destructor can be erased without calling the allocator's <code>destroy()</code>.
/*!
When this is <code>true</code> and the stored type has a trivial destructor, <code>erase_begin()</code>,
<code>erase_end()</code>, <code>clear()</code> and the destructor of <code>circular_buffer</code> run in constant
time and do not call <code>destroy()</code> on the allocator for the erased elements. Otherwise each element is
destroyed through the allocator and these operations are linear.

It is <code>true</code> only for the default allocator, whose <code>destroy()</code> does nothing more than call
the destructor. Specialize it to derive from <code>boost::true_type</code> for an allocator that does not need
<code>destroy()</code> calls for elements with a trivial destructor:
\code
namespace boost {
template <class T>
struct circular_buffer_trivial_destroy<MyAllocator<T> > : true_type {};
}
\endcode
*/
template <class Alloc>
struct circular_buffer_trivial_destroy
: is_same<Alloc,
BOOST_DEDUCED_TYPENAME circular_buffer_default_allocator<BOOST_DEDUCED_TYPENAME Alloc::value_type>::type> {};

template <class T, class Alloc = BOOST_DEDUCED_TYPENAME circular_buffer_default_allocator<T>::type>
class circular_buffer;

template <class T, class Alloc = BOOST_CB_DEFAULT_ALLOCATOR(T)>
template <class T, class Alloc = BOOST_DEDUCED_TYPENAME circular_buffer_default_allocator<T>::type>
class circular_buffer_space_optimized;

#undef BOOST_CB_DEFAULT_ALLOCATOR

} // namespace boost

#endif // #if !defined(BOOST_CIRCULAR_BUFFER_FWD_HPP)
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ run base_test.cpp : : : <define>"BOOST_CB_ENABLE_DEBUG=1" : base_test_dbg ;
run space_optimized_test.cpp : : : <define>"BOOST_CB_ENABLE_DEBUG=1" : space_optimized_test_dbg ;
run soft_iterator_invalidation.cpp ;
run constant_erase_test.cpp ;
run constant_erase_test.cpp : : : <define>"BOOST_CB_ENABLE_DEBUG=1" : constant_erase_test_dbg ;
compile bounded_buffer_comparison.cpp : <threading>multi <library>/boost/thread//boost_thread <library>/boost/timer//boost_timer ;
Loading
Loading