1/*
2 * Copyright (C) 2010 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 _CUTILS_LIST_H_
18#define _CUTILS_LIST_H_
19
20#include <stddef.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif /* __cplusplus */
25
26struct listnode
27{
28    struct listnode *next;
29    struct listnode *prev;
30};
31
32#define node_to_item(node, container, member) \
33    (container *) (((char*) (node)) - offsetof(container, member))
34
35#define list_declare(name) \
36    struct listnode name = { \
37        .next = &name, \
38        .prev = &name, \
39    }
40
41#define list_for_each(node, list) \
42    for (node = (list)->next; node != (list); node = node->next)
43
44#define list_for_each_reverse(node, list) \
45    for (node = (list)->prev; node != (list); node = node->prev)
46
47void list_init(struct listnode *list);
48void list_add_tail(struct listnode *list, struct listnode *item);
49void list_remove(struct listnode *item);
50
51#define list_empty(list) ((list) == (list)->next)
52#define list_head(list) ((list)->next)
53#define list_tail(list) ((list)->prev)
54
55#ifdef __cplusplus
56};
57#endif /* __cplusplus */
58
59#endif
60