blob: 945729ab2fa5799f7e1f7c6a0d028f8dc1934bce [file] [log] [blame]
Colin Crossed8a7d82010-04-19 17:05:34 -07001/*
Mark Salyzyn0ea36242013-11-27 12:37:56 -08002 * Copyright (C) 2008-2013 The Android Open Source Project
Colin Crossed8a7d82010-04-19 17:05:34 -07003 *
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
Dima Zavinda04c522011-09-01 17:09:44 -070017#ifndef _CUTILS_LIST_H_
18#define _CUTILS_LIST_H_
Colin Crossed8a7d82010-04-19 17:05:34 -070019
Olivier Baillyb93e5812010-11-17 11:47:23 -080020#include <stddef.h>
Kenny Root0009b732012-03-15 21:45:00 -070021
Kenny Root77a62ce2012-03-20 13:46:24 -070022#ifdef __cplusplus
23extern "C" {
24#endif /* __cplusplus */
Olivier Baillyb93e5812010-11-17 11:47:23 -080025
Colin Crossed8a7d82010-04-19 17:05:34 -070026struct 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
Colin Cross44b65d02010-04-20 14:32:50 -070044#define list_for_each_reverse(node, list) \
45 for (node = (list)->prev; node != (list); node = node->prev)
46
Andrew Boie30fb83b2013-09-12 15:30:40 -070047#define list_for_each_safe(node, next, list) \
48 for (node = (list)->next, next = node->next; \
49 node != (list); \
50 node = next, next = node->next)
51
Mark Salyzyn0ea36242013-11-27 12:37:56 -080052static inline void list_init(struct listnode *node)
53{
54 node->next = node;
55 node->prev = node;
56}
57
58static inline void list_add_tail(struct listnode *head, struct listnode *item)
59{
60 item->next = head;
61 item->prev = head->prev;
62 head->prev->next = item;
63 head->prev = item;
64}
65
66static inline void list_remove(struct listnode *item)
67{
68 item->next->prev = item->prev;
69 item->prev->next = item->next;
70}
Colin Crossed8a7d82010-04-19 17:05:34 -070071
72#define list_empty(list) ((list) == (list)->next)
73#define list_head(list) ((list)->next)
74#define list_tail(list) ((list)->prev)
75
Kenny Root77a62ce2012-03-20 13:46:24 -070076#ifdef __cplusplus
77};
78#endif /* __cplusplus */
Kenny Root0009b732012-03-15 21:45:00 -070079
Colin Crossed8a7d82010-04-19 17:05:34 -070080#endif