File: list.h

package info (click to toggle)
ploop 1.15-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,372 kB
  • sloc: ansic: 16,133; sh: 413; makefile: 222; python: 144
file content (121 lines) | stat: -rw-r--r-- 3,007 bytes parent folder | download | duplicates (4)
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
#ifndef __LIST_H__
#define __LIST_H__
#include "stdlib.h"

struct list_elem;
typedef struct {
	struct list_elem *prev, *next;
} list_head_t;

typedef struct list_elem {
	struct list_elem *prev, *next;
} list_elem_t;

#ifdef __cplusplus
extern "C" {
#endif
static inline void list_head_init(list_head_t *head)
{
	head->next = (list_elem_t *)head;
	head->prev = (list_elem_t *)head;
}

static inline void list_elem_init(list_elem_t *entry)
{
	entry->next = entry;
	entry->prev = entry;
}

static inline void list_add(list_elem_t *new_el, list_head_t *list)
{
	new_el->next = list->next;
	new_el->prev = (list_elem_t *)list;
	list->next->prev = new_el;
	list->next = new_el;
}

static inline void list_add_tail(list_elem_t *new_el, list_head_t *list)
{
	new_el->next = (list_elem_t *)list;
	new_el->prev = list->prev;
	list->prev->next = new_el;
	list->prev = new_el;
}

static inline void list_del(list_elem_t *el)
{
	el->prev->next = el->next;
	el->next->prev = el->prev;
	el->prev = NULL;
	el->next = NULL;
}

static inline void list_del_init(list_elem_t *el)
{
	el->prev->next = el->next;
	el->next->prev = el->prev;
	list_elem_init(el);
}

static inline int list_is_init(const list_head_t *h)
{
	return h->next == NULL;
}

static inline int list_empty(const list_head_t *h)
{
	if (list_is_init(h))
		return 1;
	return h->next == (const list_elem_t *)h;
}

static inline int list_elem_inserted(const list_elem_t *el)
{
	return el->next != el;
}

static inline void list_moveall(list_head_t *src, list_head_t *dst)
{
	list_add((list_elem_t *)dst, src);
	list_del((list_elem_t *)src);
	list_head_init(src);
}
#ifdef __cplusplus
}
#endif


#define LIST_HEAD_INIT(head) {(void *) &(head), (void *) &(head)}

#define LIST_HEAD(name) \
	list_head_t name = LIST_HEAD_INIT(name)

#define list_entry(ptr, type, field)					\
	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->field)))

#define list_first_entry(head, type, field)				\
	list_entry((head)->next, type, field)

#define list_for_each(entry, head, field)				\
	for (entry = list_entry((head)->next, typeof(*entry), field);\
	     &entry->field != (list_elem_t*)(head);			\
	     entry = list_entry(entry->field.next, typeof(*entry), field))

#define list_for_each_prev(entry, head, field)				\
	for (entry = list_entry((head)->prev, typeof(*entry), field);\
	     &entry->field != (list_elem_t*)(head);			\
	     entry = list_entry(entry->field.prev, typeof(*entry), field))

#define list_for_each_prev_continue(pos, head, field)			\
	for (pos = list_entry(pos->field.prev, typeof(*pos), field);	\
	     &pos->field != (list_elem_t*)(head);			\
	     pos = list_entry(pos->field.prev, typeof(*pos), field))

#define list_for_each_safe(entry, tmp, head, field)			\
	for (entry = list_entry((head)->next, typeof(*entry), field),\
		tmp = list_entry(entry->field.next, typeof(*entry), field); \
	     &entry->field != (list_elem_t*)(head);			\
	     entry = tmp,						\
		tmp = list_entry(tmp->field.next, typeof(*tmp), field))

#endif /* __LIST_H__ */