Skip to content

Commit

Permalink
buffer: make the Buffer capacity increase in powers of two
Browse files Browse the repository at this point in the history
This makes sure the number of reallocs is in O(log N).

Signed-off-by: Peter Lieven <pl@kamp.de>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Message-id: 1446203414-4013-2-git-send-email-kraxel@redhat.com

[ rebased to util/buffer.c ]

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
  • Loading branch information
plieven authored and kraxel committed Nov 5, 2015
1 parent 79cf9fa commit 5c10dbb
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion util/buffer.c
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 5c10dbb

Please sign in to comment.