diff options
author | Peter Lieven <pl@kamp.de> | 2015-10-30 12:10:13 +0100 |
---|---|---|
committer | Gerd Hoffmann <kraxel@redhat.com> | 2015-11-05 09:09:55 +0100 |
commit | 4ec5ba151ff3f2ac8dc44dabd058eca5846654a6 (patch) | |
tree | 0bb77d9226c4b022f797efb321d9f25da1fbc56c /util | |
parent | fd95243372f7657c2d24aed269e3be01bed1b87c (diff) | |
download | qemu-4ec5ba151ff3f2ac8dc44dabd058eca5846654a6.tar.gz qemu-4ec5ba151ff3f2ac8dc44dabd058eca5846654a6.tar.bz2 qemu-4ec5ba151ff3f2ac8dc44dabd058eca5846654a6.zip |
buffer: factor out buffer_adj_size
Signed-off-by: Peter Lieven <pl@kamp.de>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
Message-id: 1446203414-4013-19-git-send-email-kraxel@redhat.com
Diffstat (limited to 'util')
-rw-r--r-- | util/buffer.c | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/util/buffer.c b/util/buffer.c index 31f1d9f286..fe5a44e708 100644 --- a/util/buffer.c +++ b/util/buffer.c @@ -30,6 +30,14 @@ static size_t buffer_req_size(Buffer *buffer, size_t len) pow2ceil(buffer->offset + len)); } +static void buffer_adj_size(Buffer *buffer, size_t len) +{ + size_t old = buffer->capacity; + buffer->capacity = buffer_req_size(buffer, len); + buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); + trace_buffer_resize(buffer->name ?: "unnamed", + old, buffer->capacity); +} void buffer_init(Buffer *buffer, const char *name, ...) { @@ -42,8 +50,6 @@ void buffer_init(Buffer *buffer, const char *name, ...) void buffer_shrink(Buffer *buffer) { - size_t old; - /* * Only shrink in case the used size is *much* smaller than the * capacity, to avoid bumping up & down the buffers all the time. @@ -54,24 +60,13 @@ void buffer_shrink(Buffer *buffer) return; } - old = buffer->capacity; - buffer->capacity = pow2ceil(buffer->offset); - buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_SHRINK_SIZE); - buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); - trace_buffer_resize(buffer->name ?: "unnamed", - old, buffer->capacity); + buffer_adj_size(buffer, 0); } void buffer_reserve(Buffer *buffer, size_t len) { - size_t old; - if ((buffer->capacity - buffer->offset) < len) { - old = buffer->capacity; - buffer->capacity = buffer_req_size(buffer, len); - buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); - trace_buffer_resize(buffer->name ?: "unnamed", - old, buffer->capacity); + buffer_adj_size(buffer, len); } } |