Files
my-os-project2/kernel/rbuf/rbuf.c

35 lines
698 B
C

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "rbuf.h"
#include "kprintf.h"
#include "hal/hal.h"
void rbuf_init(RBuf *rbuf, uint8_t *buf, size_t bufsize) {
rbuf->buffer = buf;
rbuf->tail = 0;
rbuf->head = 0;
rbuf->cap = bufsize;
rbuf->count = 0;
}
int32_t rbuf_push(RBuf *rbuf, uint8_t data) {
if (rbuf->count == rbuf->cap) {
return -1;
}
rbuf->buffer[rbuf->head] = data;
rbuf->head = (rbuf->head + 1) % rbuf->cap;
rbuf->count++;
return 0;
}
int32_t rbuf_pop(RBuf *rbuf, uint8_t *data) {
if (rbuf->count == 0) {
return -1;
}
*data = rbuf->buffer[rbuf->tail];
rbuf->tail = (rbuf->tail + 1) % rbuf->cap;
rbuf->count--;
return 0;
}