diff options
Diffstat (limited to 'xserver/include/list.h')
-rw-r--r-- | xserver/include/list.h | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/xserver/include/list.h b/xserver/include/list.h index 96c0bcbd6..11de7c561 100644 --- a/xserver/include/list.h +++ b/xserver/include/list.h @@ -26,6 +26,8 @@ #ifndef _XORG_LIST_H_ #define _XORG_LIST_H_ +#include <stddef.h> /* offsetof() */ + /** * @file Classic doubly-link circular list implementation. * For real usage examples of the linked list, see the file test/list.c @@ -117,7 +119,7 @@ struct xorg_list { * * @param The list to initialized. */ -static void +static inline void xorg_list_init(struct xorg_list *list) { list->next = list->prev = list; @@ -232,7 +234,7 @@ xorg_list_is_empty(struct xorg_list *head) */ #ifndef container_of #define container_of(ptr, type, member) \ - (type *)((char *)(ptr) - (char *) &((type *)0)->member) + (type *)((char *)(ptr) - offsetof(type, member)) #endif /** @@ -271,9 +273,20 @@ xorg_list_is_empty(struct xorg_list *head) #define xorg_list_last_entry(ptr, type, member) \ xorg_list_entry((ptr)->prev, type, member) -#define __container_of(ptr, sample, member) \ - (void *)((char *)(ptr) \ - - ((char *)&(sample)->member - (char *)(sample))) +#ifdef HAVE_TYPEOF +#define __container_of(ptr, sample, member) \ + container_of(ptr, typeof(*sample), member) +#else +/* This implementation of __container_of has undefined behavior according + * to the C standard, but it works in many cases. If your compiler doesn't + * support typeof() and fails with this implementation, please try a newer + * compiler. + */ +#define __container_of(ptr, sample, member) \ + (void *)((char *)(ptr) \ + - ((char *)&(sample)->member - (char *)(sample))) +#endif + /** * Loop through the list given by head and set pos to struct in the list. * @@ -345,7 +358,7 @@ xorg_list_is_empty(struct xorg_list *head) * struct foo *element = list; * while ((element = nt_list_next(element, next)) { } * - * This macro is not safe for node deletion. Use xorg_list_for_each_entry_safe + * This macro is not safe for node deletion. Use nt_list_for_each_entry_safe * instead. * * @param list The list or current element. @@ -453,7 +466,7 @@ xorg_list_is_empty(struct xorg_list *head) #define nt_list_del(_entry, _list, _type, _member) \ do { \ _type *__e = _entry; \ - if (__e == NULL) break; \ + if (__e == NULL || _list == NULL) break; \ if ((_list) == __e) { \ _list = __e->_member; \ } else { \ |