Merge branches 'pm-cpuidle', 'pm-sleep' and 'pm-domains'
[linux-block.git] / include / linux / container_of.h
CommitLineData
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>
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); \
e1edc277
RV
19 static_assert(__same_type(*(ptr), ((type *)0)->member) || \
20 __same_type(*(ptr), void), \
21 "pointer type mismatch in container_of()"); \
d2a8ebbf
AS
22 ((type *)(__mptr - offsetof(type, member))); })
23
24/**
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.
29 *
30 * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
31 */
32#define container_of_safe(ptr, type, member) ({ \
33 void *__mptr = (void *)(ptr); \
e1edc277
RV
34 static_assert(__same_type(*(ptr), ((type *)0)->member) || \
35 __same_type(*(ptr), void), \
36 "pointer type mismatch in container_of_safe()"); \
d2a8ebbf
AS
37 IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
38 ((type *)(__mptr - offsetof(type, member))); })
39
40#endif /* _LINUX_CONTAINER_OF_H */