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: Wed, 01 Nov 2006 15:28:17 -0800
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

Bruno Haible <address@hidden> writes:

> clean-temp.c:277:           xmalloc (new_allocated * sizeof (struct tempdir * 
> volatile));
> fatal-signal.c:210:     xmalloc (new_actions_allocated * sizeof 
> (actions_entry_t));
> fstrcmp.c:623:      buffer = (int *) xmalloc (bufmax * (2 * sizeof (int)));
> getgroups.c:53:      gbuf = xmalloc (n * sizeof *gbuf);
> gl_array_list.c:92:     (const void **) xmalloc (count * sizeof (const void 
> *));
> gl_carray_list.c:65:    (struct gl_list_impl *) xmalloc (sizeof (struct 
> gl_list_impl));
> group-member.c:60:  group = xmalloc (n_group_slots * sizeof *group);
>
> The sinner is mostly me, but not only me :-)

If you look at the context of those lines in getgroups.c and
group-member.c, you'll see that those lines are OK.  (So I'm afraid
the "sinner" is whoever's in charge of the rest.  :-)

> How about adding a macro with type argument, like we had a few years ago
> (before xnmalloc was invented)?

Yes, we used to have macros like this:

  # define XMALLOC(Type, N_bytes) ((Type *) xmalloc (sizeof (Type) * (N_bytes)))
  # define XCALLOC(Type, N_bytes) ((Type *) xcalloc (sizeof (Type), (N_bytes)))
  # define XREALLOC(Ptr, Type, N_bytes) \
    ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_bytes)))

Eventually we phased them out
<http://lists.gnu.org/archive/html/bug-gnulib/2003-10/msg00054.html>
partly, as I recall, because I'm not a big fan of function-like
macros that can't be implemented as functions.

Instead of changing xalloc.h, you could write this:

  struct foobartype *r = xnrealloc (r = NULL, 3, sizeof *r);

Kind of yucky, though.

XALLOC_WITH_EXPRESSION seems a bit tricky, since the expression
argument is evaluated in C++ but not in C.

I suppose the simplest thing to do would be to add what you're calling
XALLOC_WITH_TYPE, though I think I'd prefer the name XNMALLOC.  We
could do something similar for xcalloc.  Something like this:

#define XNMALLOC(n, t) ((t *) xnmalloc (n, sizeof (t)))
#define XCALLOC(n, t)  ((t *) xcalloc  (n, sizeof (t)))

This isn't right for an arbitrary C type name 't' due to C's bizarre
syntax for type names.  And it runs afoul of the rule that
function-like macros should act like functions.  But maybe it would
work for you?




reply via email to

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