162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef _LINUX_CONTAINER_OF_H 362306a36Sopenharmony_ci#define _LINUX_CONTAINER_OF_H 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci#include <linux/build_bug.h> 662306a36Sopenharmony_ci#include <linux/stddef.h> 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#define typeof_member(T, m) typeof(((T*)0)->m) 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci/** 1162306a36Sopenharmony_ci * container_of - cast a member of a structure out to the containing structure 1262306a36Sopenharmony_ci * @ptr: the pointer to the member. 1362306a36Sopenharmony_ci * @type: the type of the container struct this is embedded in. 1462306a36Sopenharmony_ci * @member: the name of the member within the struct. 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * WARNING: any const qualifier of @ptr is lost. 1762306a36Sopenharmony_ci */ 1862306a36Sopenharmony_ci#define container_of(ptr, type, member) ({ \ 1962306a36Sopenharmony_ci void *__mptr = (void *)(ptr); \ 2062306a36Sopenharmony_ci static_assert(__same_type(*(ptr), ((type *)0)->member) || \ 2162306a36Sopenharmony_ci __same_type(*(ptr), void), \ 2262306a36Sopenharmony_ci "pointer type mismatch in container_of()"); \ 2362306a36Sopenharmony_ci ((type *)(__mptr - offsetof(type, member))); }) 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci/** 2662306a36Sopenharmony_ci * container_of_const - cast a member of a structure out to the containing 2762306a36Sopenharmony_ci * structure and preserve the const-ness of the pointer 2862306a36Sopenharmony_ci * @ptr: the pointer to the member 2962306a36Sopenharmony_ci * @type: the type of the container struct this is embedded in. 3062306a36Sopenharmony_ci * @member: the name of the member within the struct. 3162306a36Sopenharmony_ci */ 3262306a36Sopenharmony_ci#define container_of_const(ptr, type, member) \ 3362306a36Sopenharmony_ci _Generic(ptr, \ 3462306a36Sopenharmony_ci const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ 3562306a36Sopenharmony_ci default: ((type *)container_of(ptr, type, member)) \ 3662306a36Sopenharmony_ci ) 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci#endif /* _LINUX_CONTAINER_OF_H */ 39