Skip to content

Commit

Permalink
HACKING: document preference for g_new instead of g_malloc
Browse files Browse the repository at this point in the history
This patch documents the preference for g_new instead of g_malloc. The
reasons were adapted from commit b45c03f.

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]>
  • Loading branch information
mopsfelder authored and Michael Tokarev committed May 20, 2018
1 parent 0b816e9 commit f7c922e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions HACKING
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit f7c922e

Please sign in to comment.