From: Peter Lieven Date: Fri, 30 Oct 2015 11:09:56 +0000 (+0100) Subject: buffer: make the Buffer capacity increase in powers of two X-Git-Tag: qemu-xen-4.8.0-rc1~681^2~19 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=5c10dbb7b577370e86ff459973b06d530c3777cf;p=qemu-xen.git buffer: make the Buffer capacity increase in powers of two This makes sure the number of reallocs is in O(log N). Signed-off-by: Peter Lieven Reviewed-by: Daniel P. Berrange Signed-off-by: Gerd Hoffmann Message-id: 1446203414-4013-2-git-send-email-kraxel@redhat.com [ rebased to util/buffer.c ] Signed-off-by: Gerd Hoffmann --- diff --git a/util/buffer.c b/util/buffer.c index cedd055680..7ddd693fa8 100644 --- a/util/buffer.c +++ b/util/buffer.c @@ -20,10 +20,13 @@ #include "qemu/buffer.h" +#define BUFFER_MIN_INIT_SIZE 4096 + void buffer_reserve(Buffer *buffer, size_t len) { if ((buffer->capacity - buffer->offset) < len) { - buffer->capacity += (len + 1024); + buffer->capacity = pow2ceil(buffer->offset + len); + buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_INIT_SIZE); buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); } }