2022-07-03 11:28:44 -05:00
|
|
|
#include "circbuf.h"
|
|
|
|
|
2022-07-05 15:12:48 -05:00
|
|
|
#include "util.h"
|
2022-07-03 19:24:53 -05:00
|
|
|
#include "assert.h"
|
2022-07-03 11:28:44 -05:00
|
|
|
|
2022-07-19 15:13:15 -05:00
|
|
|
struct circbuf *circbuf_make(size_t size, unsigned int len)
|
|
|
|
{
|
|
|
|
struct circbuf *new = malloc(sizeof(*new));
|
|
|
|
new->len = powof2(len);
|
|
|
|
new->data = calloc(sizeof(short), new->len);
|
|
|
|
new->read = new->write = 0;
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2022-07-05 15:12:48 -05:00
|
|
|
struct circbuf circbuf_init(size_t size, unsigned int len)
|
2022-07-03 11:28:44 -05:00
|
|
|
{
|
|
|
|
struct circbuf new;
|
2022-07-05 15:12:48 -05:00
|
|
|
new.len = powof2(len);
|
2022-07-03 19:24:53 -05:00
|
|
|
new.data = calloc(sizeof(short), new.len);
|
|
|
|
new.read = new.write = 0;
|
2022-07-03 11:28:44 -05:00
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
int cbuf_size(struct circbuf *buf) {
|
|
|
|
return buf->write - buf->read;
|
|
|
|
}
|
2022-07-03 11:28:44 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
int cbuf_empty(struct circbuf *buf) {
|
|
|
|
return buf->read == buf->write;
|
|
|
|
}
|
2022-07-03 11:28:44 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
int cbuf_full(struct circbuf *buf) {
|
2022-07-06 17:17:06 -05:00
|
|
|
return cbuf_size(buf) >= buf->len;
|
2022-07-03 19:24:53 -05:00
|
|
|
}
|
2022-07-03 11:28:44 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
uint32_t cbuf_mask(struct circbuf *buf, uint32_t n) {
|
|
|
|
return n & (buf->len-1);
|
|
|
|
}
|
2022-07-03 11:28:44 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
void cbuf_push(struct circbuf *buf, short data) {
|
2022-07-06 17:17:06 -05:00
|
|
|
//assert(!cbuf_full(buf));
|
2022-07-03 11:28:44 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
buf->data[cbuf_mask(buf,buf->write++)] = data;
|
|
|
|
}
|
2022-07-03 11:28:44 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
short cbuf_shift(struct circbuf *buf) {
|
2022-07-06 17:17:06 -05:00
|
|
|
//assert(!cbuf_empty(buf));
|
2022-07-03 19:24:53 -05:00
|
|
|
return buf->data[cbuf_mask(buf, buf->read++)];
|
2022-07-03 11:28:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|