1 /*
2  * list.h, list
3  *
4  * Copyright (c) 2009-2010 Wind River Systems, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef __LIST_H
20 #define __LIST_H
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 struct list {
27     struct list *next;
28     struct list *prev;
29 
30     void *data;
31 };
32 
33 void __list_init(struct list *);
34 
35 struct list *__list_alloc(void);
36 struct list *list_alloc(void *);
37 
38 void __list_free(struct list *);
39 void list_free_all(struct list *);
40 
41 struct list *__list_last(struct list *);
42 struct list *__list_first(struct list *);
43 struct list *__list_entry(struct list *, int);
44 int list_length(struct list *);
45 
46 struct list *__list_add_before(struct list *, struct list *);
47 struct list *__list_add_after(struct list *, struct list *);
48 struct list *__list_add_head(struct list *, struct list *);
49 struct list *__list_add_tail(struct list *, struct list *);
50 struct list *list_add_head(struct list *, void *);
51 struct list *list_add_tail(struct list *, void *);
52 
53 struct list *__list_remove(struct list *, struct list *);
54 struct list *__list_delete(struct list *, struct list *);
55 struct list *list_delete(struct list *, void *);
56 struct list *list_delete_all(struct list *, void *);
57 
58 struct list *list_find(struct list *, void *);
59 struct list *list_find_reverse(struct list *, void *);
60 
61 #define __list_next(entry) ((entry) ? (entry->next) : NULL)
62 #define __list_prev(entry) ((entry) ? (entry->prev) : NULL)
63 
64 #define list_foreach(list, ptr)         \
65     for (ptr = list;                    \
66          ptr != NULL;                   \
67          ptr = __list_next(ptr))
68 
69 #define list_foreach_safe(list, ptr, nxt)                       \
70     for (ptr = list, nxt = __list_next(ptr);                    \
71          ptr != NULL;						\
72          ptr = nxt, nxt = __list_next(ptr))
73 
74 #define list_foreach_reverse(list, ptr)         \
75     for (ptr = __list_last(list);               \
76          ptr != NULL;                           \
77          ptr = __list_prev(ptr))
78 
79 #define list_foreach_reverse_safe(list, ptr, prv)               \
80     for (ptr = __list_last(list), prv = __list_prev(ptr);      \
81          ptr != NULL;						\
82          ptr = prv, prv = __list_prev(ptr))
83 
84 #ifdef __cplusplus
85 } /* extern "C" */
86 #endif
87 
88 #endif /* __LIST_H */
89