3 Şubat 2014 Pazartesi

Implementing A Circular Buffer (Ring Buffer)

A ring buffer is generally used in streaming applications such as music streaming or video streaming. When you stream media from somewhere the bytes of the media will be written on a ring buffer and after it received by the client new data will be written on the buffer. So that you will use a fixed-size data structure and will be able to do real-time streaming without using a big data structure which is not very easy to handle when it comes from the socket.

General implementation of a ring buffer

There are two cases while reading/writing from/to a ring buffer:
First one is as you guess, is to write/read directly as if our ring buffer is an ordinary buffer. This case is suitable if income data doesn't causes overflow. If ring buffer overflows after incoming data you must have another case to handle it. This is the second case and it is very comprehensible as well as the first case. If your data overflows then push it from the beginning of the data structure. And in both cases you should adjust your begin/end indexes (or you can use pointers). These indexes holds the address/index of the last data on the buffer.

I implemented my own ring buffer data structure using C++ language like this:

Hiç yorum yok:

Yorum Gönder