Code Coverage: vinyl-cache/include/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 <string.h>     // string functions if no builtin
40
#include <stddef.h>
41
#include <stdint.h>
42
43
#ifdef VDEF_H_INCLUDED
44
#  error "vdef.h included multiple times"
45
#endif
46
#define VDEF_H_INCLUDED
47
48
/* Safe printf into a fixed-size buffer */
49
#define bprintf(buf, fmt, ...)                                          \
50 464976
        do {                                                            \
51 464976
                int ibprintf;                                           \
52 464976
                ibprintf = snprintf(buf, sizeof buf, fmt, __VA_ARGS__); \
53 464976
                assert(ibprintf >= 0 && ibprintf < (int)sizeof buf);    \
54 464976
        } while (0)
55
56
/* Safe printf into a fixed-size buffer */
57
#define vbprintf(buf, fmt, ap)                                          \
58 41755
        do {                                                            \
59 41755
                int ivbprintf;                                          \
60 41755
                ivbprintf = vsnprintf(buf, sizeof buf, fmt, ap);        \
61 41755
                assert(ivbprintf >= 0 && ivbprintf < (int)sizeof buf);  \
62 41755
        } while (0)
63
64
/* Safe strcpy into a fixed-size buffer */
65
#define bstrcpy(dst, src)                                               \
66 40523
        do {                                                            \
67 40523
                assert(vstrlen(src) + 1 <= sizeof (dst));               \
68 40523
                strcpy((dst), (src));                                   \
69 40523
        } while (0)
70
71
// TODO #define strcpy BANNED
72
// TODO then revert 0fa4baead49f0a45f68d3db0b7743c5e4e93ad4d
73
// TODO and replace with flexelint exception
74
75
/* Close and discard filedescriptor */
76
#define closefd(fdp)                            \
77 564129
        do {                                    \
78 564054
                assert(*(fdp) >= 0);            \
79 564054
                AZ(close(*(fdp)));              \
80 564054
                *(fdp) = -1;                    \
81 564054
        } while (0)
82
83
#if !defined(__has_feature)
84
#  define __has_feature(x)      0
85
#endif
86
87
#ifndef __GNUC_PREREQ__
88
# if defined __GNUC__ && defined __GNUC_MINOR__
89
#  define __GNUC_PREREQ__(maj, min) \
90
        (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
91
# else
92
#  define __GNUC_PREREQ__(maj, min) 0
93
# endif
94
#endif
95
96
#if __GNUC_PREREQ__(2, 95) || defined(__INTEL_COMPILER)
97
#  define v_printflike_(f,a) __attribute__((format(printf, f, a)))
98
#else
99
#  define v_printflike_(f,a)
100
#endif
101
102
#define v_noreturn_ __attribute__((__noreturn__))
103
104
#ifdef __GNUC__
105
#  define v_deprecated_ __attribute__((deprecated))
106
#else
107
#  define v_deprecated_
108
#endif
109
110
#if __GNUC_PREREQ__(4,4) // added 2008-07-23
111
#  define v_dont_optimize __attribute__((optimize("O")))
112
#else
113
#  define v_dont_optimize
114
#endif
115
116
#ifdef HAVE___GCOV_FLUSH
117
#  define v_gcov_flush() __gcov_flush()
118
int __gcov_flush(void);
119
#elif defined HAVE___GCOV_DUMP
120
#  define v_gcov_flush() __gcov_dump()
121
void __gcov_dump(void);
122
#elif defined HAVE___LLVM_GCOV_FLUSH
123
#  define v_gcov_flush() __llvm_gcov_flush()
124
int __llvm_gcov_flush(void);
125
#else
126 11
#  define v_gcov_flush() do { } while (0)
127
#endif
128
129
/*********************************************************************
130
 * Fundamental numerical limits
131
  * These limits track RFC8941
132
 * We use hex notation because 999999999999.999 is not perfectly
133
 * representable in ieee64 doubles.
134
 */
135
136 58317
#define VRT_INTEGER_MAX 999999999999999
137 11990
#define VRT_INTEGER_MIN -999999999999999
138 617804
#define VRT_DECIMAL_MAX 0x1.d1a94a1fffff8p+39
139 1027382
#define VRT_DECIMAL_MIN -0x1.d1a94a1fffff8p+39
140
141
/*********************************************************************
142
 * Pointer alignment magic
143
 */
144
145
#if defined(__sparc__)
146
/* NB: Overbroad test for 32bit userland on 64bit SPARC cpus. */
147
#  define PALGN     (sizeof(double) - 1)        /* size of alignment */
148
#else
149 913958
#  define PALGN     (sizeof(void *) - 1)        /* size of alignment */
150
#endif
151
#define PAOK(p)     (((uintptr_t)(p) & PALGN) == 0)     /* is aligned */
152 39360
#define PRNDDN(p)   ((uintptr_t)(p) & ~PALGN)           /* Round down */
153 436789
#define PRNDUP(p)   (((uintptr_t)(p) + PALGN) & ~PALGN) /* Round up */
154
155
/*********************************************************************
156
 * To be used as little as possible to wash off const/volatile etc.
157
 */
158 408171
#define TRUST_ME(ptr)   ((void*)(uintptr_t)(ptr))
159
160
/**********************************************************************
161
 * Generic power-2 rounding macros
162
 */
163
164 92
#define PWR2(x)     ((((x)-1UL)&(x))==0)                /* Is a power of two */
165 7197
#define RDN2(x, y)  ((x)&(~((uintptr_t)(y)-1UL)))       /* PWR2(y) true */
166 62128
#define RUP2(x, y)  (((x)+((y)-1))&(~((uintptr_t)(y)-1UL))) /* PWR2(y) true */
167
168
/**********************************************************************
169
 * Find the minimum or maximum values.
170
 * Only evaluate the expression once and perform type checking.
171
 */
172
173
/* ref: https://stackoverflow.com/a/17624752 */
174
175
#define VINDIRECT(a, b, c)      a ## b ## c
176
#define VCOMBINE(a, b, c)       VINDIRECT(a, b, c)
177
178
#if defined(__COUNTER__)
179
#       define VUNIQ_NAME(base) VCOMBINE(base, __LINE__, __COUNTER__)
180
#else
181
#       define VUNIQ_NAME(base) VCOMBINE(base, __LINE__, 0)
182
#endif
183
184
#ifdef _lint
185
#define typeof(x) __typeof__(x)
186
#endif
187
188
/* ref: https://gcc.gnu.org/onlinedocs/gcc/Typeof.html */
189
190
#define _vtake(op, ta, tb, a, b, _va, _vb)                              \
191 16365907
        ({                                                              \
192 16365907
        ta _va = (a);                                                   \
193 16365907
        tb _vb = (b);                                                   \
194 16365907
        (void)(&_va == &_vb);                                           \
195 16365907
        _va op _vb ? _va : _vb;                                         \
196 16365907
})
197
198
#define opmin <
199
#define opmax >
200 16365907
#define vtake(n, ta, tb, a, b)  _vtake(op ## n, ta, tb, a, b,           \
201 16365357
    VUNIQ_NAME(_v ## n ## A), VUNIQ_NAME(_v ## n ## B))
202
203 79924
#define vmin(a, b)              vtake(min, typeof(a), typeof(b), a, b)
204 1012688
#define vmax(a, b)              vtake(max, typeof(a), typeof(b), a, b)
205
206 15069717
#define vmin_t(type, a, b)      vtake(min, type, type, a, b)
207 203582
#define vmax_t(type, a, b)      vtake(max, type, type, a, b)
208
209
/**********************************************************************
210
 * Clamp the value between two limits.
211
 */
212
213
#define vlimit(a, l, u)         vmax((l), vmin((a), (u)))
214 1193
#define vlimit_t(type, a, l, u) vmax_t(type, (l), vmin_t(type, (a), (u)))
215
216
/**********************************************************************
217
 * Number of elements in an array
218
 *
219
 * Data on bikeshedding:
220
 * https://thephd.dev/the-big-array-size-survey-for-c-results
221
 */
222 147085
#define vcountof(arr)   ((sizeof(arr) / sizeof((arr)[0])))
223
224
/**********************************************************************
225
 * FlexeLint and compiler shutuppery
226
 */
227
228
/*
229
 * In OO-light situations, functions have to match their prototype
230
 * even if that means not const'ing a const'able argument.
231
 * The typedef should be specified as argument to the macro.
232
 */
233
#define v_matchproto_(xxx)              /*lint --e{818} */
234
235
/*
236
 * State variables may change value before we have considered the
237
 * previous value
238
 */
239
#define v_statevariable_(varname)       varname /*lint -esym(838,varname) */
240
241
#ifdef __SUNPRO_C
242
#  define NEEDLESS(s)           {}
243
#else
244 4111
#  define NEEDLESS(s)           s
245
#endif
246
247
#if __GNUC_PREREQ__(2, 7)
248
#  define v_unused_ __attribute__((__unused__))
249
#else
250
#  define v_unused_
251
#endif
252
253
#if __GNUC_PREREQ__(4, 3) || defined(__clang__)
254
#  define v_cold_    __attribute__((cold))
255
#  define v_used_(x) __attribute__((x, used))
256
#else
257
#  define v_cold_
258
#  define v_used_(x)
259
#endif
260
261
#if defined __has_attribute
262
#  if __has_attribute(counted_by)
263
#    define v_counted_by_(field) __attribute__((counted_by(field)))
264
#  endif
265
#endif
266
267
#ifndef v_counted_by_
268
#  define v_counted_by_(field)
269
#endif
270
271
/* VTIM API overhaul WIP */
272
typedef double vtim_mono;
273
typedef double vtim_real;
274
typedef double vtim_dur;
275
276
/**********************************************************************
277
 * txt (vas.h needed for the macros)
278
 */
279
280
typedef struct {
281
        const char              *b;
282
        const char              *e;
283
} txt;
284
285 2139834
#define Tcheck(t)       do { (void)pdiff((t).b, (t).e); } while (0)
286 223986
#define Tlen(t)         (pdiff((t).b, (t).e))
287 21
#define Tstr(s)         (/*lint -e(446)*/ (txt){(s), (s) + vstrlen(s)})
288 1519
#define Tstreq(t, s)    (Tlen(t) == vstrlen(s) && !vmemcmp((t).b, (s), Tlen(t)))
289 39082
#define Tforeach(c, t)  for ((c) = (t).b; (c) < (t).e; (c)++)
290
291
/* #3020 dummy definitions until PR is merged*/
292 2044
#define LIKELY(x)       (x)
293 7369
#define UNLIKELY(x)     (x)
294
295
#ifndef __DEQUALIFY
296
#define __DEQUALIFY(type, var)  ((type)(uintptr_t)(const volatile void *)(var))
297
#endif
298
299
/**********************************************************************
300
 * vqueue.h presupposes the __containerof() macro for VSTAILQ_LAST(),
301
 * which is in FreeBSD cdefs.h.
302
 *
303
 * This differs from cdefs.h in that uintptr_t is taken from stdint.h, and
304
 * offsetof() is taken from stddef.h.
305
 */
306
307
/*
308
 * Given the pointer x to the member m of the struct s, return
309
 * a pointer to the containing structure.  When using GCC, we first
310
 * assign pointer x to a local variable, to check that its type is
311
 * compatible with member m.
312
 */
313
#ifndef __containerof
314
#if __GNUC_PREREQ__(3, 1)
315
#define __containerof(x, s, m) ({                                       \
316
        const volatile __typeof(((s *)0)->m) *__x = (x);                \
317
        __DEQUALIFY(s *, (const volatile char *)__x - offsetof(s, m));  \
318
})
319
#else
320
#define __containerof(x, s, m)                                          \
321
        __DEQUALIFY(s *, (const volatile char *)(x) - offsetof(s, m))
322
#endif
323
#endif
324
325
/**********************************************************************
326
 * various optinal built-ins
327
 *
328
 * https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions
329
 *
330
 */
331
#ifndef __has_builtin
332
#  define __has_builtin(x) 0
333
#endif
334
335
// string builtins in order of documentation
336
337
#if __has_builtin(__builtin_memchr)
338 33516
#  define vmemchr(s, c, n) __builtin_memchr(s, c, n)
339
#else
340
#  define vmemchr(s, c, n) memchr(s, c, n)
341
#endif
342
343
#if __has_builtin(__builtin_memcmp)
344 83599
#  define vmemcmp(s1, s2, n) __builtin_memcmp(s1, s2, n)
345
#else
346
#  define vmemcmp(s1, s2, n) memcmp(s1, s2, n)
347
#endif
348
349
#if __has_builtin(__builtin_strchr)
350
#  define vstrchr(s, c, n) __builtin_strchr(s, c, n)
351
#else
352
#  define vstrchr(s, c, n) strchr(s, c, n)
353
#endif
354
355
#if __has_builtin(__builtin_strcmp)
356 27342725
#  define vstrcmp(s1, s2) __builtin_strcmp(s1, s2)
357
#else
358
#  define vstrcmp(s1, s2) strcmp(s1, s2)
359
#endif
360
361
#if __has_builtin(__builtin_strlen)
362 6683009
#  define vstrlen(s) __builtin_strlen(s)
363
#else
364
#  define vstrlen(s) strlen(s)
365
#endif
366
367
#if __has_builtin(__builtin_strncmp)
368 153590232
#  define vstrncmp(s1, s2, n) __builtin_strncmp(s1, s2, n)
369
#else
370
#  define vstrncmp(s1, s2, n) strncmp(s1, s2, n)
371
#endif
372
373
374
#if __has_builtin(__builtin_memcpy)
375 5573091
#  define vmemcpy(d, s, n) __builtin_memcpy(d, s, n)
376
#else
377
#  define vmemcpy(d, s, n) memcpy(d, s, n)
378
#endif
379
380
#if __has_builtin(__builtin_memmove)
381 31845
#  define vmemmove(d, s, n) __builtin_memmove(d, s, n)
382
#else
383
#  define vmemmove(d, s, n) memmove(d, s, n)
384
#endif
385
386
// ... to be extended