| | 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 _PTOK(call, var) \ |
| 93 |
|
do { \ |
| 94 |
|
int var = call; \ |
| 95 |
|
if (!var) \ |
| 96 |
|
break; \ |
| 97 |
|
errno = var; \ |
| 98 |
|
WRONG(#call " failed"); \ |
| 99 |
|
} while (0) |
| 100 |
|
|
| 101 |
|
#define PTOK(call) _PTOK(call, VUNIQ_NAME(_pterr)) |
| 102 |
|
|
| 103 |
|
#define INCOMPL() \ |
| 104 |
|
do { \ |
| 105 |
|
VAS_Fail(__func__, __FILE__, __LINE__, \ |
| 106 |
|
"", VAS_INCOMPLETE); \ |
| 107 |
|
} while (0) |
| 108 |
|
|
| 109 |
|
/* |
| 110 |
|
* Most of this nightmare is stolen from FreeBSD's <cdefs.h> |
| 111 |
|
*/ |
| 112 |
|
#ifndef __has_extension |
| 113 |
|
# define __has_extension(x) 0 |
| 114 |
|
#endif |
| 115 |
|
|
| 116 |
|
#if __STDC_VERSION__ - 0 >= 202311L |
| 117 |
|
# define v_static_assert static_assert |
| 118 |
|
#elif __STDC_VERSION__ - 0 >= 201112L |
| 119 |
|
# define v_static_assert _Static_assert |
| 120 |
|
#elif __has_extension(c_static_assert) |
| 121 |
|
# define v_static_assert _Static_assert |
| 122 |
|
#elif __GNUC_PREREQ__(4,6) && !defined(__cplusplus) |
| 123 |
|
# define v_static_assert _Static_assert |
| 124 |
|
#else |
| 125 |
|
# if defined(__COUNTER__) |
| 126 |
|
# define v_static_assert(x, y) __v_static_assert(x, __COUNTER__) |
| 127 |
|
# else |
| 128 |
|
# define v_static_assert(x, y) __v_static_assert(x, __LINE__) |
| 129 |
|
# endif |
| 130 |
|
# define __v_static_assert(x, y) ___v_static_assert(x, y) |
| 131 |
|
# define ___v_static_assert(x, y) \ |
| 132 |
|
typedef char __vassert_## y[(x) ? 1 : -1] v_unused_ |
| 133 |
|
#endif |
| 134 |
|
|
| 135 |
|
/* |
| 136 |
|
* A normal pointer difference is signed, but when we don't want a negative |
| 137 |
|
* value this little tool will make sure we don't get that. |
| 138 |
|
*/ |
| 139 |
|
|
| 140 |
|
static inline size_t |
| 141 |
1391722 |
pdiff(const void *b, const void *e) |
| 142 |
|
{ |
| 143 |
|
|
| 144 |
1391722 |
AN(b); |
| 145 |
1391722 |
AN(e); |
| 146 |
1391722 |
assert(b <= e); |
| 147 |
1391722 |
return ((size_t)((const char *)e - (const char *)b)); |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
#endif |