José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA. |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 18 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | * The above copyright notice and this permission notice (including the |
| 23 | * next paragraph) shall be included in all copies or substantial portions |
| 24 | * of the Software. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | |
| 28 | /** |
| 29 | * \file |
| 30 | * List macros heavily inspired by the Linux kernel |
| 31 | * list handling. No list looping yet. |
| 32 | * |
| 33 | * Is not threadsafe, so common operations need to |
| 34 | * be protected using an external mutex. |
| 35 | */ |
| 36 | |
José Fonseca | b9da379 | 2008-02-19 15:07:53 +0900 | [diff] [blame] | 37 | #ifndef _U_DOUBLE_LIST_H_ |
| 38 | #define _U_DOUBLE_LIST_H_ |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | #include <stddef.h> |
| 42 | |
| 43 | |
| 44 | struct list_head |
| 45 | { |
| 46 | struct list_head *prev; |
| 47 | struct list_head *next; |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | #define LIST_INITHEAD(__item) \ |
| 52 | do { \ |
| 53 | (__item)->prev = (__item); \ |
| 54 | (__item)->next = (__item); \ |
| 55 | } while (0) |
| 56 | |
| 57 | #define LIST_ADD(__item, __list) \ |
| 58 | do { \ |
| 59 | (__item)->prev = (__list); \ |
| 60 | (__item)->next = (__list)->next; \ |
| 61 | (__list)->next->prev = (__item); \ |
| 62 | (__list)->next = (__item); \ |
| 63 | } while (0) |
| 64 | |
| 65 | #define LIST_ADDTAIL(__item, __list) \ |
| 66 | do { \ |
| 67 | (__item)->next = (__list); \ |
| 68 | (__item)->prev = (__list)->prev; \ |
| 69 | (__list)->prev->next = (__item); \ |
| 70 | (__list)->prev = (__item); \ |
| 71 | } while(0) |
| 72 | |
José Fonseca | bc56e87 | 2008-04-10 22:57:21 +0900 | [diff] [blame] | 73 | #define LIST_REPLACE(__from, __to) \ |
| 74 | do { \ |
| 75 | (__to)->prev = (__from)->prev; \ |
| 76 | (__to)->next = (__from)->next; \ |
| 77 | (__from)->next->prev = (__to); \ |
| 78 | (__from)->prev->next = (__to); \ |
| 79 | } while (0) |
| 80 | |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 81 | #define LIST_DEL(__item) \ |
| 82 | do { \ |
| 83 | (__item)->prev->next = (__item)->next; \ |
| 84 | (__item)->next->prev = (__item)->prev; \ |
| 85 | } while(0) |
| 86 | |
| 87 | #define LIST_DELINIT(__item) \ |
| 88 | do { \ |
| 89 | (__item)->prev->next = (__item)->next; \ |
| 90 | (__item)->next->prev = (__item)->prev; \ |
| 91 | (__item)->next = (__item); \ |
| 92 | (__item)->prev = (__item); \ |
| 93 | } while(0) |
| 94 | |
| 95 | #define LIST_ENTRY(__type, __item, __field) \ |
| 96 | ((__type *)(((char *)(__item)) - offsetof(__type, __field))) |
| 97 | |
José Fonseca | 3708aae | 2009-03-23 12:05:07 +0000 | [diff] [blame^] | 98 | #define LIST_IS_EMPTY(__list) \ |
| 99 | ((__list)->next == (__list)) |
| 100 | |
José Fonseca | b478362 | 2007-11-23 17:22:54 +0000 | [diff] [blame] | 101 | |
José Fonseca | b9da379 | 2008-02-19 15:07:53 +0900 | [diff] [blame] | 102 | #endif /*_U_DOUBLE_LIST_H_*/ |