xref: /linux-6.15/include/linux/container_of.h (revision 23680f0b)
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  * WARNING: any const qualifier of @ptr is lost.
17  */
18 #define container_of(ptr, type, member) ({				\
19 	void *__mptr = (void *)(ptr);					\
20 	static_assert(__same_type(*(ptr), ((type *)0)->member) ||	\
21 		      __same_type(*(ptr), void),			\
22 		      "pointer type mismatch in container_of()");	\
23 	((type *)(__mptr - offsetof(type, member))); })
24 
25 #endif	/* _LINUX_CONTAINER_OF_H */
26