Memset versus Memzero

Submitted by Jeremy
on September 23, 2007 - 9:53am

A short thread on the lkml discussed the lack of a memzero function in the Linux kernel. Cyrill Gorcunov asked, "could anyone tell me why there is no official memzero function (or macros) in the kernel?" Arjan van de Ven explained, "it doesn't add value.... memset with a constant 0 is just as fast (since the compiler knows it's 0) than any wrapper around it, and the syntax around it is otherwise the same." Linux creator Linus Torvalds went on to explain:

"The reason we have 'clear_page()' is not because the value we're writing is constant - that doesn't really help/change anything at all. We could have had a 'fill_page()' that sets the value to any random byte, it's just that zero is the only value that we really care about.

"So the reason we have 'clear_page()' is because the *size* and *alignment* is constant and known at compile time, and unlike the value you write, that actually matters. So 'memzero()' would never really make sense as anything but a syntactic wrapper around 'memset(x,0,size)'."


From: Cyrill Gorcunov <gorcunov@...>
Subject: memset as memzero
Date: Sep 22, 4:33 am 2007

Hi list,

could anyone tell me why there is no official memzero function (or macros) in
the kernel. As I see a lot of kernel parts calls for it (defying own macros
as alias to memset). Maybe there is a special reason not to do so? Actually
my suggestion is to define _one_ general macros for this.

Cyrill

-


From: Arjan van de Ven <arjan@...> Subject: Re: memset as memzero Date: Sep 22, 3:46 pm 2007

On Sat, 22 Sep 2007 12:33:55 +0400
Cyrill Gorcunov wrote:

> Hi list,
>
> could anyone tell me why there is no official memzero function (or
> macros) in the kernel.

it doesn't add value.... memset with a constant 0 is just as fast
(since the compiler knows it's 0) than any wrapper around it, and the
syntax around it is otherwise the same.

> As I see a lot of kernel parts calls for it
> (defying own macros as alias to memset). Maybe there is a special
> reason not to do so? Actually my suggestion is to define _one_
> general macros for this.

my suggestion is to nuke all the macros and just use memset().

-


From: Linus Torvalds <torvalds@...> Subject: Re: memset as memzero Date: Sep 22, 2:53 pm 2007

On Sat, 22 Sep 2007, Arjan van de Ven wrote:
>
> it doesn't add value.... memset with a constant 0 is just as fast
> (since the compiler knows it's 0) than any wrapper around it, and the
> syntax around it is otherwise the same.

Indeed.

The reason we have "clear_page()" is not because the value we're writing
is constant - that doesn't really help/change anything at all. We could
have had a "fill_page()" that sets the value to any random byte, it's just
that zero is the only value that we really care about.

So the reason we have "clear_page()" is because the *size* and *alignment*
is constant and known at compile time, and unlike the value you write,
that actually matters.

So "memzero()" would never really make sense as anything but a syntactic
wrapper around "memset(x,0,size)".

Linus
-