File setup open can fail in multiple functions, not just open()
[fio.git] / list.h
CommitLineData
ebac4655
JA
1#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
4#undef offsetof
5#ifdef __compiler_offsetof
6#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
7#else
8#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
9#endif
10
11#define container_of(ptr, type, member) ({ \
12 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
13 (type *)( (char *)__mptr - offsetof(type,member) );})
14
15/*
16 * Simple doubly linked list implementation.
17 *
18 * Some of the internal functions ("__xxx") are useful when
19 * manipulating whole lists rather than single entries, as
20 * sometimes we already know the next/prev entries and we can
21 * generate better code by using them directly rather than
22 * using the generic single-entry routines.
23 */
24
25struct list_head {
26 struct list_head *next, *prev;
27};
28
29#define LIST_HEAD_INIT(name) { &(name), &(name) }
30
5f350952
JA
31#define LIST_HEAD(name) \
32 struct list_head name = LIST_HEAD_INIT(name)
33
ebac4655
JA
34#define INIT_LIST_HEAD(ptr) do { \
35 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
36} while (0)
37
38/*
39 * Insert a new entry between two known consecutive entries.
40 *
41 * This is only for internal list manipulation where we know
42 * the prev/next entries already!
43 */
44static inline void __list_add(struct list_head *new,
45 struct list_head *prev,
46 struct list_head *next)
47{
48 next->prev = new;
49 new->next = next;
50 new->prev = prev;
51 prev->next = new;
52}
53
54/**
55 * list_add - add a new entry
56 * @new: new entry to be added
57 * @head: list head to add it after
58 *
59 * Insert a new entry after the specified head.
60 * This is good for implementing stacks.
61 */
62static inline void list_add(struct list_head *new, struct list_head *head)
63{
64 __list_add(new, head, head->next);
65}
66
67static inline void list_add_tail(struct list_head *new, struct list_head *head)
68{
69 __list_add(new, head->prev, head);
70}
71
72/*
73 * Delete a list entry by making the prev/next entries
74 * point to each other.
75 *
76 * This is only for internal list manipulation where we know
77 * the prev/next entries already!
78 */
79static inline void __list_del(struct list_head * prev, struct list_head * next)
80{
81 next->prev = prev;
82 prev->next = next;
83}
84
85/**
86 * list_del - deletes entry from list.
87 * @entry: the element to delete from the list.
88 * Note: list_empty on entry does not return true after this, the entry is
89 * in an undefined state.
90 */
91static inline void list_del(struct list_head *entry)
92{
93 __list_del(entry->prev, entry->next);
94 entry->next = NULL;
95 entry->prev = NULL;
96}
97
94a08ec6
JA
98/**
99 * list_del_init - deletes entry from list and reinitialize it.
100 * @entry: the element to delete from the list.
101 */
102static inline void list_del_init(struct list_head *entry)
103{
104 __list_del(entry->prev, entry->next);
105 INIT_LIST_HEAD(entry);
106}
107
ebac4655
JA
108/**
109 * list_empty - tests whether a list is empty
110 * @head: the list to test.
111 */
112static inline int list_empty(const struct list_head *head)
113{
114 return head->next == head;
115}
116
117/**
118 * list_entry - get the struct for this entry
119 * @ptr: the &struct list_head pointer.
120 * @type: the type of the struct this is embedded in.
121 * @member: the name of the list_struct within the struct.
122 */
123#define list_entry(ptr, type, member) \
124 container_of(ptr, type, member)
125
126/**
127 * list_for_each - iterate over a list
128 * @pos: the &struct list_head to use as a loop counter.
129 * @head: the head for your list.
130 */
131#define list_for_each(pos, head) \
132 for (pos = (head)->next; pos != (head); pos = pos->next)
133
134/**
135 * list_for_each_safe - iterate over a list safe against removal of list entry
136 * @pos: the &struct list_head to use as a loop counter.
137 * @n: another &struct list_head to use as temporary storage
138 * @head: the head for your list.
139 */
140#define list_for_each_safe(pos, n, head) \
141 for (pos = (head)->next, n = pos->next; pos != (head); \
142 pos = n, n = pos->next)
143
144#endif