From mboxrd@z Thu Jan 1 00:00:00 1970 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 From: Murilo Opsfelder Araujo Date: Tue, 15 May 2018 10:49:50 -0300 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20180515134950.3755-1-muriloo@linux.ibm.com> Subject: [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Murilo Opsfelder Araujo , David Hildenbrand , Eduardo Habkost , Markus Armbruster , Paolo Bonzini 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 --- 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 failure, 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. +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 = 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. -- 2.14.3