stmbl/shared/ringbuf.h

65 lines
1.4 KiB
C
Raw Permalink Normal View History

#pragma once
2017-01-10 00:56:32 +00:00
#include <string.h>
/**
* Ringbuffer structure
*
*/
struct ringbuf {
2018-04-06 23:36:17 +00:00
char *buf; ///< Pointer to buffer memory
unsigned bufsize; ///< Size of buffer memory
2017-09-06 02:20:06 +00:00
volatile unsigned pos; ///< Current read position
volatile unsigned len; ///< Length of data in buffer
};
2017-09-06 02:20:06 +00:00
#define RINGBUF(size) \
{ .buf = (char[(size)]){0}, .bufsize = (size) }
/**
* Read a single byte from a buffer
*
* \param rb pointer to ringbuffer struct
* \param data pointer to data byte
* \return number of bytes read (0 if buffer was empty)
*/
2017-01-10 00:56:32 +00:00
int rb_getc(struct ringbuf *rb, char *data);
/**
* Write a single byte to a buffer
*
* \param rb pointer to ringbuffer struct
* \param data pointer to data byte
* \return number of bytes written (0 if buffer was full)
*/
2017-01-10 00:56:32 +00:00
int rb_putc(struct ringbuf *rb, const char data);
/**
* Read from a buffer
*
*/
2017-01-10 00:56:32 +00:00
int rb_read(struct ringbuf *rb, void *data, int len);
/**
* Write to a buffer
*
* \param rb pointer to ringbuffer struct
* \param data pointer to data byte
* \return number of bytes written (0 if buffer was full)
*
*/
2017-01-10 00:56:32 +00:00
int rb_write(struct ringbuf *rb, const void *data, int len);
2017-01-10 00:56:32 +00:00
/**
* undo rb_read
*
* \param rb pointer to ringbuffer struct
* \param len length to reset read pointer
* \return 0 on error
*
*/
int rb_undo(struct ringbuf *rb, int len);
2017-01-10 00:56:32 +00:00
int rb_getline(struct ringbuf *rb, char *ptr, int len);