Mistake on this page?
Report an issue in GitHub or email us

CircularBuffer

The CircularBuffer class provides APIs to push and pop data from a buffer. You should check if the buffer is full before pushing the data because a full buffer overwrites the data. The empty API is available to check contents in buffer before performing the pop operation.

CircularBuffer class is interrupt safe; all data operations are performed inside the critical section.

CircularBuffer is a templated class supporting different datatypes. The declaration of the CircularBuffer class must specify datatype and buffer size.

Memory considerations

The actual data buffer is allocated as part of the CircularBuffer object memory.

Declaration example

This is an example of BUF_SIZE long integer CircularBuffer:

CircularBuffer<int, BUF_SIZE> buf;

CircularBuffer class reference

Public Member Functions
void push (const T &data)
 Push the transaction to the buffer. More...
void push (const T *src, CounterType len)
 Push the transaction to the buffer. More...
void push (mbed::Span< const T > src)
 Push the transaction to the buffer. More...
bool pop (T &data)
 Pop from the buffer. More...
CounterType pop (T *dest, CounterType len)
 Pop multiple elements from the buffer. More...
mbed::Span< T > pop (mbed::Span< T > dest)
 Pop multiple elements from the buffer. More...
bool empty () const
 Check if the buffer is empty. More...
bool full () const
 Check if the buffer is full. More...
void reset ()
 Reset the buffer. More...
CounterType size () const
 Get the number of elements currently stored in the circular_buffer. More...
bool peek (T &data) const
 Peek into circular buffer without popping. More...

CircularBuffer example

/*
 * Copyright (c) 2017 - 2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "platform/CircularBuffer.h"

#define BUF_SIZE    10

CircularBuffer<char, BUF_SIZE> buf;
char data_stream[] = "DataToBeAddedToBuffer";

int main()
{
    uint32_t bytes_written = 0;

    while (!buf.full()) {
        buf.push(data_stream[bytes_written++]);
    }

    printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n",
           (buf.full() ? "true" : "false"),
           (buf.empty() ? "true" : "false"));
    printf("Bytes written %ld \n", bytes_written);

    // If buffer is full, contents will be over-written
    buf.push(data_stream[bytes_written++]);

    char data = 0;
    printf("Buffer contents: ");
    while (!buf.empty()) {
        buf.pop(data);
        printf("%c", data);
    }
    printf("\n");

    printf("Circular buffer is full: \"%s\" or empty: \"%s\" \n",
           (buf.full() ? "true" : "false"),
           (buf.empty() ? "true" : "false"));

    return 0;

}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.