bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnulib] extended xmalloc.c


From: Paul Eggert
Subject: Re: [Bug-gnulib] extended xmalloc.c
Date: 11 Oct 2003 14:50:45 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Bruno Haible <address@hidden> writes:

> Looks good to me, except for four things:

Three of those things are moot for now, if we're going to defer
this such a change until some program other than Emacs needs them.

For the fourth thing:

> > +  if (SIZE_MAX / s < n)
> > +    return NULL;
> 
> Divisions are often more expensive than other arithmetic operations, and
> often calloc is called with n==1 or s==1. So it's probably a win in terms
> of speed to write
> 
>     if (n > 1 && s > 1 && SIZE_MAX / s < n)
>       return NULL;

For xalloc.h I plan to attack this in a different way, by supplying
one function for the common case where n==1 or s==1, and another (more
general) function that allows arbitrary n and s.  So for each kind of
memory allocation, we'll have two functions.  Something like this:

void *xmalloc (size_t s);
void *xnmalloc (size_t n, size_t s);

void *xzalloc (size_t s);
void *xcalloc (size_t n, size_t s);

void *xrealloc (void *p, size_t s);
void *xnrealloc (void *p, size_t n, size_t s);

The less-general functions (xmalloc, xzalloc, xrealloc) don't need to
do size_t overflow checking.  The more-general functions (xnmalloc,
xcalloc, xnrealloc) don't need to bother about optimizing the common
case where n==1 or s==1, since that case should normally be handled by
the less-general function.

There will also be a clone function that works like this:

void *
xclone (void *p, size_t s)
{
  return memcpy (xmalloc (s), p, s);
}

though I suppose we should make it inline, for speed.  There is no
real need for a 'xnclone (P, N, S)' call, though, since 'xclone (P, N
* S)' should work without any need for an arithmetic overflow check.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]