1 /* 2 * Copyright (c) 2025-2026 Texas Instruments Incorporated - https://www.ti.com 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 /* 8 * Container Offset Macro 9 * 10 * This header provides the ti_container_of macro for obtaining a pointer to 11 * the enclosing structure given a pointer to a member within that structure. 12 */ 13 14 #ifndef TI_CONTAINER_OF_H 15 #define TI_CONTAINER_OF_H 16 17 #include <stddef.h> 18 19 #include <ti_build_assert.h> 20 21 #define ti_check_types_match(expr1, expr2) \ 22 TI_BUILD_ASSERT_OR_ZERO(sizeof(expr1) == sizeof(expr2)) 23 24 /* 25 * ti_container_of - get pointer to enclosing structure 26 * @member_ptr: pointer to the structure member 27 * @containing_type: the type this member is within 28 * @member: the name of this member within the structure. 29 * 30 * Given a pointer to a member of a structure, this macro does pointer 31 * subtraction to return the pointer to the enclosing type. 32 */ 33 #define ti_container_of(member_ptr, containing_type, member) \ 34 ((containing_type *) ((char *)(member_ptr) - offsetof(containing_type, member)) \ 35 + ti_check_types_match(*(member_ptr), ((containing_type *)0)->member)) 36 37 #endif /* TI_CONTAINER_OF_H */ 38