STM32F4DoorControl/ringbuffer.h

56 lines
1.6 KiB
C
Raw Permalink Normal View History

2021-02-13 19:39:00 +00:00
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
typedef struct {
uint32_t head;
uint32_t tail;
uint32_t size;
uint8_t data[];
} ringbuffer;
2021-02-19 20:31:06 +00:00
#define RINGBUFFER_STORAGE(NAME, SIZE) \
2021-02-19 17:01:56 +00:00
uint8_t buf_##NAME##__LINE__[sizeof(ringbuffer) + SIZE]; \
ringbuffer *NAME = (ringbuffer *)buf_##NAME##__LINE__;
2021-02-19 20:31:06 +00:00
#define RINGBUFFER_INIT(NAME, SIZE) \
2021-02-19 17:01:56 +00:00
NAME->head = NAME->tail = 0; \
NAME->size = SIZE;
2021-02-19 20:31:06 +00:00
#define RINGBUFFER(NAME, SIZE) \
2021-02-13 19:39:00 +00:00
RINGBUFFER_STORAGE(NAME, SIZE); \
RINGBUFFER_INIT(NAME, SIZE);
2021-02-19 17:01:56 +00:00
/** Fully empties the ringbuffer */
2021-02-13 19:39:00 +00:00
void ringbuffer_rst(ringbuffer *buf);
2021-02-19 17:01:56 +00:00
/** The total capacity of the ringbuffer */
2021-02-13 19:39:00 +00:00
unsigned ringbuffer_capacity(ringbuffer *buf);
2021-02-19 17:01:56 +00:00
/** The free capacity of the ringbuffer */
2021-02-13 19:39:00 +00:00
unsigned ringbuffer_free(ringbuffer *buf);
2021-02-19 17:01:56 +00:00
/** The used capacity of the ringbuffer */
2021-02-13 19:39:00 +00:00
unsigned ringbuffer_level(ringbuffer *buf);
2021-02-19 17:01:56 +00:00
/** Non-zero if the ringbuffer is empty */
2021-02-13 19:39:00 +00:00
unsigned ringbuffer_empty(ringbuffer *buf);
2021-02-19 17:01:56 +00:00
/** Add data to the ringbuffer, return actual length of added data */
unsigned ringbuffer_add(ringbuffer *buf, void const *dat, unsigned len);
2021-02-13 19:39:00 +00:00
2021-02-19 17:01:56 +00:00
/** Get data from the ringbuffer, return actual length of retrieved data */
2021-02-13 21:16:17 +00:00
unsigned ringbuffer_get(ringbuffer *buf, void *dst, unsigned len);
2021-02-19 17:01:56 +00:00
/** Get data from the ringbuffer, but do not remove it. Return actual length of retrieved data. */
2021-02-13 21:16:17 +00:00
unsigned ringbuffer_peek(ringbuffer *buf, void *dst, unsigned len);
2021-02-19 17:01:56 +00:00
/** Remove data from the ringbuffer without reading it */
2021-02-13 19:39:00 +00:00
void ringbuffer_skip(ringbuffer *buf, unsigned len);
#ifdef __cplusplus
}
#endif
#endif