-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontainer_of.c
53 lines (43 loc) · 1.35 KB
/
container_of.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
struct s {
int one;
char two;
};
/*
* This function is intended to simulate a function that is passed a
* value for which it needs to get the struct that the value is a member
* of.
*/
void something(char* value) {
struct s* sp = container_of(value, struct s, two);
printf("struct for two:%p\n", sp);
printf("sp.two: %c\n", sp->two);
}
int main(int argc, char** argv) {
// the following is a gnu extension called brace-group within expression
// where the compiler will evaluate the complete block and use the last
// value in the block:
int nr = ( { int x = 10; x + 4; } );
printf("nr:%d\n", nr);
// Notice that this is used in the container_of macro, it returns the
// value from the last statement.
//
int x = 1;
typeof(x) y = 2;
printf("%d %d\n", x, y);
int* p = &((struct s*)0)->one;
char* p2 = &((struct s*)0)->two;
printf("%p\n", p);
printf("%p\n", p2);
struct s s1;
struct s* sp = container_of(&s1.two, struct s, two);
struct s s2 = {1, 'a'};
printf("s=%p\n", &s2);
printf("s.two=%c\n", s2.two);
something(&s2.two);
return 0;
}