From mboxrd@z Thu Jan 1 00:00:00 1970 From: Avi Kivity Subject: Re: [PATCH] Introduce QEMU_NEW() Date: Mon, 25 Jul 2011 14:35:45 +0300 Message-ID: <4E2D5511.4060002@redhat.com> References: <1311583872-362-1-git-send-email-avi@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org To: Alexander Graf Return-path: Received: from mx1.redhat.com ([209.132.183.28]:24978 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751719Ab1GYLfw (ORCPT ); Mon, 25 Jul 2011 07:35:52 -0400 In-Reply-To: Sender: kvm-owner@vger.kernel.org List-ID: On 07/25/2011 12:32 PM, Alexander Graf wrote: > On 25.07.2011, at 10:51, Avi Kivity wrote: > > > qemu_malloc() is type-unsafe as it returns a void pointer. Introduce > > QEMU_NEW() (and QEMU_NEWZ()), which return the correct type. > > What does this buy you over > > type *x = qemu_malloc(sizeof(type)); > > ? I find the non-C++ version easier to read even. > I'm using it as MemoryRegion *phys_flash = QEMU_NEW(MemoryRegion); instead of MemoryRegion *phys_flash = qemu_malloc(sizeof(*phys_flash)); I find it shorter, and if I make a mistake, the compiler shouts at me instead of a runtime crash. -- error compiling committee.c: too many arguments to function From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:55147) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QlJRv-0008UI-28 for qemu-devel@nongnu.org; Mon, 25 Jul 2011 07:35:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QlJRu-0007JB-8b for qemu-devel@nongnu.org; Mon, 25 Jul 2011 07:35:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:30131) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QlJRu-0007J7-00 for qemu-devel@nongnu.org; Mon, 25 Jul 2011 07:35:50 -0400 Message-ID: <4E2D5511.4060002@redhat.com> Date: Mon, 25 Jul 2011 14:35:45 +0300 From: Avi Kivity MIME-Version: 1.0 References: <1311583872-362-1-git-send-email-avi@redhat.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH] Introduce QEMU_NEW() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alexander Graf Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org On 07/25/2011 12:32 PM, Alexander Graf wrote: > On 25.07.2011, at 10:51, Avi Kivity wrote: > > > qemu_malloc() is type-unsafe as it returns a void pointer. Introduce > > QEMU_NEW() (and QEMU_NEWZ()), which return the correct type. > > What does this buy you over > > type *x = qemu_malloc(sizeof(type)); > > ? I find the non-C++ version easier to read even. > I'm using it as MemoryRegion *phys_flash = QEMU_NEW(MemoryRegion); instead of MemoryRegion *phys_flash = qemu_malloc(sizeof(*phys_flash)); I find it shorter, and if I make a mistake, the compiler shouts at me instead of a runtime crash. -- error compiling committee.c: too many arguments to function