xref: /linux-6.15/include/linux/container_of.h (revision 848dba78)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_CONTAINER_OF_H
3 #define _LINUX_CONTAINER_OF_H
4 
5 #include <linux/build_bug.h>
6 #include <linux/err.h>
7 
8 #define typeof_member(T, m)	typeof(((T*)0)->m)
9 
10 /**
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.
15  *
16  */
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))); })
23 
24 #endif	/* _LINUX_CONTAINER_OF_H */
25