From patchwork Fri Oct 2 19:09:15 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: 525747 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 D81C81402F0 for ; Sat, 3 Oct 2015 05:32:08 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=G2TYitq5; dkim-atps=neutral Received: from localhost ([::1]:34661 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zi63q-0004W8-Q1 for incoming@patchwork.ozlabs.org; Fri, 02 Oct 2015 15:32:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33961) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zi5in-0007as-8q for qemu-devel@nongnu.org; Fri, 02 Oct 2015 15:10:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zi5ii-0007Ah-Rj for qemu-devel@nongnu.org; Fri, 02 Oct 2015 15:10:21 -0400 Received: from mail-wi0-x22e.google.com ([2a00:1450:400c:c05::22e]:37801) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zi5ii-0007Ac-Lm for qemu-devel@nongnu.org; Fri, 02 Oct 2015 15:10:16 -0400 Received: by wicfx3 with SMTP id fx3so43178693wic.0 for ; Fri, 02 Oct 2015 12:10:16 -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=G2TYitq5Zz+NtON1WqCY70TmUZci5028pl5DwZ6+aMU2DJWZ/u7hllYQFMtveBl3rs XbP1lBK5T8aHnDWzaMvYo5mRa6c2t/gX3T5X4PXNnRSKHM7rB+/KVSQh6VM+nef5jq3L IjG7usgHu4cGdAcauGL0GxYYgMxxOdJNUSmz+4JNyP93hsEnHj6JSIBHOvH2O1O8gHYh fC9P4Fu2i/6f6klkDLcd+KUeQnc6FHp3Q+7Fq0ORJQ+1rMyaJwA0fhBtIpzYHmRAXba1 vO12Dy1KhpoUzOtWpXOMRogCIkGQoYJLa0uG0nBoGVRsfYZuiOttsPeB+SqxTT+YS8fc f7aQ== X-Received: by 10.180.206.45 with SMTP id ll13mr479312wic.6.1443813016164; Fri, 02 Oct 2015 12:10:16 -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 ka10sm12709927wjc.30.2015.10.02.12.10.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Oct 2015 12:10:15 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Fri, 2 Oct 2015 21:09:15 +0200 Message-Id: <1443812991-17356-13-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1443812991-17356-1-git-send-email-marcandre.lureau@redhat.com> References: <1443812991-17356-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: 2a00:1450:400c:c05::22e Cc: 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] [PATCH v5 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 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 *));