1 /*- 2 * Copyright (c) 2006 Verdens Gang AS 3 * Copyright (c) 2012 Fastly Inc 4 * Copyright (c) 2006-2015 Varnish Software AS 5 * All rights reserved. 6 * 7 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk> 8 * Author: Rogier 'DocWilco' Mulhuijzen <rogier@fastly.com> 9 * 10 * Inspired by FreeBSD's <sys/cdefs.h> 11 * 12 * SPDX-License-Identifier: BSD-2-Clause 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * Names of the form "v_[a-z_]*_" is reserved for this file. 36 * 37 * This file should always be the first non <...> include in a .c file. 38 */ 39 40 #ifdef VDEF_H_INCLUDED 41 # error "vdef.h included multiple times" 42 #endif 43 #define VDEF_H_INCLUDED 44 45 /* Safe printf into a fixed-size buffer */ 46 #define bprintf(buf, fmt, ...) \ 47 do { \ 48 int ibprintf; \ 49 ibprintf = snprintf(buf, sizeof buf, fmt, __VA_ARGS__); \ 50 assert(ibprintf >= 0 && ibprintf < (int)sizeof buf); \ 51 } while (0) 52 53 /* Safe printf into a fixed-size buffer */ 54 #define vbprintf(buf, fmt, ap) \ 55 do { \ 56 int ivbprintf; \ 57 ivbprintf = vsnprintf(buf, sizeof buf, fmt, ap); \ 58 assert(ivbprintf >= 0 && ivbprintf < (int)sizeof buf); \ 59 } while (0) 60 61 /* Safe strcpy into a fixed-size buffer */ 62 #define bstrcpy(dst, src) \ 63 do { \ 64 assert(strlen(src) + 1 <= sizeof (dst)); \ 65 strcpy((dst), (src)); \ 66 } while (0) 67 68 // TODO #define strcpy BANNED 69 // TODO then revert 0fa4baead49f0a45f68d3db0b7743c5e4e93ad4d 70 // TODO and replace with flexelint exception 71 72 /* Close and discard filedescriptor */ 73 #define closefd(fdp) \ 74 do { \ 75 assert(*(fdp) >= 0); \ 76 AZ(close(*(fdp))); \ 77 *(fdp) = -1; \ 78 } while (0) 79 80 #ifndef __GNUC_PREREQ__ 81 # if defined __GNUC__ && defined __GNUC_MINOR__ 82 # define __GNUC_PREREQ__(maj, min) \ 83 (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min))) 84 # else 85 # define __GNUC_PREREQ__(maj, min) 0 86 # endif 87 #endif 88 89 #if __GNUC_PREREQ__(2, 95) || defined(__INTEL_COMPILER) 90 # define v_printflike_(f,a) __attribute__((format(printf, f, a))) 91 #else 92 # define v_printflike_(f,a) 93 #endif 94 95 #define v_noreturn_ __attribute__((__noreturn__)) 96 97 #ifdef __GNUC__ 98 # define v_deprecated_ __attribute__((deprecated)) 99 #else 100 # define v_deprecated_ 101 #endif 102 103 #if __GNUC_PREREQ__(4,4) // added 2008-07-23 104 # define v_dont_optimize __attribute__((optimize("O"))) 105 #else 106 # define v_dont_optimize 107 #endif 108 109 /********************************************************************* 110 * Pointer alignment magic 111 */ 112 113 #if defined(__sparc__) 114 /* NB: Overbroad test for 32bit userland on 64bit SPARC cpus. */ 115 # define PALGN (sizeof(double) - 1) /* size of alignment */ 116 #else 117 # define PALGN (sizeof(void *) - 1) /* size of alignment */ 118 #endif 119 #define PAOK(p) (((uintptr_t)(p) & PALGN) == 0) /* is aligned */ 120 #define PRNDDN(p) ((uintptr_t)(p) & ~PALGN) /* Round down */ 121 #define PRNDUP(p) (((uintptr_t)(p) + PALGN) & ~PALGN) /* Round up */ 122 123 /********************************************************************* 124 * To be used as little as possible to wash off const/volatile etc. 125 */ 126 #define TRUST_ME(ptr) ((void*)(uintptr_t)(ptr)) 127 128 /********************************************************************** 129 * Generic power-2 rounding macros 130 */ 131 132 #define PWR2(x) ((((x)-1UL)&(x))==0) /* Is a power of two */ 133 #define RDN2(x, y) ((x)&(~((uintptr_t)(y)-1UL))) /* PWR2(y) true */ 134 #define RUP2(x, y) (((x)+((y)-1))&(~((uintptr_t)(y)-1UL))) /* PWR2(y) true */ 135 136 /********************************************************************** 137 * FlexeLint and compiler shutuppery 138 */ 139 140 /* 141 * In OO-light situations, functions have to match their prototype 142 * even if that means not const'ing a const'able argument. 143 * The typedef should be specified as argument to the macro. 144 */ 145 #define v_matchproto_(xxx) /*lint --e{818} */ 146 147 /* 148 * State variables may change value before we have considered the 149 * previous value 150 */ 151 #define v_statevariable_(varname) varname /*lint -esym(838,varname) */ 152 153 #ifdef __SUNPRO_C 154 # define NEEDLESS(s) {} 155 #else 156 # define NEEDLESS(s) s 157 #endif 158 159 #if __GNUC_PREREQ__(2, 7) 160 # define v_unused_ __attribute__((__unused__)) 161 #else 162 # define v_unused_ 163 #endif 164 165 /* VTIM API overhaul WIP */ 166 typedef double vtim_mono; 167 typedef double vtim_real; 168 typedef double vtim_dur; 169