[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] HACKING: document preference for g_new instead

From: Eric Blake
Subject: Re: [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc
Date: Tue, 15 May 2018 13:11:39 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0

adding qemu-trivial as a potential tree for this to go through

On 05/15/2018 08:49 AM, Murilo Opsfelder Araujo wrote:
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:

Cc: address@hidden
Cc: David Hildenbrand <address@hidden>
Cc: Eduardo Habkost <address@hidden>
Cc: Markus Armbruster <address@hidden>
Cc: Paolo Bonzini <address@hidden>
Signed-off-by: Murilo Opsfelder Araujo <address@hidden>
  HACKING | 9 +++++++++
  1 file changed, 9 insertions(+)

diff --git a/HACKING b/HACKING
index 4125c97d8d..0fc3e0fc04 100644
@@ -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
+  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.

Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

reply via email to

[Prev in Thread] Current Thread [Next in Thread]