Skip to content

Commit

Permalink
util: use offsetof in container_of
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
GabrielL authored and whot committed May 15, 2017
1 parent 695facc commit 3236ee0
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/libinput-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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); \
Expand Down

0 comments on commit 3236ee0

Please sign in to comment.