util: use offsetof in container_of

gcc and clang supports offsetof (defined in stddef.h) as defined by C99
and POSIX.1-2001, use it in container_of.

Signed-off-by: Gabriel Laskar <gabriel@lse.epita.fr>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Gabriel Laskar 2017-05-14 16:33:16 +02:00 committed by Peter Hutterer
parent 695facc130
commit 3236ee0d90

View file

@ -34,6 +34,7 @@
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
@ -86,8 +87,8 @@ void list_remove(struct list *elm);
bool list_empty(const struct list *list);
#define container_of(ptr, sample, member) \
(__typeof__(sample))((char *)(ptr) - \
((char *)&((typeof(sample))0)->member))
(__typeof__(sample))((char *)(ptr) - \
offsetof(__typeof__(*sample), member))
#define list_for_each(pos, head, member) \
for (pos = 0, pos = container_of((head)->next, pos, member); \