core: add ringbuffer data structure

This commit is contained in:
2025-07-28 22:13:41 +01:00
parent 5d6423057a
commit 5bac4db7ed
3 changed files with 413 additions and 1 deletions

View File

@@ -0,0 +1,52 @@
#ifndef BLUELIB_CORE_RINGBUFFER_H_
#define BLUELIB_CORE_RINGBUFFER_H_
#include <blue/core/misc.h>
#include <blue/core/status.h>
typedef enum b_ringbuffer_flags {
B_RINGBUFFER_SELF_MALLOC = 0x01u,
B_RINGBUFFER_BUFFER_MALLOC = 0x02u,
B_RINGBUFFER_READ_LOCKED = 0x04u,
B_RINGBUFFER_WRITE_LOCKED = 0x08u,
} b_ringbuffer_flags;
typedef struct b_ringbuffer {
b_ringbuffer_flags r_flags;
void *r_buf, *r_opened_buf;
unsigned long r_capacity, r_opened_capacity;
unsigned long r_write_ptr, r_read_ptr;
} b_ringbuffer;
BLUE_API b_ringbuffer *b_ringbuffer_create(size_t capacity);
BLUE_API b_ringbuffer *b_ringbuffer_create_with_buffer(void *ptr, size_t capacity);
BLUE_API b_status b_ringbuffer_init(b_ringbuffer *buf, size_t capacity);
BLUE_API b_status b_ringbuffer_init_with_buffer(
b_ringbuffer *buf, void *ptr, size_t capacity);
BLUE_API b_status b_ringbuffer_reset(b_ringbuffer *buf);
BLUE_API b_status b_ringbuffer_destroy(b_ringbuffer *buf);
BLUE_API b_status b_ringbuffer_read(
b_ringbuffer *buf, void *p, size_t count, size_t *nr_read);
BLUE_API b_status b_ringbuffer_write(
b_ringbuffer *buf, const void *p, size_t count, size_t *nr_written);
BLUE_API int b_ringbuffer_getc(b_ringbuffer *buf);
BLUE_API b_status b_ringbuffer_putc(b_ringbuffer *buf, int c);
BLUE_API size_t b_ringbuffer_write_capacity_remaining(const b_ringbuffer *buf);
BLUE_API size_t b_ringbuffer_available_data_remaining(const b_ringbuffer *buf);
BLUE_API b_status b_ringbuffer_open_read_buffer(
b_ringbuffer *buf, void **ptr, size_t *length);
BLUE_API b_status b_ringbuffer_close_read_buffer(
b_ringbuffer *buf, void **ptr, size_t nr_read);
BLUE_API b_status b_ringbuffer_open_write_buffer(
b_ringbuffer *buf, void **ptr, size_t *capacity);
BLUE_API b_status b_ringbuffer_close_write_buffer(
b_ringbuffer *buf, void **ptr, size_t nr_written);
#endif

View File

@@ -3,7 +3,7 @@
#include <blue/core/misc.h>
#define B_OK(status) ((status) == B_SUCCESS)
#define B_OK(status) ((enum b_status)((uintptr_t)(status)) == B_SUCCESS)
#define B_ERR(status) ((status) != B_SUCCESS)
typedef enum b_status {
@@ -16,14 +16,18 @@ typedef enum b_status {
B_ERR_BAD_STATE,
B_ERR_NO_ENTRY,
B_ERR_NO_DATA,
B_ERR_NO_SPACE,
B_ERR_UNKNOWN_FUNCTION,
B_ERR_BAD_FORMAT,
B_ERR_IO_FAILURE,
B_ERR_IS_DIRECTORY,
B_ERR_NOT_DIRECTORY,
B_ERR_PERMISSION_DENIED,
B_ERR_BUSY,
B_ERR_COMPRESSION_FAILURE,
} b_status;
BLUE_API const char *b_status_to_string(b_status status);
BLUE_API const char *b_status_description(b_status status);
#endif