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