Skip to content

About

Boost.org circular_buffer module

Resources

Stars

64 stars

Watchers

7 watching

Forks

Repository files navigation

Boost

Boost.CircularBuffer

A fixed-capacity, STL-compliant ring buffer for C++03 and later.

Branch CI Documentation
master CI Documentation
develop CI Documentation

Introduction

A circular buffer, also called a ring or cyclic buffer, holds a fixed number of elements. When it is full, each new element overwrites the oldest one.

boost::circular_buffer is a standard container that behaves this way. It works like std::deque or std::vector, with random-access iterators and constant-time insertion and removal at either end, and it works with the standard algorithms. Once its capacity is set, it doesn't allocate memory until you change the capacity yourself.

A circular buffer: items stored clockwise from begin() to end(), with free space beyond

#include <boost/circular_buffer.hpp>

int main()
{
    // Create a circular buffer with a capacity for 3 integers.
    boost::circular_buffer<int> cb(3);

    cb.push_back(1);
    cb.push_back(2);
    cb.push_back(3);  // The buffer is full: 1 2 3

    cb.push_back(4);  // Overwrite 1 with 4: 2 3 4
    cb.push_back(5);  // Overwrite 2 with 5: 3 4 5

    cb.pop_back();    // 5 is removed: 3 4
    cb.pop_front();   // 3 is removed: 4

    return cb[0] == 4 ? 0 : 1;
}

Features

  • Header-only. There's nothing to build or link.
  • C++03 and later. CI tests C++03 through the latest standard with GCC, Clang and MSVC.
  • Fixed memory. The buffer doesn't allocate behind your back, so it suits real-time and performance-critical code.
  • STL-compliant. It has random-access iterators, allocator support and move semantics, and works with the <algorithm> functions.
  • Contiguous storage. The elements are held in at most two contiguous pieces. array_one(), array_two() and linearize() give C APIs direct access to them.
  • Debug support. With BOOST_CB_ENABLE_DEBUG=1, using an invalidated iterator triggers an assertion.

There's also boost::circular_buffer_space_optimized, which takes the same fixed maximum capacity but allocates memory only as it needs it, and frees it again as the buffer shrinks.

Typical uses

  • Keeping the most recent N samples, readings or log lines.
  • A cache of the last N items inserted.
  • A fixed-capacity FIFO or LIFO queue.
  • The storage for a bounded producer/consumer queue. See example/circular_buffer_bound_example.cpp.

Like the standard containers, circular_buffer isn't thread-safe. If several threads share one buffer, you have to provide the locking yourself.

Getting started

Boost.CircularBuffer is part of Boost. If you have Boost installed, you already have it: just include <boost/circular_buffer.hpp>.

With CMake:

find_package(Boost REQUIRED)
target_link_libraries(my_app PRIVATE Boost::headers)

Within the Boost superproject, link Boost::circular_buffer instead.

The library depends only on other header-only Boost libraries: Assert, ConceptCheck, Config, Core, Move, ThrowException and TypeTraits.

Documentation

  • Documentation: introduction, rationale, examples and the full reference.
  • example/: small programs you can compile.

Building the tests

From a Boost superproject checkout, with this repository at libs/circular_buffer:

./b2 libs/circular_buffer/test cxxstd=03,11,17

Or as a standalone CMake project against an installed Boost (1.85 or newer, CMake 3.10 or newer):

cmake -S . -B build -G Ninja -DBUILD_TESTING=ON
ninja -C build check            # build and run the tests
ninja -C build check-valgrind   # the same under valgrind

Git branches

Branch Purpose
master The latest release. Builds with the master branch of the Boost superproject.
develop Work for the next release. Pull requests go here.

Contributing

Bug reports, questions and pull requests are welcome on GitHub. Please send pull requests to develop, and include tests for both containers where they apply.

License

Distributed under the Boost Software License, Version 1.0.

Originally written by Jan Gaspar (2003–2008).

The Boost logo is a trademark of The C++ Alliance, used according to the Boost Logo Policy.

About

Boost.org circular_buffer module

Resources

Stars

64 stars

Watchers

7 watching

Forks

Releases

Packages

Contributors

Languages