| 1 |
/* Simple Plugin API |
| 2 |
* |
| 3 |
* Copyright © 2018 Wim Taymans |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef SPA_LIST_H |
| 26 |
#define SPA_LIST_H |
| 27 |
|
| 28 |
#ifdef __cplusplus |
| 29 |
extern "C" { |
| 30 |
#endif |
| 31 |
|
| 32 |
struct spa_list { |
| 33 |
struct spa_list *next; |
| 34 |
struct spa_list *prev; |
| 35 |
}; |
| 36 |
|
| 37 |
#define SPA_LIST_INIT(list) (struct spa_list){ list, list }; |
| 38 |
|
| 39 |
static inline void spa_list_init(struct spa_list *list) |
| 40 |
{ |
| 41 |
*list = SPA_LIST_INIT(list); |
| 42 |
} |
| 43 |
|
| 44 |
#define spa_list_is_empty(l) ((l)->next == (l)) |
| 45 |
|
| 46 |
static inline void spa_list_insert(struct spa_list *list, struct spa_list *elem) |
| 47 |
{ |
| 48 |
elem->prev = list; |
| 49 |
elem->next = list->next; |
| 50 |
list->next = elem; |
| 51 |
elem->next->prev = elem; |
| 52 |
} |
| 53 |
|
| 54 |
static inline void spa_list_insert_list(struct spa_list *list, struct spa_list *other) |
| 55 |
{ |
| 56 |
if (spa_list_is_empty(other)) |
| 57 |
return; |
| 58 |
other->next->prev = list; |
| 59 |
other->prev->next = list->next; |
| 60 |
list->next->prev = other->prev; |
| 61 |
list->next = other->next; |
| 62 |
} |
| 63 |
|
| 64 |
static inline void spa_list_remove(struct spa_list *elem) |
| 65 |
{ |
| 66 |
elem->prev->next = elem->next; |
| 67 |
elem->next->prev = elem->prev; |
| 68 |
} |
| 69 |
|
| 70 |
#define spa_list_first(head, type, member) \ |
| 71 |
SPA_CONTAINER_OF((head)->next, type, member) |
| 72 |
|
| 73 |
#define spa_list_last(head, type, member) \ |
| 74 |
SPA_CONTAINER_OF((head)->prev, type, member) |
| 75 |
|
| 76 |
#define spa_list_append(list, item) \ |
| 77 |
spa_list_insert((list)->prev, item) |
| 78 |
|
| 79 |
#define spa_list_prepend(list, item) \ |
| 80 |
spa_list_insert(list, item) |
| 81 |
|
| 82 |
#define spa_list_is_end(pos, head, member) \ |
| 83 |
(&(pos)->member == (head)) |
| 84 |
|
| 85 |
#define spa_list_next(pos, member) \ |
| 86 |
SPA_CONTAINER_OF((pos)->member.next, __typeof__(*pos), member) |
| 87 |
|
| 88 |
#define spa_list_prev(pos, member) \ |
| 89 |
SPA_CONTAINER_OF((pos)->member.prev, __typeof__(*pos), member) |
| 90 |
|
| 91 |
#define spa_list_consume(pos, head, member) \ |
| 92 |
for (pos = spa_list_first(head, __typeof__(*pos), member); \ |
| 93 |
!spa_list_is_empty(head); \ |
| 94 |
pos = spa_list_first(head, __typeof__(*pos), member)) |
| 95 |
|
| 96 |
#define spa_list_for_each_next(pos, head, curr, member) \ |
| 97 |
for (pos = spa_list_first(curr, __typeof__(*pos), member); \ |
| 98 |
!spa_list_is_end(pos, head, member); \ |
| 99 |
pos = spa_list_next(pos, member)) |
| 100 |
|
| 101 |
#define spa_list_for_each_prev(pos, head, curr, member) \ |
| 102 |
for (pos = spa_list_last(curr, __typeof__(*pos), member); \ |
| 103 |
!spa_list_is_end(pos, head, member); \ |
| 104 |
pos = spa_list_prev(pos, member)) |
| 105 |
|
| 106 |
#define spa_list_for_each(pos, head, member) \ |
| 107 |
spa_list_for_each_next(pos, head, head, member) |
| 108 |
|
| 109 |
#define spa_list_for_each_reverse(pos, head, member) \ |
| 110 |
spa_list_for_each_prev(pos, head, head, member) |
| 111 |
|
| 112 |
#define spa_list_for_each_safe_next(pos, tmp, head, curr, member) \ |
| 113 |
for (pos = spa_list_first(curr, __typeof__(*pos), member); \ |
| 114 |
tmp = spa_list_next(pos, member), \ |
| 115 |
!spa_list_is_end(pos, head, member); \ |
| 116 |
pos = tmp) |
| 117 |
|
| 118 |
#define spa_list_for_each_safe_prev(pos, tmp, head, curr, member) \ |
| 119 |
for (pos = spa_list_last(curr, __typeof__(*pos), member); \ |
| 120 |
tmp = spa_list_prev(pos, member), \ |
| 121 |
!spa_list_is_end(pos, head, member); \ |
| 122 |
pos = tmp) |
| 123 |
|
| 124 |
#define spa_list_for_each_safe(pos, tmp, head, member) \ |
| 125 |
spa_list_for_each_safe_next(pos, tmp, head, head, member) |
| 126 |
|
| 127 |
#define spa_list_for_each_safe_reverse(pos, tmp, head, member) \ |
| 128 |
spa_list_for_each_safe_prev(pos, tmp, head, head, member) |
| 129 |
|
| 130 |
#define spa_list_cursor_start(cursor, head, member) \ |
| 131 |
spa_list_prepend(head, &(cursor).member) |
| 132 |
|
| 133 |
#define spa_list_for_each_cursor(pos, cursor, head, member) \ |
| 134 |
for(pos = spa_list_first(&(cursor).member, __typeof__(*(pos)), member); \ |
| 135 |
spa_list_remove(&(pos)->member), \ |
| 136 |
spa_list_append(&(cursor).member, &(pos)->member), \ |
| 137 |
!spa_list_is_end(pos, head, member); \ |
| 138 |
pos = spa_list_next(&cursor, member)) |
| 139 |
|
| 140 |
#define spa_list_cursor_end(cursor, member) \ |
| 141 |
spa_list_remove(&(cursor).member) |
| 142 |
|
| 143 |
#ifdef __cplusplus |
| 144 |
} /* extern "C" */ |
| 145 |
#endif |
| 146 |
|
| 147 |
#endif /* SPA_LIST_H */ |