From 10fc916425cbd4650bf8c8a56f8c9428bed052d7 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 20 Jul 2014 23:56:12 -0700 Subject: [PATCH] Revise circle buffer types --- src/util/circle-buffer.c | 16 ++++++++++------ src/util/circle-buffer.h | 11 ++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/util/circle-buffer.c b/src/util/circle-buffer.c index f98569be5..bb4a1ead2 100644 --- a/src/util/circle-buffer.c +++ b/src/util/circle-buffer.c @@ -2,13 +2,13 @@ #ifndef NDEBUG static int _checkIntegrity(struct CircleBuffer* buffer) { - if ((int8_t*) buffer->writePtr - (int8_t*) buffer->readPtr == buffer->size) { + if ((int8_t*) buffer->writePtr - (int8_t*) buffer->readPtr == (ssize_t) buffer->size) { return 1; } - if (buffer->capacity - buffer->size == ((int8_t*) buffer->writePtr - (int8_t*) buffer->readPtr)) { + if ((ssize_t) (buffer->capacity - buffer->size) == ((int8_t*) buffer->writePtr - (int8_t*) buffer->readPtr)) { return 1; } - if (buffer->capacity - buffer->size == ((int8_t*) buffer->readPtr - (int8_t*) buffer->writePtr)) { + if ((ssize_t) (buffer->capacity - buffer->size) == ((int8_t*) buffer->readPtr - (int8_t*) buffer->writePtr)) { return 1; } return 0; @@ -26,10 +26,14 @@ void CircleBufferDeinit(struct CircleBuffer* buffer) { buffer->data = 0; } -unsigned CircleBufferSize(const struct CircleBuffer* buffer) { +size_t CircleBufferSize(const struct CircleBuffer* buffer) { return buffer->size; } +size_t CircleBufferCapacity(const struct CircleBuffer* buffer) { + return buffer->capacity; +} + void CircleBufferClear(struct CircleBuffer* buffer) { buffer->size = 0; buffer->readPtr = buffer->data; @@ -140,7 +144,7 @@ int CircleBufferRead32(struct CircleBuffer* buffer, int32_t* value) { return 4; } -int CircleBufferRead(struct CircleBuffer* buffer, void* output, size_t length) { +size_t CircleBufferRead(struct CircleBuffer* buffer, void* output, size_t length) { int8_t* data = buffer->readPtr; if (buffer->size == 0) { return 0; @@ -171,7 +175,7 @@ int CircleBufferRead(struct CircleBuffer* buffer, void* output, size_t length) { return length; } -int CircleBufferDump(const struct CircleBuffer* buffer, void* output, size_t length) { +size_t CircleBufferDump(const struct CircleBuffer* buffer, void* output, size_t length) { int8_t* data = buffer->readPtr; if (buffer->size == 0) { return 0; diff --git a/src/util/circle-buffer.h b/src/util/circle-buffer.h index 2e6ec55d2..776f62253 100644 --- a/src/util/circle-buffer.h +++ b/src/util/circle-buffer.h @@ -5,21 +5,22 @@ struct CircleBuffer { void* data; - unsigned capacity; - unsigned size; + size_t capacity; + size_t size; void* readPtr; void* writePtr; }; void CircleBufferInit(struct CircleBuffer* buffer, unsigned capacity); void CircleBufferDeinit(struct CircleBuffer* buffer); -unsigned CircleBufferSize(const struct CircleBuffer* buffer); +size_t CircleBufferSize(const struct CircleBuffer* buffer); +size_t CircleBufferCapacity(const struct CircleBuffer* buffer); void CircleBufferClear(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); -int CircleBufferRead(struct CircleBuffer* buffer, void* output, size_t length); -int CircleBufferDump(const struct CircleBuffer* buffer, void* output, size_t length); +size_t CircleBufferRead(struct CircleBuffer* buffer, void* output, size_t length); +size_t CircleBufferDump(const struct CircleBuffer* buffer, void* output, size_t length); #endif