This is the mail archive of the
mailing list for the glibc project.
Re: [PATCH] Use calloc (x, y) instead malloc (x * y).
- From: Rich Felker <dalias at aerifal dot cx>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: Andreas Jaeger <aj at suse dot com>, OndÅej BÃlka <neleai at seznam dot cz>, libc-alpha at sourceware dot org
- Date: Fri, 25 Oct 2013 13:30:12 -0400
- Subject: Re: [PATCH] Use calloc (x, y) instead malloc (x * y).
- Authentication-results: sourceware.org; auth=none
- References: <20131025084008 dot GA17443 at domone> <526A319B dot 80909 at suse dot com> <Pine dot LNX dot 4 dot 64 dot 1310251530510 dot 12877 at digraph dot polyomino dot org dot uk>
On Fri, Oct 25, 2013 at 03:38:32PM +0000, Joseph S. Myers wrote:
> On Fri, 25 Oct 2013, Andreas Jaeger wrote:
> > What about adding a wrapper around malloc instead?
> Note that, for efficiency, in such an (inline / macro) wrapper you want to
> distinguish the argument counting the number of elements (not constant)
> and the argument counting the size of an element (almost always constant,
> so SIZE_MAX / size gets folded to a constant) - and make sure each call
> passes the arguments in the right order, which may not always be the order
> the existing multiplication is written in.
Couldn't you just use __builtin_constant_p to automatically choose
whichever one is constant?
On the other hand, if you want to minimize size rather than time,
skipping a macro and just using a function call would be better.