From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MBaoI-0007LA-US for qemu-devel@nongnu.org; Tue, 02 Jun 2009 16:42:15 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MBaoE-0007GJ-33 for qemu-devel@nongnu.org; Tue, 02 Jun 2009 16:42:14 -0400 Received: from [199.232.76.173] (port=52846 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MBaoD-0007Fv-Lr for qemu-devel@nongnu.org; Tue, 02 Jun 2009 16:42:09 -0400 Received: from smtp-out.google.com ([216.239.33.17]:44916) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MBaoB-0000fP-5D for qemu-devel@nongnu.org; Tue, 02 Jun 2009 16:42:08 -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 n52Kg3lD001209 for ; Tue, 2 Jun 2009 21:42:03 +0100 Received: from ewy5 (ewy5.prod.google.com [10.241.103.5]) by spaceape7.eur.corp.google.com with ESMTP id n52Kfgf2018194 for ; Tue, 2 Jun 2009 13:42:02 -0700 Received: by ewy5 with SMTP id 5so12417842ewy.30 for ; Tue, 02 Jun 2009 13:42:02 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <200906022104.55121.paul@codesourcery.com> References: <200905290758.11551.jcd@tribudubois.net> <200906021913.09517.paul@codesourcery.com> <60cad3f0906021249t435fc1a8w4f89027e0863ff52@mail.gmail.com> <200906022104.55121.paul@codesourcery.com> Date: Tue, 2 Jun 2009 22:42:02 +0200 Message-ID: <60cad3f0906021342s36cdbd52r6056ed552ba1ae82@mail.gmail.com> Subject: Re: [Qemu-devel] [PATCH] use qemu_malloc and friends consistently From: David Turner Content-Type: multipart/alternative; boundary=001485f7c5a02fcd9a046b639388 List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paul Brook Cc: Kevin Wolf , Jean-Christophe Dubois , qemu-devel@nongnu.org, Gerd Hoffmann --001485f7c5a02fcd9a046b639388 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Tue, Jun 2, 2009 at 10:04 PM, Paul Brook wrote: > > > My point is that > > #define QEMU_NEW(type) ((type *)qemu_malloc(sizeof(type))) > foo *ptr = QEMU_NEW(foo); > > is just as safe as > > #define QEMU_NEW(ptr) (ptr) = qemu_malloc(sizeof(*(ptr))) > foo *ptr; > QEMU_NEW(ptr); > > Because the compiler will catch the type mismatch. > I still don't see the point. There is no type mismatch in the first version since the C standard mandates that a (void*) *must* be silently casted into any other typed pointer (unlike C++ which forbids this). I think you're afraid of the following case instead: foo* ptr; ptr = QEMU_MEW(bar); => compiler will complain that 'ptr' is not a bar* But this is not possible with the first version anyway. > > Paul > --001485f7c5a02fcd9a046b639388 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Tue, Jun 2, 2009 at 10:04 PM, Paul Br= ook <paul@cod= esourcery.com> wrote:


My point is that

#define QEMU_NEW(type) ((type *)qemu_malloc(sizeof(type)))
=A0foo *ptr =3D QEMU_NEW(foo);

is just as safe as

#define QEMU_NEW(ptr) (ptr) =3D qemu_malloc(sizeof(*(ptr)))
=A0foo *ptr;
=A0QEMU_NEW(ptr);

Because the compiler will catch the type mismatch.

I still don't see = the point.
There is no type mismatch in the first version since the C st= andard mandates that a (void*) *must* be silently casted into any other typ= ed pointer (unlike C++ which forbids this).

I think you're afraid of the following case instead:

foo*=A0= ptr;
ptr =3D QEMU_MEW(bar);=A0=A0 =3D>=A0=A0 compiler will complain = that 'ptr' is not a bar*

But this is not possible with the f= irst version anyway.

=A0

Paul

--001485f7c5a02fcd9a046b639388--