From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NtMEc-0002jG-Vk for qemu-devel@nongnu.org; Sun, 21 Mar 2010 10:34:35 -0400 Received: from [199.232.76.173] (port=38252 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NtMEb-0002j8-LS for qemu-devel@nongnu.org; Sun, 21 Mar 2010 10:34:33 -0400 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1NtMEa-0002NL-Be for qemu-devel@nongnu.org; Sun, 21 Mar 2010 10:34:33 -0400 Received: from mx1.redhat.com ([209.132.183.28]:31728) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NtMEZ-0002NH-Vi for qemu-devel@nongnu.org; Sun, 21 Mar 2010 10:34:32 -0400 Date: Sun, 21 Mar 2010 16:31:03 +0200 From: "Michael S. Tsirkin" Subject: Re: [Qemu-devel] Re: [PATCH 0/9] Virtio cleanups Message-ID: <20100321143103.GD12758@redhat.com> References: <20100318064015.GA16973@redhat.com> <20100318074239.GA23474@redhat.com> <20100318090711.GB23649@redhat.com> <20100319014159.GB14108@shareable.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20100319014159.GB14108@shareable.org> List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jamie Lokier Cc: qemu-devel@nongnu.org, Juan Quintela On Fri, Mar 19, 2010 at 01:41:59AM +0000, Jamie Lokier wrote: > Juan Quintela wrote: > > vstrucut virtio_common *create_virtio_comon(...., size we really want); > > Again, this implements superclass/subclass as well as you can implemnt > > it in C. > > It would be more type-safe for create_virtio_common() to be a macro > taking the subclass *type* rather than sizeof. > > And it would make the calls short: No need to cast the result, because > the macro would return the desired type (doing the cast itself). > > #define create_virtio_common(...., type) \ > ((type *)_create_virtio_common(...., sizeof(type))) > > Once you have that, it's easy to change to add a field name and > container_of: > > #define create_virtio_common(...., type, field) \ > (container_of(_create_virtio_common(...., sizeof(type)), type, field)) > > That gives you malloc in common init, and type-safe callers everywhere > (no possibility for mistaken sizeof). > > I think it's a simpler to use API and better at protecting against > caller mistakes; you may disagree. > > -- Jamie That's version 1 of my patch. Version 2 removed even need for macro completely by moving allocations to the caller. -- MST