From nobody Sat Apr 20 12:30:34 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.ibm.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1526392344212118.74083024190043; Tue, 15 May 2018 06:52:24 -0700 (PDT) Received: from localhost ([::1]:39280 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fIaNL-0004Yq-F5 for importer@patchew.org; Tue, 15 May 2018 09:52:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34045) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fIaLx-00042q-WF for qemu-devel@nongnu.org; Tue, 15 May 2018 09:50:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fIaLu-0006Xk-R3 for qemu-devel@nongnu.org; Tue, 15 May 2018 09:50:58 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:37614) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fIaLu-0006XM-Hs for qemu-devel@nongnu.org; Tue, 15 May 2018 09:50:54 -0400 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w4FDkSbS036309 for ; Tue, 15 May 2018 09:50:52 -0400 Received: from e31.co.us.ibm.com (e31.co.us.ibm.com [32.97.110.149]) by mx0a-001b2d01.pphosted.com with ESMTP id 2hywq1s7cy-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 15 May 2018 09:50:52 -0400 Received: from localhost by e31.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 15 May 2018 07:50:51 -0600 Received: from b03cxnp08025.gho.boulder.ibm.com (9.17.130.17) by e31.co.us.ibm.com (192.168.1.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 15 May 2018 07:50:48 -0600 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w4FDolFc22610190; Tue, 15 May 2018 06:50:47 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D786278043; Tue, 15 May 2018 07:50:47 -0600 (MDT) Received: from kermit-br-ibm-com.br.ibm.com (unknown [9.18.235.183]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP id 9FA897803F; Tue, 15 May 2018 07:50:46 -0600 (MDT) From: Murilo Opsfelder Araujo To: qemu-devel@nongnu.org Date: Tue, 15 May 2018 10:49:50 -0300 X-Mailer: git-send-email 2.14.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 x-cbid: 18051513-8235-0000-0000-00000D7FAA0E X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00009029; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000260; SDB=6.01032669; UDB=6.00527955; IPR=6.00811800; MB=3.00021126; MTD=3.00000008; XFM=3.00000015; UTC=2018-05-15 13:50:50 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18051513-8236-0000-0000-000040F77CF5 Message-Id: <20180515134950.3755-1-muriloo@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-05-15_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1805150140 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of 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: Markus Armbruster , David Hildenbrand , Eduardo Habkost , Paolo Bonzini , Murilo Opsfelder Araujo Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 This patch documents the preference for g_new instead of g_malloc. The reasons were adapted from commit b45c03f585ea9bb1af76c73e82195418c294919d. Discussion in QEMU's mailing list: http://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg03238.html Cc: qemu-devel@nongnu.org Cc: David Hildenbrand Cc: Eduardo Habkost Cc: Markus Armbruster Cc: Paolo Bonzini Signed-off-by: Murilo Opsfelder Araujo Reviewed-by: Alex Benn=C3=A9e Reviewed-by: David Hildenbrand Reviewed-by: Eric Blake --- HACKING | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/HACKING b/HACKING index 4125c97d8d..0fc3e0fc04 100644 --- a/HACKING +++ b/HACKING @@ -118,6 +118,15 @@ Please note that g_malloc will exit on allocation fail= ure, so there is no need to test for failure (as you would have to with malloc). Calling g_malloc with a zero size is valid and will return NULL. =20 +Prefer g_new(T, n) instead of g_malloc(sizeof(T) * n) for the following +reasons: + + a. It catches multiplication overflowing size_t; + b. It returns T * instead of void *, letting compiler catch more type + errors. + +Declarations like T *v =3D g_malloc(sizeof(*v)) are acceptable, though. + Memory allocated by qemu_memalign or qemu_blockalign must be freed with qemu_vfree, since breaking this will cause problems on Win32. =20 --=20 2.14.3