1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _LIST_H_
18 #define _LIST_H_
19 
20 #include <stdbool.h>
21 
22 struct link_t
23 {
24     struct link_t *prev, *next;
25 } typedef link_t;
26 
27 #define list_iterate(list, cur_link, tmp_link)                  \
28     for ((cur_link) = (list)->next,                             \
29             (tmp_link) = (cur_link) ? (cur_link)->next : NULL;  \
30          (cur_link) != NULL && (cur_link) != (list);            \
31          (cur_link) = (tmp_link), (tmp_link) = (cur_link)->next)
32 
33 #define DECLARE_LIST(list) \
34     link_t list = { .prev = &list, .next = &list }
35 
list_init(struct link_t * list)36 static inline void list_init(struct link_t *list)
37 {
38     list->prev = list->next = list;
39 }
40 
list_add_tail(struct link_t * list,struct link_t * item)41 static inline void list_add_tail(struct link_t *list, struct link_t *item)
42 {
43     if (!list->next)
44         list_init(list);
45 
46     item->prev = list->prev;
47     item->next = list;
48     list->prev->next = item;
49     list->prev = item;
50 }
51 
list_delete(struct link_t * item)52 static inline void list_delete(struct link_t *item)
53 {
54     item->prev->next = item->next;
55     item->next->prev = item->prev;
56     item->next = item->prev = item;
57 }
58 
list_is_empty(struct link_t * list)59 static inline bool list_is_empty(struct link_t *list)
60 {
61     return !list->next || list->next == list;
62 }
63 
64 #endif
65 
66