File: ao_list.h

package info (click to toggle)
altos 1.6.8-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 63,748 kB
  • ctags: 36,577
  • sloc: ansic: 92,100; java: 36,273; makefile: 6,495; xml: 3,096; sh: 1,971; pascal: 1,581
file content (214 lines) | stat: -rw-r--r-- 6,202 bytes parent folder | download | duplicates (2)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#ifndef _AO_LIST_H_
#define _AO_LIST_H_

#include <stddef.h>

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

static inline void
ao_list_init(struct ao_list *list)
{
	list->next = list->prev = list;
}

static inline void
__ao_list_add(struct ao_list *list, struct ao_list *prev, struct ao_list *next)
{
	next->prev = list;
	list->next = next;
	list->prev = prev;
	prev->next = list;
}

/**
 * Insert a new element after the given list head. The new element does not
 * need to be initialised as empty list.
 * The list changes from:
 *      head → some element → ...
 * to
 *      head → new element → older element → ...
 *
 * Example:
 * struct foo *newfoo = malloc(...);
 * ao_list_add(&newfoo->entry, &bar->list_of_foos);
 *
 * @param entry The new element to prepend to the list.
 * @param head The existing list.
 */
static inline void
ao_list_insert(struct ao_list *entry, struct ao_list *head)
{
    __ao_list_add(entry, head, head->next);
}

/**
 * Append a new element to the end of the list given with this list head.
 *
 * The list changes from:
 *      head → some element → ... → lastelement
 * to
 *      head → some element → ... → lastelement → new element
 *
 * Example:
 * struct foo *newfoo = malloc(...);
 * ao_list_append(&newfoo->entry, &bar->list_of_foos);
 *
 * @param entry The new element to prepend to the list.
 * @param head The existing list.
 */
static inline void
ao_list_append(struct ao_list *entry, struct ao_list *head)
{
    __ao_list_add(entry, head->prev, head);
}

static inline void
__ao_list_del(struct ao_list *prev, struct ao_list *next)
{
    next->prev = prev;
    prev->next = next;
}

/**
 * Remove the element from the list it is in. Using this function will reset
 * the pointers to/from this element so it is removed from the list. It does
 * NOT free the element itself or manipulate it otherwise.
 *
 * Using ao_list_del on a pure list head (like in the example at the top of
 * this file) will NOT remove the first element from
 * the list but rather reset the list as empty list.
 *
 * Example:
 * ao_list_del(&foo->entry);
 *
 * @param entry The element to remove.
 */
static inline void
ao_list_del(struct ao_list *entry)
{
    __ao_list_del(entry->prev, entry->next);
    ao_list_init(entry);
}

/**
 * Check if the list is empty.
 *
 * Example:
 * ao_list_is_empty(&bar->list_of_foos);
 *
 * @return True if the list contains one or more elements or False otherwise.
 */
static inline uint8_t
ao_list_is_empty(struct ao_list *head)
{
    return head->next == head;
}

/**
 * Returns a pointer to the container of this list element.
 *
 * Example:
 * struct foo* f;
 * f = container_of(&foo->entry, struct foo, entry);
 * assert(f == foo);
 *
 * @param ptr Pointer to the struct ao_list.
 * @param type Data type of the list element.
 * @param member Member name of the struct ao_list field in the list element.
 * @return A pointer to the data struct containing the list head.
 */
#define ao_container_of(ptr, type, member) \
	((type *)((void *) ((char *)(ptr) - offsetof(type, member))))

/**
 * Alias of ao_container_of
 */
#define ao_list_entry(ptr, type, member) \
    ao_container_of(ptr, type, member)

/**
 * Retrieve the first list entry for the given list pointer.
 *
 * Example:
 * struct foo *first;
 * first = ao_list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
 *
 * @param ptr The list head
 * @param type Data type of the list element to retrieve
 * @param member Member name of the struct ao_list field in the list element.
 * @return A pointer to the first list element.
 */
#define ao_list_first_entry(ptr, type, member) \
    ao_list_entry((ptr)->next, type, member)

/**
 * Retrieve the last list entry for the given listpointer.
 *
 * Example:
 * struct foo *first;
 * first = ao_list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
 *
 * @param ptr The list head
 * @param type Data type of the list element to retrieve
 * @param member Member name of the struct ao_list field in the list element.
 * @return A pointer to the last list element.
 */
#define ao_list_last_entry(ptr, type, member) \
    ao_list_entry((ptr)->prev, type, member)

/**
 * Loop through the list given by head and set pos to struct in the list.
 *
 * Example:
 * struct foo *iterator;
 * ao_list_for_each_entry(iterator, &bar->list_of_foos, entry) {
 *      [modify iterator]
 * }
 *
 * This macro is not safe for node deletion. Use ao_list_for_each_entry_safe
 * instead.
 *
 * @param pos Iterator variable of the type of the list elements.
 * @param head List head
 * @param member Member name of the struct ao_list in the list elements.
 *
 */
#define ao_list_for_each_entry(pos, head, type, member)			\
    for (pos = ao_container_of((head)->next, type, member);		\
	 &pos->member != (head);					\
	 pos = ao_container_of(pos->member.next, type, member))

/**
 * Loop through the list, keeping a backup pointer to the element. This
 * macro allows for the deletion of a list element while looping through the
 * list.
 *
 * See ao_list_for_each_entry for more details.
 */
#define ao_list_for_each_entry_safe(pos, tmp, head, type, member)		\
	for ((pos = ao_container_of((head)->next, type, member)),		\
	     (tmp = ao_container_of(pos->member.next, type, member)); 		\
	     &pos->member != (head);						\
	     (pos = tmp), (tmp = ao_container_of(pos->member.next, type, member)))

#endif /* _AO_LIST_H_ */