16b48797aSToomas Soome /* 26b48797aSToomas Soome * This file and its contents are supplied under the terms of the 36b48797aSToomas Soome * Common Development and Distribution License ("CDDL"), version 1.0. 46b48797aSToomas Soome * You may only use this file in accordance with the terms of version 56b48797aSToomas Soome * 1.0 of the CDDL. 66b48797aSToomas Soome * 76b48797aSToomas Soome * A full copy of the text of the CDDL should have accompanied this 86b48797aSToomas Soome * source. A copy of the CDDL is also available via the Internet at 96b48797aSToomas Soome * http://www.illumos.org/license/CDDL. 106b48797aSToomas Soome */ 116b48797aSToomas Soome 126b48797aSToomas Soome /* 136b48797aSToomas Soome * Copyright 2017 Toomas Soome <tsoome@me.com> 146b48797aSToomas Soome */ 156b48797aSToomas Soome 166b48797aSToomas Soome #ifndef _SYS_STDDEF_H 176b48797aSToomas Soome #define _SYS_STDDEF_H 186b48797aSToomas Soome 196b48797aSToomas Soome /* 206b48797aSToomas Soome * Commonly used macros and definitions. 216b48797aSToomas Soome */ 226b48797aSToomas Soome 236b48797aSToomas Soome #ifdef __cplusplus 246b48797aSToomas Soome extern "C" { 256b48797aSToomas Soome #endif 266b48797aSToomas Soome 276b48797aSToomas Soome #if !defined(offsetof) 286b48797aSToomas Soome #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) 296b48797aSToomas Soome #define offsetof(s, m) __builtin_offsetof(s, m) 306b48797aSToomas Soome #else 316b48797aSToomas Soome #if __cplusplus >= 199711L 326b48797aSToomas Soome #define offsetof(s, m) (std::size_t)(&(((s *)NULL)->m)) 336b48797aSToomas Soome #else 346b48797aSToomas Soome #define offsetof(s, m) ((size_t)(&(((s *)NULL)->m))) 356b48797aSToomas Soome #endif 366b48797aSToomas Soome #endif 376b48797aSToomas Soome #endif /* !offsetof */ 386b48797aSToomas Soome 396b48797aSToomas Soome #if !defined(container_of) 40*ac7a00bdSToomas Soome 41*ac7a00bdSToomas Soome /* 42*ac7a00bdSToomas Soome * We must not expose container_of() to userland, but we want it 43*ac7a00bdSToomas Soome * to be available for early boot and for the kernel. 44*ac7a00bdSToomas Soome */ 45*ac7a00bdSToomas Soome #if ((defined(_KERNEL) || defined(_FAKE_KERNEL)) && !defined(_KMEMUSER)) || \ 46*ac7a00bdSToomas Soome (defined(_BOOT) && defined(_KMEMUSER)) 476b48797aSToomas Soome #define container_of(m, s, name) \ 486b48797aSToomas Soome (void *)((uintptr_t)(m) - (uintptr_t)offsetof(s, name)) 49*ac7a00bdSToomas Soome #endif 506b48797aSToomas Soome #endif /* !container_of */ 516b48797aSToomas Soome 526b48797aSToomas Soome #ifdef __cplusplus 536b48797aSToomas Soome } 546b48797aSToomas Soome #endif 556b48797aSToomas Soome 566b48797aSToomas Soome #endif /* _SYS_STDDEF_H */ 57