diff mbox

[1/4] vnc: make the Buffer capacity increase in powers of two

Message ID 1440670734-5616-2-git-send-email-pl@kamp.de
State New
Headers show

Commit Message

Peter Lieven Aug. 27, 2015, 10:18 a.m. UTC
This makes sure the number of reallocs is in O(log N).

Signed-off-by: Peter Lieven <pl@kamp.de>
---
 ui/vnc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Gerd Hoffmann Sept. 3, 2015, 8:56 a.m. UTC | #1
On Do, 2015-08-27 at 12:18 +0200, Peter Lieven wrote:
> This makes sure the number of reallocs is in O(log N).

Looks good, applied.
diff mbox

Patch

diff --git a/ui/vnc.c b/ui/vnc.c
index caf82f5..8cfd2d8 100644
--- a/ui/vnc.c
+++ b/ui/vnc.c
@@ -653,10 +653,13 @@  void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
     vnc_write_s32(vs, encoding);
 }
 
+#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->capacity + len);
+        buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_INIT_SIZE);
         buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
     }
 }