vmod/vmodttl: fixed bug related to luns not ordered and/or not starting from zero.
[ht-drivers.git] / include / list_extra.h
blob5dfb1e257a762ba591653a17d8634080f640d1c3
1 /**
2 * @file list_extra.h
4 * @brief Extra handy list utils.
6 * @author Yury Georgievskiy. CERN AB/CO.
8 * @date May, 2008
10 * Can be used by the driver and in the user space.
12 * @version 1.0 ygeorgie 15/05/2008 Creation date.
14 #ifndef _LIST_EXTRA_H_INCLUDE_
15 #define _LIST_EXTRA_H_INCLUDE_
17 #if defined (__linux__) && defined(__KERNEL__)
18 /* for kernel only */
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/version.h>
22 #endif
24 #if !defined(__KERNEL__) && !defined(__LYNXOS)
25 /* for user-space only */
26 #define kfree free
27 #endif
29 //!< A way to declare list in a header file
30 #define DECLARE_GLOB_LIST_HEAD(list_name) \
31 _DECL struct list_head list_name _INIT(LIST_HEAD_INIT(list_name))
33 /**
34 * list_capacity - how many elements in the list
36 * @head: the list to test.
38 static inline int list_capacity(struct list_head *head)
40 int cntr = 0;
41 struct list_head *at = head;
43 while (at->next != head) {
44 at = at->next;
45 ++cntr;
48 return cntr;
51 /**
52 * list_next - pointer to the next element in the list
54 * @head: current list element
56 static inline struct list_head* list_next(struct list_head *head)
58 return head->next;
62 /**
63 * list_prev - pointer to the previous element in the list
65 * @head: current list element
67 static inline struct list_head* list_prev(struct list_head *head)
69 return head->prev;
73 /**
74 * list_idx - get element of the list
76 * @head: the list in question
77 * @idx: list element index (starting from 0) to get
79 static inline struct list_head* list_idx(struct list_head *head, int idx)
81 int cntr = 0;
82 struct list_head *at = NULL;
84 list_for_each(at, head) {
85 if (cntr++ == idx)
86 break;
88 return at;
92 /**
93 * list_free_all_safe - delete and free all elements safe from the list
95 * All list elements should be allocated entities only!
97 * @head: the head for your list
98 * @type: the type of the struct this is embedded in
99 * @member: the name of the list_struct within the struct
101 #define list_free_all_safe(head, type, member) \
102 do { \
103 type *__element; \
104 struct list_head *__list, *__safe; \
106 list_for_each_safe(__list, __safe, head) { \
107 __element = list_entry(__list, type, member); \
108 list_del(&__element->member); \
109 kfree(__element); \
111 } while (0)
114 #ifndef list_last_entry
115 #define list_last_entry(ptr, type, member) \
116 list_entry((ptr)->prev, type, member)
117 #endif
119 #endif /* _LIST_EXTRA_H_INCLUDE_ */