* [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc
@ 2018-05-15 13:49 Murilo Opsfelder Araujo
2018-05-15 14:14 ` Eric Blake
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Murilo Opsfelder Araujo @ 2018-05-15 13:49 UTC (permalink / raw)
To: qemu-devel
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 <david@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc
2018-05-15 13:49 [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc Murilo Opsfelder Araujo
@ 2018-05-15 14:14 ` Eric Blake
2018-05-15 15:11 ` Alex Bennée
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Eric Blake @ 2018-05-15 14:14 UTC (permalink / raw)
To: Murilo Opsfelder Araujo, qemu-devel
Cc: Markus Armbruster, David Hildenbrand, Eduardo Habkost, Paolo Bonzini
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:
> http://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg03238.html
>
> Cc: qemu-devel@nongnu.org
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Eduardo Habkost <ehabkost@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
> ---
> HACKING | 9 +++++++++
> 1 file changed, 9 insertions(+)
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc
2018-05-15 13:49 [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc Murilo Opsfelder Araujo
2018-05-15 14:14 ` Eric Blake
@ 2018-05-15 15:11 ` Alex Bennée
2018-05-15 18:11 ` Eric Blake
2018-05-16 9:29 ` David Hildenbrand
3 siblings, 0 replies; 5+ messages in thread
From: Alex Bennée @ 2018-05-15 15:11 UTC (permalink / raw)
To: Murilo Opsfelder Araujo
Cc: qemu-devel, Markus Armbruster, David Hildenbrand,
Eduardo Habkost, Paolo Bonzini
Murilo Opsfelder Araujo <muriloo@linux.ibm.com> writes:
> 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 <david@redhat.com>
> Cc: Eduardo Habkost <ehabkost@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> ---
> 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.
--
Alex Bennée
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc
2018-05-15 13:49 [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc Murilo Opsfelder Araujo
2018-05-15 14:14 ` Eric Blake
2018-05-15 15:11 ` Alex Bennée
@ 2018-05-15 18:11 ` Eric Blake
2018-05-16 9:29 ` David Hildenbrand
3 siblings, 0 replies; 5+ messages in thread
From: Eric Blake @ 2018-05-15 18:11 UTC (permalink / raw)
To: Murilo Opsfelder Araujo, qemu-devel
Cc: Markus Armbruster, David Hildenbrand, Eduardo Habkost,
Paolo Bonzini, qemu-trivial
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:
> http://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg03238.html
>
> Cc: qemu-devel@nongnu.org
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Eduardo Habkost <ehabkost@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
> ---
> 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.
>
>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc
2018-05-15 13:49 [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc Murilo Opsfelder Araujo
` (2 preceding siblings ...)
2018-05-15 18:11 ` Eric Blake
@ 2018-05-16 9:29 ` David Hildenbrand
3 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand @ 2018-05-16 9:29 UTC (permalink / raw)
To: Murilo Opsfelder Araujo, qemu-devel
Cc: Eduardo Habkost, Markus Armbruster, Paolo Bonzini
On 15.05.2018 15:49, 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:
> http://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg03238.html
>
> Cc: qemu-devel@nongnu.org
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Eduardo Habkost <ehabkost@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
> ---
> 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:
Should we make this stronger? s/Prefer/Use/ ? Because I think that for
this use case we have an agreement (sizeof(T) vs. sizeof(*var)).
> +
> + 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 seems to be the right place to start documenting such stuff.
Reviewed-by: David Hildenbrand <david@redhat.com>
--
Thanks,
David / dhildenb
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-05-16 9:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-15 13:49 [Qemu-devel] [PATCH] HACKING: document preference for g_new instead of g_malloc Murilo Opsfelder Araujo
2018-05-15 14:14 ` Eric Blake
2018-05-15 15:11 ` Alex Bennée
2018-05-15 18:11 ` Eric Blake
2018-05-16 9:29 ` David Hildenbrand
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.