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.

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...
bool pop (T &data)
 Pop the transaction 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) 2016-2016, ARM Limited, All Rights Reserved
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#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 %d \n", bytes_written);
    
    // If buffer is full, contents will be over-written
    buf.push(data_stream[bytes_written++]);
    
    char data;
    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.