From: Laszlo Ersek <lersek@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-trivial@nongnu.org, Thomas Huth <thuth@redhat.com>,
qemu-devel@nongnu.org
Subject: Re: [Qemu-trivial] [Qemu-devel] [PATCH 4/5] tests/i44fx-test: No need for zeroing memory before memset
Date: Fri, 9 Oct 2015 10:08:35 +0200 [thread overview]
Message-ID: <56177603.40307@redhat.com> (raw)
In-Reply-To: <87fv1k1qu8.fsf@blackfin.pond.sub.org>
On 10/09/15 08:46, Markus Armbruster wrote:
> Laszlo Ersek <lersek@redhat.com> writes:
>
>> On 10/08/15 21:35, Thomas Huth wrote:
>>> Change a g_malloc0 into g_malloc since the following
>>> memset fills the whole buffer anyway.
>>>
>>> Cc: Laszlo Ersek <lersek@redhat.com>
>>> Signed-off-by: Thomas Huth <thuth@redhat.com>
>>> ---
>>> tests/i440fx-test.c | 2 +-
>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c
>>> index d0bc8de..7fa1709 100644
>>> --- a/tests/i440fx-test.c
>>> +++ b/tests/i440fx-test.c
>>> @@ -191,7 +191,7 @@ static void write_area(uint32_t start, uint32_t end, uint8_t value)
>>> uint32_t size = end - start + 1;
>>> uint8_t *data;
>>>
>>> - data = g_malloc0(size);
>>> + data = g_malloc(size);
>>> memset(data, value, size);
>>> memwrite(start, data, size);
>>>
>>>
>>
>> Technically you are right of course, but I remember some historical mess
>> around this, in this file.
>>
>> Plus I vaguely recall g_new[0]() being the most recent preference.
>>
>> https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html#g-new
>>
>> See e.g. commit 97f3ad3551. Markus?
>
> TL;DR: the patch is fine.
>
> The argument of g_malloc() isn't always the size of a type. But when it
> is, you should be using g_new() instead. The commit message explains:
>
> g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer,
> for two reasons. One, it catches multiplication overflowing size_t.
> Two, it returns T * rather than void *, which lets the compiler catch
> more type errors.
>
> The multiplication argument applies only when n != 1.
>
> The type checking argument applies regardless of n. That's why the
> commit also transfroms patterns like g_malloc(sizeof(T)).
>
> When the argument isn't written as sizeof a type, but could be, we enter
> "matter of taste" territory. For instance, some may perefer the
> idiomatic T *p = g_malloc(sizeof(*p)) to the more tightly typed
> p = g_new(T, 1). I therefore leave them alone. Commit messsage again:
>
> This commit only touches allocations with size arguments of the form
> sizeof(T).
>
> A separate issue is a zeroing memset() after allocation. Please use a
> zeroing allocator instead, because that's more concise. Precedence:
> commit 0bd0adb.
>
> Now let's apply this to the patch.
>
> The memset() isn't zeroing, so "use a zeroing allocator instead" doesn't
> apply. Before the patch, the code uses one additionally, which is
> wasteful. The patch stops the waste.
>
> The size argument is not the size of any type. You could still write
>
> data = g_new(uint8_t, size);
>
> but the extra verbosity pretty clearly outweighs what we could gain from
> type checking here.
>
Okay, thanks!
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Cheers
Laszlo
WARNING: multiple messages have this Message-ID (diff)
From: Laszlo Ersek <lersek@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-trivial@nongnu.org, Thomas Huth <thuth@redhat.com>,
qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 4/5] tests/i44fx-test: No need for zeroing memory before memset
Date: Fri, 9 Oct 2015 10:08:35 +0200 [thread overview]
Message-ID: <56177603.40307@redhat.com> (raw)
In-Reply-To: <87fv1k1qu8.fsf@blackfin.pond.sub.org>
On 10/09/15 08:46, Markus Armbruster wrote:
> Laszlo Ersek <lersek@redhat.com> writes:
>
>> On 10/08/15 21:35, Thomas Huth wrote:
>>> Change a g_malloc0 into g_malloc since the following
>>> memset fills the whole buffer anyway.
>>>
>>> Cc: Laszlo Ersek <lersek@redhat.com>
>>> Signed-off-by: Thomas Huth <thuth@redhat.com>
>>> ---
>>> tests/i440fx-test.c | 2 +-
>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c
>>> index d0bc8de..7fa1709 100644
>>> --- a/tests/i440fx-test.c
>>> +++ b/tests/i440fx-test.c
>>> @@ -191,7 +191,7 @@ static void write_area(uint32_t start, uint32_t end, uint8_t value)
>>> uint32_t size = end - start + 1;
>>> uint8_t *data;
>>>
>>> - data = g_malloc0(size);
>>> + data = g_malloc(size);
>>> memset(data, value, size);
>>> memwrite(start, data, size);
>>>
>>>
>>
>> Technically you are right of course, but I remember some historical mess
>> around this, in this file.
>>
>> Plus I vaguely recall g_new[0]() being the most recent preference.
>>
>> https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html#g-new
>>
>> See e.g. commit 97f3ad3551. Markus?
>
> TL;DR: the patch is fine.
>
> The argument of g_malloc() isn't always the size of a type. But when it
> is, you should be using g_new() instead. The commit message explains:
>
> g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer,
> for two reasons. One, it catches multiplication overflowing size_t.
> Two, it returns T * rather than void *, which lets the compiler catch
> more type errors.
>
> The multiplication argument applies only when n != 1.
>
> The type checking argument applies regardless of n. That's why the
> commit also transfroms patterns like g_malloc(sizeof(T)).
>
> When the argument isn't written as sizeof a type, but could be, we enter
> "matter of taste" territory. For instance, some may perefer the
> idiomatic T *p = g_malloc(sizeof(*p)) to the more tightly typed
> p = g_new(T, 1). I therefore leave them alone. Commit messsage again:
>
> This commit only touches allocations with size arguments of the form
> sizeof(T).
>
> A separate issue is a zeroing memset() after allocation. Please use a
> zeroing allocator instead, because that's more concise. Precedence:
> commit 0bd0adb.
>
> Now let's apply this to the patch.
>
> The memset() isn't zeroing, so "use a zeroing allocator instead" doesn't
> apply. Before the patch, the code uses one additionally, which is
> wasteful. The patch stops the waste.
>
> The size argument is not the size of any type. You could still write
>
> data = g_new(uint8_t, size);
>
> but the extra verbosity pretty clearly outweighs what we could gain from
> type checking here.
>
Okay, thanks!
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Cheers
Laszlo
next prev parent reply other threads:[~2015-10-09 15:21 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-08 19:35 [Qemu-trivial] [PATCH 0/5] Small optimizations for code using g_malloc0 + memset/memcpy Thomas Huth
2015-10-08 19:35 ` [Qemu-devel] " Thomas Huth
2015-10-08 19:35 ` [Qemu-trivial] [PATCH 1/5] hw/dma/pxa2xx: Remove superfluous memset Thomas Huth
2015-10-08 19:35 ` [Qemu-devel] " Thomas Huth
2015-10-08 20:59 ` [Qemu-trivial] " Eric Blake
2015-10-08 20:59 ` Eric Blake
2015-10-09 6:39 ` [Qemu-trivial] " Thomas Huth
2015-10-09 6:39 ` Thomas Huth
2015-10-08 19:35 ` [Qemu-trivial] [PATCH 2/5] hw/scsi/spapr_vscsi: " Thomas Huth
2015-10-08 19:35 ` [Qemu-devel] " Thomas Huth
2015-10-08 21:00 ` [Qemu-trivial] " Eric Blake
2015-10-08 21:00 ` Eric Blake
2015-10-09 1:51 ` [Qemu-trivial] " David Gibson
2015-10-09 1:51 ` [Qemu-devel] " David Gibson
2015-10-08 19:35 ` [Qemu-trivial] [PATCH 3/5] hw/input/tsc210x: " Thomas Huth
2015-10-08 19:35 ` [Qemu-devel] " Thomas Huth
2015-10-08 23:34 ` [Qemu-trivial] " Eric Blake
2015-10-08 23:34 ` Eric Blake
2015-10-09 6:40 ` [Qemu-trivial] " Thomas Huth
2015-10-09 6:40 ` Thomas Huth
2015-10-08 19:35 ` [Qemu-trivial] [PATCH 4/5] tests/i44fx-test: No need for zeroing memory before memset Thomas Huth
2015-10-08 19:35 ` [Qemu-devel] " Thomas Huth
2015-10-08 20:24 ` [Qemu-trivial] " Laszlo Ersek
2015-10-08 20:24 ` [Qemu-devel] " Laszlo Ersek
2015-10-09 6:36 ` [Qemu-trivial] " Thomas Huth
2015-10-09 6:36 ` [Qemu-devel] " Thomas Huth
2015-10-09 6:46 ` [Qemu-trivial] " Markus Armbruster
2015-10-09 6:46 ` Markus Armbruster
2015-10-09 8:08 ` Laszlo Ersek [this message]
2015-10-09 8:08 ` Laszlo Ersek
2015-10-08 19:35 ` [Qemu-trivial] [PATCH 5/5] linux-user/syscall: Replace g_malloc0 + memcpy with g_memdup Thomas Huth
2015-10-08 19:35 ` [Qemu-devel] " Thomas Huth
2015-10-08 23:32 ` [Qemu-trivial] " Eric Blake
2015-10-08 23:32 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=56177603.40307@redhat.com \
--to=lersek@redhat.com \
--cc=armbru@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-trivial@nongnu.org \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.