bug-gnulib
[Top][All Lists]
Advanced

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

Re: [bug-gnulib] xmalloc, xnmalloc


From: Paul Eggert
Subject: Re: [bug-gnulib] xmalloc, xnmalloc
Date: Thu, 02 Nov 2006 09:58:32 -0800
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

Bruno Haible <address@hidden> writes:

>> #define XNMALLOC(n, t) ((t *) xnmalloc (n, sizeof (t)))
>> #define XCALLOC(n, t)  ((t *) xcalloc  (n, sizeof (t)))
>
> Yes, this looks good. I'll use these names.

OK.

> #ifdef __cplusplus
> #define XALLOC_WITH_EXPRESSION(N,EXPR) xalloc_with_expression (N, &(EXPR))

That doesn't look right in general, since there can be side effects in
computing the address of EXPR.  But since you prefer the XALLOC_WITH_TYPE
variants anyway, perhaps it's not worth looking into this.

How about this further elaboration of the XNMALLOC idea?  It's a bit
more efficient in the case where n is 1.

#if !HAVE__BUILTIN_CONSTANT_P
# define __builtin_constant_p(n) 0
#endif

#define __XALLOC_IS_1(x) (__builtin_constant_p(n) && ((n) == 1))

/* Return an array of N newly allocated objects each of type T.  */
#define XNMALLOC(n, t) \
  ((t *) (__XALLOC_IS_1 (n) ? xmalloc (sizeof (t)) : xnmalloc (n, sizeof (t)))

/* Likewise, but the objects are initialized to zero.  */
#define XCALLOC(n, t)  \
  ((t *) (__XALLOC_IS_1 (n) ? xzalloc (sizeof (t)) : xcalloc (n, sizeof (t)))


Another possibility would be to have XMALLOC and XZALLOC macros, where
it's assumed N is 1; this would be more efficient on non-GCC
platforms.  Maybe that's better, since it's a bit clearer anyway.
(But perhaps you don't need the XMALLOC/XZALLOC stuff at all.)




reply via email to

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