mailing list of musl libc
 help / color / mirror / code / Atom feed
* Query regarding malloc if statement
@ 2017-06-19 15:16 Jamie Mccrae
  2017-06-19 18:34 ` Markus Wichmann
  0 siblings, 1 reply; 7+ messages in thread
From: Jamie Mccrae @ 2017-06-19 15:16 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 1131 bytes --]

Hi,

I'm using musl to compile a cross-distro application which I've been having problems with and whilst discussing the problem the developer of another project, was shown a musl malloc function which manually checks the contents of each byte and changes it to 0 if the byte is non-0. This code is in src/malloc/malloc.c as so:

void *__malloc0(size_t n)
{
        void *p = malloc(n);
        if (p && !IS_MMAPPED(MEM_TO_CHUNK(p))) {
                size_t *z;
                n = (n + sizeof *z - 1)/sizeof *z;
                for (z=p; n; n--, z++) if (*z) *z=0;
        }
        return p;
}


This code causes thousands of errors when using valgrind (in excess of 800,000 for my application) due to checking the value of each byte before it has been set and I have to agree with this other developer that I'm at a loss as to why this is performed. If you step through the array and just set each byte to 0 then there will be no read-before-initialisation error and the function will run much faster due to not having to retrieve the data. Why not instead use:

                for (z=p; n; n--, z++) *z=0;

[-- Attachment #2: Type: text/html, Size: 2093 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-06-20 23:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-19 15:16 Query regarding malloc if statement Jamie Mccrae
2017-06-19 18:34 ` Markus Wichmann
2017-06-19 21:02   ` Jamie Mccrae
2017-06-20  4:14     ` Markus Wichmann
2017-06-20 14:35       ` Szabolcs Nagy
2017-06-20 20:04         ` Markus Wichmann
2017-06-20 23:08           ` A. Wilcox

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).