-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
86 lines (60 loc) · 2.41 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include "List_Entry.h"
void offsetof_test(void){
struct list_objectExample test;
int len;
printf("struct len = %d\r\n", sizeof(struct list_objectExample));
len = offsetof(struct list_objectExample, data1);
printf("data1 offset = %d\r\n", len);
len = offsetof(struct list_objectExample, data2);
printf("data2 offset = %d\r\n", len);
len = offsetof(struct list_objectExample, string);
printf("string offset = %d\r\n", len);
}
void container_test(void){
struct list_objectExample container_test_init= { .data1 = 12, .data2=66, .string = "list_entry"};
uint8_t *char_ptr = &container_test_init.data1;
struct list_objectExample* test_object = container_of(char_ptr, struct list_objectExample, data1);
printf("init test struct address is: %p\r\n", &container_test_init);
printf("get test struct address is: %p\r\n", test_object);
printf("test_struct->data1 = %d\r\n"
"test_struct->data2 = %d\r\n"
"test_struct->string = %s\r\n"
,test_object->data1, test_object->data2, test_object->string);
}
void list_test(){
struct list_objectExample Head_node;
//初始化作为头结点
INIT_LIST_HEAD(&Head_node.list);
struct list_objectExample node1 = {1, 2, "node1"};
struct list_objectExample node2 = {3, 4, "node2"};
//添加两个节点
list_add(&node1.list, &(Head_node.list));
list_add(&node2.list, &(Head_node.list));
struct list_objectExample* struct_ptr;
//正向遍历节点, 并在每次遍历之后打印,
//该宏函数的内部用了for循环, 所以可以在后面加其他函数进行执行.
list_for_each_entry(struct_ptr, &(Head_node.list), list){
printf("current list string is: %s\r\n", struct_ptr->string);
};
printf("delete node1.\r\n");
list_del(&(node1.list));
list_for_each_entry(struct_ptr, &(Head_node.list), list){
printf("current list string is: %s\r\n", struct_ptr->string);
};
printf("add node1 to the tail of list.\r\n");
list_add_tail(&(node1.list), &(Head_node.list));
list_for_each_entry(struct_ptr, &(Head_node.list), list){
if(struct_ptr == &node1){
printf("node1 has been found.\r\n");
printf("node1.string: %s\r\n", node1.string);
}
};
}
int main() {
printf("Hello, World!\n");
// offsetof_test();
// container_test();
list_test();
return 0;
}