From mboxrd@z Thu Jan 1 00:00:00 1970 From: Konrad Rzeszutek Wilk Subject: Re: [PATCH v3 07/11] tmem: Make the uint64_t oid[3] a proper structure: tmem_oid Date: Tue, 1 Sep 2015 11:18:43 -0400 Message-ID: <20150901151843.GC11425@l.oracle.com> References: <1440788000-32635-1-git-send-email-konrad.wilk@oracle.com> <1440788000-32635-8-git-send-email-konrad.wilk@oracle.com> <55E458E5020000780009E574@prv-mh.provo.novell.com> <20150831153720.GA9931@l.oracle.com> <55E4874702000078000D7A72@prv-mh.provo.novell.com> <20150831161416.GA7982@l.oracle.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="bp/iNruPH9dso1Pn" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1ZWnKo-000650-Uu for xen-devel@lists.xenproject.org; Tue, 01 Sep 2015 15:18:55 +0000 Content-Disposition: inline In-Reply-To: <20150831161416.GA7982@l.oracle.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Jan Beulich Cc: andrew.cooper3@citrix.com, wei.liu2@citrix.com, xen-devel@lists.xenproject.org List-Id: xen-devel@lists.xenproject.org --bp/iNruPH9dso1Pn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Aug 31, 2015 at 12:14:16PM -0400, Konrad Rzeszutek Wilk wrote: > > >And the 'container_of' macro looks to require only one level of > > >nesting. > > > > I'm pretty sure the macro can deal with both. > > OK, let me experiement with it as at the first blush it does not work for me. I can't get it to work. My understanding (and the test case - see attached) seem to agree that the 'container_of' is to be used when you have the pointer to the nested member (oid) and want the pointer to the structure in which it is embedded in (op). We have the opposite case - we have the pointer to the structure in which it was embedded (op): struct tmem_op *op; oidp = (struct tmem_oid *)&op.u.gen.oid[0]; Hence using the container_of macro won't work. --bp/iNruPH9dso1Pn Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="a.c" #include struct tmem_oid { unsigned long oid[3]; }; struct internal { unsigned int cmd; struct tmem_oid oid; }; struct tmem_op { unsigned int x; union { struct { unsigned int cmd; } g; struct internal c; } u; }; #define container_of(ptr, type, member) ({ \ typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) #define offsetof(a,b) __builtin_offsetof(a,b) void main(void) { struct tmem_op op; struct tmem_oid *_oid; struct tmem_oid *_src; struct internal *_i; struct internal *_q; printf("%d \n", offsetof(struct tmem_op, u.c.oid)); // 16 printf("%d \n", offsetof(struct tmem_op, u.c.oid.oid)); // 16 printf("%d \n", offsetof(struct internal, oid)); // 8 _i = &op.u.c; printf("%d\n", (void *)_i - (void *)&op); // 8 _oid = &op.u.c.oid; printf("%d\n", (void *)_oid - (void *)&op); // 16 /* Pointer to struct tmem_oid (_oid), getting the pointer to 'struct internal' */ _q = container_of(_oid, struct internal, oid); printf("%d\n", (void *)_q - (void *)&op); // 8 /* Having the pointer to outer ('*op') we can get the pointer to the internal. */ _src = (struct tmem_oid *)((void *)&op + offsetof(struct tmem_op, u.c.Xoid.oid)); printf("%d\n", (void *)_src - (void *)&op); // 16 }; --bp/iNruPH9dso1Pn Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --bp/iNruPH9dso1Pn--