Files
cAI/cKit/Foundation/c_FastByteRingBuffer.c
T

136 lines
3.8 KiB
C
Raw Normal View History

2026-08-10 01:21:15 +08:00
#include <c_FastByteRingBuffer.h>
#include <c_Memory.h>
#include <c_Alignment.h>
C_STATIC_FORCE_INLINE
c_size_t round_up_to_pow2(c_size_t v) {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
#if (defined(__WORDSIZE) && __WORDSIZE == 64) || defined(_WIN64) || defined(__x86_64__) || defined(__aarch64__)
v |= v >> 32;
#endif
v++;
return v;
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
c_err_t c_FastByteRingBuffer_Init(c_FastByteRingBuffer_t* self, c_size_t capacity) {
if (!self || capacity == 0) return C_ERR_PARAM;
// 自動向上對齊,確保符合 Power of Two
self->capacity = round_up_to_pow2(capacity);
self->mask = self->capacity - 1; // 建立遮罩
self->head = 0;
self->tail = 0;
self->is_full = C_FALSE;
self->buffer = (uint8_t*)malloc(self->capacity);
if (!self->buffer) {
self->capacity = 0;
self->mask = 0;
return C_ERR_NOMEM;
}
return C_ERR_SUCCESS;
}
void c_FastByteRingBuffer_Destroy(c_FastByteRingBuffer_t* self) {
if (!self) return;
C_FREE(self->buffer);
self->capacity = 0;
self->mask = 0;
self->head = 0;
self->tail = 0;
self->is_full = C_FALSE;
}
// 寫入單一單元組 (極速 O(1))
c_err_t c_FastByteRingBuffer_WriteByte(c_FastByteRingBuffer_t* self, uint8_t byte) {
if (!self || !self->buffer) return C_ERR_PARAM;
if (self->is_full) return C_ERR_FULL;
self->buffer[self->tail] = byte;
// 使用高速位元與運算取代模除 %
self->tail = (self->tail + 1) & self->mask;
if (self->tail == self->head) {
self->is_full = C_TRUE;
}
return C_ERR_SUCCESS;
}
// 讀取單一單元組 (極速 O(1))
c_err_t c_FastByteRingBuffer_ReadByte(c_FastByteRingBuffer_t* self, uint8_t* out_byte) {
if (!self || !self->buffer || !out_byte) return C_ERR_PARAM;
if (c_FastByteRingBuffer_IsEmpty(self)) return C_ERR_EMPTY;
*out_byte = self->buffer[self->head];
// 使用高速位元與運算取代模除 %
self->head = (self->head + 1) & self->mask;
self->is_full = C_FALSE;
return C_ERR_SUCCESS;
}
// 區塊寫入
c_size_t c_FastByteRingBuffer_WriteBuffer(c_FastByteRingBuffer_t* self, const uint8_t* src, c_size_t len) {
if (!self || !self->buffer || !src || len == 0) return 0;
c_size_t bytes_written = 0;
while (bytes_written < len && !self->is_full) {
self->buffer[self->tail] = src[bytes_written];
self->tail = (self->tail + 1) & self->mask;
if (self->tail == self->head) {
self->is_full = C_TRUE;
}
bytes_written++;
}
return bytes_written;
}
// 區塊讀取
c_size_t c_FastByteRingBuffer_ReadBuffer(c_FastByteRingBuffer_t* self, uint8_t* dest, c_size_t len) {
if (!self || !self->buffer || !dest || len == 0) return 0;
c_size_t bytes_read = 0;
while (bytes_read < len && !c_FastByteRingBuffer_IsEmpty(self)) {
dest[bytes_read] = self->buffer[self->head];
self->head = (self->head + 1) & self->mask;
self->is_full = C_FALSE;
bytes_read++;
}
return bytes_read;
}
c_size_t c_FastByteRingBuffer_GetSize(const c_FastByteRingBuffer_t* self) {
if (!self || !self->buffer) return 0;
if (self->is_full) return self->capacity;
if (self->tail >= self->head) {
return self->tail - self->head;
} else {
return self->capacity + self->tail - self->head;
}
}
c_bool_t c_FastByteRingBuffer_IsEmpty(const c_FastByteRingBuffer_t* self) {
if (!self) return C_TRUE;
return (self->head == self->tail) && !self->is_full;
}
c_bool_t c_FastByteRingBuffer_IsFull(const c_FastByteRingBuffer_t* self) {
if (!self) return C_FALSE;
return self->is_full;
}