On Tue, Jun 02, 2009 at 02:47:57AM -0500, Anthony Liguori wrote:
> Gerd Hoffmann wrote:
> > On 05/29/09 23:12, David Turner wrote:
> >> I would even suggest providing helper macros to make the programmer's
> >> intent
> >> even more clear
> >> and less error-prone, as in:
> >>
> >> #define QEMU_NEW(ptr) (ptr) =
> >> qemu_alloc(sizeof(*(ptr)))
> >> #define QEMU_NEW_ARRAY(ptr,cnt) (ptr) =
> >> qemu_calloc((cnt),sizeof(*(ptr)))
> >> #define QEMU_RENEW_ARRAY(ptr,cnt) (ptr) =
> >> qemu_realloc((ptr),(cnt),sizeof(*(ptr)))
> >> #define QEMU_FREE_ARRAY(ptr) qemu_free(ptr)
> >
> > The idea to have allocators for arrays (and have them allow
> > zero-length arrays) is fine. I wouldn't create two macros for new and
> > renew array, you can just use usual realloc semantics (ptr == NULL ->
> > alloc).
> >
> > Also I don't like the syntax that much as you'll have the IMHO
> > non-intuitive code like this:
> >
> > QEMU_NEW_ARRAY(ptr, ...);
> >
> > instead of
> >
> > ptr = QEMU_NEW_ARRAY(...);
> >
> > then. I don't see another easy way to get the automagic sizeof(*ptr)
> > stuff done though.
>
> I've always liked glib's memory functions. It does OOM error handling
> and returns NULL when size == 0.
If you look at the problems associated with malloc there are many common