Skip to content

Commit 009b4d5

Browse files
author
Felix Fietkau
committed
list.h: implement type safety for container_of()
Signed-off-by: Felix Fietkau <[email protected]>
1 parent cdb9f71 commit 009b4d5

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

list.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
#define prefetch(x)
3636

3737
#ifndef container_of
38-
#define container_of(ptr, type, member) ( \
39-
(type *)( (char *)ptr - offsetof(type,member) ))
38+
#define container_of(ptr, type, member) \
39+
({ \
40+
const typeof(((type *) NULL)->member) *__mptr = (ptr); \
41+
(type *) ((char *) __mptr - offsetof(type, member)); \
42+
})
4043
#endif
4144

4245
struct list_head {

0 commit comments

Comments
 (0)