|
139 | 139 | /* The type of the iterator 'i' is automatically determined by the type of 'head', and declared in the |
140 | 140 | * loop. Hence, do not declare the same variable in the outer scope. Sometimes, we set 'head' through |
141 | 141 | * hashmap_get(). In that case, you need to explicitly cast the result. */ |
| 142 | +#define LIST_FOREACH_WITH_NEXT(name,i,n,head) \ |
| 143 | + for (typeof(*(head)) *n, *i = (head); i && (n = i->name##_next, true); i = n) |
| 144 | + |
142 | 145 | #define LIST_FOREACH(name,i,head) \ |
143 | | - for (typeof(*(head)) *i = (head); i; i = i->name##_next) |
| 146 | + LIST_FOREACH_WITH_NEXT(name, i, UNIQ_T(n, UNIQ), head) |
144 | 147 |
|
145 | | -#define LIST_FOREACH_SAFE(name,i,n,head) \ |
146 | | - for (typeof(*(head)) *n, *i = (head); i && ((n = i->name##_next), 1); i = n) |
| 148 | +#define _LIST_FOREACH_WITH_PREV(name,i,p,start) \ |
| 149 | + for (typeof(*(start)) *p, *i = (start); i && (p = i->name##_prev, true); i = p) |
147 | 150 |
|
148 | | -#define LIST_FOREACH_BACKWARDS(name,i,p) \ |
149 | | - for (typeof(*(p)) *i = (p); i; i = i->name##_prev) |
| 151 | +#define LIST_FOREACH_BACKWARDS(name,i,start) \ |
| 152 | + _LIST_FOREACH_WITH_PREV(name, i, UNIQ_T(p, UNIQ), start) |
150 | 153 |
|
151 | 154 | /* Iterate through all the members of the list p is included in, but skip over p */ |
152 | 155 | #define LIST_FOREACH_OTHERS(name,i,p) \ |
|
0 commit comments