File: list.h

package info (click to toggle)
tayga 0.9.2-8
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 668 kB
  • sloc: ansic: 5,546; sh: 1,176; makefile: 23
file content (80 lines) | stat: -rw-r--r-- 2,098 bytes parent folder | download | duplicates (3)
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
/* Linux-style linked list implementation */

#ifndef __LIST_H__
#define __LIST_H__

#include <stddef.h> /* for offsetof() macro */

struct list_head {
	struct list_head *next;
	struct list_head *prev;
};

/* Declare an empty list */
#define LIST_HEAD(x) struct list_head x = { &x, &x }

/* Initialize an empty list (required for all malloc'd list_heads) */
static inline void INIT_LIST_HEAD(struct list_head *x)
{
	x->next = x;
	x->prev = x;
}

/* Remove an entry from its current list (if any) and insert it into the
 * beginning of the given list */
static inline void list_add(struct list_head *x, struct list_head *head)
{
	x->next->prev = x->prev;
	x->prev->next = x->next;
	x->next = head->next;
	x->prev = head;
	head->next = x;
	x->next->prev = x;
}

/* Remove an entry from its current list (if any) and insert it into the
 * end of the given list */
static inline void list_add_tail(struct list_head *x, struct list_head *head)
{
	x->next->prev = x->prev;
	x->prev->next = x->next;
	x->prev = head->prev;
	x->next = head;
	head->prev = x;
	x->prev->next = x;
}

/* Remove an entry from its current list and reinitialize it */
static inline void list_del(struct list_head *x)
{
	x->next->prev = x->prev;
	x->prev->next = x->next;
	x->next = x;
	x->prev = x;
}

#define list_del_init list_del
#define list_move list_add
#define list_move_tail list_add_tail

/* Test if list is empty (contains no other entries) */
static inline int list_empty(const struct list_head *x)
{
	return x->next == x;
}

/* Get a pointer to the object containing x, which is of type "type" and 
 * embeds x as a field called "field" */
#define list_entry(x, type, field) ({ \
		const typeof( ((type *)0)->field ) *__mptr = (x); \
		(type *)( (char *)__mptr - offsetof(type, field) );})

/* Iterate over all items in the list */
#define list_for_each(x, head) \
	for (x = (head)->next; x != (head); x = x->next)

/* Iterate over all items in the list, possibly deleting some */
#define list_for_each_safe(x, n, head) \
	for (x = (head)->next, n = x->next; x != (head); x = n, n = x->next)

#endif /* __LIST_H__ */