summaryrefslogtreecommitdiffstats
path: root/lib/list/list.h
blob: ad562e4cc9bedece8d299893329d084f1abddefd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef _LIST_H
#define _LIST_H

#include <util/util.h>

struct list_item {
	struct list_item *prev, *next;
};

struct list {
	struct list_item head;
};

#define list_for_each(_list, _pos) \
	for (_pos = (_list)->head.next; _pos != ((_list)->head); _pos = _pos->next)

#define list_entry(_ptr, _type, _member, _list) \
	(&container_of(_ptr, _type, _member)->_member == &((_list)->head) \
	? NULL \
	: container_of(_ptr, _type, _member))

#define list_prev_entry(_list, _pos, _member) \
	list_entry(_pos->_member.prev, typeof(*_pos), _member, _list)

#define list_next_entry(_list, _pos, _member) \
	list_entry(_pos->_member.next, typeof(*_pos), _member, _list)

#define list_for_each_entry(_list, _pos, _member) \
	for (_pos = list_entry((_list)->head.next, typeof(*_pos), _member, _list); \
		_pos; _pos = list_next_entry(_list, _pos, _member))

#define list_for_each_entry_continue(_list, _pos, _member) \
	for (; _pos; _pos = list_next_entry(_list, _pos, _member))

#define list_for_each_entry_safe(_list, _pos, _tmp, _member) \
	for (_pos = container_of((_list)->head.next, typeof(*_pos), _member), \
		_tmp = container_of(_pos->_member.next, typeof(*_pos), \
				_member); \
	     &_pos->_member != &(_list)->head; \
	     _pos = _tmp, \
	     _tmp = container_of(_tmp->_member.next, typeof(*_pos), _member))

#define DEFINE_LIST(_list) struct list _list = { \
	.head = { \
		.next = &_list.head, \
		.prev = &_list.head \
	} \
}

#define STATIC_LIST(_list) static DEFINE_LIST(_list)

void list_init(struct list *list);
void list_insert_before(struct list_item *next, struct list_item *item);
void list_insert_after(struct list_item *prev, struct list_item *item);
void list_remove(struct list_item *item);

static inline void list_add(struct list *list, struct list_item *item)
{
	list_insert_after(&list->head, item);
}
static inline void list_add_tail(struct list *list, struct list_item *item)
{
	list_insert_before(&list->head, item);
}

#endif /* _LIST_H */
OpenPOWER on IntegriCloud