Bruno Haible <[EMAIL PROTECTED]> 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.)