From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MBOjC-0006nk-KY for qemu-devel@nongnu.org; Tue, 02 Jun 2009 03:48:10 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MBOj7-0006lN-D2 for qemu-devel@nongnu.org; Tue, 02 Jun 2009 03:48:09 -0400 Received: from [199.232.76.173] (port=54958 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MBOj7-0006lI-7H for qemu-devel@nongnu.org; Tue, 02 Jun 2009 03:48:05 -0400 Received: from mx20.gnu.org ([199.232.41.8]:10547) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MBOj6-0001Fp-Il for qemu-devel@nongnu.org; Tue, 02 Jun 2009 03:48:04 -0400 Received: from qw-out-1920.google.com ([74.125.92.144]) by mx20.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MBOj4-0006n7-TE for qemu-devel@nongnu.org; Tue, 02 Jun 2009 03:48:03 -0400 Received: by qw-out-1920.google.com with SMTP id 4so4185526qwk.4 for ; Tue, 02 Jun 2009 00:48:02 -0700 (PDT) Message-ID: <4A24D92D.4010704@codemonkey.ws> Date: Tue, 02 Jun 2009 02:47:57 -0500 From: Anthony Liguori MIME-Version: 1.0 Subject: Re: [Qemu-devel] [PATCH] use qemu_malloc and friends consistently References: <200905290758.11551.jcd@tribudubois.net> <4A1FD6E2.9020006@redhat.com> <200905291407.26757.paul@codesourcery.com> <200905291917.10535.jseward@acm.org> <60cad3f0905291412m670c7a6cw45f9b51f3122ddfb@mail.gmail.com> <4A24D40E.2060704@redhat.com> In-Reply-To: <4A24D40E.2060704@redhat.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Gerd Hoffmann Cc: Kevin Wolf , David Turner , Jean-Christophe Dubois , Paul Brook , qemu-devel@nongnu.org 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. http://library.gnome.org/devel/glib/stable/glib-Memory-Allocation.html Regards, Anthony Liguori > cheers, > Gerd > > >