varnish-cache/include/vas.h
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2011 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 *
31
 * assert(), AN() and AZ() are static checks that should not happen.
32
 *      In general asserts should be cheap, such as checking return
33
 *      values and similar.
34
 * diagnostic() are asserts which are so expensive that we may want
35
 *      to compile them out for performance at a later date.
36
 * xxxassert(), XXXAN() and XXXAZ() marks conditions we ought to
37
 *      handle gracefully, such as malloc failure.
38
 */
39
40
#ifndef VAS_H_INCLUDED
41
#define VAS_H_INCLUDED
42
43
44
#include <errno.h>
45
#include <stddef.h>     // size_t
46
47
const char * VAS_errtxt(int e);
48
49
enum vas_e {
50
        VAS_WRONG,
51
        VAS_MISSING,
52
        VAS_ASSERT,
53
        VAS_INCOMPLETE,
54
        VAS_VCL,
55
};
56
57
typedef void vas_f(const char *, const char *, int, const char *, enum vas_e);
58
59
extern vas_f *VAS_Fail_Func v_noreturn_;
60
extern vas_f VAS_Fail v_noreturn_;
61
62
#ifdef WITHOUT_ASSERTS
63
#define assert(e)       ((void)(e))
64
#else /* WITH_ASSERTS */
65
#define assert(e)                                                       \
66
do {                                                                    \
67
        if (!(e)) {                                                     \
68
                VAS_Fail(__func__, __FILE__, __LINE__,                  \
69
                    #e, VAS_ASSERT);                                    \
70
        }                                                               \
71
} while (0)
72
#endif
73
74
#define xxxassert(e)                                                    \
75
do {                                                                    \
76
        if (!(e)) {                                                     \
77
                VAS_Fail(__func__, __FILE__, __LINE__,                  \
78
                    #e, VAS_MISSING);                                   \
79
        }                                                               \
80
} while (0)
81
82
/* Assert zero return value */
83
#define AZ(foo)         do { assert((foo) == 0); } while (0)
84
#define AN(foo)         do { assert((foo) != 0); } while (0)
85
#define XXXAZ(foo)      do { xxxassert((foo) == 0); } while (0)
86
#define XXXAN(foo)      do { xxxassert((foo) != 0); } while (0)
87
#define diagnostic(foo) assert(foo)
88
#define WRONG(expl)                                                     \
89
do {                                                                    \
90
        VAS_Fail(__func__, __FILE__, __LINE__, expl, VAS_WRONG);        \
91
} while (0)
92
93
#define INCOMPL()                                                       \
94
do {                                                                    \
95
        VAS_Fail(__func__, __FILE__, __LINE__,                          \
96
            "", VAS_INCOMPLETE);                                        \
97
} while (0)
98
99
/*
100
 * Most of this nightmare is stolen from FreeBSD's <cdefs.h>
101
 */
102
#ifndef __has_extension
103
#  define __has_extension(x)    0
104
#endif
105
106
#if __has_extension(c_static_assert)
107
#   define v_static_assert _Static_assert
108
#elif __GNUC_PREREQ__(4,6) && !defined(__cplusplus)
109
#   define v_static_assert _Static_assert
110
#else
111
#   if defined(__COUNTER__)
112
#       define v_static_assert(x, y)    __v_static_assert(x, __COUNTER__)
113
#   else
114
#       define v_static_assert(x, y)    __v_static_assert(x, __LINE__)
115
#   endif
116
#   define __v_static_assert(x, y)      ___v_static_assert(x, y)
117
#   define ___v_static_assert(x, y) \
118
                typedef char __vassert_## y[(x) ? 1 : -1] v_unused_
119
#endif
120
121
/*
122
 * A normal pointer difference is signed, but when we don't want a negative
123
 * value this little tool will make sure we don't get that.
124
 */
125
126
static inline size_t
127 3036554
pdiff(const void *b, const void *e)
128
{
129
130 3036554
        assert(b <= e);
131 3036589
        return ((size_t)((const char *)e - (const char *)b));
132
}
133
134
#endif