Loading...
Searching...
No Matches
container.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014 Freie Universität Berlin
3 * 2017 HAW-Hamburg
4 *
5 * This file is subject to the terms and conditions of the GNU Lesser
6 * General Public License v2.1. See the file LICENSE in the top level
7 * directory for more details.
8 */
9
21#ifndef CONTAINER_H
22#define CONTAINER_H
23
24#include <stddef.h>
25#include <stdint.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/* uncrustify gets mightily confused by these macros... */
32/* begin{code-style-ignore} */
33
47#if __STDC_VERSION__ >= 201112L
48# define container_of(PTR, TYPE, MEMBER) \
49 (_Generic((PTR), \
50 const __typeof__ (((TYPE *) 0)->MEMBER) *: \
51 ((TYPE *) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER))), \
52 __typeof__ (((TYPE *) 0)->MEMBER) *: \
53 ((TYPE *) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER))) \
54 ))
55#elif defined __GNUC__
56# define container_of(PTR, TYPE, MEMBER) \
57 (__extension__ ({ \
58 __extension__ const __typeof__ (((TYPE *) 0)->MEMBER) *__m____ = (PTR); \
59 ((TYPE *) ((uintptr_t) __m____ - offsetof(TYPE, MEMBER))); \
60 }))
61#else
62# define container_of(PTR, TYPE, MEMBER) \
63 ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
64#endif
65
74#define index_of(ARRAY, ELEMENT) (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof((ARRAY)[0]))
75
82#ifndef ARRAY_SIZE
83#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
84#endif
85
86#ifdef __cplusplus
87}
88#endif
89
90#endif /* CONTAINER_H */