From patchwork Mon Oct 12 16:41:07 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: 529208 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 A3A4D1402B0 for ; Tue, 13 Oct 2015 03:47:35 +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=XzJ6AvAC; dkim-atps=neutral Received: from localhost ([::1]:56644 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZlgG5-00067D-DX for incoming@patchwork.ozlabs.org; Mon, 12 Oct 2015 12:47:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60513) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZlgDy-0002dG-5n for qemu-devel@nongnu.org; Mon, 12 Oct 2015 12:45:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZlgDu-0001mE-Vt for qemu-devel@nongnu.org; Mon, 12 Oct 2015 12:45:22 -0400 Received: from mail-pa0-x235.google.com ([2607:f8b0:400e:c03::235]:35017) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZlgDu-0001m7-Qy for qemu-devel@nongnu.org; Mon, 12 Oct 2015 12:45:18 -0400 Received: by pabve7 with SMTP id ve7so100498884pab.2 for ; Mon, 12 Oct 2015 09:45:18 -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=XzJ6AvACLCh7MwN4yozHbO8GDPZPdqj1vzXjYPvBo7PchsCpaxaR2+6rHLthyqDjlO N4ngHMVsAUIspZl5P2X7mX/ZPlfejjRrGN769BqchT3DCdCdtdBdfzW7ECleEgjnxT92 oZYawF0thv7PcF7dW7s+ZqpUP6CHIMOosfpeWvZmanrpDLUcT3KfiUb8QPOJuKAsS3pD rV1c+FE5Qy1nNTm+TydviXq/aprKKih5nuo3ow1YCMkJkxvizXkTn59u+RxENuxjWfS9 LjxMfUIRH7CMFrmvn1mhTMGTmmGo/zM+hKxxufnfA0oXN+UwyRDp1IYylQbOce/fpgV5 PerA== X-Received: by 10.68.189.69 with SMTP id gg5mr35730046pbc.118.1444668318442; Mon, 12 Oct 2015 09:45:18 -0700 (PDT) Received: from localhost (APoitiers-257-1-35-234.w90-38.abo.wanadoo.fr. [90.38.210.234]) by smtp.gmail.com with ESMTPSA id xd10sm19404107pab.25.2015.10.12.09.45.16 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Oct 2015 09:45:17 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Mon, 12 Oct 2015 18:41:07 +0200 Message-Id: <1444668104-22955-14-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1444668104-22955-1-git-send-email-marcandre.lureau@redhat.com> References: <1444668104-22955-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:400e:c03::235 Cc: peter.maydell@linaro.org, drjones@redhat.com, claudio.fontana@huawei.com, stefanha@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , pbonzini@redhat.com, cam@cs.ualberta.ca Subject: [Qemu-devel] [PULL v2 13/50] 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 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 *));