diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..afea3cb --- /dev/null +++ b/src/util.c @@ -0,0 +1,11 @@ +#include +#include + +#include "util.h" + +void* _util_safe_container_of_helper(void* ptr, size_t offset) { + if (ptr == NULL) + return NULL; + else + return (char*) ptr - offset; +} diff --git a/src/util.h b/src/util.h index 6d1f6bf..2761d71 100644 --- a/src/util.h +++ b/src/util.h @@ -3,6 +3,8 @@ #include +#include "internal.h" + #ifndef _SSIZE_T_DEFINED #define SSIZE_T_DEFINED typedef intptr_t ssize_t; @@ -10,6 +12,9 @@ typedef intptr_t ssize_t; #define array_count(a) (sizeof(a) / (sizeof((a)[0]))) +#define safe_container_of(ptr, type, member) \ + ((type*) _util_safe_container_of_helper((ptr), offsetof(type, member))) + #define container_of(ptr, type, member) \ ((type*) ((char*) (ptr) -offsetof(type, member))) @@ -21,4 +26,6 @@ typedef intptr_t ssize_t; (unused)) int __static_assert_##__LINE__[(condition) ? 1 : -1]; #endif +EPOLL_INTERNAL void* _util_safe_container_of_helper(void* ptr, size_t offset); + #endif /* EPOLL_UTIL_H_ */