1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_CONTAINER_OF_H
3 #define _LINUX_CONTAINER_OF_H
5 #include <linux/build_bug.h>
8 #define typeof_member(T, m) typeof(((T*)0)->m)
11 * container_of - cast a member of a structure out to the containing structure
12 * @ptr: the pointer to the member.
13 * @type: the type of the container struct this is embedded in.
14 * @member: the name of the member within the struct.
17 #define container_of(ptr, type, member) ({ \
18 void *__mptr = (void *)(ptr); \
19 static_assert(__same_type(*(ptr), ((type *)0)->member) || \
20 __same_type(*(ptr), void), \
21 "pointer type mismatch in container_of()"); \
22 ((type *)(__mptr - offsetof(type, member))); })
25 * container_of_safe - cast a member of a structure out to the containing structure
26 * @ptr: the pointer to the member.
27 * @type: the type of the container struct this is embedded in.
28 * @member: the name of the member within the struct.
30 * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
32 #define container_of_safe(ptr, type, member) ({ \
33 void *__mptr = (void *)(ptr); \
34 static_assert(__same_type(*(ptr), ((type *)0)->member) || \
35 __same_type(*(ptr), void), \
36 "pointer type mismatch in container_of_safe()"); \
37 IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
38 ((type *)(__mptr - offsetof(type, member))); })
40 #endif /* _LINUX_CONTAINER_OF_H */