Code Coverage: vinyl-cache/bin/vinyltest/vtest2/lib/vdef.h
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2012 Fastly Inc
3
 * Copyright (c) 2006-2015 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 * Author: Rogier 'DocWilco' Mulhuijzen <rogier@fastly.com>
8
 *
9
 * Inspired by FreeBSD's <sys/cdefs.h>
10
 *
11
 * SPDX-License-Identifier: BSD-2-Clause
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions
15
 * are met:
16
 * 1. Redistributions of source code must retain the above copyright
17
 *    notice, this list of conditions and the following disclaimer.
18
 * 2. Redistributions in binary form must reproduce the above copyright
19
 *    notice, this list of conditions and the following disclaimer in the
20
 *    documentation and/or other materials provided with the distribution.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
26
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
 * SUCH DAMAGE.
33
 *
34
 * Names of the form "v_[a-z_]*_" is reserved for this file.
35
 *
36
 * This file should always be the first non <...> include in a .c file.
37
 */
38
39
#include <stddef.h>
40
#include <stdint.h>
41
42
#ifdef VDEF_H_INCLUDED
43
#  error "vdef.h included multiple times"
44
#endif
45
#define VDEF_H_INCLUDED
46
47
/* Safe printf into a fixed-size buffer */
48
#define bprintf(buf, fmt, ...)                                          \
49
        do {                                                            \
50
                int ibprintf;                                           \
51
                ibprintf = snprintf(buf, sizeof buf, fmt, __VA_ARGS__); \
52
                assert(ibprintf >= 0 && ibprintf < (int)sizeof buf);    \
53
        } while (0)
54
55
/* Safe printf into a fixed-size buffer */
56
#define vbprintf(buf, fmt, ap)                                          \
57
        do {                                                            \
58
                int ivbprintf;                                          \
59
                ivbprintf = vsnprintf(buf, sizeof buf, fmt, ap);        \
60
                assert(ivbprintf >= 0 && ivbprintf < (int)sizeof buf);  \
61
        } while (0)
62
63
/* Safe strcpy into a fixed-size buffer */
64
#define bstrcpy(dst, src)                                               \
65
        do {                                                            \
66
                assert(vstrlen(src) + 1 <= sizeof (dst));               \
67
                strcpy((dst), (src));                                   \
68
        } while (0)
69
70
// TODO #define strcpy BANNED
71
// TODO then revert 0fa4baead49f0a45f68d3db0b7743c5e4e93ad4d
72
// TODO and replace with flexelint exception
73
74
/* Close and discard filedescriptor */
75
#define closefd(fdp)                            \
76 0
        do {                                    \
77 0
                assert(*(fdp) >= 0);            \
78 0
                AZ(close(*(fdp)));              \
79 0
                *(fdp) = -1;                    \
80 0
        } while (0)
81
82
#if !defined(__has_feature)
83
#  define __has_feature(x)      0
84
#endif
85
86
#ifndef __GNUC_PREREQ__
87
# if defined __GNUC__ && defined __GNUC_MINOR__
88
#  define __GNUC_PREREQ__(maj, min) \
89
        (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
90
# else
91
#  define __GNUC_PREREQ__(maj, min) 0
92
# endif
93
#endif
94
95
#if __GNUC_PREREQ__(2, 95) || defined(__INTEL_COMPILER)
96
#  define v_printflike_(f,a) __attribute__((format(printf, f, a)))
97
#else
98
#  define v_printflike_(f,a)
99
#endif
100
101
#define v_noreturn_ __attribute__((__noreturn__))
102
103
#ifdef __GNUC__
104
#  define v_deprecated_ __attribute__((deprecated))
105
#else
106
#  define v_deprecated_
107
#endif
108
109
#if __GNUC_PREREQ__(4,4) // added 2008-07-23
110
#  define v_dont_optimize __attribute__((optimize("O")))
111
#else
112
#  define v_dont_optimize
113
#endif
114
115
#ifdef HAVE___GCOV_FLUSH
116
#  define v_gcov_flush() __gcov_flush()
117
int __gcov_flush(void);
118
#elif defined HAVE___GCOV_DUMP
119
#  define v_gcov_flush() __gcov_dump()
120
void __gcov_dump(void);
121
#elif defined HAVE___LLVM_GCOV_FLUSH
122
#  define v_gcov_flush() __llvm_gcov_flush()
123
int __llvm_gcov_flush(void);
124
#else
125
#  define v_gcov_flush() do { } while (0)
126
#endif
127
128
/*********************************************************************
129
 * Fundamental numerical limits
130
  * These limits track RFC8941
131
 * We use hex notation because 999999999999.999 is not perfectly
132
 * representable in ieee64 doubles.
133
 */
134
135
#define VRT_INTEGER_MAX 999999999999999
136
#define VRT_INTEGER_MIN -999999999999999
137
#define VRT_DECIMAL_MAX 0x1.d1a94a1fffff8p+39
138
#define VRT_DECIMAL_MIN -0x1.d1a94a1fffff8p+39
139
140
/*********************************************************************
141
 * Pointer alignment magic
142
 */
143
144
#if defined(__sparc__)
145
/* NB: Overbroad test for 32bit userland on 64bit SPARC cpus. */
146
#  define PALGN     (sizeof(double) - 1)        /* size of alignment */
147
#else
148
#  define PALGN     (sizeof(void *) - 1)        /* size of alignment */
149
#endif
150
#define PAOK(p)     (((uintptr_t)(p) & PALGN) == 0)     /* is aligned */
151
#define PRNDDN(p)   ((uintptr_t)(p) & ~PALGN)           /* Round down */
152
#define PRNDUP(p)   (((uintptr_t)(p) + PALGN) & ~PALGN) /* Round up */
153
154
/*********************************************************************
155
 * To be used as little as possible to wash off const/volatile etc.
156
 */
157
#define TRUST_ME(ptr)   ((void*)(uintptr_t)(ptr))
158
159
/**********************************************************************
160
 * Generic power-2 rounding macros
161
 */
162
163
#define PWR2(x)     ((((x)-1UL)&(x))==0)                /* Is a power of two */
164
#define RDN2(x, y)  ((x)&(~((uintptr_t)(y)-1UL)))       /* PWR2(y) true */
165
#define RUP2(x, y)  (((x)+((y)-1))&(~((uintptr_t)(y)-1UL))) /* PWR2(y) true */
166
167
/**********************************************************************
168
 * Find the minimum or maximum values.
169
 * Only evaluate the expression once and perform type checking.
170
 */
171
172
/* ref: https://stackoverflow.com/a/17624752 */
173
174
#define VINDIRECT(a, b, c)      a ## b ## c
175
#define VCOMBINE(a, b, c)       VINDIRECT(a, b, c)
176
177
#if defined(__COUNTER__)
178
#       define VUNIQ_NAME(base) VCOMBINE(base, __LINE__, __COUNTER__)
179
#else
180
#       define VUNIQ_NAME(base) VCOMBINE(base, __LINE__, 0)
181
#endif
182
183
#ifdef _lint
184
#define typeof(x) __typeof__(x)
185
#endif
186
187
/* ref: https://gcc.gnu.org/onlinedocs/gcc/Typeof.html */
188
189
#define _vtake(op, ta, tb, a, b, _va, _vb)                              \
190
        ({                                                              \
191
        ta _va = (a);                                                   \
192
        tb _vb = (b);                                                   \
193
        (void)(&_va == &_vb);                                           \
194
        _va op _vb ? _va : _vb;                                         \
195
})
196
197
#define opmin <
198
#define opmax >
199
#define vtake(n, ta, tb, a, b)  _vtake(op ## n, ta, tb, a, b,           \
200
    VUNIQ_NAME(_v ## n ## A), VUNIQ_NAME(_v ## n ## B))
201
202
#define vmin(a, b)              vtake(min, typeof(a), typeof(b), a, b)
203
#define vmax(a, b)              vtake(max, typeof(a), typeof(b), a, b)
204
205
#define vmin_t(type, a, b)      vtake(min, type, type, a, b)
206
#define vmax_t(type, a, b)      vtake(max, type, type, a, b)
207
208
/**********************************************************************
209
 * Clamp the value between two limits.
210
 */
211
212
#define vlimit(a, l, u)         vmax((l), vmin((a), (u)))
213
#define vlimit_t(type, a, l, u) vmax_t(type, (l), vmin_t(type, (a), (u)))
214
215
/**********************************************************************
216
 * Number of elements in an array
217
 *
218
 * Data on bikeshedding:
219
 * https://thephd.dev/the-big-array-size-survey-for-c-results
220
 */
221
#define vcountof(arr)   ((sizeof(arr) / sizeof((arr)[0])))
222
223
/**********************************************************************
224
 * FlexeLint and compiler shutuppery
225
 */
226
227
/*
228
 * In OO-light situations, functions have to match their prototype
229
 * even if that means not const'ing a const'able argument.
230
 * The typedef should be specified as argument to the macro.
231
 */
232
#define v_matchproto_(xxx)              /*lint --e{818} */
233
234
/*
235
 * State variables may change value before we have considered the
236
 * previous value
237
 */
238
#define v_statevariable_(varname)       varname /*lint -esym(838,varname) */
239
240
#ifdef __SUNPRO_C
241
#  define NEEDLESS(s)           {}
242
#else
243
#  define NEEDLESS(s)           s
244
#endif
245
246
#if __GNUC_PREREQ__(2, 7)
247
#  define v_unused_ __attribute__((__unused__))
248
#else
249
#  define v_unused_
250
#endif
251
252
#if __GNUC_PREREQ__(4, 3) || defined(__clang__)
253
#  define v_cold_    __attribute__((cold))
254
#  define v_used_(x) __attribute__((x, used))
255
#else
256
#  define v_cold_
257
#  define v_used_(x)
258
#endif
259
260
#if defined __has_attribute
261
#  if __has_attribute(counted_by)
262
#    define v_counted_by_(field) __attribute__((counted_by(field)))
263
#  endif
264
#endif
265
266
#ifndef v_counted_by_
267
#  define v_counted_by_(field)
268
#endif
269
270
/* VTIM API overhaul WIP */
271
typedef double vtim_mono;
272
typedef double vtim_real;
273
typedef double vtim_dur;
274
275
/**********************************************************************
276
 * txt (vas.h needed for the macros)
277
 */
278
279
typedef struct {
280
        const char              *b;
281
        const char              *e;
282
} txt;
283
284
#define Tcheck(t)       do { (void)pdiff((t).b, (t).e); } while (0)
285
#define Tlen(t)         (pdiff((t).b, (t).e))
286
#define Tstr(s)         (/*lint -e(446)*/ (txt){(s), (s) + vstrlen(s)})
287
#define Tstreq(t, s)    (Tlen(t) == vstrlen(s) && !vmemcmp((t).b, (s), Tlen(t)))
288
#define Tforeach(c, t)  for ((c) = (t).b; (c) < (t).e; (c)++)
289
290
/* #3020 dummy definitions until PR is merged*/
291
#define LIKELY(x)       (x)
292
#define UNLIKELY(x)     (x)
293
294
#ifndef __DEQUALIFY
295
#define __DEQUALIFY(type, var)  ((type)(uintptr_t)(const volatile void *)(var))
296
#endif
297
298
/**********************************************************************
299
 * vqueue.h presupposes the __containerof() macro for VSTAILQ_LAST(),
300
 * which is in FreeBSD cdefs.h.
301
 *
302
 * This differs from cdefs.h in that uintptr_t is taken from stdint.h, and
303
 * offsetof() is taken from stddef.h.
304
 */
305
306
/*
307
 * Given the pointer x to the member m of the struct s, return
308
 * a pointer to the containing structure.  When using GCC, we first
309
 * assign pointer x to a local variable, to check that its type is
310
 * compatible with member m.
311
 */
312
#ifndef __containerof
313
#if __GNUC_PREREQ__(3, 1)
314
#define __containerof(x, s, m) ({                                       \
315
        const volatile __typeof(((s *)0)->m) *__x = (x);                \
316
        __DEQUALIFY(s *, (const volatile char *)__x - offsetof(s, m));  \
317
})
318
#else
319
#define __containerof(x, s, m)                                          \
320
        __DEQUALIFY(s *, (const volatile char *)(x) - offsetof(s, m))
321
#endif
322
#endif
323
324
/**********************************************************************
325
 * various optinal built-ins
326
 *
327
 * https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions
328
 *
329
 */
330
#ifndef __has_builtin
331
#  define __has_builtin(x) 0
332
#endif
333
334
// string builtins in order of documentation
335
336
#if __has_builtin(__builtin_memchr)
337
#  define vmemchr(s, c, n) __builtin_memchr(s, c, n)
338
#else
339
#  define vmemchr(s, c, n) memchr(s, c, n)
340
#endif
341
342
#if __has_builtin(__builtin_memcmp)
343
#  define vmemcmp(s1, s2, n) __builtin_memcmp(s1, s2, n)
344
#else
345
#  define vmemcmp(s1, s2, n) memcmp(s1, s2, n)
346
#endif
347
348
#if __has_builtin(__builtin_strchr)
349
#  define vstrchr(s, c, n) __builtin_strchr(s, c, n)
350
#else
351
#  define vstrchr(s, c, n) strchr(s, c, n)
352
#endif
353
354
#if __has_builtin(__builtin_strcmp)
355
#  define vstrcmp(s1, s2) __builtin_strcmp(s1, s2)
356
#else
357
#  define vstrcmp(s1, s2) strcmp(s1, s2)
358
#endif
359
360
#if __has_builtin(__builtin_strlen)
361
#  define vstrlen(s) __builtin_strlen(s)
362
#else
363
#  define vstrlen(s) strlen(s)
364
#endif
365
366
#if __has_builtin(__builtin_strncmp)
367
#  define vstrncmp(s1, s2, n) __builtin_strncmp(s1, s2, n)
368
#else
369
#  define vstrncmp(s1, s2, n) strncmp(s1, s2, n)
370
#endif
371
372
373
#if __has_builtin(__builtin_memcpy)
374
#  define vmemcpy(d, s, n) __builtin_memcpy(d, s, n)
375
#else
376
#  define vmemcpy(d, s, n) memcpy(d, s, n)
377
#endif
378
379
#if __has_builtin(__builtin_memmove)
380
#  define vmemmove(d, s, n) __builtin_memmove(d, s, n)
381
#else
382
#  define vmemmove(d, s, n) memmove(d, s, n)
383
#endif
384
385
// ... to be extended