util: add safe_container_of() macro definition
This commit is contained in:
parent
bb23a2b9f0
commit
1e24f44238
11
src/util.c
Normal file
11
src/util.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
void* _util_safe_container_of_helper(void* ptr, size_t offset) {
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
else
|
||||
return (char*) ptr - offset;
|
||||
}
|
||||
@ -3,6 +3,8 @@
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#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_ */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user