xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
To: Jan Beulich <jbeulich@suse.com>
Cc: andrew.cooper3@citrix.com, wei.liu2@citrix.com,
	xen-devel@lists.xenproject.org
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	[thread overview]
Message-ID: <20150901151843.GC11425@l.oracle.com> (raw)
In-Reply-To: <20150831161416.GA7982@l.oracle.com>

[-- Attachment #1: Type: text/plain, Size: 760 bytes --]

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.

[-- Attachment #2: a.c --]
[-- Type: text/plain, Size: 1334 bytes --]

#include <stdio.h>

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
};

[-- Attachment #3: Type: text/plain, Size: 126 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

  parent reply	other threads:[~2015-09-01 15:18 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-28 18:53 [PATCH v3] Tmem bug-fixes and cleanups Konrad Rzeszutek Wilk
2015-08-28 18:53 ` [PATCH v3 01/11] tmem: Don't crash/hang/leak hypervisor when using shared pools within an guest Konrad Rzeszutek Wilk
2015-08-28 18:53 ` [PATCH v3 02/11] tmem: Add ASSERT in obj_rb_insert for pool->rwlock lock Konrad Rzeszutek Wilk
2015-08-31 11:24   ` Jan Beulich
2015-08-28 18:53 ` [PATCH v3 03/11] tmem: Remove in xc_tmem_control_oid duplicate set_xen_guest_handle call Konrad Rzeszutek Wilk
2015-08-28 18:53 ` [PATCH v3 04/11] tmem: Remove xc_tmem_control mystical arg3 Konrad Rzeszutek Wilk
2015-08-28 18:53 ` [PATCH v3 05/11] tmem: Move TMEM_CONTROL subop of tmem hypercall to sysctl Konrad Rzeszutek Wilk
2015-08-31 11:32   ` Jan Beulich
2015-08-28 18:53 ` [PATCH v3 06/11] tmem: Remove the old tmem control XSM checks as it is part of sysctl hypercall Konrad Rzeszutek Wilk
2015-08-28 18:53 ` [PATCH v3 07/11] tmem: Make the uint64_t oid[3] a proper structure: tmem_oid Konrad Rzeszutek Wilk
2015-08-31 11:38   ` Jan Beulich
2015-08-31 15:37     ` Konrad Rzeszutek Wilk
2015-08-31 15:56       ` Jan Beulich
2015-08-31 16:14         ` Konrad Rzeszutek Wilk
2015-09-01  7:04           ` Jan Beulich
2015-09-01 15:23             ` Konrad Rzeszutek Wilk
2015-09-01 15:54               ` Jan Beulich
2015-09-01 16:55                 ` Konrad Rzeszutek Wilk
2015-09-01 20:11                 ` Konrad Rzeszutek Wilk
2015-09-02  6:43                   ` Jan Beulich
2015-09-01 15:18           ` Konrad Rzeszutek Wilk [this message]
2015-09-01 15:37             ` Jan Beulich
2015-09-01 15:53               ` Konrad Rzeszutek Wilk
2015-08-28 18:53 ` [PATCH v3 08/11] tmem/sysctl: Use 'struct tmem_oid' for every user Konrad Rzeszutek Wilk
2015-08-31 11:42   ` Jan Beulich
2015-08-28 18:53 ` [PATCH v3 09/11] tmem: Use 'struct tmem_oid' in tmem_handle and move it to sysctl header Konrad Rzeszutek Wilk
2015-08-31 11:44   ` Jan Beulich
2015-09-01 20:12   ` Konrad Rzeszutek Wilk
2015-09-02  6:46     ` Jan Beulich
2015-08-28 18:53 ` [PATCH v3 10/11] tmem: Remove extra spaces at end and some hard tabbing Konrad Rzeszutek Wilk
2015-08-28 18:53 ` [PATCH v3 11/11] tmem: Spelling and full stop surgery Konrad Rzeszutek Wilk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150901151843.GC11425@l.oracle.com \
    --to=konrad.wilk@oracle.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).