xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xensource.com
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH 7/9] libxl: New convenience macro CONTAINER_OF
Date: Fri, 13 Jan 2012 19:25:26 +0000	[thread overview]
Message-ID: <1326482728-10733-8-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1326482728-10733-1-git-send-email-ian.jackson@eu.citrix.com>

Provide a convenient and type-safe wrapper which does the correct
dance to subtract offsetof.  This is very similar to the
"container_of" macro in the Linux kernel, but it has an additional
feature that instead of the type argument you may also pass an
expression of that type; this makes initialising a variable with
CONTAINER_OF easier.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
 tools/libxl/libxl_internal.h |   29 +++++++++++++++++++++++++++++
 1 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 594b9fb..213b5f9 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1238,6 +1238,35 @@ _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao);
  * Convenience macros.
  */
 
+/*
+ * CONTAINER_OF work like this.  Given:
+ *    typedef struct {
+ *      ...
+ *      member_type member_name;
+ *      ...
+ *    } outer_type;
+ *    outer_type outer, *outer_var;
+ *    member_type *inner_ptr = &outer->member_name;
+ *
+ * Then, effectively:
+ *    outer_type *CONTAINER_OF(member_type *inner_ptr,
+ *                             *outer_var, // or type name for outer_type
+ *                             member_name);
+ *
+ * So that:
+ *    CONTAINER_OF(inner_ptr, *outer_var, member_name) == &outer
+ *    CONTAINER_OF(inner_ptr, outer_type, member_name) == &outer
+ */
+#define CONTAINER_OF(inner_ptr, outer, member_name)                     \
+    ({                                                                  \
+        typeof(outer) *container_of_;                                   \
+        container_of_ = (void*)((char*)(inner_ptr) -                    \
+                                offsetof(typeof(outer), member_name));  \
+        (void)(&container_of_->member_name ==                           \
+               (typeof(inner_ptr))0) /* type check */;                  \
+        container_of_;                                                  \
+    })
+
 
 /*
  * All of these assume (or define)
-- 
1.7.2.5

  parent reply	other threads:[~2012-01-13 19:25 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-13 19:25 [PATCH v7 0/9] libxl: New event API Ian Jackson
2012-01-13 19:25 ` [PATCH 1/9] libxl: New API for providing OS events to libxl Ian Jackson
2012-01-18 16:35   ` Ian Campbell
2012-01-18 17:06     ` Ian Jackson
2012-01-13 19:25 ` [PATCH 2/9] ocaml, libxl: support "private" fields Ian Jackson
2012-01-18 14:03   ` Ian Campbell
2012-01-13 19:25 ` [PATCH 3/9] libxl: New event generation API Ian Jackson
2012-01-18 17:33   ` Ian Campbell
2012-01-24 16:23     ` Ian Jackson
2012-01-24 16:38       ` Ian Campbell
2012-01-24 18:43         ` Ian Jackson
2012-01-13 19:25 ` [PATCH 4/9] libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec Ian Jackson
2012-01-13 19:25 ` [PATCH 5/9] libxl: Permit multithreaded event waiting Ian Jackson
2012-01-19 10:01   ` Ian Campbell
2012-01-24 16:34     ` Ian Jackson
2012-01-13 19:25 ` [PATCH 6/9] libxl: Asynchronous/long-running operation infrastructure Ian Jackson
2012-01-19 10:44   ` Ian Campbell
2012-01-24 17:27     ` Ian Jackson
2012-01-25 10:48       ` Ian Campbell
2012-01-25 14:45         ` Ian Jackson
2012-01-25 16:56           ` Ian Jackson
2012-01-13 19:25 ` Ian Jackson [this message]
2012-01-18 14:04   ` [PATCH 7/9] libxl: New convenience macro CONTAINER_OF Ian Campbell
2012-01-13 19:25 ` [PATCH 8/9] libxl: Introduce libxl__ev_devstate Ian Jackson
2012-01-19 10:54   ` Ian Campbell
2012-01-24 17:33     ` Ian Jackson
2012-01-25 10:57       ` Ian Campbell
2012-01-25 14:49         ` Ian Jackson
2012-01-13 19:25 ` [PATCH 9/9] libxl: Convert to asynchronous: device removal Ian Jackson
2012-01-19 11:55   ` Ian Campbell
2012-01-24 17:39     ` Ian Jackson

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=1326482728-10733-8-git-send-email-ian.jackson@eu.citrix.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /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).