threadsafe
Ondřej Hruška 8 years ago
parent 46e243dd2c
commit a3485b9ced
  1. 27
      README.md
  2. 2
      circbuf.h

@ -1,7 +1,9 @@
Circular byte buffer Circular byte buffer
==================== ====================
This is a circular buffer implementation, useful for embedded systems (buffer for UART RX queue etc). This is a circular buffer implementation, useful mainly for embedded systems (buffer for UART RX/TX queues etc).
It should be reliable with producent / consumer threads (no race conditions, as no length variable is used).
Usage Usage
@ -9,25 +11,34 @@ Usage
```c ```c
#include <stdint.h> #include <stdint.h>
#include "circbuf.h"
uint8_t buffer[32]; // array backing the buffer circbuf_t cb; // buffer instance
CircularBuffer cb; // buffer instance
void main() void main()
{ {
cbuf_init(&cb, buffer, 32); // init the buffer char c;
cbuf_init(&cb, 32); // init the buffer
// now it's ready for use! // now it's ready for use!
cbuf_write(&cb, 'A');
if (cbuf_read(&cb, &c)) {
printf("%c", c);
}
cbuf_deinit(&cb); // free the backing array (in embedded system you don't usually need to, allocate once and keep it)
} }
``` ```
Many function return success flag, so make sure to check the return values. Most functions return a success flag (true - success), so make sure to check the return values.
False is returned on buffer overflow / underflow, attempted read past available data size etc. See the header file for details. False is returned on buffer overflow / underflow. See the header file for details.
License License
------- -------
Do whatever you want with the code. I don't care, public domain (I guess).

@ -4,7 +4,7 @@
// Circular Character Buffer implementation // Circular Character Buffer implementation
typedef struct CircularBuffer_struct { typedef struct {
uint8_t *buffer; uint8_t *buffer;
uint16_t capacity; uint16_t capacity;
uint16_t read_pos; uint16_t read_pos;

Loading…
Cancel
Save