aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorGerd Hoffmann <kraxel@redhat.com>2015-10-30 12:10:00 +0100
committerGerd Hoffmann <kraxel@redhat.com>2015-11-05 09:08:41 +0100
commit1ff36b5d4d00a4e3633eb960bf2be562f5e47dbf (patch)
tree4534dbf57bfeaf9611baa4b1041282a48c81b138 /util
parent830a9583206a051c240b74c3f688a015dc5d2967 (diff)
downloadqemu-1ff36b5d4d00a4e3633eb960bf2be562f5e47dbf.zip
qemu-1ff36b5d4d00a4e3633eb960bf2be562f5e47dbf.tar.gz
qemu-1ff36b5d4d00a4e3633eb960bf2be562f5e47dbf.tar.bz2
buffer: add buffer_shrink
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Reviewed-by: Peter Lieven <pl@kamp.de> Reviewed-by: Daniel P. Berrange <berrange@redhat.com> Message-id: 1446203414-4013-6-git-send-email-kraxel@redhat.com
Diffstat (limited to 'util')
-rw-r--r--util/buffer.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/util/buffer.c b/util/buffer.c
index e8f798e..234e33d 100644
--- a/util/buffer.c
+++ b/util/buffer.c
@@ -20,7 +20,8 @@
#include "qemu/buffer.h"
-#define BUFFER_MIN_INIT_SIZE 4096
+#define BUFFER_MIN_INIT_SIZE 4096
+#define BUFFER_MIN_SHRINK_SIZE 65536
void buffer_init(Buffer *buffer, const char *name, ...)
{
@@ -31,6 +32,23 @@ void buffer_init(Buffer *buffer, const char *name, ...)
va_end(ap);
}
+void buffer_shrink(Buffer *buffer)
+{
+ /*
+ * Only shrink in case the used size is *much* smaller than the
+ * capacity, to avoid bumping up & down the buffers all the time.
+ * realloc() isn't exactly cheap ...
+ */
+ if (buffer->offset < (buffer->capacity >> 3) &&
+ buffer->capacity > BUFFER_MIN_SHRINK_SIZE) {
+ return;
+ }
+
+ buffer->capacity = pow2ceil(buffer->offset);
+ buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_SHRINK_SIZE);
+ buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
+}
+
void buffer_reserve(Buffer *buffer, size_t len)
{
if ((buffer->capacity - buffer->offset) < len) {