aboutsummaryrefslogtreecommitdiff
path: root/src/fifo_buffer.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2011-11-27 20:58:57 +0100
committerMax Kellermann <max@duempel.org>2011-11-28 07:45:15 +0100
commitf5468493521c0fd081dc63f920eaf613f746358c (patch)
tree0abe89cf0286e4f5c72dc54f6531aa9f23abb363 /src/fifo_buffer.c
parenta85af593f170f4081b44157d82e746ddc12cdc91 (diff)
fifo_buffer: add function fifo_buffer_realloc()
For growing FIFO buffers.
Diffstat (limited to 'src/fifo_buffer.c')
-rw-r--r--src/fifo_buffer.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/src/fifo_buffer.c b/src/fifo_buffer.c
index 65cb2ffd..915fb057 100644
--- a/src/fifo_buffer.c
+++ b/src/fifo_buffer.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2010 The Music Player Daemon Project
+ * Copyright (C) 2003-2011 The Music Player Daemon Project
* http://www.musicpd.org
*
* Redistribution and use in source and binary forms, with or without
@@ -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)
{