]> Git Repo - qemu.git/commitdiff
HACKING: document preference for g_new instead of g_malloc
authorMurilo Opsfelder Araujo <[email protected]>
Tue, 15 May 2018 13:49:50 +0000 (10:49 -0300)
committerMichael Tokarev <[email protected]>
Sun, 20 May 2018 05:32:09 +0000 (08:32 +0300)
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: [email protected]
Cc: David Hildenbrand <[email protected]>
Cc: Eduardo Habkost <[email protected]>
Cc: Markus Armbruster <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Signed-off-by: Murilo Opsfelder Araujo <[email protected]>
Reviewed-by: Eric Blake <[email protected]>
Reviewed-by: Alex BennĂ©e <[email protected]>
Reviewed-by: David Hildenbrand <[email protected]>
Signed-off-by: Michael Tokarev <[email protected]>
HACKING

diff --git a/HACKING b/HACKING
index 4125c97d8d177b11f0d4367d6d81224fdd2ff2ba..0fc3e0fc04fe6ad00ceb954c25319aa522d5af64 100644 (file)
--- 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.
 
This page took 0.02372 seconds and 4 git commands to generate.