From patchwork Tue Oct 6 19:19:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 526947 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:4830:134:3::10]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id B4F55140D89 for ; Wed, 7 Oct 2015 06:34:32 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=K+bQwqnj; dkim-atps=neutral Received: from lists.gnu.org ([208.118.235.17]:60202) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXvC-0005WR-8V for incoming@patchwork.ozlabs.org; Tue, 06 Oct 2015 15:29:10 -0400 Received: from localhost ([::1]:53363 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXvB-0004Ql-TH for incoming@patchwork.ozlabs.org; Tue, 06 Oct 2015 15:29:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59885) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXoB-0001aS-BB for qemu-devel@nongnu.org; Tue, 06 Oct 2015 15:22:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZjXn4-00046S-Uv for qemu-devel@nongnu.org; Tue, 06 Oct 2015 15:21:55 -0400 Received: from mail-qg0-x22e.google.com ([2607:f8b0:400d:c04::22e]:35062) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXmx-0003t2-7S for qemu-devel@nongnu.org; Tue, 06 Oct 2015 15:20:39 -0400 Received: by qgt47 with SMTP id 47so183753945qgt.2 for ; Tue, 06 Oct 2015 12:20:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=LCs8A7V9R9r0RiM9kde5YhflFnEOGNAz2ohb8T18N8Y=; b=K+bQwqnj9gFJkLJDauYrOOCd/CXhwlxCdJ7SVM9Wk39HkVdRI2/aIbDe49j7YWyc+X bw2p1Wd/htjYsRrWtUIpH9KUcJFpc2wLqe26KDesSwW3O5opx6j3BrWBca5ZvZGAj5KH MyejYj9OYUf8KsmI1y1XDRtGLGzwUCIxO1rNMXEFcDRxPPgOndnb9kzSMkLFbzFSkHIC NIApuFOUVO/UhcpXJAdImZffJw+XFr93ZVgvmsktwVFgDELrp0+8zIDslaooQ99pscQ5 qQm38C/AQojLiD5CaFb2+CDkJtDaHnRif/VppA790CD26WpG2oAgYlnO1Vdo3MNJhl2s 7IgQ== X-Received: by 10.140.38.167 with SMTP id t36mr49385569qgt.66.1444159238815; Tue, 06 Oct 2015 12:20:38 -0700 (PDT) Received: from localhost (bne75-h02-31-39-163-232.dsl.sta.abo.bbox.fr. [31.39.163.232]) by smtp.gmail.com with ESMTPSA id l8sm14521250qge.31.2015.10.06.12.20.37 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2015 12:20:37 -0700 (PDT) From: marcandre.lureau@redhat.com To: peter.maydell@linaro.org Date: Tue, 6 Oct 2015 21:19:08 +0200 Message-Id: <1444159184-18153-13-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1444159184-18153-1-git-send-email-marcandre.lureau@redhat.com> References: <1444159184-18153-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::22e Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , qemu-devel@nongnu.org Subject: [Qemu-devel] [PULL 12/48] ivshmem: simplify around increase_dynamic_storage() 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 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x From: Marc-André Lureau Set the number of peers and array allocation in a single place. Rename to better reflect the function content. Signed-off-by: Marc-André Lureau Reviewed-by: Claudio Fontana --- hw/misc/ivshmem.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 3787398..6f41960 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -417,30 +417,28 @@ static void close_guest_eventfds(IVShmemState *s, int posn) /* this function increase the dynamic storage need to store data about other * guests */ -static int increase_dynamic_storage(IVShmemState *s, int new_min_size) +static int resize_peers(IVShmemState *s, int new_min_size) { - int j, old_nb_alloc; + int j, old_size; /* limit number of max peers */ if (new_min_size <= 0 || new_min_size > IVSHMEM_MAX_PEERS) { return -1; } - - old_nb_alloc = s->nb_peers; - - if (new_min_size >= s->nb_peers) { - /* +1 because #new_min_size is used as last array index */ - s->nb_peers = new_min_size + 1; - } else { + if (new_min_size <= s->nb_peers) { return 0; } + old_size = s->nb_peers; + s->nb_peers = new_min_size; + IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers); + s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer)); /* zero out new pointers */ - for (j = old_nb_alloc; j < s->nb_peers; j++) { + for (j = old_size; j < s->nb_peers; j++) { s->peers[j].eventfds = NULL; s->peers[j].nb_eventfds = 0; } @@ -508,8 +506,8 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size) /* make sure we have enough space for this guest */ if (incoming_posn >= s->nb_peers) { - if (increase_dynamic_storage(s, incoming_posn) < 0) { - error_report("increase_dynamic_storage() failed"); + if (resize_peers(s, incoming_posn + 1) < 0) { + error_report("failed to resize peers array"); if (incoming_fd != -1) { close(incoming_fd); } @@ -812,12 +810,9 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp) } /* we allocate enough space for 16 guests and grow as needed */ - s->nb_peers = 16; + resize_peers(s, 16); s->vm_id = -1; - /* allocate/initialize space for interrupt handling */ - s->peers = g_malloc0(s->nb_peers * sizeof(Peer)); - pci_register_bar(dev, 2, attr, &s->bar); s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));