Fix circle buffers
Jeffrey Pfau jeffrey@endrift.com
Thu, 03 Oct 2013 01:25:51 -0700
2 files changed,
27 insertions(+),
11 deletions(-)
M
src/util/circle-buffer.c
→
src/util/circle-buffer.c
@@ -6,6 +6,7 @@
void CircleBufferInit(struct CircleBuffer* buffer, unsigned capacity) { buffer->data = malloc(capacity); buffer->capacity = capacity; + buffer->size = 0; buffer->readPtr = buffer->data; buffer->writePtr = buffer->data; }@@ -16,19 +17,29 @@ buffer->data = 0;
} unsigned CircleBufferSize(const struct CircleBuffer* buffer) { - ptrdiff_t size = (int8_t*) buffer->writePtr - (int8_t*) buffer->readPtr; - if (size < 0) { - return buffer->capacity - size; - } - return size; + return buffer->size; } -int CircleBufferWrite32(struct CircleBuffer* buffer, int32_t value) { - uint32_t* data = buffer->writePtr; - if ((int8_t*) buffer->writePtr + 1 == buffer->readPtr) { +int CircleBufferWrite8(struct CircleBuffer* buffer, int8_t value) { + int8_t* data = buffer->writePtr; + if (buffer->size + sizeof(int8_t) > buffer->capacity) { return 0; } - if ((int8_t*) buffer->writePtr == (int8_t*) buffer->data + buffer->capacity - 1 && buffer->readPtr == buffer->data) { + *data = value; + ++data; + ptrdiff_t size = (int8_t*) data - (int8_t*) buffer->data; + if (size < buffer->capacity) { + buffer->writePtr = data; + } else { + buffer->writePtr = buffer->data; + } + buffer->size += sizeof(int8_t); + return 1; +} + +int CircleBufferWrite32(struct CircleBuffer* buffer, int32_t value) { + int32_t* data = buffer->writePtr; + if (buffer->size + sizeof(int32_t) > buffer->capacity) { return 0; } *data = value;@@ -39,12 +50,13 @@ buffer->writePtr = data;
} else { buffer->writePtr = buffer->data; } + buffer->size += sizeof(int32_t); return 1; } int CircleBufferRead8(struct CircleBuffer* buffer, int8_t* value) { int8_t* data = buffer->readPtr; - if (buffer->readPtr == buffer->writePtr) { + if (buffer->size < sizeof(int8_t)) { return 0; } *value = *data;@@ -55,12 +67,13 @@ buffer->readPtr = data;
} else { buffer->readPtr = buffer->data; } + buffer->size -= sizeof(int8_t); return 1; } int CircleBufferRead32(struct CircleBuffer* buffer, int32_t* value) { int32_t* data = buffer->readPtr; - if (buffer->readPtr == buffer->writePtr) { + if (buffer->size < sizeof(int32_t)) { return 0; } *value = *data;@@ -71,5 +84,6 @@ buffer->readPtr = data;
} else { buffer->readPtr = buffer->data; } + buffer->size -= sizeof(int32_t); return 1; }
M
src/util/circle-buffer.h
→
src/util/circle-buffer.h
@@ -6,6 +6,7 @@
struct CircleBuffer { void* data; unsigned capacity; + unsigned size; void* readPtr; void* writePtr; };@@ -13,6 +14,7 @@
void CircleBufferInit(struct CircleBuffer* buffer, unsigned capacity); void CircleBufferDeinit(struct CircleBuffer* buffer); unsigned CircleBufferSize(const struct CircleBuffer* buffer); +int CircleBufferWrite8(struct CircleBuffer* buffer, int8_t value); int CircleBufferWrite32(struct CircleBuffer* buffer, int32_t value); int CircleBufferRead8(struct CircleBuffer* buffer, int8_t* value); int CircleBufferRead32(struct CircleBuffer* buffer, int32_t* value);