willemt/cbuffer

A circular buffer written in C using Posix calls to create a contiguously mapped memory space. BSD Licensed.

C

272

30 commits

updated May 10, 2021

See the code

README

Circular buffers are tricky. The caller needs to know if the polled item crosses the maximum size of the underlying array.

This circular buffer uses a *mmap magic trick* to make the caller's life easier.

**This is how we do it:**

1. mmap is used to mirror the buffer like below:

.. figure:: doc/circular_buffer_mmap.png


2. the "mirrored" buffer is then placed beside the buffer. When the user polls the item it doesn't matter if the item crosses the buffer's boundary:

.. figure:: doc/circular_buffer_mmap_portal.png

Compatibility
-------------
This only works on UNIX systems.

Windows support existed in d10238600a82de32fe1cb01bec8667d3a67e382c but was removed. This is because Windows doesn't actually have mmap and trying to get the same behaviour is painful. Maybe you should check this out instead: https://github.com/willemt/bipbuffer

Not written in Markdown, so it's shown here as plain text — view it formatted on GitHub.

Contributors

willemt

30 commits

Languages

C

90.9%

Makefile

5.0%

Shell

4.0%

willemt/cbuffer

A circular buffer written in C using Posix calls to create a contiguously mapped memory space. BSD Licensed.

C

272

30 commits

updated May 10, 2021

See the code

README

Circular buffers are tricky. The caller needs to know if the polled item crosses the maximum size of the underlying array.

This circular buffer uses a *mmap magic trick* to make the caller's life easier.

**This is how we do it:**

1. mmap is used to mirror the buffer like below:

.. figure:: doc/circular_buffer_mmap.png


2. the "mirrored" buffer is then placed beside the buffer. When the user polls the item it doesn't matter if the item crosses the buffer's boundary:

.. figure:: doc/circular_buffer_mmap_portal.png

Compatibility
-------------
This only works on UNIX systems.

Windows support existed in d10238600a82de32fe1cb01bec8667d3a67e382c but was removed. This is because Windows doesn't actually have mmap and trying to get the same behaviour is painful. Maybe you should check this out instead: https://github.com/willemt/bipbuffer

Not written in Markdown, so it's shown here as plain text — view it formatted on GitHub.

Contributors

willemt

30 commits

Languages

C

90.9%

Makefile

5.0%

Shell

4.0%