blob: dfdc53be60366eadb5683f1aef8323ef14a162cd [file] [log] [blame]
Colin Crossed8a7d82010-04-19 17:05:34 -07001/*
Mark Salyzyna6aad4c2013-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 = { \
Chih-Hung Hsieh85244e82017-08-01 13:49:57 -070037 .next = &(name), \
38 .prev = &(name), \
Colin Crossed8a7d82010-04-19 17:05:34 -070039 }
40
41#define list_for_each(node, list) \
Chih-Hung Hsieh85244e82017-08-01 13:49:57 -070042 for ((node) = (list)->next; (node) != (list); (node) = (node)->next)
Colin Crossed8a7d82010-04-19 17:05:34 -070043
Colin Cross44b65d02010-04-20 14:32:50 -070044#define list_for_each_reverse(node, list) \
Chih-Hung Hsieh85244e82017-08-01 13:49:57 -070045 for ((node) = (list)->prev; (node) != (list); (node) = (node)->prev)
Colin Cross44b65d02010-04-20 14:32:50 -070046
Thierry Escande8cc74272014-04-30 02:15:15 +020047#define list_for_each_safe(node, n, list) \
Chih-Hung Hsieh85244e82017-08-01 13:49:57 -070048 for ((node) = (list)->next, (n) = (node)->next; \
49 (node) != (list); \
50 (node) = (n), (n) = (node)->next)
Andrew Boie30fb83b2013-09-12 15:30:40 -070051
Mark Salyzyna6aad4c2013-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
Samuel Ortizf8a10892014-04-30 02:12:39 +020066static inline void list_add_head(struct listnode *head, struct listnode *item)
67{
68 item->next = head->next;
69 item->prev = head;
70 head->next->prev = item;
71 head->next = item;
72}
73
Mark Salyzyna6aad4c2013-11-27 12:37:56 -080074static inline void list_remove(struct listnode *item)
75{
76 item->next->prev = item->prev;
77 item->prev->next = item->next;
78}
Colin Crossed8a7d82010-04-19 17:05:34 -070079
80#define list_empty(list) ((list) == (list)->next)
81#define list_head(list) ((list)->next)
82#define list_tail(list) ((list)->prev)
83
Kenny Root77a62ce2012-03-20 13:46:24 -070084#ifdef __cplusplus
85};
86#endif /* __cplusplus */
Kenny Root0009b732012-03-15 21:45:00 -070087
Colin Crossed8a7d82010-04-19 17:05:34 -070088#endif