STM32F4DoorControl/ringbuffer.h
2021-02-13 22:16:17 +01:00

39 lines
1.0 KiB
C

#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;
#define RINGBUFFER_STORAGE(NAME, SIZE) uint8_t buf_##NAME##__LINE__[sizeof(ringbuffer) + SIZE]; ringbuffer *NAME = (ringbuffer*)buf_##NAME##__LINE__;
#define RINGBUFFER_INIT(NAME, SIZE) NAME->head = NAME->tail = 0; NAME->size = SIZE;
#define RINGBUFFER(NAME, SIZE) \
RINGBUFFER_STORAGE(NAME, SIZE); \
RINGBUFFER_INIT(NAME, SIZE);
void ringbuffer_rst(ringbuffer *buf);
unsigned ringbuffer_capacity(ringbuffer *buf);
unsigned ringbuffer_free(ringbuffer *buf);
unsigned ringbuffer_level(ringbuffer *buf);
unsigned ringbuffer_empty(ringbuffer *buf);
unsigned ringbuffer_add(ringbuffer *buf, void *dat, unsigned len);
unsigned ringbuffer_get(ringbuffer *buf, void *dst, unsigned len);
unsigned ringbuffer_peek(ringbuffer *buf, void *dst, unsigned len);
void ringbuffer_skip(ringbuffer *buf, unsigned len);
#ifdef __cplusplus
}
#endif
#endif