Loading...
Searching...
No Matches
container.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2014 Freie Universität Berlin
3 * SPDX-FileCopyrightText: 2017 HAW-Hamburg
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
19
20#include <stddef.h>
21#include <stdint.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/* uncrustify gets mightily confused by these macros... */
28/* begin{code-style-ignore} */
29
43#if __STDC_VERSION__ >= 201112L
44# define container_of(PTR, TYPE, MEMBER) \
45 (_Generic((PTR), \
46 const __typeof__ (((TYPE *) 0)->MEMBER) *: \
47 ((TYPE *) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER))), \
48 __typeof__ (((TYPE *) 0)->MEMBER) *: \
49 ((TYPE *) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER))) \
50 ))
51#elif defined __GNUC__
52# define container_of(PTR, TYPE, MEMBER) \
53 (__extension__ ({ \
54 __extension__ const __typeof__ (((TYPE *) 0)->MEMBER) *__m____ = (PTR); \
55 ((TYPE *) ((uintptr_t) __m____ - offsetof(TYPE, MEMBER))); \
56 }))
57#else
58# define container_of(PTR, TYPE, MEMBER) \
59 ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
60#endif
61
70#define index_of(ARRAY, ELEMENT) (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof((ARRAY)[0]))
71
78#ifndef ARRAY_SIZE
79#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
80#endif
81
82#ifdef __cplusplus
83}
84#endif
85