Round and round goes the buffer …
The concept of circular buffer (or ring buffer) is well known. It is immensely useful when dealing with buffering and processing stream of data. All in all, it’s pretty simple to implement, you just have an array of bytes that you treat like a ring by maintaining a begin pointer and an end pointer. When you get at the end you just start at the beginning again and with a little of modulo math magic, things work out.
This is trivial and works fine when you access the array item by item. However when you want to do things like memcpy or alike, the wrapping problem arises : You have to make sure the zone you’re accessing is not crossing the end of the buffer. If it does you’d need to split your access in two.
There is however a method to make things more transparent to your application by using the MMU to do the job for you : Simply map the same memory twice in a row ! There are several way to do that, using mmap or shm but the principle is always the same.
If you need more details, and even example code, go to the « Circular_buffer » wikipedia entry. IMHO this is a neat little trick that’s worth mentioning.

Twitter
Youtube
SlideShare
LinkedIn
Facebook