Да, сука, да!
‰ cat cbuf.h cbuf.c
#ifndef __CBUF_H__
#define __CBUF_H__
#include <stdint.h>
typedef struct {
uint8_t *buf;
uint16_t head, tail, len;
} cbuf_t;
void cbuf_init(cbuf_t *cbuf, uint8_t *buf, unsigned len);
unsigned cbuf_read(cbuf_t *cbuf, uint8_t *buf, unsigned count);
unsigned cbuf_write(cbuf_t *cbuf, const uint8_t *buf, unsigned count);
#endif
#include <stdint.h>
#include "cbuf.h"
void cbuf_init(cbuf_t *cbuf, uint8_t *buf, unsigned len) {
cbuf->buf = buf;
cbuf->head = 0;
cbuf->tail = 0;
cbuf->len = len;
}
unsigned cbuf_read(cbuf_t *cbuf, uint8_t *buf, unsigned count) {
unsigned i;
for (i = 0; i < count; ++i) {
if (cbuf->tail == cbuf->head) {
break;
}
buf[i] = cbuf->buf[cbuf->tail];
cbuf->tail = (cbuf->tail + 1) % cbuf->len;
}
return i;
}
unsigned cbuf_write(cbuf_t *cbuf, const uint8_t *buf, unsigned count) {
for (unsigned i = 0; i < count; ++i) {
cbuf->buf[cbuf->head] = buf[i];
cbuf->head = (cbuf->head + 1) % cbuf->len;
}
return count;
}