]> granicus.if.org Git - strace/blob - macros.h
rtnl_neightbl: always decode struct ndt_config and struct ndt_stats
[strace] / macros.h
1 /*
2  * Copyright (c) 2001-2019 The strace developers.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  */
7
8 #ifndef STRACE_MACROS_H
9 # define STRACE_MACROS_H
10
11 # include <stdbool.h>
12 # include <stddef.h>
13 # include <sys/types.h>
14
15 # include "gcc_compat.h"
16
17 # define ARRAY_SIZE(a_) (sizeof(a_) / sizeof((a_)[0]) + MUST_BE_ARRAY(a_))
18
19 # define ARRSZ_PAIR(a_) a_, ARRAY_SIZE(a_)
20
21 # define STRINGIFY(...)         #__VA_ARGS__
22 # define STRINGIFY_VAL(...)     STRINGIFY(__VA_ARGS__)
23
24 # ifndef MAX
25 #  define MAX(a, b)             (((a) > (b)) ? (a) : (b))
26 # endif
27 # ifndef MIN
28 #  define MIN(a, b)             (((a) < (b)) ? (a) : (b))
29 # endif
30 # define CLAMP(val, min, max)   MIN(MAX(min, val), max)
31
32 # ifndef ROUNDUP_DIV
33 #  define ROUNDUP_DIV(val_, div_) (((val_) + (div_) - 1) / (div_))
34 # endif
35
36 # ifndef ROUNDUP
37 #  define ROUNDUP(val_, div_) (ROUNDUP_DIV((val_), (div_)) * (div_))
38 # endif
39
40 # ifndef offsetofend
41 #  define offsetofend(type_, member_)   \
42         (offsetof(type_, member_) + sizeof(((type_ *)0)->member_))
43 # endif
44
45 # ifndef cast_ptr
46 #  define cast_ptr(type_, var_) \
47         ((type_) (uintptr_t) (const volatile void *) (var_))
48 # endif
49
50 # ifndef containerof
51 /**
52  * Return a pointer to a structure that contains the provided variable.
53  *
54  * @param ptr_    Pointer to data that is a field of the container structure.
55  * @param struct_ Type of the container structure.
56  * @param member_ Name of the member field.
57  * @return  Pointer to the container structure.
58  */
59 #  define containerof(ptr_, struct_, member_)   \
60         cast_ptr(struct_ *,                     \
61                  (const volatile char *) (ptr_) - offsetof(struct_, member_))
62 # endif
63
64 static inline bool
65 is_filled(const char *ptr, char fill, size_t size)
66 {
67         while (size--)
68                 if (*ptr++ != fill)
69                         return false;
70
71         return true;
72 }
73
74 # define IS_ARRAY_ZERO(arr_)    \
75         is_filled((const char *) (arr_), 0, sizeof(arr_) + MUST_BE_ARRAY(arr_))
76
77 # ifndef BIT
78 #  define BIT(x_) (1U << (x_))
79 # endif
80
81 # define FLAG(name_) name_ = BIT(name_##_BIT)
82
83 #endif /* !STRACE_MACROS_H */