Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 1 | /* |
Mark Salyzyn | 0ea3624 | 2013-11-27 12:37:56 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008-2013 The Android Open Source Project |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 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 Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 17 | #ifndef _CUTILS_LIST_H_ |
| 18 | #define _CUTILS_LIST_H_ |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 19 | |
Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 20 | #include <stddef.h> |
Kenny Root | 0009b73 | 2012-03-15 21:45:00 -0700 | [diff] [blame] | 21 | |
Kenny Root | 77a62ce | 2012-03-20 13:46:24 -0700 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif /* __cplusplus */ |
Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 25 | |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 26 | struct 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 Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 44 | #define list_for_each_reverse(node, list) \ |
| 45 | for (node = (list)->prev; node != (list); node = node->prev) |
| 46 | |
Andrew Boie | 30fb83b | 2013-09-12 15:30:40 -0700 | [diff] [blame] | 47 | #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 Salyzyn | 0ea3624 | 2013-11-27 12:37:56 -0800 | [diff] [blame] | 52 | static inline void list_init(struct listnode *node) |
| 53 | { |
| 54 | node->next = node; |
| 55 | node->prev = node; |
| 56 | } |
| 57 | |
| 58 | static 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 | |
| 66 | static inline void list_remove(struct listnode *item) |
| 67 | { |
| 68 | item->next->prev = item->prev; |
| 69 | item->prev->next = item->next; |
| 70 | } |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 71 | |
| 72 | #define list_empty(list) ((list) == (list)->next) |
| 73 | #define list_head(list) ((list)->next) |
| 74 | #define list_tail(list) ((list)->prev) |
| 75 | |
Kenny Root | 77a62ce | 2012-03-20 13:46:24 -0700 | [diff] [blame] | 76 | #ifdef __cplusplus |
| 77 | }; |
| 78 | #endif /* __cplusplus */ |
Kenny Root | 0009b73 | 2012-03-15 21:45:00 -0700 | [diff] [blame] | 79 | |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 80 | #endif |