Revert "Update gitignore"
[libside.git] / include / tgif / macros.h
CommitLineData
f611d0c3
MD
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
d04d4903
MD
6#ifndef _TGIF_MACROS_H
7#define _TGIF_MACROS_H
f611d0c3 8
6e46f5e6 9#include <stddef.h>
8e87e437 10#include <limits.h>
6e46f5e6 11
f611d0c3
MD
12/* Helper macros */
13
d04d4903 14#define TGIF_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
f611d0c3
MD
15
16/*
d04d4903 17 * Compound literals with static storage are needed by TGIF
f611d0c3
MD
18 * instrumentation.
19 * Compound literals are part of the C99 and C11 standards, but not
20 * part of the C++ standards. They are supported by most C++ compilers
21 * though.
22 *
23 * Example use:
24 * static struct mystruct *var = LTTNG_UST_COMPOUND_LITERAL(struct mystruct, { 1, 2, 3 });
25 */
d04d4903 26#define TGIF_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ }
f611d0c3 27
d04d4903
MD
28#define tgif_likely(x) __builtin_expect(!!(x), 1)
29#define tgif_unlikely(x) __builtin_expect(!!(x), 0)
f611d0c3 30
d04d4903 31#define TGIF_PARAM(...) __VA_ARGS__
f611d0c3 32
b59abc69 33/*
d04d4903 34 * tgif_container_of - Get the address of an object containing a field.
b59abc69
MD
35 *
36 * @ptr: pointer to the field.
37 * @type: type of the object.
38 * @member: name of the field within the object.
39 */
d04d4903 40#define tgif_container_of(ptr, type, member) \
b59abc69
MD
41 __extension__ \
42 ({ \
43 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
44 (type *)((char *)__ptr - offsetof(type, member)); \
45 })
46
d04d4903 47#define tgif_struct_field_sizeof(_struct, _field) \
88bab79c 48 sizeof(((_struct * )NULL)->_field)
8e87e437 49
f61301bb 50#if defined(__SIZEOF_LONG__)
d04d4903 51#define TGIF_BITS_PER_LONG (__SIZEOF_LONG__ * 8)
f61301bb 52#elif defined(_LP64)
d04d4903 53#define TGIF_BITS_PER_LONG 64
f61301bb 54#else
d04d4903 55#define TGIF_BITS_PER_LONG 32
f61301bb
MD
56#endif
57
d04d4903 58#define TGIF_PACKED __attribute__((packed))
de4ae6e8 59
d04d4903 60#endif /* _TGIF_MACROS_H */
This page took 0.025321 seconds and 4 git commands to generate.