diff options
Diffstat (limited to '')
-rw-r--r-- | src/fifo_buffer.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/fifo_buffer.c b/src/fifo_buffer.c index e5735f5eb..915fb0579 100644 --- a/src/fifo_buffer.c +++ b/src/fifo_buffer.c @@ -58,6 +58,39 @@ fifo_buffer_new(size_t size) return buffer; } +static void +fifo_buffer_move(struct fifo_buffer *buffer); + +struct fifo_buffer * +fifo_buffer_realloc(struct fifo_buffer *buffer, size_t new_size) +{ + if (buffer == NULL) + return new_size > 0 + ? fifo_buffer_new(new_size) + : NULL; + + /* existing data must fit in new size */ + assert(new_size >= buffer->end - buffer->start); + + if (new_size == 0) { + fifo_buffer_free(buffer); + return NULL; + } + + /* compress the buffer when we're shrinking and the tail of + the buffer would exceed the new size */ + if (buffer->end > new_size) + fifo_buffer_move(buffer); + + /* existing data must fit in new size: second check */ + assert(buffer->end <= new_size); + + buffer = g_realloc(buffer, sizeof(*buffer) - sizeof(buffer->buffer) + + new_size); + buffer->size = new_size; + return buffer; +} + void fifo_buffer_free(struct fifo_buffer *buffer) { @@ -66,6 +99,22 @@ fifo_buffer_free(struct fifo_buffer *buffer) g_free(buffer); } +size_t +fifo_buffer_capacity(const struct fifo_buffer *buffer) +{ + assert(buffer != NULL); + + return buffer->size; +} + +size_t +fifo_buffer_available(const struct fifo_buffer *buffer) +{ + assert(buffer != NULL); + + return buffer->end - buffer->start; +} + void fifo_buffer_clear(struct fifo_buffer *buffer) { |