Rewrite ps2kbproc, rbuf (kernel ring buffer) and pipe read/write, Change to -O0 in kernel code

This commit is contained in:
2025-09-20 16:50:40 +02:00
parent 222e846881
commit 0c65bd9891
8 changed files with 39 additions and 33 deletions

View File

@ -1,37 +1,34 @@
#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) {
size_t next;
next = rbuf->head + 1;
if (next >= rbuf->cap) {
next = 0;
}
if (next == rbuf->tail) {
if (rbuf->count == rbuf->cap) {
return -1;
}
rbuf->buffer[rbuf->head] = data;
rbuf->head = next;
rbuf->head = (rbuf->head + 1) % rbuf->cap;
rbuf->count++;
return 0;
}
int32_t rbuf_pop(RBuf *rbuf, uint8_t *data) {
size_t next;
if (rbuf->head == rbuf->tail) {
if (rbuf->count == 0) {
return -1;
}
next = rbuf->tail + 1;
if (next >= rbuf->cap) {
next = 0;
}
*data = rbuf->buffer[rbuf->tail];
rbuf->tail = next;
rbuf->tail = (rbuf->tail + 1) % rbuf->cap;
rbuf->count--;
return 0;
}

View File

@ -3,15 +3,19 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
typedef struct {
uint8_t *buffer;
size_t head;
size_t tail;
size_t head;
size_t cap;
size_t count;
bool full;
} RBuf;
int32_t rbuf_push(RBuf *rbuf, uint8_t data);
int32_t rbuf_pop(RBuf *rbuf, uint8_t *data);
void rbuf_init(RBuf *rbuf, uint8_t *buf, size_t bufsize);
#endif // RBUF_RBUF_H_