blob: 72395f47bda1a64df5f0c59861c709e9fe6f5851 [file] [log] [blame]
Colin Crossed8a7d82010-04-19 17:05:34 -07001/*
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
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
Colin Crossed8a7d82010-04-19 17:05:34 -070052void list_init(struct listnode *list);
53void list_add_tail(struct listnode *list, struct listnode *item);
54void list_remove(struct listnode *item);
55
56#define list_empty(list) ((list) == (list)->next)
57#define list_head(list) ((list)->next)
58#define list_tail(list) ((list)->prev)
59
Kenny Root77a62ce2012-03-20 13:46:24 -070060#ifdef __cplusplus
61};
62#endif /* __cplusplus */
Kenny Root0009b732012-03-15 21:45:00 -070063
Colin Crossed8a7d82010-04-19 17:05:34 -070064#endif