Browse Source

Added container_of(). This seems about the best place to put it, since

the definition of it uses offsetof(), and the two concepts are related.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
508dcdb303
1 changed files with 7 additions and 2 deletions
  1. 7
    2
      src/include/stddef.h

+ 7
- 2
src/include/stddef.h View File

2
 #define STDDEF_H
2
 #define STDDEF_H
3
 
3
 
4
 /* for size_t */
4
 /* for size_t */
5
-#include "stdint.h"
5
+#include <stdint.h>
6
 
6
 
7
 #undef NULL
7
 #undef NULL
8
-#define NULL	((void *)0)
8
+#define NULL ((void *)0)
9
 
9
 
10
 #undef offsetof
10
 #undef offsetof
11
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
11
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
12
 
12
 
13
+#undef container_of
14
+#define container_of(ptr, type, member) ({                      \
15
+	const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
16
+	(type *)( (char *)__mptr - offsetof(type,member) );})
17
+
13
 #endif /* STDDEF_H */
18
 #endif /* STDDEF_H */

Loading…
Cancel
Save