X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Futil%2Flist.h;h=8a18c4b0d8d8b99f500359082a5d1906b2d1a95b;hb=c2d0cc8b8d5bd2f3b194642fd0187283da51d4ae;hp=d4b485174fc7391fc2dde6a127e7d224dc02d75f;hpb=958fc04dc51a2561c8598f42df59e3d9139e56a7;p=mesa.git diff --git a/src/util/list.h b/src/util/list.h index d4b485174fc..8a18c4b0d8d 100644 --- a/src/util/list.h +++ b/src/util/list.h @@ -41,7 +41,13 @@ #include #include #include +#include "c99_compat.h" +#ifdef DEBUG +# define list_assert(cond, msg) assert(cond && msg) +#else +# define list_assert(cond, msg) (void)(0 && (cond)) +#endif struct list_head { @@ -71,12 +77,18 @@ static inline void list_addtail(struct list_head *item, struct list_head *list) list->prev = item; } +static inline bool list_is_empty(const struct list_head *list); + static inline void list_replace(struct list_head *from, struct list_head *to) { - to->prev = from->prev; - to->next = from->next; - from->next->prev = to; - from->prev->next = to; + if (list_is_empty(from)) { + list_inithead(to); + } else { + to->prev = from->prev; + to->next = from->next; + from->next->prev = to; + from->prev->next = to; + } } static inline void list_del(struct list_head *item) @@ -94,12 +106,20 @@ static inline void list_delinit(struct list_head *item) item->prev = item; } -static inline bool list_empty(struct list_head *list) +static inline bool list_is_empty(const struct list_head *list) { return list->next == list; } -static inline unsigned list_length(struct list_head *list) +/** + * Returns whether the list has exactly one element. + */ +static inline bool list_is_singular(const struct list_head *list) +{ + return list->next != NULL && list->next != list && list->next->next == list; +} + +static inline unsigned list_length(const struct list_head *list) { struct list_head *node; unsigned length = 0; @@ -110,7 +130,7 @@ static inline unsigned list_length(struct list_head *list) static inline void list_splice(struct list_head *src, struct list_head *dst) { - if (list_empty(src)) + if (list_is_empty(src)) return; src->next->prev = dst; @@ -121,7 +141,7 @@ static inline void list_splice(struct list_head *src, struct list_head *dst) static inline void list_splicetail(struct list_head *src, struct list_head *dst) { - if (list_empty(src)) + if (list_is_empty(src)) return; src->prev->next = dst; @@ -130,7 +150,7 @@ static inline void list_splicetail(struct list_head *src, struct list_head *dst) dst->prev = src->prev; } -static inline void list_validate(struct list_head *list) +static inline void list_validate(const struct list_head *list) { struct list_head *node; assert(list->next->prev == list && list->prev->next == list); @@ -138,19 +158,9 @@ static inline void list_validate(struct list_head *list) assert(node->next->prev == node && node->prev->next == node); } -#define LIST_INITHEAD(__item) list_inithead(__item) -#define LIST_ADD(__item, __list) list_add(__item, __list) -#define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list) -#define LIST_REPLACE(__from, __to) list_replace(__from, __to) -#define LIST_DEL(__item) list_del(__item) -#define LIST_DELINIT(__item) list_delinit(__item) - #define LIST_ENTRY(__type, __item, __field) \ ((__type *)(((char *)(__item)) - offsetof(__type, __field))) -#define LIST_IS_EMPTY(__list) \ - ((__list)->next == (__list)) - /** * Cast from a pointer to a member of a struct back to the containing struct. * @@ -197,21 +207,27 @@ static inline void list_validate(struct list_head *list) pos = container_of(pos->member.prev, pos, member)) #define list_for_each_entry(type, pos, head, member) \ - for (type *pos = LIST_ENTRY(type, (head)->next, member); \ + for (type *pos = LIST_ENTRY(type, (head)->next, member), \ + *__next = LIST_ENTRY(type, pos->member.next, member); \ &pos->member != (head); \ - pos = LIST_ENTRY(type, pos->member.next, member)) + pos = LIST_ENTRY(type, pos->member.next, member), \ + list_assert(pos == __next, "use _safe iterator"), \ + __next = LIST_ENTRY(type, __next->member.next, member)) #define list_for_each_entry_safe(type, pos, head, member) \ for (type *pos = LIST_ENTRY(type, (head)->next, member), \ *__next = LIST_ENTRY(type, pos->member.next, member); \ &pos->member != (head); \ pos = __next, \ - __next = LIST_ENTRY(type, __next->member.next, member)) + __next = LIST_ENTRY(type, __next->member.next, member)) #define list_for_each_entry_rev(type, pos, head, member) \ - for (type *pos = LIST_ENTRY(type, (head)->prev, member); \ + for (type *pos = LIST_ENTRY(type, (head)->prev, member), \ + *__prev = LIST_ENTRY(type, pos->member.prev, member); \ &pos->member != (head); \ - pos = LIST_ENTRY(type, pos->member.prev, member)) + pos = LIST_ENTRY(type, pos->member.prev, member), \ + list_assert(pos == __prev, "use _safe iterator"), \ + __prev = LIST_ENTRY(type, __prev->member.prev, member)) #define list_for_each_entry_safe_rev(type, pos, head, member) \ for (type *pos = LIST_ENTRY(type, (head)->prev, member), \