]> granicus.if.org Git - strace/blob - macros.h
Fix preprocessor indentation
[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 <sys/types.h>
13
14 # include "gcc_compat.h"
15
16 # define ARRAY_SIZE(a_) (sizeof(a_) / sizeof((a_)[0]) + MUST_BE_ARRAY(a_))
17
18 # define ARRSZ_PAIR(a_) a_, ARRAY_SIZE(a_)
19
20 # define STRINGIFY(...)         #__VA_ARGS__
21 # define STRINGIFY_VAL(...)     STRINGIFY(__VA_ARGS__)
22
23 # ifndef MAX
24 #  define MAX(a, b)             (((a) > (b)) ? (a) : (b))
25 # endif
26 # ifndef MIN
27 #  define MIN(a, b)             (((a) < (b)) ? (a) : (b))
28 # endif
29 # define CLAMP(val, min, max)   MIN(MAX(min, val), max)
30
31 # ifndef ROUNDUP_DIV
32 #  define ROUNDUP_DIV(val_, div_) (((val_) + (div_) - 1) / (div_))
33 # endif
34
35 # ifndef ROUNDUP
36 #  define ROUNDUP(val_, div_) (ROUNDUP_DIV((val_), (div_)) * (div_))
37 # endif
38
39 # ifndef offsetofend
40 #  define offsetofend(type_, member_)   \
41         (offsetof(type_, member_) + sizeof(((type_ *)0)->member_))
42 # endif
43
44 # ifndef cast_ptr
45 #  define cast_ptr(type_, var_) \
46         ((type_) (uintptr_t) (const volatile void *) (var_))
47 # endif
48
49 # ifndef containerof
50 /**
51  * Return a pointer to a structure that contains the provided variable.
52  *
53  * @param ptr_    Pointer to data that is a field of the container structure.
54  * @param struct_ Type of the container structure.
55  * @param member_ Name of the member field.
56  * @return  Pointer to the container structure.
57  */
58 #  define containerof(ptr_, struct_, member_)   \
59         cast_ptr(struct_ *,                     \
60                  (const volatile char *) (ptr_) - offsetof(struct_, member_))
61 # endif
62
63 static inline bool
64 is_filled(const char *ptr, char fill, size_t size)
65 {
66         while (size--)
67                 if (*ptr++ != fill)
68                         return false;
69
70         return true;
71 }
72
73 # define IS_ARRAY_ZERO(arr_)    \
74         is_filled((const char *) (arr_), 0, sizeof(arr_) + MUST_BE_ARRAY(arr_))
75
76 #endif /* !STRACE_MACROS_H */