diff options
author | Felix Fietkau <nbd@nbd.name> | 2022-04-29 12:57:25 +0200 |
---|---|---|
committer | Felix Fietkau <nbd@nbd.name> | 2022-04-29 12:57:52 +0200 |
commit | 45210ce14136d9410d7139fba816c4465b4c7af5 (patch) | |
tree | 0d11b2b03e1b06aecbf0305ccf56f92d5fead652 | |
parent | f2d6752901f2f2d8612fb43e10061570c9198af1 (diff) | |
download | libubox-45210ce14136d9410d7139fba816c4465b4c7af5.tar.gz |
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>
-rw-r--r-- | list.h | 8 |
1 files changed, 8 insertions, 0 deletions
@@ -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; |