From: Felix Fietkau Date: Fri, 29 Apr 2022 10:57:25 +0000 (+0200) Subject: list.h: add container_of_safe macro X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=45210ce14136d9410d7139fba816c4465b4c7af5;p=project%2Flibubox.git 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 --- diff --git a/list.h b/list.h index a2cb241..6aa7b2a 100644 --- a/list.h +++ b/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;