* defined central in compiler.h, no code duplication
* offsetof() uses builtin in clang and icc now (fixes icc warning), and
stddef.h default else (e.g. tcc)
* container_of() has non-gcc fallback (works with tcc)
---
compiler.h | 25 +++++++++++++++++++++++++
list.h | 18 +-----------------
rbtree.h | 15 +--------------
3 files changed, 27 insertions(+), 31 deletions(-)
diff --git a/compiler.h b/compiler.h
index 98f6566..87fc3ed 100644
--- a/compiler.h
+++ b/compiler.h
@@ -5,11 +5,16 @@
#ifndef COMPILER_H
#define COMPILER_H
+#include <stddef.h>
+
/*
* GCC 2.96 or compatible required
*/
#if defined(__GNUC__)
+#undef offsetof
+#define offsetof(type, member) __builtin_offsetof(type, member)
+
/* Optimization: Condition @x is likely */
#define likely(x) __builtin_expect(!!(x), 1)
@@ -41,4 +46,24 @@
#endif
+
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ *
+ * @ptr: the pointer to the member.
+ * @type: the type of the container struct this is embedded in.
+ * @member: the name of the member within the struct.
+ *
+ */
+#define container_of_portable(ptr, type, member) \
+ ((type *)( (char *)(ptr) - offsetof(type,member) ))
+#undef container_of
+#if defined(__GNUC__)
+#define container_of(ptr, type, member) ({ \
+ const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
+ container_of_portable(__mptr, type, member);})
+#else
+#define container_of(ptr, type, member) container_of_portable(ptr, type, member)
+#endif
+
#endif
diff --git a/list.h b/list.h
index 0a28cf2..0abf70a 100644
--- a/list.h
+++ b/list.h
@@ -2,7 +2,7 @@
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
-#include <stdlib.h>
+#include "compiler.h" /* container_of */
static inline void prefetch(const void *x)
{
@@ -192,22 +192,6 @@ static inline void list_splice_init(struct list_head *list,
}
}
-#undef offsetof
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-
-/**
- * container_of - cast a member of a structure out to the containing structure
- *
- * @ptr: the pointer to the member.
- * @type: the type of the container struct this is embedded in.
- * @member: the name of the member within the struct.
- *
- */
-#undef container_of
-#define container_of(ptr, type, member) ({ \
- const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - offsetof(type,member) );})
-
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
diff --git a/rbtree.h b/rbtree.h
index 2554be6..cc7020f 100644
--- a/rbtree.h
+++ b/rbtree.h
@@ -96,22 +96,9 @@ static inline struct page * rb_insert_page_cache(struct inode * inode,
#ifndef _LINUX_RBTREE_H
#define _LINUX_RBTREE_H
+#include "compiler.h" /* container_of */
#include <stddef.h>
-/**
- * container_of - cast a member of a structure out to the containing structure
- *
- * @ptr: the pointer to the member.
- * @type: the type of the container struct this is embedded in.
- * @member: the name of the member within the struct.
- *
- */
-#undef container_of
-#define container_of(ptr, type, member) ({ \
- const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - offsetof(type,member) );})
-
-
struct rb_node
{
unsigned long rb_parent_color;
--
1.7.2.3