41 lines
1.5 KiB
C
41 lines
1.5 KiB
C
|
#ifndef LIST_H
|
||
|
#define LIST_H
|
||
|
|
||
|
/*
|
||
|
* Assuming ptr is a pointer to a member variable of type parent_type, and ptr
|
||
|
* is named member in parent_type, find the address of the struct which owns
|
||
|
* ptr.
|
||
|
*/
|
||
|
#define container(ptr, parent_type, member) \
|
||
|
((parent_type *) ((char *)ptr - (char *)&((parent_type *)0)->member))
|
||
|
|
||
|
/*
|
||
|
* Given a pointer to a struct (head) of type dlist_node, which points to a
|
||
|
* list of type parent_type, and a pointer of that type (it), this macro will
|
||
|
* execute the contained instructions once for each list element, with 'it' set
|
||
|
* to each element in the list, in turn.
|
||
|
*/
|
||
|
#define for_each_list(it, head, parent_type, member) \
|
||
|
for (it = container((head)->next, parent_type, member); it->member.next != (head); it = container(it->member.next, parent_type, member))
|
||
|
|
||
|
#define for_each_list_noinit(it, head, parent_type, member) \
|
||
|
for (; it->member.next != (head); it = container(it->member.next, parent_type, member))
|
||
|
|
||
|
struct dlist_node {
|
||
|
struct dlist_node *next, *prev;
|
||
|
};
|
||
|
|
||
|
void init_list(struct dlist_node *n);
|
||
|
int list_empty(struct dlist_node *n);
|
||
|
|
||
|
//functions for dealing with single list elements
|
||
|
void insert_after(struct dlist_node *n, struct dlist_node *to_add);
|
||
|
void insert_before(struct dlist_node *n, struct dlist_node *to_add);
|
||
|
void remove(struct dlist_node *to_remove);
|
||
|
|
||
|
//functions for dealing with one or more elements
|
||
|
void remove_splice(struct dlist_node *from, struct dlist_node *to);
|
||
|
void insert_splice_before(struct dlist_node *n, struct dlist_node *first, struct dlist_node *last);
|
||
|
|
||
|
#endif /* LIST_H */
|