Questions tagged [circular-buffer]

A circular buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

A circular buffer (AKA cyclic buffer, ring buffer, or circular queue) is a data structure that uses a single fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

The circular buffer uses two pointers to indicate the current beginning and ending of the data on the buffer. The old/"First In" data is deleted from the buffer as it is read, moving forward the beginning pointer. New data is added at the end of the buffer (moving forward the corresponding pointer). The buffer physical storage is the same during all the operation if the reading speed is faster than the data acquisition process.

The useful property of a circular buffer is that it does not need to have its elements shuffled around when one is consumed. (If a non-circular buffer were used then it would be necessary to shift all elements when one is consumed.) In other words, the circular buffer is well suited as a FIFO (first in first out) buffer while a standard, non-circular buffer is well suited as a LIFO (last in first out) buffer.

Circular buffering makes a good implementation strategy for a queue that has fixed maximum size. Should a maximum size be adopted for a queue, then a circular buffer is a completely ideal implementation; all queue operations are constant time. However, expanding a circular buffer requires shifting memory, which is comparatively costly. For arbitrarily expanding queues, a Linked list approach may be preferred instead.

Check http://en.wikipedia.org/wiki/Circular_buffer for more information.

417 questions
14
votes
1 answer

Why must a ring buffer size be a power of 2?

Why must a ring buffer size be a power of 2?
BobAlmond
  • 459
  • 5
  • 12
13
votes
2 answers

How do you iterate backward over circular buffer without a conditional?

Iterating forward through a circular buffer without using a conditional is easy with the remainder operator... iterator = (iterator + 1) % buffer_size; I can't for the life of me figure out the reverse operation, iterating backward.
Nick Strupat
  • 4,928
  • 4
  • 44
  • 56
13
votes
1 answer

What is the difference between a Ring Buffer and a Circular Linked List?

What is the difference between a Ring Buffer and a Circular Linked List? What purpose does Ring Buffer serve that Circular Linked List cannot or vice versa?
user366312
  • 16,949
  • 65
  • 235
  • 452
12
votes
3 answers

Implementing a fixed-size log file, or a circular buffer on disk

I checked this question, but it's not what I'm looking for. I'm trying to figure out how to cap a log file's size (say, 10MB), and as soon as it's hit, either: start writing to the beginning, rather than appending, or keep appending, but delete…
warren
  • 32,620
  • 21
  • 85
  • 124
11
votes
1 answer

Video recording to a circular buffer on Android

I'm looking for the best way (if any...) to capture continuous video to a circular buffer on the SD card, allowing the user to capture events after they have happened. The standard video recording API allows you to just write directly to a file, and…
lacop
  • 2,024
  • 4
  • 22
  • 36
11
votes
1 answer

Understanding Linux Kernel Circular Buffer

There is an article at: http://lwn.net/Articles/378262/ that describes the Linux kernels circular buffer implementation. I have some questions: Here is the "producer": spin_lock(&producer_lock); unsigned long head = buffer->head; unsigned long tail…
dicroce
  • 45,396
  • 28
  • 101
  • 140
10
votes
5 answers

improving C circular buffer efficiency

I'd like some help improving the efficiency of my circular buffer code. I had a look around stackoverflow and found that (nearly) all of the topics on circular buffers are about the uses of such a buffer or the basic implementation of a circular…
Gurba
  • 157
  • 1
  • 2
  • 8
10
votes
5 answers

Efficient circular list

I want a simple yet efficient circular buffer/queue. If I use std::vector, I have to do this: if ( v.size() >= limit ) { std::vector it = v.begin(); v.insert( it, data ); v.erase( it+1 ); } Is there any simpler solution?
mahmood
  • 23,197
  • 49
  • 147
  • 242
10
votes
6 answers

Simplified algorithm for calculating remaining space in a circular buffer?

I was wonder if there is a simpler (single) way to calculate the remaining space in a circular buffer than this? int remaining = (end > start) ? end-start : bufferSize - start + end;
Dynite
  • 2,313
  • 5
  • 30
  • 38
9
votes
3 answers

C /C++ Lock-free (or nonblocking) Ring Buffer that OVERWRITES oldest data?

I'm trying to find a way to make a Lock Free OR Non-blocking way to make a Ring Buffer for single consumer / single consumer that will over-write the oldest data int the buffer. I've read a lot of lock-free algorithms that work when you "return…
Nik
  • 239
  • 4
  • 10
9
votes
2 answers

std::upper_bound returns const iterator in const member function

Here is a class that contains a boost::circular_buffer of some struct. I make a typedef for iterators into the contained circular_buffer. My problem is this: when the doWork function is marked const, the returned value of std::upper_bound is not…
Chris
  • 474
  • 2
  • 8
  • 22
8
votes
3 answers

Circular buffer implementation in C

I have found pseudo code on how to implement a circular buffer. // Producer. while (true) { /* produce item v */ while ((in+1)%n == out) /* Wait. */; b[in] = v; in = (in + 1) % n } // Consumer. while (true) { while (in == out) /*…
bruce12
  • 85
  • 1
  • 4
8
votes
3 answers

ring buffer without priority inversion

I have a high-priority process that needs to pass data to a low-priority process. I've written a basic ring buffer to handle the passing of data: class RingBuffer { public: RingBuffer(int size); ~RingBuffer(); int count() {return…
user168715
  • 5,469
  • 1
  • 31
  • 42
8
votes
2 answers

How to use a ring data structure in window functions

I have data that is arranged in a ring structure (or circular buffer), that is it can be expressed as sequences that cycle: ...-1-2-3-4-5-1-2-3-.... See this picture to get an idea of a 5-part ring: I'd like to create a window query that can…
Mike T
  • 41,085
  • 18
  • 152
  • 203
8
votes
1 answer

Read data into a circular buffer

Is it possible to use boost::circular_buffer with boost::asio? Specifically I want to read a fixed number of bytes with boost::asio::async_write and store them directly in the circular buffer without copying. Some example code would be very nice!
Robert Hegner
  • 9,014
  • 7
  • 62
  • 98
1
2
3
27 28