2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2017 Toomas Soome <tsoome@me.com>
16 #ifndef _SYS_CONTAINEROF_H
17 #define _SYS_CONTAINEROF_H
20 * __containerof macro for private use in illumos.
22 * __containerof(ptr, type, member) will return pointer to the data
23 * structure of given type, calculated based on the offset of 'member'
24 * in the structure 'type'.
26 * For this macro to work, we should be certain of the pointer type.
29 #include <sys/stddef.h>
35 #if !defined(__containerof)
38 * The extension to support statements and declarations in expressions,
39 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html, is available
41 * We perform the assignment below to try and provide additional type safety.
43 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
44 #define __containerof(m, s, name) ( \
46 const volatile __typeof(((s *)0)->name) *__m = (m); \
47 (void *)((uintptr_t)__m - (uintptr_t)offsetof(s, name)); \
50 #define __containerof(m, s, name) \
51 (void *)((uintptr_t)(m) - (uintptr_t)offsetof(s, name))
59 #endif /* _SYS_CONTAINEROF_H */