From patchwork Mon Nov 16 17:25:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 545305 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3E5BF141462 for ; Tue, 17 Nov 2015 11:44:17 +1100 (AEDT) Received: from localhost ([::1]:53305 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZyUNb-0007le-8X for incoming@patchwork.ozlabs.org; Mon, 16 Nov 2015 19:44:15 -0500 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) Received: from nilsson.home.kraxel.org (ovpn-116-57.ams2.redhat.com [10.36.116.57]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tAGHPeg1018733; Mon, 16 Nov 2015 12:25:40 -0500 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id 0C18E81948; Mon, 16 Nov 2015 18:25:38 +0100 (CET) From: Gerd Hoffmann To: qemu-devel@nongnu.org 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> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Gerd Hoffmann Subject: [Qemu-devel] [PULL 05/20] buffer: add buffer_shrink X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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) {