From patchwork Wed Aug 3 17:04:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 9261631 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5C8676048B for ; Wed, 3 Aug 2016 17:04:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E42127C2D for ; Wed, 3 Aug 2016 17:04:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3007727F10; Wed, 3 Aug 2016 17:04:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7920D27C2D for ; Wed, 3 Aug 2016 17:04:45 +0000 (UTC) Received: from localhost ([::1]:35901 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUzax-0006V6-Ec for patchwork-qemu-devel@patchwork.kernel.org; Wed, 03 Aug 2016 13:04:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50543) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUzab-0006Uw-SL for qemu-devel@nongnu.org; Wed, 03 Aug 2016 13:04:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bUzaX-0001nn-P5 for qemu-devel@nongnu.org; Wed, 03 Aug 2016 13:04:17 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:35663) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUzaX-0001nU-I1 for qemu-devel@nongnu.org; Wed, 03 Aug 2016 13:04:13 -0400 Received: by mail-wm0-x243.google.com with SMTP id i5so37290693wmg.2 for ; Wed, 03 Aug 2016 10:04:13 -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; bh=ShBGgXF6t/93QxL35fVGlotN+ZgbgZzCV7HIS6Z6sHM=; b=VQmgn41FgZpl3x+UNP+70oN4AJ0YxTSw/3Y1+Kpkb515QDTeiUM8sMvA8OrhF45KTP hMqfO0/uEOBwyhKVzaxCbOvrbQEPAJ6W6zR8fiUW4WIBYIQ41K6MxF73TE2RhcDQIDsy tVVTcA2L+DkXDiNzzyH4hafx9RxYeYc7NcTmQ3s+adqrqM/Ao+nl4jH0bPPmU23kv1Zt I8FgQ5UwuV/Jq06lxDjhHZr0s9f1OzVdsAQPtMWYXihk7v/z48cKh/Z5Wd5zCRUwp1hT k+z8UluH5x1qRw0KiIl9aykrStH4Km7S+MLn00hBTx87vgPk9Mr/YjMAg1pvmzFvVcjW 3CmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=ShBGgXF6t/93QxL35fVGlotN+ZgbgZzCV7HIS6Z6sHM=; b=AaVevnXTyiwgbERa8O/dDfV46f3MkQOpjFq335ZDGL4JPnnWjgo2aT39SVYishZCJT 04Pvs3dJU8qTkBtzlRaOhcmxV0dXT3UkQT48xWpHj7YkBcot1q4a9MRGGmTbQYWTsf5H ICqOV4bEzBnqSnxElvivyJssQo0LMlqxIzdVptFuFucbItOBQ/9HLbyBRxxGjoED51kA SlfMpynd8UvywfgtrMGCEOXiE4oDAi74mXDbKUe3gZ2ryted66sn+YXzuBME1kj/5VVV oeCx/gK952KawVU3IfT9a1La4JV7bPAGti/zvm2tMbQKiySRPfiCzuef7S7OoWv/7ZKM uELw== X-Gm-Message-State: AEkoouvWQLfIO8pC8NBkW4Pjc2uhpb+82SxmFKIP4+lz6n1fRutqkWcw3gyfGKAL5w8tCQ== X-Received: by 10.28.223.139 with SMTP id w133mr26081299wmg.90.1470243852581; Wed, 03 Aug 2016 10:04:12 -0700 (PDT) Received: from donizetti.redhat.com (94-39-152-88.adsl-ull.clienti.tiscali.it. [94.39.152.88]) by smtp.gmail.com with ESMTPSA id bj8sm8581680wjc.49.2016.08.03.10.04.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Aug 2016 10:04:11 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 3 Aug 2016 19:04:08 +0200 Message-Id: <1470243848-11763-2-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1470243848-11763-1-git-send-email-pbonzini@redhat.com> References: <1470243848-11763-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PULL 11/25] qdist: use g_renew and g_new instead of g_realloc and g_malloc. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Emilio G. Cota" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: "Emilio G. Cota" This is safer against overflow. g_renew is available in all version of glib, while g_realloc_n is only available in 2.24. Signed-off-by: Emilio G. Cota Message-Id: <1469459025-23606-3-git-send-email-cota@braap.org> [Rewritten to use g_new/g_renew. - Paolo] Signed-off-by: Paolo Bonzini --- util/qdist.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/util/qdist.c b/util/qdist.c index eb2236c..e95722b 100644 --- a/util/qdist.c +++ b/util/qdist.c @@ -16,7 +16,7 @@ void qdist_init(struct qdist *dist) { - dist->entries = g_malloc(sizeof(*dist->entries)); + dist->entries = g_new(struct qdist_entry, 1); dist->size = 1; dist->n = 0; } @@ -62,8 +62,7 @@ void qdist_add(struct qdist *dist, double x, long count) if (unlikely(dist->n == dist->size)) { dist->size *= 2; - dist->entries = g_realloc(dist->entries, - sizeof(*dist->entries) * (dist->size)); + dist->entries = g_renew(struct qdist_entry, dist->entries, dist->size); } dist->n++; entry = &dist->entries[dist->n - 1]; @@ -188,7 +187,7 @@ void qdist_bin__internal(struct qdist *to, const struct qdist *from, size_t n) } } /* they're equally spaced, so copy the dist and bail out */ - to->entries = g_realloc_n(to->entries, n, sizeof(*to->entries)); + to->entries = g_renew(struct qdist_entry, to->entries, n); to->n = from->n; memcpy(to->entries, from->entries, sizeof(*to->entries) * to->n); return;