]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_list.h
uLUt: do not include malloc.h, use stdlib.h through ul_utmalloc.h.
[ulut.git] / ulut / ul_list.h
1 #ifndef _UL_LISTS_H
2 #define _UL_LISTS_H
3
4 #include "ul_utdefs.h"
5 #include "ul_listbase.h"
6 #include "ul_itbase.h"
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 typedef struct list_head ul_list_node_t;
13 typedef struct list_head ul_list_head_t;
14
15 #define UL_LIST_CUST_DEC(cust_prefix, cust_head_t, cust_item_t,\
16                 cust_head_field, cust_node_field) \
17 \
18 static inline cust_item_t * \
19 cust_prefix##_node2item(const cust_head_t *head, const ul_list_node_t *node) \
20 {\
21   (void)head;\
22   return UL_CONTAINEROF(node, cust_item_t, cust_node_field);\
23 }\
24 \
25 static inline void \
26 cust_prefix##_init_head(cust_head_t *head)\
27 {\
28   INIT_LIST_HEAD(&head->cust_head_field);\
29 }\
30 static inline void \
31 cust_prefix##_init_detached(cust_item_t *item){\
32   INIT_LIST_HEAD(&item->cust_node_field);\
33 }\
34 static inline cust_item_t *\
35 cust_prefix##_first(const cust_head_t *head)\
36 {\
37   ul_list_node_t *n=head->cust_head_field.next;\
38   return (n!=&head->cust_head_field)?cust_prefix##_node2item(head,n):NULL;\
39 }\
40 static inline cust_item_t *\
41 cust_prefix##_last(const cust_head_t *head)\
42 {\
43   ul_list_node_t *n=head->cust_head_field.prev;\
44   return (n!=&head->cust_head_field)?cust_prefix##_node2item(head,n):NULL;\
45 }\
46 static inline cust_item_t *\
47 cust_prefix##_next(const cust_head_t *head, const cust_item_t *item)\
48 {\
49   ul_list_node_t *n=item->cust_node_field.next;\
50   return (n!=&head->cust_head_field)?cust_prefix##_node2item(head,n):NULL;\
51 }\
52 static inline cust_item_t *\
53 cust_prefix##_prev(const cust_head_t *head, const cust_item_t *item)\
54 {\
55   ul_list_node_t *n=item->cust_node_field.prev;\
56   return (n!=&head->cust_head_field)?cust_prefix##_node2item(head,n):NULL;\
57 }\
58 static inline int \
59 cust_prefix##_is_empty(const cust_head_t *head)\
60 {\
61   return head->cust_head_field.next==&head->cust_head_field;\
62 }\
63 static inline void \
64 cust_prefix##_ins_head(cust_head_t *head, cust_item_t *item)\
65 {\
66   list_add(&item->cust_node_field, &head->cust_head_field);\
67 }\
68 static inline void \
69 cust_prefix##_ins_tail(cust_head_t *head, cust_item_t *item)\
70 {\
71   list_add_tail(&item->cust_node_field, &head->cust_head_field);\
72 }\
73 static inline void \
74 cust_prefix##_insert(cust_head_t *head, cust_item_t *item)\
75 {\
76   cust_prefix##_ins_tail(head, item);\
77 }\
78 static inline void \
79 cust_prefix##_delete(cust_head_t *head, cust_item_t *item)\
80 {\
81   (void)head;\
82   list_del_init(&item->cust_node_field);\
83 }\
84 static inline void \
85 cust_prefix##_del_item(cust_item_t *item)\
86 {\
87   list_del_init(&item->cust_node_field);\
88 }\
89 static inline cust_item_t *\
90 cust_prefix##_cut_first(cust_head_t *head)\
91 {\
92   ul_list_node_t *n=head->cust_head_field.next;\
93   if(n==&head->cust_head_field) return NULL;\
94   list_del_init(n);\
95   return cust_prefix##_node2item(head,n);\
96 }\
97 /*** Iterators ***/\
98 UL_ITBASE_UL_DEC(cust_prefix, cust_head_t, cust_item_t)
99
100
101 #define ul_list_for_each(cust_prefix, head, ptr) \
102         for(ptr=cust_prefix##_first(head);ptr;ptr=cust_prefix##_next((head),ptr))
103
104 #define ul_list_for_each_rev(cust_prefix, head, ptr) \
105         for(ptr=cust_prefix##_last(head);ptr;ptr=cust_prefix##_prev((head),ptr))
106
107 #define ul_list_for_each_cut(cust_prefix, head, ptr) \
108         for(;(ptr=cust_prefix##_cut_first(head));)
109
110 #ifdef __cplusplus
111 } /* extern "C"*/
112 #endif
113
114 #endif /* _UL_LISTS_H */