38 lines
569 B
C
38 lines
569 B
C
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include "rbuf.h"
|
|
|
|
int32_t rbuf_push(RBuf *rbuf, uint8_t data) {
|
|
size_t next;
|
|
|
|
next = rbuf->head + 1;
|
|
if (next >= rbuf->cap) {
|
|
next = 0;
|
|
}
|
|
|
|
if (next == rbuf->tail) {
|
|
return -1;
|
|
}
|
|
|
|
rbuf->buffer[rbuf->head] = data;
|
|
rbuf->head = next;
|
|
return 0;
|
|
}
|
|
|
|
int32_t rbuf_pop(RBuf *rbuf, uint8_t *data) {
|
|
size_t next;
|
|
|
|
if (rbuf->head == rbuf->tail) {
|
|
return -1;
|
|
}
|
|
|
|
next = rbuf->tail + 1;
|
|
if (next >= rbuf->cap) {
|
|
next = 0;
|
|
}
|
|
|
|
*data = rbuf->buffer[rbuf->tail];
|
|
rbuf->tail = next;
|
|
return 0;
|
|
}
|