PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
list.h
1#ifndef LIST_H_
2#define LIST_H_
3
4#include <stddef.h>
5
6#define container_of(ptr, type, member) ({ \
7 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
8 (type *)( (char *)__mptr - offsetof(type,member) ); })
9
11{
12 struct list_head *next, *prev;
13};
14
15static inline void list_init(struct list_head *list)
16{
17 list->next = list;
18 list->prev = list;
19}
20
21static inline void list_insert(struct list_head *list, struct list_head *node)
22{
23 struct list_head *prev = list->prev;
24
25 node->next = list;
26 node->prev = list->prev;
27 list->prev = node;
28 prev->next = node;
29}
30
31static inline int list_empty(struct list_head *list)
32{
33 return list == list->next;
34}
35
36static inline void list_join(struct list_head *prev, struct list_head *next)
37{
38 prev->next = next;
39 next->prev = prev;
40}
41
42static inline void list_remove(struct list_head *node)
43{
44 list_join(node->prev, node->next);
45 node->next = NULL;
46 node->prev = NULL;
47}
48
49static inline int list_node_is_last(struct list_head *list)
50{
51 return list->next == list->prev;
52}
53
54static inline int list_same_node(struct list_head *node1, struct list_head *node2)
55{
56 return node1 == node2;
57}
58
59#define list_first_entry(ptr, type, member) \
60 container_of((ptr)->next, type, member)
61
62#define list_next_entry(pos, member) \
63 container_of((pos)->member.next, typeof(*(pos)), member)
64
65#define list_entry_is_head(pos, head, member) \
66 (&(pos)->member == (head))
67
68#define list_for_each_it(var, list) \
69 for ((var) = (list)->next; \
70 !list_same_node((var), list); \
71 (var) = (var)->next)
72
73#define list_for_each(var, head, member) \
74 for ((var) = list_first_entry(head, typeof(*(var)), member); \
75 !list_entry_is_head(var, head, member); \
76 (var) = list_next_entry(var, member))
77
78#define list_for_each_safe_it(var, n, list) \
79 for ((var) = (list)->next, (n) = (var)->next; \
80 !list_same_node((var), list); \
81 (var) = (n), (n) = (var)->next)
82
83#define list_for_each_safe(var, head, member) \
84 typeof((var)) var##__safe; \
85 for ((var) = list_first_entry(head, typeof(*(var)), member), \
86 (var##__safe) = list_next_entry(var, member); \
87 !list_entry_is_head(var, head, member); \
88 (var) = (var##__safe), (var##__safe) = list_next_entry(var##__safe, member))
89
90static inline int list_length(struct list_head *list)
91{
92 struct list_head *i;
93 int len = 0;
94
95 list_for_each_it(i, list)
96 {
97 len++;
98 }
99
100 return len;
101}
102
103
104#endif // LIST_H_