list.h: add container_of_safe macro
It works like container_of, except that it also deals with NULL pointers Signed-off-by: Felix Fietkau <nbd@nbd.name>
This commit is contained in:
parent
f2d6752901
commit
45210ce141
1 changed files with 8 additions and 0 deletions
8
list.h
8
list.h
|
@ -42,6 +42,14 @@
|
|||
})
|
||||
#endif
|
||||
|
||||
#ifndef container_of_safe
|
||||
#define container_of_safe(ptr, type, member) \
|
||||
({ \
|
||||
const __typeof__(((type *) NULL)->member) *__mptr = (ptr); \
|
||||
__mptr ? (type *)((char *) __mptr - offsetof(type, member)) : NULL; \
|
||||
})
|
||||
#endif
|
||||
|
||||
struct list_head {
|
||||
struct list_head *next;
|
||||
struct list_head *prev;
|
||||
|
|
Loading…
Reference in a new issue