This is the mail archive of the
mailing list for the glibc project.
Re: [RFC][PATCH] Add reallocarray function.
- From: Paul Eggert <eggert at cs dot ucla dot edu>
- To: RÃdiger Sonderfeld <ruediger at c-plusplus dot de>, libc-alpha at sourceware dot org
- Date: Sun, 18 May 2014 14:05:19 -0700
- Subject: Re: [RFC][PATCH] Add reallocarray function.
- Authentication-results: sourceware.org; auth=none
- References: <5040375 dot QgetdG6juS at descartes>
RÃdiger Sonderfeld wrote:
+ /* size_t is unsigned so the behavior on overflow is defined. */
+ INTERNAL_SIZE_T bytes = nmemb * elem_size;
+#define HALF_INTERNAL_SIZE_T \
+ (((INTERNAL_SIZE_T) 1) << (8 * sizeof (INTERNAL_SIZE_T) / 2))
+ if (__builtin_expect ((nmemb | elem_size) >= HALF_INTERNAL_SIZE_T, 0))
+ if (elem_size != 0 && bytes / elem_size != nmemb)
Instead of cut and pasting this code from calloc, please refactor so
that the code is present only once, with the goal of optimizing it when
the GCC folks get their act together and have a function like the
__builtin_umul_overflow function that Clang has had since January. This
will let calloc and reallocarray do the unsigned multiplication and
inspect the hardware's overflow bit directly, which is nicer than the