From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43343) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zp2CT-0008TU-At for qemu-devel@nongnu.org; Wed, 21 Oct 2015 18:49:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zp2CQ-0001cc-4F for qemu-devel@nongnu.org; Wed, 21 Oct 2015 18:49:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49100) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zp2CP-0001cY-SZ for qemu-devel@nongnu.org; Wed, 21 Oct 2015 18:49:38 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 6AB18A3049 for ; Wed, 21 Oct 2015 22:49:37 +0000 (UTC) Received: from [10.3.113.176] (ovpn-113-176.phx2.redhat.com [10.3.113.176]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t9LMnbI8027151 for ; Wed, 21 Oct 2015 18:49:37 -0400 From: Eric Blake Message-ID: <5628167C.9020802@redhat.com> Date: Wed, 21 Oct 2015 16:49:32 -0600 MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="1Ano3RD9UkcMnWxWFgrV4l9F02xvWwXpq" Subject: [Qemu-devel] DO_UPCAST confusion List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "qemu-devel@nongnu.org" This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --1Ano3RD9UkcMnWxWFgrV4l9F02xvWwXpq Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable In include/qemu/compiler.h, we have this gem: > #ifndef container_of > #define container_of(ptr, type, member) ({ \ > const typeof(((type *) 0)->member) *__mptr =3D (ptr); \ > (type *) ((char *) __mptr - offsetof(type, member));}) > #endif >=20 > /* Convert from a base type to a parent type, with compile time checkin= g. */ > #ifdef __GNUC__ > #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ > char __attribute__((unused)) offset_must_be_zero[ \ > -offsetof(type, field)]; \ > container_of(dev, type, field);})) > #else > #define DO_UPCAST(type, field, dev) container_of(dev, type, field) > #endif That comment is horrible. In object-oriented programming, there are two common sets of terminology: base and derived classes (derived adds on to base) parent and child classes (child inherits from parent) Going from "base type" to "parent type" makes NO SENSE, since those two terms are synonyms between the two terminologies. Furthermore, according to these references: https://programmers.stackexchange.com/questions/148615/what-is-upcasting-= downcasting http://en.wikipedia.org/wiki/Downcasting the act of going from a base class to a derived class (or from a parent type to a child type) is considered downcasting. Yet our definition of DO_UPCAST is used to go from a base class to the derived class. Example usage in blockdev.c is to declare a derived class: > /* internal snapshot private data */ > typedef struct InternalSnapshotState { > BlkTransactionState common; =2E.. > }; then to take a generic parent pointer and convert it into the derived typ= e: > static void internal_snapshot_prepare(BlkTransactionState *common, > Error **errp) > { =2E.. > InternalSnapshotState *state; =2E.. > state =3D DO_UPCAST(InternalSnapshotState, common, common); I much prefer the name container_of() (which is a bit more obvious that it is finding the container or derived type that embeds the parent type), but if we have to keep the ugly name, could we at least clean up the comment to make sense, and fix the name to be DO_DOWNCAST to match what it is actually doing? --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --1Ano3RD9UkcMnWxWFgrV4l9F02xvWwXpq Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJWKBZ8AAoJEKeha0olJ0Nq/Q4H/iwpwGC+zaSNwsRPCMCX77B3 rNXPAE7LAyIBFM4oEvEI7HCuzEjhIDjl/c/fUsQnt4j03lNf4W8R2QLBEDeJWe1P 6QjgX4mmuxvLuP0UmXL5QCGXkiEVcAlkGWPqX/+khAN6EwVzAEupo/18JT3TMwoh Q6J0vrVO2s4c8Jc/iZvH/QRY95L/uW8aL9N0h8OEXhsDVeWcWYnzpmfsc2WBKJjK MkhyA7da8eQoql//qfHY+z3LLAZzU70ETI+Vqc5i8RC5kF5nPa7+h0VDh2JNFOMT CWf3kJJ7KR/tciHfeesvaVxIhbQo8qLtIYgJJIyHWPQZfirbFHwqYZIZrZr4xJ0= =D5O4 -----END PGP SIGNATURE----- --1Ano3RD9UkcMnWxWFgrV4l9F02xvWwXpq--