mhd_limits.h (6684B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2015-2024 Evgeny Grin (Karlson2k) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file src/mhd2/mhd_limits.h 41 * @brief limits values definitions 42 * @author Karlson2k (Evgeny Grin) 43 * 44 * This file provides maximum types values as macros. Macros may not work 45 * in preprocessor expressions, while macros always work in compiler 46 * expressions. 47 * This file does not include <stdint.h> and other type-definitions files. 48 * For best portability, make sure that this file is included after required 49 * type-definitions files. 50 */ 51 52 #ifndef MHD_LIMITS_H 53 #define MHD_LIMITS_H 54 55 #include "mhd_sys_options.h" 56 57 #ifdef HAVE_LIMITS_H 58 # include <limits.h> 59 #endif /* HAVE_LIMITS_H */ 60 61 #define mhd_UNSIGNED_TYPE_MAX(type) ((type) (~((type) 0))) 62 63 /* Assume 8 bits per byte, no padding bits. */ 64 #define mhd_SIGNED_TYPE_MAX(type) \ 65 ( (type) ((( ((type) 1) << (sizeof(type) * 8 - 2)) - 1) * 2 + 1) ) 66 67 /* The maximum value for signed type, based on knowledge of unsigned counterpart 68 type */ 69 #define mhd_SIGNED_TYPE_MAX2(type, utype) \ 70 ((type) (((utype) (~((utype) 0))) >> 1)) 71 72 #define mhd_IS_TYPE_SIGNED(type) (((type) 0) > ((type) - 1)) 73 74 #ifndef mhd_USE_PREDEF_LIMITS 75 # if defined(__GNUC__) || defined(__clang__) 76 # define mhd_USE_PREDEF_LIMITS 77 # endif 78 #endif 79 80 #ifndef INT_MAX 81 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__INT_MAX__) 82 # define INT_MAX __INT_MAX__ 83 # else /* ! __INT_MAX__ */ 84 # define INT_MAX mhd_SIGNED_TYPE_MAX2 (int, unsigned int) 85 # endif /* ! __INT_MAX__ */ 86 #endif /* ! INT_MAX */ 87 88 #ifndef UINT_MAX 89 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__UINT_MAX__) 90 # define UINT_MAX __UINT_MAX__ 91 # else /* ! __UINT_MAX__ */ 92 # define UINT_MAX mhd_UNSIGNED_TYPE_MAX (unsigned int) 93 # endif /* ! __UINT_MAX__ */ 94 #endif /* !UINT_MAX */ 95 96 #ifndef LONG_MAX 97 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__LONG_MAX__) 98 # define LONG_MAX __LONG_MAX__ 99 # else /* ! __LONG_MAX__ */ 100 # define LONG_MAX mhd_SIGNED_TYPE_MAX2 (long, unsigned long) 101 # endif /* ! __LONG_MAX__ */ 102 #endif /* !LONG_MAX */ 103 104 #ifndef ULONG_MAX 105 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__ULONG_MAX__) 106 # define ULONG_MAX __ULONG_MAX__ 107 # else /* ! __ULONG_MAX__ */ 108 # define ULONG_MAX mhd_UNSIGNED_TYPE_MAX (unsigned long) 109 # endif /* ! __ULONG_MAX__ */ 110 #endif /* !ULONG_MAX */ 111 112 #ifndef ULLONG_MAX 113 # ifdef ULONGLONG_MAX 114 # define ULLONG_MAX ULONGLONG_MAX 115 # else /* ! ULONGLONG_MAX */ 116 # define ULLONG_MAX mhd_UNSIGNED_TYPE_MAX (unsigned long long) 117 # endif /* ! ULONGLONG_MAX */ 118 #endif /* !ULLONG_MAX */ 119 120 #ifndef INT32_MAX 121 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__INT32_MAX__) 122 # define INT32_MAX __INT32_MAX__ 123 # else /* ! __INT32_MAX__ */ 124 # define INT32_MAX ((int32_t) 0x7FFFFFFF) 125 # endif /* ! __INT32_MAX__ */ 126 #endif /* !INT32_MAX */ 127 128 #ifndef UINT32_MAX 129 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__UINT32_MAX__) 130 # define UINT32_MAX __UINT32_MAX__ 131 # else /* ! __UINT32_MAX__ */ 132 # define UINT32_MAX ((uint32_t) 0xFFFFFFFFU) 133 # endif /* ! __UINT32_MAX__ */ 134 #endif /* !UINT32_MAX */ 135 136 #ifndef INT64_MAX 137 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__INT64_MAX__) 138 # define INT64_MAX __INT64_MAX__ 139 # else /* ! __INT64_MAX__ */ 140 # define INT64_MAX ((int64_t) 0x7FFFFFFFFFFFFFFF) 141 # endif /* ! __UINT64_MAX__ */ 142 #endif /* !INT64_MAX */ 143 144 #ifndef UINT64_MAX 145 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__UINT64_MAX__) 146 # define UINT64_MAX __UINT64_MAX__ 147 # else /* ! __UINT64_MAX__ */ 148 # define UINT64_MAX ((uint64_t) 0xFFFFFFFFFFFFFFFFU) 149 # endif /* ! __UINT64_MAX__ */ 150 #endif /* !UINT64_MAX */ 151 152 #ifndef SIZE_MAX 153 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__SIZE_MAX__) 154 # define SIZE_MAX __SIZE_MAX__ 155 # else /* ! __SIZE_MAX__ */ 156 # define SIZE_MAX mhd_UNSIGNED_TYPE_MAX (size_t) 157 # endif /* ! __SIZE_MAX__ */ 158 #endif /* !SIZE_MAX */ 159 160 #ifndef SSIZE_MAX 161 # if defined(mhd_USE_PREDEF_LIMITS) && defined(__SSIZE_MAX__) 162 # define SSIZE_MAX __SSIZE_MAX__ 163 # else 164 # define SSIZE_MAX mhd_SIGNED_TYPE_MAX2 (ssize_t, size_t) 165 # endif 166 #endif /* ! SSIZE_MAX */ 167 168 #ifndef OFF_T_MAX 169 # ifdef OFF_MAX 170 # define OFF_T_MAX OFF_MAX 171 # elif defined(OFFT_MAX) 172 # define OFF_T_MAX OFFT_MAX 173 # elif defined(__APPLE__) && defined(__MACH__) 174 # define OFF_T_MAX INT64_MAX 175 # else 176 # define OFF_T_MAX mhd_SIGNED_TYPE_MAX (off_t) 177 # endif 178 #endif /* !OFF_T_MAX */ 179 180 #if defined(_LARGEFILE64_SOURCE) && !defined(OFF64_T_MAX) 181 # define OFF64_T_MAX mhd_SIGNED_TYPE_MAX (off64_t) 182 #endif /* _LARGEFILE64_SOURCE && !OFF64_T_MAX */ 183 184 #ifndef TIME_T_MAX 185 # define TIME_T_MAX ((time_t) \ 186 (mhd_IS_TYPE_SIGNED (time_t) ? \ 187 mhd_SIGNED_TYPE_MAX (time_t) : \ 188 mhd_UNSIGNED_TYPE_MAX (time_t))) 189 #endif /* !TIME_T_MAX */ 190 191 #ifndef TIMEVAL_TV_SEC_MAX 192 # ifndef _WIN32 193 # define mhd_TIMEVAL_TV_SEC_MAX TIME_T_MAX 194 # else /* _WIN32 */ 195 # define mhd_TIMEVAL_TV_SEC_MAX LONG_MAX 196 # endif /* _WIN32 */ 197 #endif /* !TIMEVAL_TV_SEC_MAX */ 198 199 #endif /* MHD_LIMITS_H */