qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 01/19] buffer: make the Buffer capacity increase in


From: Gerd Hoffmann
Subject: [Qemu-devel] [PATCH 01/19] buffer: make the Buffer capacity increase in powers of two
Date: Fri, 30 Oct 2015 12:09:56 +0100

From: Peter Lieven <address@hidden>

This makes sure the number of reallocs is in O(log N).

Signed-off-by: Peter Lieven <address@hidden>

[ rebased to util/buffer.c ]

Signed-off-by: Gerd Hoffmann <address@hidden>
---
 util/buffer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/util/buffer.c b/util/buffer.c
index cedd055..7ddd693 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);
     }
 }
-- 
1.8.3.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]