This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Use calloc (x, y) instead malloc (x * y).


Joseph S. Myers wrote:
> 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.

An example of exactly that is the xnmalloc function in gnulib's lib/xalloc.h,
which uses the macro xalloc_oversized(N, S) to detect arithmetic overflow
efficiently when allocating an array of N objects, each of size S.
Please feel free to adapt or copy that code into glibc.

http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/xalloc.h
http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/xalloc-oversized.h


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]