summaryrefslogtreecommitdiff
path: root/qcom/qrtr/src/list.h
blob: d740743a8d6b85559e5edbe6b95c0fb842f7697e (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef _LIST_H_
#define _LIST_H_

#ifndef offsetof
#define offsetof(type, md) ((unsigned long)&((type *)0)->md)
#endif

#ifndef container_of
#define container_of(ptr, type, member) \
  ((type *)((char *)(ptr) - offsetof(type, member)))
#endif

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

struct list {
	struct list_item *head;
	struct list_item *tail;
};

#define LIST_INIT(name) { 0, 0 }

#define LIST(name) \
	struct list name = LIST_INIT(name)

#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

static inline void list_init(struct list *list)
{
	list->head = 0;
	list->tail = 0;
}

static inline void list_append(struct list *list, struct list_item *item)
{
	item->next = 0;
	item->prev = list->tail;
	if (list->tail != 0)
		list->tail->next = item;
	else
		list->head = item;
	list->tail = item;
}

static inline void list_prepend(struct list *list, struct list_item *item)
{
	item->prev = 0;
	item->next = list->head;
	if (list->head == 0)
		list->tail = item;
	list->head = item;
}

static inline void list_insert(struct list *list, struct list_item *after, struct list_item *item)
{
	if (after == 0) {
		list_prepend(list, item);
		return;
	}
	item->prev = after;
	item->next = after->next;
	after->next = item;
	if (item->next)
		item->next->prev = item;
	if (list->tail == after)
		list->tail = item;
}

static inline void list_remove(struct list *list, struct list_item *item)
{
	if (item->next)
		item->next->prev = item->prev;
	if (list->head == item) {
		list->head = item->next;
		if (list->head == 0)
			list->tail = 0;
	} else {
		item->prev->next = item->next;
		if (list->tail == item)
			list->tail = item->prev;
	}
	item->prev = item->next = 0;
}

static inline struct list_item *list_pop(struct list *list)
{
	struct list_item *item;
	item = list->head;
	if (item == 0)
		return 0;
	list_remove(list, item);
	return item;
}

static inline struct list_item *list_last(struct list *list)
{
	return list->tail;
}

static inline struct list_item *list_first(struct list *list)
{
	return list->head;
}


static inline struct list_item *list_next(struct list_item *item)
{
	return item->next;
}

#define list_push list_append

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

#define list_for_each_after(_node, _iter) \
  for (_iter = (_node)->next; (_iter) != 0; _iter = (_iter)->next)

#define list_for_each_safe(_list, _iter, _bkup) \
  for (_iter = (_list)->head; (_iter) != 0 && ((_bkup = (_iter)->next) || 1); _iter = (_bkup))

#define list_for_each_safe_after(_node, _iter, _bkup) \
  for (_iter = (_node)->next; (_iter) != 0 && ((_bkup = (_iter)->next) || 1); _iter = (_bkup))

#endif