From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56499) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZyNXF-0003N6-L2 for qemu-devel@nongnu.org; Mon, 16 Nov 2015 12:25:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZyNXB-0000Pv-T3 for qemu-devel@nongnu.org; Mon, 16 Nov 2015 12:25:45 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43788) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZyNXB-0000Pi-N4 for qemu-devel@nongnu.org; Mon, 16 Nov 2015 12:25:41 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 586FF935D4 for ; Mon, 16 Nov 2015 17:25:41 +0000 (UTC) From: Gerd Hoffmann Date: Mon, 16 Nov 2015 18:25:20 +0100 Message-Id: <1447694735-3420-6-git-send-email-kraxel@redhat.com> In-Reply-To: <1447694735-3420-1-git-send-email-kraxel@redhat.com> References: <1447694735-3420-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PULL 05/20] buffer: add buffer_shrink List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Gerd Hoffmann Signed-off-by: Gerd Hoffmann Reviewed-by: Peter Lieven Reviewed-by: Daniel P. Berrange Message-id: 1446203414-4013-6-git-send-email-kraxel@redhat.com --- include/qemu/buffer.h | 10 ++++++++++ util/buffer.c | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/qemu/buffer.h b/include/qemu/buffer.h index 1358df1..0a69b3a 100644 --- a/include/qemu/buffer.h +++ b/include/qemu/buffer.h @@ -52,6 +52,16 @@ void buffer_init(Buffer *buffer, const char *name, ...) GCC_FMT_ATTR(2, 3); /** + * buffer_shrink: + * @buffer: the buffer object + * + * Try to shrink the buffer. Checks current buffer capacity and size + * and reduces capacity in case only a fraction of the buffer is + * actually used. + */ +void buffer_shrink(Buffer *buffer); + +/** * buffer_reserve: * @buffer: the buffer object * @len: the minimum required free space 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) { -- 1.8.3.1