From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MBbOj-0003SP-Pz for qemu-devel@nongnu.org; Tue, 02 Jun 2009 17:19:53 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MBbOe-0003J1-DI for qemu-devel@nongnu.org; Tue, 02 Jun 2009 17:19:53 -0400 Received: from [199.232.76.173] (port=48025 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MBbOe-0003Hd-2A for qemu-devel@nongnu.org; Tue, 02 Jun 2009 17:19:48 -0400 Received: from smtp-out.google.com ([216.239.33.17]:48245) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MBbOb-0001w6-LJ for qemu-devel@nongnu.org; Tue, 02 Jun 2009 17:19:47 -0400 Received: from spaceape7.eur.corp.google.com (spaceape7.eur.corp.google.com [172.28.16.141]) by smtp-out.google.com with ESMTP id n52LJiXa022419 for ; Tue, 2 Jun 2009 22:19:44 +0100 Received: from fxm25 (fxm25.prod.google.com [10.184.13.25]) by spaceape7.eur.corp.google.com with ESMTP id n52LJgKP011920 for ; Tue, 2 Jun 2009 14:19:42 -0700 Received: by fxm25 with SMTP id 25so8133133fxm.20 for ; Tue, 02 Jun 2009 14:19:42 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <200906022158.49202.paul@codesourcery.com> References: <200905290758.11551.jcd@tribudubois.net> <200906022104.55121.paul@codesourcery.com> <60cad3f0906021342s36cdbd52r6056ed552ba1ae82@mail.gmail.com> <200906022158.49202.paul@codesourcery.com> Date: Tue, 2 Jun 2009 23:19:40 +0200 Message-ID: <60cad3f0906021419j1f728cafy55359f647eb971ef@mail.gmail.com> Subject: Re: [Qemu-devel] [PATCH] use qemu_malloc and friends consistently From: David Turner Content-Type: multipart/alternative; boundary=001636c5b0eed44fa6046b6419a4 List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paul Brook Cc: Kevin Wolf , Gerd Hoffmann , qemu-devel@nongnu.org, Jean-Christophe Dubois --001636c5b0eed44fa6046b6419a4 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Tue, Jun 2, 2009 at 10:58 PM, Paul Brook wrote: > > I think you're afraid of the following case instead: > > > > foo* ptr; > > ptr =3D QEMU_MEW(bar); =3D> compiler will complain that 'ptr' is no= t a > bar* > > > >But this is not possible with the first version anyway. > > Rubbish. Works perfectly. > I think there is a misunderstanding here, what I mean is that: #define QEMU_NEW(ptr) (ptr) =3D malloc(sizeof(*ptr)) foo* ptr; QEMU_NEW(ptr); Will never try to allocate sizeof(bar) bytes, anyway. The rest is taste/personal preference, and there is no point fighting about it. As I said, I just want to say that abort() on qemu_malloc(0) is not the shiny good thing some people are expecting, and that there are better ways to fight potentia= l developer errors. > > cat test.c < #include > #define QEMU_NEW(type) (type*)malloc(sizeof(type)) > typedef struct { int x; } foo; > typedef struct { double y; } bar; > int main(int argc, char **argv) > { > foo *ptr =3D QEMU_NEW(bar); > return 0; > } > EOF > gcc test.c > test.c: In function =91main=92: > test.c:7: initialization from incompatible pointer type > > Paul > --001636c5b0eed44fa6046b6419a4 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable

On Tue, Jun 2, 2009 at 10:58 PM, Paul Br= ook <paul@cod= esourcery.com> wrote:
> I think you're afraid of the following case inst= ead:
>
> foo* =A0ptr;
> ptr =3D QEMU_MEW(bar); =A0 =3D> =A0 compiler will complain that = 9;ptr' is not a bar*
>
>But this is not possible with the first version anyway.

Rubbish. Works perfectly.

I think there is a misunderstanding here, what I mean= is that:

#define=A0 QEMU_NEW(ptr)=A0 (ptr) =3D malloc(sizeof(*ptr))=

foo*=A0 ptr;
QEMU_NEW(ptr);

Will never try to allocate si= zeof(bar) bytes, anyway.
The rest is taste/personal preference, and there is no point fighting about= it.

As I said, I just want to say that abort() on qemu_malloc(0) is= not the shiny good thing
some people are expecting, and that there are = better ways to fight potential developer errors.
=A0

cat =A0test.c <<EOF
#include <stdlib.h>
#define QEMU_NEW(type) (type*)malloc(sizeof(type))
typedef struct { int x; } foo;
typedef struct { double y; } bar;
int main(int argc, char **argv)
{
=A0foo *ptr =3D QEMU_NEW(bar);
=A0return 0;
}
EOF
gcc test.c
test.c: In function =91main=92:
test.c:7: initialization from incompatible pointer type

Paul

--001636c5b0eed44fa6046b6419a4--