mbgtools-lx  4.2.8
mbgklist.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: mbgklist.h 1.4 2018/09/13 05:27:28 thomas-b REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Userspace implementation of Linux Kernel's list.h
10  *
11  * -----------------------------------------------------------------------
12  * $Log: mbgklist.h $
13  * Revision 1.4 2018/09/13 05:27:28 thomas-b
14  * Added macro to get nth item of mbgklist
15  * Revision 1.3 2017/07/05 09:52:39 martin
16  * Safe loop macros added by philipp.
17  * Check if 'typeof' is supported based on the type of compiler.
18  * Reformatted code to conform to standard header file format.
19  * Updated function prototypes.
20  * Revision 1.2 2015/10/06 07:08:45 philipp
21  * Added functions to loop containers of list entries
22  * Revision 1.1 2015/09/09 10:42:27 martin
23  * Initial revision.
24  *
25  **************************************************************************/
26 
27 #ifndef _MBGKLIST_H
28 #define _MBGKLIST_H
29 
30 /* Other headers to be included */
31 
32 #include <mbg_cof.h>
33 
34 
35 #ifdef _MBGKLIST
36  #define _ext
37  #define _DO_INIT
38 #else
39  #define _ext extern
40 #endif
41 
42 
43 /* Start of header body */
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 
50 #define MBG_KLIST_INIT(name) { &(name), &(name) }
51 
52 #define MBG_KLIST_DECLARE(name) \
53  struct mbg_klist_head name = MBG_KLIST_INIT(name)
54 
55 #define mbg_klist_for_each(head, pos) \
56  for (pos = (head)->next; pos != (head); pos = pos->next)
57 
58 #define mbg_klist_for_each_safe(head, pos, n) \
59  for (pos = (head)->next, n = (pos)->next; \
60  pos != (head); \
61  pos = n, n = pos->next)
62 
63 #define mbg_klist_for_each_rev(head, pos) \
64  for (pos = (head)->prev; pos != (head); pos = pos->prev)
65 
66 #define mbg_klist_for_each_rev_safe(head, pos, n) \
67  for (pos = (head)->prev, n = (pos)->prev; \
68  pos != (head); \
69  pos = n, n = pos->prev)
70 
71 #define mbg_klist_nth_item(head, pos, n) \
72  do { \
73  unsigned i; \
74  for (pos = (head)->next, i = 0; i < n; pos = pos->next, ++i); \
75  } while ( 0 )
76 
77 #define mbg_klist_entry(ptr, type, member) \
78  mbg_container_of(ptr, type, member)
79 
80 #define mbg_klist_first_entry(ptr, type, member) \
81  mbg_klist_entry((ptr)->next, type, member)
82 
83 #define mbg_klist_last_entry(ptr, type, member) \
84  mbg_klist_entry((ptr)->prev, type, member)
85 
86 
87 
88 #if defined( __GNUC__ ) || defined( __clang__ ) // "typeof" supported
89 
90 #define mbg_klist_next_entry(pos, member) \
91  mbg_klist_entry((pos)->member.next, typeof(*pos), member)
92 
93 #define mbg_klist_prev_entry(pos, member) \
94  mbg_klist_entry((pos)->member.prev, typeof(*pos), member)
95 
96 #define mbg_klist_for_each_entry(head, pos, member) \
97  for (pos = mbg_klist_first_entry(head, typeof(*pos), member); \
98  &pos->member != (head); \
99  pos = mbg_klist_next_entry(pos, member))
100 
101 #define mbg_klist_for_each_entry_rev(head, pos, member) \
102  for (pos = mbg_klist_last_entry(head, typeof(*pos), member); \
103  &pos->member != (head); \
104  pos = mbg_klist_prev_entry(pos, member))
105 
106 #define mbg_klist_for_each_entry_safe(head, pos, n, member) \
107  for (pos = mbg_klist_first_entry(head, typeof(*pos), member), \
108  n = mbg_klist_next_entry(pos, member); \
109  &pos->member != (head); \
110  pos = n, n = mbg_klist_next_entry(pos, member))
111 
112 #define mbg_klist_for_each_entry_rev_safe(head, pos, n, member) \
113  for (pos = mbg_klist_last_entry(head, typeof(*pos), member), \
114  n = mbg_klist_prev_entry(pos, member); \
115  &pos->member != (head); \
116  pos = n, n = mbg_klist_prev_entry(pos, member))
117 
118 #endif
119 
120 
121 
123 {
126 };
127 
128 
129 
130 static __mbg_inline
131 void mbg_klist_init( struct mbg_klist_head *head )
132 {
133  head->next = head;
134  head->prev = head;
135 }
136 
137 
138 static __mbg_inline
140 {
141  next->prev = item;
142  item->next = next;
143  item->prev = prev;
144  prev->next = item;
145 }
146 
147 
148 static __mbg_inline
149 void mbg_klist_prepend_item( struct mbg_klist_head *head, struct mbg_klist_head *item )
150 {
151  __mbg_klist_add_item( item, head, head->next );
152 }
153 
154 
155 static __mbg_inline
156 void mbg_klist_append_item( struct mbg_klist_head *head, struct mbg_klist_head *item )
157 {
158  __mbg_klist_add_item( item, head->prev, head );
159 }
160 
161 
162 static __mbg_inline
164 {
165  next->prev = prev;
166  prev->next = next;
167 }
168 
169 
170 static __mbg_inline
172 {
173  __mbg_klist_delete_item( item->prev, item->next );
174 }
175 
176 
177 static __mbg_inline
179 {
180  __mbg_klist_delete_item( item->prev, item->next );
181  mbg_klist_init( item );
182 }
183 
184 
185 static __mbg_inline
186 void mbg_klist_replace_item( struct mbg_klist_head *old, struct mbg_klist_head *item )
187 {
188  item->next = old->next;
189  item->next->prev = item;
190  item->prev = old->prev;
191  item->prev->next = item;
192 }
193 
194 
195 static __mbg_inline
197 {
198  mbg_klist_replace_item( old, item );
199  mbg_klist_init( item );
200 }
201 
202 
203 static __mbg_inline
205 {
206  mbg_klist_delete_item( item );
207  mbg_klist_prepend_item( head, item );
208 }
209 
210 
211 static __mbg_inline
212 void mbg_klist_move_append_item( struct mbg_klist_head *head, struct mbg_klist_head *item )
213 {
214  mbg_klist_delete_item( item );
215  mbg_klist_append_item( head, item );
216 }
217 
218 
219 static __mbg_inline
220 int mbg_klist_is_first( const struct mbg_klist_head *head, const struct mbg_klist_head *item )
221 {
222  return ( ( item->prev == head ) ? 1 : 0 );
223 }
224 
225 
226 static __mbg_inline
227 int mbg_klist_is_last( const struct mbg_klist_head *head, const struct mbg_klist_head *item )
228 {
229  return ( ( item->next == head ) ? 1 : 0 );
230 }
231 
232 
233 static __mbg_inline
234 int mbg_klist_is_empty( const struct mbg_klist_head *head )
235 {
236  return ( ( head->next == head ) ? 1 : 0 );
237 }
238 
239 
240 static __mbg_inline
241 void __mbg_klist_add_list( const struct mbg_klist_head *list, struct mbg_klist_head *prev, struct mbg_klist_head *next )
242 {
243  struct mbg_klist_head *first = list->next;
244  struct mbg_klist_head *last = list->prev;
245 
246  first->prev = prev;
247  prev->next = first;
248 
249  last->next = next;
250  next->prev = last;
251 }
252 
253 
254 static __mbg_inline
255 void mbg_klist_prepend_list( struct mbg_klist_head *head, const struct mbg_klist_head *list )
256 {
257  if ( !mbg_klist_is_empty( list ) )
258  __mbg_klist_add_list( list, head, head->next );
259 }
260 
261 
262 static __mbg_inline
263 void mbg_klist_append_list( struct mbg_klist_head *head, const struct mbg_klist_head *list )
264 {
265  if ( !mbg_klist_is_empty( list ) )
266  __mbg_klist_add_list( list, head->prev, head );
267 }
268 
269 
270 static __mbg_inline
272 {
273  if ( !mbg_klist_is_empty( list ) )
274  {
275  __mbg_klist_add_list( list, head, head->next );
276  mbg_klist_init( list );
277  }
278 }
279 
280 
281 static __mbg_inline
282 void mbg_klist_append_list_init( struct mbg_klist_head *head, struct mbg_klist_head *list )
283 {
284  if ( !mbg_klist_is_empty( list ) )
285  {
286  __mbg_klist_add_list( list, head->prev, head );
287  mbg_klist_init( list );
288  }
289 }
290 
291 
292 /* ----- function prototypes begin ----- */
293 
294 /* This section was generated automatically */
295 /* by MAKEHDR, do not remove the comments. */
296 
312  void mbg_klist_sort( void *priv, struct mbg_klist_head *head, int (*cmp)( void *priv, struct mbg_klist_head *a, struct mbg_klist_head *b ) ) ;
313 
314 
315 /* ----- function prototypes end ----- */
316 
317 #ifdef __cplusplus
318 }
319 #endif
320 
321 /* End of header body */
322 
323 #undef _ext
324 #undef _DO_INIT
325 
326 #endif /* _MBGKLIST_H */
static __mbg_inline void mbg_klist_append_list(struct mbg_klist_head *head, const struct mbg_klist_head *list)
Definition: mbgklist.h:263
static __mbg_inline int mbg_klist_is_empty(const struct mbg_klist_head *head)
Definition: mbgklist.h:234
static __mbg_inline void __mbg_klist_add_item(struct mbg_klist_head *item, struct mbg_klist_head *prev, struct mbg_klist_head *next)
Definition: mbgklist.h:139
static __mbg_inline void mbg_klist_move_append_item(struct mbg_klist_head *head, struct mbg_klist_head *item)
Definition: mbgklist.h:212
static __mbg_inline void mbg_klist_append_item(struct mbg_klist_head *head, struct mbg_klist_head *item)
Definition: mbgklist.h:156
static __mbg_inline void mbg_klist_replace_item(struct mbg_klist_head *old, struct mbg_klist_head *item)
Definition: mbgklist.h:186
static __mbg_inline void mbg_klist_append_list_init(struct mbg_klist_head *head, struct mbg_klist_head *list)
Definition: mbgklist.h:282
static __mbg_inline void mbg_klist_init(struct mbg_klist_head *head)
Definition: mbgklist.h:131
static __mbg_inline int mbg_klist_is_first(const struct mbg_klist_head *head, const struct mbg_klist_head *item)
Definition: mbgklist.h:220
struct mbg_klist_head * next
Definition: mbgklist.h:125
static __mbg_inline int mbg_klist_is_last(const struct mbg_klist_head *head, const struct mbg_klist_head *item)
Definition: mbgklist.h:227
static __mbg_inline void mbg_klist_replace_item_init(struct mbg_klist_head *old, struct mbg_klist_head *item)
Definition: mbgklist.h:196
static __mbg_inline void mbg_klist_prepend_item(struct mbg_klist_head *head, struct mbg_klist_head *item)
Definition: mbgklist.h:149
void mbg_klist_sort(void *priv, struct mbg_klist_head *head, int(*cmp)(void *priv, struct mbg_klist_head *a, struct mbg_klist_head *b))
Sort a list.
static __mbg_inline void mbg_klist_move_prepend_item(struct mbg_klist_head *head, struct mbg_klist_head *item)
Definition: mbgklist.h:204
struct mbg_klist_head * prev
Definition: mbgklist.h:124
static __mbg_inline void mbg_klist_prepend_list(struct mbg_klist_head *head, const struct mbg_klist_head *list)
Definition: mbgklist.h:255
static __mbg_inline void mbg_klist_delete_item_init(struct mbg_klist_head *item)
Definition: mbgklist.h:178
static __mbg_inline void mbg_klist_delete_item(struct mbg_klist_head *item)
Definition: mbgklist.h:171
static __mbg_inline void __mbg_klist_delete_item(struct mbg_klist_head *prev, struct mbg_klist_head *next)
Definition: mbgklist.h:163
static __mbg_inline void __mbg_klist_add_list(const struct mbg_klist_head *list, struct mbg_klist_head *prev, struct mbg_klist_head *next)
Definition: mbgklist.h:241
static __mbg_inline void mbg_klist_prepend_list_init(struct mbg_klist_head *head, struct mbg_klist_head *list)
Definition: mbgklist.h:271