Giter Club home page Giter Club logo

ringbuf's Introduction

ringbuf

ringbuf is a lock-free, single producer/single consumer (SPSC) ring buffer/circular buffer implementation using C11 atomics.

Its aim is to be as simple as possible, while being correct. So no fancy look ahead functions, memory reserving or similar.

ringbuf consists of 4 functions new, read, write, and free.

The backing buffer consists of an uint8_t array of user specified size. All read and write operations copy either from or to this buffer.

ringbuf is non-blocking for both read and write operations. read and write operations will return the number of bytes read/written.

ringbuf does partial read and write depending on how full/empty the ring buffer is and how big the user supplied buffers are.

ringbuf was originally developed for audio applications, aiding in the separation of audio decoding and PCM output.

Usage

To use the single file header library simply drop ringbuf.h into your source directory and include it.

#include "ringbuf.h"

Then create a ringbuf instance. The size will be rounded up to the next power of 2.

struct ringbuf * ringbuf_new(size_t capacity);

Now create your producer and consumer threads. The producer can now write to the buffer by calling ringbuf_write.

size_t ringbuf_write(struct ringbuf *rb, uint8_t *buf, size_t buf_size);

Make sure to check the returned size, as it might be smaller than buf_size depending on how full the ring buffer is.

On the consumer thread you can now read from the ring buffer by calling ringbuf_read.

size_t ringbuf_read(struct ringbuf *rb, uint8_t *buf, size_t buf_size);

This will read a maximum of buf_size bytes into the buffer buf, depending on how full the ring buffer is. Check the return value on how many bytes were actually copied.

Once all operations have completed make sure to free the ring buffer by calling:

void ringbuf_free(struct ringbuf *rb);

Dependencies

Runtime

  • A C11 compatible compiler with C11 atomics support

Test

  • Cmocka

License

ISC license, see the LICENSE file for more details.

ringbuf's People

Contributors

szanni avatar

Stargazers

 avatar Yunho Jeon avatar Uffe Jakobsen avatar Marco Vimercati avatar  avatar Fabrizio avatar  avatar Yonggang Luo avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.