elemy-utils 1.0.0
pslist.h
Go to the documentation of this file.
1#ifndef PSLIST_H
2#define PSLIST_H
3
4#include "elutils/c_decls.h"
5__BEGIN_DECLS /* till __END_DECLS */
6
17#undef offsetof
18#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
19
27#undef container_of
28#define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
29
33struct psl_hd
34{
35 struct psl_hd *next, *prev;
36};
37typedef struct psl_hd psl_hd_t;
38
40#define PSL_HD_INIT(name) { &(name), &(name) }
42#define PSL_HD(name) struct psl_hd name = PSL_HD_INIT(name)
43
45static inline void psl_init(psl_hd_t *list)
46{
47 list->next = list;
48 list->prev = list;
49}
50
57#define psl_entry(ptr, type, member) container_of(ptr, type, member)
58
65#define psl_for_each_entry(pos, head, member) \
66 for (pos = psl_entry((head)->next, typeof(*pos), member); \
67 &pos->member != (head); \
68 pos = psl_entry(pos->member.next, typeof(*pos), member))
69
77#define psl_for_each_entry_safe(pos, n, head, member) \
78 for (pos = psl_entry((head)->next, typeof(*pos), member), \
79 n = psl_entry(pos->member.next, typeof(*pos), member); \
80 &pos->member != (head); \
81 pos = n, n = psl_entry(n->member.next, typeof(*n), member))
82
90// @formatter:off
91#define psl_count(TYPE, head, member, pcount) \
92{ *pcount = 0; TYPE *pos; \
93 for (pos = psl_entry((head)->next, typeof(*pos), member); \
94 &pos->member != (head); \
95 pos = psl_entry(pos->member.next, typeof(*pos), member)) { \
96 *pcount++; \
97 } \
98}
99// @formatter:on
100
105static inline int psl_empty(const struct psl_hd *head)
106{
107 return head->next == head;
108}
109
110/*
111 * Insert a new entry between two known consecutive entries.
112 *
113 * This is only for internal list manipulation where we know
114 * the prev/next entries already!
115 */
116static inline void __psl_add(struct psl_hd *_new, struct psl_hd *prev, struct psl_hd *next)
117{
118 next->prev = _new;
119 _new->next = next;
120 _new->prev = prev;
121 prev->next = _new;
122}
123
132static inline void psl_add(struct psl_hd *_new, struct psl_hd *head)
133{
134 __psl_add(_new, head, head->next);
135}
136
145static inline void psl_add_tail(struct psl_hd *_new, struct psl_hd *head)
146{
147 __psl_add(_new, head->prev, head);
148}
149
150/*
151 * Delete a list entry by making the prev/next entries
152 * point to each other.
153 *
154 * This is only for internal list manipulation where we know
155 * the prev/next entries already!
156 */
157static inline void __psl_del(struct psl_hd *prev, struct psl_hd *next)
158{
159 next->prev = prev;
160 prev->next = next;
161}
162
163#define LIST_POISON1 ((void *) 0x00100100)
164#define LIST_POISON2 ((void *) 0x00200200)
171static inline void psl_del(struct psl_hd *entry)
172{
173 __psl_del(entry->prev, entry->next);
174 entry->next = (struct psl_hd*) LIST_POISON1;
175 entry->prev = (struct psl_hd*) LIST_POISON2;
176}
177
183static inline int psl_is_head(const struct psl_hd *list, const struct psl_hd *head)
184{
185 return list == head;
186}
187
188static inline struct psl_hd* psl_first(const struct psl_hd *head)
189{
190 return head->next;
191}
192#define PSL_FIRST_ENTRY(ptr, type, member) psl_entry(ptr->next, type, member)
193
194#define PSL_SHOW_STRING(PSL_HD, TYPE, PSL_LINK, STRING_NAME) \
195{ int i = 0; TYPE *pos; \
196 psl_for_each_entry(pos, PSL_HD, PSL_LINK) { \
197 char *str = pos->STRING_NAME; \
198 MB_DBG("%i: '%s'\n", i, str); \
199 i++; \
200 } \
201} //PSL_SHOW_STRING(PSL_HD, TYPE, PSL_LINK, STRING_NAME) =====
202
203static inline void psl_move_head2head(psl_hd_t *new_hd, psl_hd_t *old_hd) \
204{
205 new_hd->next = old_hd->next;
206 new_hd->prev = old_hd->prev;
207 new_hd->next->prev = new_hd;
208 new_hd->prev->next = new_hd;
209 psl_init(old_hd);
210}
211
213#endif /* PSLIST_H */
214
#define __END_DECLS
Definition: c_decls.h:10
#define __BEGIN_DECLS
Definition: c_decls.h:9
static void psl_move_head2head(psl_hd_t *new_hd, psl_hd_t *old_hd)
Definition: pslist.h:203
static int psl_empty(const struct psl_hd *head)
Definition: pslist.h:105
static void psl_add_tail(struct psl_hd *_new, struct psl_hd *head)
Definition: pslist.h:145
static void __psl_del(struct psl_hd *prev, struct psl_hd *next)
Definition: pslist.h:157
#define LIST_POISON2
Definition: pslist.h:164
static void psl_init(psl_hd_t *list)
init allocated list element. used for initialize dynamic allocated list.
Definition: pslist.h:45
static void __psl_add(struct psl_hd *_new, struct psl_hd *prev, struct psl_hd *next)
Definition: pslist.h:116
static int psl_is_head(const struct psl_hd *list, const struct psl_hd *head)
Definition: pslist.h:183
#define LIST_POISON1
Definition: pslist.h:163
static struct psl_hd * psl_first(const struct psl_hd *head)
Definition: pslist.h:188
static void psl_del(struct psl_hd *entry)
Definition: pslist.h:171
static void psl_add(struct psl_hd *_new, struct psl_hd *head)
Definition: pslist.h:132
Definition: pslist.h:34
struct psl_hd * next
Definition: pslist.h:35
struct psl_hd * prev
Definition: pslist.h:35