Whamcloud - gitweb
LU-8854 llapi: remove lustre specific strlcpy & strlcat functions
[fs/lustre-release.git] / libcfs / include / libcfs / util / list.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22
23 #ifndef __LIBCFS_UTIL_LIST_H__
24 #define __LIBCFS_UTIL_LIST_H__
25
26 /*
27  * Simple doubly linked list implementation.
28  *
29  * Some of the internal functions ("__xxx") are useful when
30  * manipulating whole lists rather than single entries, as
31  * sometimes we already know the next/prev entries and we can
32  * generate better code by using them directly rather than
33  * using the generic single-entry routines.
34  */
35
36 #define prefetch(a) ((void)a)
37
38 struct list_head {
39         struct list_head *next, *prev;
40 };
41
42 #define LIST_HEAD_INIT(name) { &(name), &(name) }
43
44 #define INIT_LIST_HEAD(ptr) do { \
45         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
46 } while (0)
47
48 /**
49  * Insert a new entry between two known consecutive entries.
50  *
51  * This is only for internal list manipulation where we know
52  * the prev/next entries already!
53  */
54 static inline void __list_add(struct list_head * new,
55                                   struct list_head * prev,
56                                   struct list_head * next)
57 {
58         next->prev = new;
59         new->next = next;
60         new->prev = prev;
61         prev->next = new;
62 }
63
64 /**
65  * Insert an entry at the start of a list.
66  * \param new  new entry to be inserted
67  * \param head list to add it to
68  *
69  * Insert a new entry after the specified head.
70  * This is good for implementing stacks.
71  */
72 static inline void list_add(struct list_head *new,
73                                 struct list_head *head)
74 {
75         __list_add(new, head, head->next);
76 }
77
78 /**
79  * Insert an entry at the end of a list.
80  * \param new  new entry to be inserted
81  * \param head list to add it to
82  *
83  * Insert a new entry before the specified head.
84  * This is useful for implementing queues.
85  */
86 static inline void list_add_tail(struct list_head *new,
87                                      struct list_head *head)
88 {
89         __list_add(new, head->prev, head);
90 }
91
92 /*
93  * Delete a list entry by making the prev/next entries
94  * point to each other.
95  *
96  * This is only for internal list manipulation where we know
97  * the prev/next entries already!
98  */
99 static inline void __list_del(struct list_head *prev,
100                                   struct list_head *next)
101 {
102         next->prev = prev;
103         prev->next = next;
104 }
105
106 /**
107  * Remove an entry from the list it is currently in.
108  * \param entry the entry to remove
109  * Note: list_empty(entry) does not return true after this, the entry is in an
110  * undefined state.
111  */
112 static inline void list_del(struct list_head *entry)
113 {
114         __list_del(entry->prev, entry->next);
115 }
116
117 /**
118  * Remove an entry from the list it is currently in and reinitialize it.
119  * \param entry the entry to remove.
120  */
121 static inline void list_del_init(struct list_head *entry)
122 {
123         __list_del(entry->prev, entry->next);
124         INIT_LIST_HEAD(entry);
125 }
126
127 /**
128  * Remove an entry from the list it is currently in and insert it at the start
129  * of another list.
130  * \param list the entry to move
131  * \param head the list to move it to
132  */
133 static inline void list_move(struct list_head *list,
134                                  struct list_head *head)
135 {
136         __list_del(list->prev, list->next);
137         list_add(list, head);
138 }
139
140 /**
141  * Remove an entry from the list it is currently in and insert it at the end of
142  * another list.
143  * \param list the entry to move
144  * \param head the list to move it to
145  */
146 static inline void list_move_tail(struct list_head *list,
147                                       struct list_head *head)
148 {
149         __list_del(list->prev, list->next);
150         list_add_tail(list, head);
151 }
152
153 /**
154  * Test whether a list is empty
155  * \param head the list to test.
156  */
157 static inline int list_empty(struct list_head *head)
158 {
159         return head->next == head;
160 }
161
162 /**
163  * Test whether a list is empty and not being modified
164  * \param head the list to test
165  *
166  * Tests whether a list is empty _and_ checks that no other CPU might be
167  * in the process of modifying either member (next or prev)
168  *
169  * NOTE: using list_empty_careful() without synchronization
170  * can only be safe if the only activity that can happen
171  * to the list entry is list_del_init(). Eg. it cannot be used
172  * if another CPU could re-list_add() it.
173  */
174 static inline int list_empty_careful(const struct list_head *head)
175 {
176         struct list_head *next = head->next;
177         return (next == head) && (next == head->prev);
178 }
179
180 static inline void __list_splice(struct list_head *list,
181                                      struct list_head *head)
182 {
183         struct list_head *first = list->next;
184         struct list_head *last = list->prev;
185         struct list_head *at = head->next;
186
187         first->prev = head;
188         head->next = first;
189
190         last->next = at;
191         at->prev = last;
192 }
193
194 /**
195  * Join two lists
196  * \param list the new list to add.
197  * \param head the place to add it in the first list.
198  *
199  * The contents of \a list are added at the start of \a head.  \a list is in an
200  * undefined state on return.
201  */
202 static inline void list_splice(struct list_head *list,
203                                    struct list_head *head)
204 {
205         if (!list_empty(list))
206                 __list_splice(list, head);
207 }
208
209 static inline void list_splice_tail(struct list_head *list, struct list_head *head)
210 {
211         if (!list_empty(list))
212                 __list_splice(list, head->prev);
213 }
214
215 /**
216  * Join two lists and reinitialise the emptied list.
217  * \param list the new list to add.
218  * \param head the place to add it in the first list.
219  *
220  * The contents of \a list are added at the start of \a head.  \a list is empty
221  * on return.
222  */
223 static inline void list_splice_init(struct list_head *list,
224                                         struct list_head *head)
225 {
226         if (!list_empty(list)) {
227                 __list_splice(list, head);
228                 INIT_LIST_HEAD(list);
229         }
230 }
231
232 /**
233  * Get the container of a list
234  * \param ptr    the embedded list.
235  * \param type   the type of the struct this is embedded in.
236  * \param member the member name of the list within the struct.
237  */
238 #define list_entry(ptr, type, member) \
239         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
240
241 /**
242  * Iterate over a list
243  * \param pos   the iterator
244  * \param head  the list to iterate over
245  *
246  * Behaviour is undefined if \a pos is removed from the list in the body of the
247  * loop.
248  */
249 #define list_for_each(pos, head) \
250         for (pos = (head)->next, prefetch(pos->next); pos != (head); \
251                 pos = pos->next, prefetch(pos->next))
252
253 /**
254  * Iterate over a list safely
255  * \param pos   the iterator
256  * \param n     temporary storage
257  * \param head  the list to iterate over
258  *
259  * This is safe to use if \a pos could be removed from the list in the body of
260  * the loop.
261  */
262 #define list_for_each_safe(pos, n, head) \
263         for (pos = (head)->next, n = pos->next; pos != (head); \
264                 pos = n, n = pos->next)
265
266 /**
267  * Iterate over a list continuing after existing point
268  * \param pos    the type * to use as a loop counter
269  * \param head   the list head
270  * \param member the name of the list_struct within the struct
271  */
272 #define list_for_each_entry_continue(pos, head, member)                 \
273         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
274              prefetch(pos->member.next), &pos->member != (head);        \
275              pos = list_entry(pos->member.next, typeof(*pos), member))
276
277 /**
278  * \defgroup hlist Hash List
279  * Double linked lists with a single pointer list head.
280  * Mostly useful for hash tables where the two pointer list head is too
281  * wasteful.  You lose the ability to access the tail in O(1).
282  * @{
283  */
284
285 struct hlist_node {
286         struct hlist_node *next, **pprev;
287 };
288
289 struct hlist_head {
290         struct hlist_node *first;
291 };
292
293 /* @} */
294
295 /*
296  * "NULL" might not be defined at this point
297  */
298 #ifdef NULL
299 #define NULL_P NULL
300 #else
301 #define NULL_P ((void *)0)
302 #endif
303
304 /**
305  * \addtogroup hlist
306  * @{
307  */
308
309 #define HLIST_HEAD_INIT { NULL_P }
310 #define HLIST_HEAD(name) struct hlist_head name = { NULL_P }
311 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
312 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
313
314 static inline int hlist_unhashed(const struct hlist_node *h)
315 {
316         return !h->pprev;
317 }
318
319 static inline int hlist_empty(const struct hlist_head *h)
320 {
321         return !h->first;
322 }
323
324 static inline void __hlist_del(struct hlist_node *n)
325 {
326         struct hlist_node *next = n->next;
327         struct hlist_node **pprev = n->pprev;
328         *pprev = next;
329         if (next)
330                 next->pprev = pprev;
331 }
332
333 static inline void hlist_del(struct hlist_node *n)
334 {
335         __hlist_del(n);
336 }
337
338 static inline void hlist_del_init(struct hlist_node *n)
339 {
340         if (n->pprev)  {
341                 __hlist_del(n);
342                 INIT_HLIST_NODE(n);
343         }
344 }
345
346 static inline void hlist_add_head(struct hlist_node *n,
347                                       struct hlist_head *h)
348 {
349         struct hlist_node *first = h->first;
350         n->next = first;
351         if (first)
352                 first->pprev = &n->next;
353         h->first = n;
354         n->pprev = &h->first;
355 }
356
357 /* next must be != NULL */
358 static inline void hlist_add_before(struct hlist_node *n,
359                                         struct hlist_node *next)
360 {
361         n->pprev = next->pprev;
362         n->next = next;
363         next->pprev = &n->next;
364         *(n->pprev) = n;
365 }
366
367 static inline void hlist_add_after(struct hlist_node *n,
368                                        struct hlist_node *next)
369 {
370         next->next = n->next;
371         n->next = next;
372         next->pprev = &n->next;
373
374         if(next->next)
375                 next->next->pprev  = &next->next;
376 }
377
378 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
379
380 #define hlist_for_each(pos, head) \
381         for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
382              pos = pos->next)
383
384 #define hlist_for_each_safe(pos, n, head) \
385         for (pos = (head)->first; pos && (n = pos->next, 1); \
386              pos = n)
387
388 /**
389  * Iterate over an hlist of given type
390  * \param tpos   the type * to use as a loop counter.
391  * \param pos    the &struct hlist_node to use as a loop counter.
392  * \param head   the head for your list.
393  * \param member the name of the hlist_node within the struct.
394  */
395 #define hlist_for_each_entry(tpos, pos, head, member)                    \
396         for (pos = (head)->first;                                            \
397              pos && ({ prefetch(pos->next); 1;}) &&                          \
398                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
399              pos = pos->next)
400
401 /**
402  * Iterate over an hlist continuing after existing point
403  * \param tpos   the type * to use as a loop counter.
404  * \param pos    the &struct hlist_node to use as a loop counter.
405  * \param member the name of the hlist_node within the struct.
406  */
407 #define hlist_for_each_entry_continue(tpos, pos, member)                 \
408         for (pos = (pos)->next;                                              \
409              pos && ({ prefetch(pos->next); 1;}) &&                          \
410                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
411              pos = pos->next)
412
413 /**
414  * Iterate over an hlist continuing from an existing point
415  * \param tpos   the type * to use as a loop counter.
416  * \param pos    the &struct hlist_node to use as a loop counter.
417  * \param member the name of the hlist_node within the struct.
418  */
419 #define hlist_for_each_entry_from(tpos, pos, member)                     \
420         for (; pos && ({ prefetch(pos->next); 1;}) &&                        \
421                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
422              pos = pos->next)
423
424 /**
425  * Iterate over an hlist of given type safe against removal of list entry
426  * \param tpos   the type * to use as a loop counter.
427  * \param pos    the &struct hlist_node to use as a loop counter.
428  * \param n      another &struct hlist_node to use as temporary storage
429  * \param head   the head for your list.
430  * \param member the name of the hlist_node within the struct.
431  */
432 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
433         for (pos = (head)->first;                                            \
434              pos && ({ n = pos->next; 1; }) &&                               \
435                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
436              pos = n)
437
438 /* @} */
439
440 /**
441  * Iterate over a list in reverse order
442  * \param pos   the &struct list_head to use as a loop counter.
443  * \param head  the head for your list.
444  */
445 #define list_for_each_prev(pos, head) \
446         for (pos = (head)->prev, prefetch(pos->prev); pos != (head);     \
447                 pos = pos->prev, prefetch(pos->prev))
448
449 /**
450  * Iterate over a list of given type
451  * \param pos        the type * to use as a loop counter.
452  * \param head       the head for your list.
453  * \param member     the name of the list_struct within the struct.
454  */
455 #define list_for_each_entry(pos, head, member)                          \
456         for (pos = list_entry((head)->next, typeof(*pos), member),      \
457                      prefetch(pos->member.next);                            \
458              &pos->member != (head);                                        \
459              pos = list_entry(pos->member.next, typeof(*pos), member),  \
460              prefetch(pos->member.next))
461
462 /**
463  * Iterate backwards over a list of given type.
464  * \param pos        the type * to use as a loop counter.
465  * \param head       the head for your list.
466  * \param member     the name of the list_struct within the struct.
467  */
468 #define list_for_each_entry_reverse(pos, head, member)                  \
469         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
470              prefetch(pos->member.prev), &pos->member != (head);            \
471              pos = list_entry(pos->member.prev, typeof(*pos), member))
472
473 /**
474  * Iterate over a list of given type safe against removal of list entry
475  * \param pos        the type * to use as a loop counter.
476  * \param n          another type * to use as temporary storage
477  * \param head       the head for your list.
478  * \param member     the name of the list_struct within the struct.
479  */
480 #define list_for_each_entry_safe(pos, n, head, member)                   \
481         for (pos = list_entry((head)->next, typeof(*pos), member),       \
482                 n = list_entry(pos->member.next, typeof(*pos), member);  \
483              &pos->member != (head);                                         \
484              pos = n, n = list_entry(n->member.next, typeof(*n), member))
485
486 /**
487  * Iterate backwards over a list of given type safely against removal of entry
488  * \param pos        the type * to use as a loop counter.
489  * \param n          another type * to use as temporary storage
490  * \param head       the head for your list.
491  * \param member     the name of the list_struct within the struct.
492  */
493 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
494         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
495                 n = list_entry(pos->member.prev, typeof(*pos), member); \
496              &pos->member != (head);                                    \
497              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
498
499 #endif /* __LIBCFS_UTIL_LIST_H__ */