Commit | Line | Data |
---|---|---|
d2a8ebbf AS |
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> | |
2b8e3533 | 6 | #include <linux/stddef.h> |
d2a8ebbf AS |
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 | * | |
7376e561 | 16 | * WARNING: any const qualifier of @ptr is lost. |
d2a8ebbf AS |
17 | */ |
18 | #define container_of(ptr, type, member) ({ \ | |
19 | void *__mptr = (void *)(ptr); \ | |
e1edc277 RV |
20 | static_assert(__same_type(*(ptr), ((type *)0)->member) || \ |
21 | __same_type(*(ptr), void), \ | |
22 | "pointer type mismatch in container_of()"); \ | |
d2a8ebbf AS |
23 | ((type *)(__mptr - offsetof(type, member))); }) |
24 | ||
64f6a5d1 GKH |
25 | /** |
26 | * container_of_const - cast a member of a structure out to the containing | |
27 | * structure and preserve the const-ness of the pointer | |
28 | * @ptr: the pointer to the member | |
29 | * @type: the type of the container struct this is embedded in. | |
30 | * @member: the name of the member within the struct. | |
31 | */ | |
32 | #define container_of_const(ptr, type, member) \ | |
33 | _Generic(ptr, \ | |
34 | const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ | |
35 | default: ((type *)container_of(ptr, type, member)) \ | |
36 | ) | |
37 | ||
d2a8ebbf | 38 | #endif /* _LINUX_CONTAINER_OF_H */ |