buffer: factor out buffer_req_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-18-git-send-email-kraxel@redhat.com
This commit is contained in:
Peter Lieven 2015-10-30 12:10:12 +01:00 committed by Gerd Hoffmann
parent c3d6899c5e
commit fd95243372

View File

@ -24,6 +24,13 @@
#define BUFFER_MIN_INIT_SIZE 4096
#define BUFFER_MIN_SHRINK_SIZE 65536
static size_t buffer_req_size(Buffer *buffer, size_t len)
{
return MAX(BUFFER_MIN_INIT_SIZE,
pow2ceil(buffer->offset + len));
}
void buffer_init(Buffer *buffer, const char *name, ...)
{
va_list ap;
@ -61,8 +68,7 @@ void buffer_reserve(Buffer *buffer, size_t len)
if ((buffer->capacity - buffer->offset) < len) {
old = buffer->capacity;
buffer->capacity = pow2ceil(buffer->offset + len);
buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_INIT_SIZE);
buffer->capacity = buffer_req_size(buffer, len);
buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
trace_buffer_resize(buffer->name ?: "unnamed",
old, buffer->capacity);