Re: memset as memzero



On Sat, Sep 22, 2007 at 11:53:53AM -0700, Linus Torvalds wrote:


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)".

There is one useful argument for memzero (or bzero to give it its proper
name), and that's that it's impossible to screw up.
I'm still amazed at how many times I see

memset (x,size,0);

in various code. So much so, that my editor highlights it now to spot
it during code review. As does my mail client. To be on the safe
side, I also have a cron job grepping for it in my ~/Mail/commits
for all the projects I'm interested in.

It's tragic really just how easy it is to screw it up.

Dave

--
http://www.codemonkey.org.uk
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • Re: Q about "optimizing away" "non-used" code
    ... Surely almost any compiler is going to optimise away unreachable code, ... There are instances where the area of memory needs to be cleared, ... >> The real question is can memset be used to assure that a program's ...
    (comp.lang.cpp)
  • Re: Q about "optimizing away" "non-used" code
    ... Can an optimizing compiler ignore a memset() ... > command if the memory pointed to is never again accessed after the ... That means that the compiler is allowed to rearange everything, ...
    (comp.lang.cpp)
  • Re: Memset is faster than simple loop?
    ... There's no guarantee that memset() is faster, ... on which the compiler may not be able to find when it compiles the loop. ... void *start = dst; ...
    (comp.lang.c)
  • Re: Pet hates
    ... finding a new language feature (often peculiar to the ... That's a pretty simple one that most people will accept given that every man and his dog has a different setup (platform, compiler, etc). ... As for the second point, the coder should justify the approach in the review when you ask him/her. ... :o) And by that I mean a valid answer to your question in the code review could be: ...
    (comp.lang.fortran)
  • RE: undefined reference to `memset
    ... > the needed memset if you build a stand alone program, ... There exists an inline function called memset. ... with optimization. ... Here is one of several issues with the amd64 compiler used at -O2 vs. ...
    (freebsd-stable)