chiark / gitweb /
shared: make container_of() use unique variable names
[elogind.git] / src / shared / macro.h
index 179b24c9838971d1609d1ce74d027f497ea55801..e6734804bd36a8efb7079e02dcdcfeefdce014e9 100644 (file)
@@ -79,7 +79,8 @@
 #define XCONCATENATE(x, y) x ## y
 #define CONCATENATE(x, y) XCONCATENATE(x, y)
 
-#define UNIQUE(prefix) CONCATENATE(prefix, __LINE__)
+#define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
+#define UNIQ __COUNTER__
 
 /* Rounds up */
 
@@ -124,13 +125,13 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
  * @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(ptr, type, member)                                 \
+#define container_of(ptr, type, member) __container_of(UNIQ, (ptr), type, member)
+#define __container_of(uniq, ptr, type, member)                         \
         __extension__ ({                                                \
-                        const typeof( ((type *)0)->member ) *__mptr = (ptr); \
-                        (type *)( (char *)__mptr - offsetof(type,member) ); \
-                })
+                const typeof( ((type*)0)->member ) *UNIQ_T(A, uniq) = (ptr); \
+                (type*)( (char *)UNIQ_T(A, uniq) - offsetof(type,member) ); \
+        })
 
 #undef MAX
 #define MAX(a,b)                                        \
@@ -149,6 +150,9 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
                 ((_A) > (_B)) ? (_A) : (_B),                            \
                 (void)0))
 
+/* takes two types and returns the size of the larger one */
+#define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; }))
+
 #define MAX3(x,y,z)                                     \
         __extension__ ({                                \
                         const typeof(x) _c = MAX(x,y);  \
@@ -216,7 +220,7 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
 #else
 #define assert_cc(expr)                                                 \
         DISABLE_WARNING_DECLARATION_AFTER_STATEMENT;                    \
-        struct UNIQUE(_assert_struct_) {                                \
+        struct CONCATENATE(_assert_struct_, __LINE__) {                 \
                 char x[(expr) ? 0 : -1];                                \
         };                                                              \
         REENABLE_WARNING