qemu-devel
[Top][All Lists]
Advanced

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

Re: [Qemu-devel] [PATCH] use qemu_malloc and friends consistently


From: David Turner
Subject: Re: [Qemu-devel] [PATCH] use qemu_malloc and friends consistently
Date: Tue, 2 Jun 2009 20:02:40 +0200



On Tue, Jun 2, 2009 at 9:26 AM, Gerd Hoffmann <address@hidden> 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.

The first version will extract the item size automatically for you, making it less likely that you screw things in the second version's parameter list.
But I'm not going to fight against it.

Also, my code is usually a lot more aggressive than what I proposed. I don't want to burn too many cycles on this, just stating that,
generally speaking, it is possible to use macros/wrappers to both make the code's intent more clear and reduce potential errors.

I feel this much more worthwhile than imposing an abort on qemu_malloc(0), which seems quite arbitrary, as discussed heavily on this thread.
 Apart from that, the exact details on how to achieve the above goal don't matter much.

 

cheers,
 Gerd



reply via email to

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