mhd_align.h (6268B)
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) 2021-2022 Karlson2k (Evgeny Grin) 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_align.h 41 * @brief types alignment macros 42 * @author Karlson2k (Evgeny Grin) 43 */ 44 45 #ifndef MHD_ALIGN_H 46 #define MHD_ALIGN_H 1 47 48 #include "mhd_sys_options.h" 49 50 #include "sys_base_types.h" 51 52 #if (defined(__GNUC__) && __GNUC__ < 4 && __GNUC_MINOR__ < 9 && \ 53 !defined(__clang__)) || \ 54 (defined(__clang__) && __clang_major__ < 8) || \ 55 (defined(__clang__) && __clang_major__ < 11 && \ 56 defined(__apple_build_version__)) 57 /* GCC before 4.9 and clang before 8.0 have incorrect implementation of 'alignof()' 58 which returns preferred alignment instead of minimal required alignment */ 59 # define mhd_SYS_ALIGNOF_UNRELIABLE 1 60 #elif defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1900 61 /* MSVC has the same problem as old GCC versions: 62 '__alignof()' may return "preferred" alignment instead of "required". */ 63 # define mhd_SYS_ALIGNOF_UNRELIABLE 1 64 #endif /* _MSC_VER < 1900 */ 65 66 #ifndef mhd_ALIGNOF 67 # ifndef mhd_SYS_ALIGNOF_UNRELIABLE 68 # ifdef HAVE_C_ALIGNOF 69 # if !defined(__STDC_VERSION__) || (__STDC_VERSION__ + 0) < 202311 70 # ifdef HAVE_STDALIGN_H 71 # include <stdalign.h> 72 # endif /* HAVE_STDALIGN_H */ 73 # endif 74 # if defined(alignof) || defined(__alignof_is_defined) || \ 75 (defined(__STDC_VERSION__) && (__STDC_VERSION__ + 0) >= 202311) 76 # define mhd_ALIGNOF(TYPE) alignof(TYPE) 77 # endif 78 # endif /* HAVE_C_ALIGNOF */ 79 80 # ifndef mhd_ALIGNOF 81 # if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER >= 1700 82 # define mhd_ALIGNOF(TYPE) __alignof (TYPE) 83 # endif /* _MSC_VER >= 1700 */ 84 # endif /* !mhd_ALIGNOF */ 85 # endif 86 #endif 87 88 #if !defined(mhd_ALIGNOF) && defined(__cplusplus) 89 /* __alignof__ is preferred alignment, not minimal required alignment, 90 but offsetof() trick does not work in C++, so it is the best available option */ 91 # ifdef HAVE___ALIGNOF 92 # define mhd_ALIGNOF(TYPE) __alignof__(TYPE) 93 # endif 94 #endif /* !mhd_ALIGNOF && __cplusplus */ 95 96 #ifndef mhd_ALIGNOF 97 # include "sys_offsetof.h" 98 # define mhd_ALIGNOF(TYPE) \ 99 offsetof (struct { char mhd__s; TYPE mhd__m; }, mhd__m) 100 #endif 101 102 /* Provide a limited set of alignment macros */ 103 /* The set could be extended as needed */ 104 #define mhd_UINT32_ALIGN mhd_ALIGNOF (uint_least32_t) 105 #define mhd_UINT64_ALIGN mhd_ALIGNOF (uint_least64_t) 106 #define mhd_UINT_FAST32_ALIGN mhd_ALIGNOF (uint_fast32_t) 107 108 109 #if defined(HAVE_ATTR_ALIGNED) 110 # define mhd_ALIGNED(num) __attribute__((aligned (num))) 111 #elif defined(HAVE_DECLSPEC_ALIGN) 112 # define mhd_ALIGNED(num) __declspec(align (num)) 113 #endif 114 115 #ifdef HAVE_C_ALIGNAS 116 # if !defined(__STDC_VERSION__) || (__STDC_VERSION__ + 0) < 202311 117 # ifdef HAVE_STDALIGN_H 118 # include <stdalign.h> 119 # endif /* HAVE_STDALIGN_H */ 120 # endif /* before C23 */ 121 # if defined(alignas) || defined(__alignas_is_defined) || \ 122 (defined(__STDC_VERSION__) && (__STDC_VERSION__ + 0) >= 202311) 123 # define mhd_ALIGNED_AS(type) alignas (type) 124 # ifndef mhd_ALIGNED 125 # define mhd_ALIGNED(num) alignas (num) 126 # endif /* ! mhd_ALIGNED */ 127 # endif /* alignas || C23 or later */ 128 #endif /* ! HAVE_C_ALIGNAS */ 129 130 #ifndef mhd_ALIGNED 131 # define mhd_ALIGNED(num) /* Empty fallback */ 132 #endif 133 134 #ifndef mhd_ALIGNED_AS 135 # define mhd_ALIGNED_AS(type) mhd_ALIGNED (mhd_ALIGNOF (type)) 136 #endif 137 138 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 8 139 # define mhd_ALIGNED_8 mhd_ALIGNED (8) 140 #else 141 # define mhd_ALIGNED_8 mhd_ALIGNED_AS (void*) 142 #endif 143 144 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 16 145 # define mhd_ALIGNED_16 mhd_ALIGNED (16) 146 #else 147 # define mhd_ALIGNED_16 mhd_ALIGNED_8 148 #endif 149 150 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 32 151 # define mhd_ALIGNED_32 mhd_ALIGNED (32) 152 #else 153 # define mhd_ALIGNED_32 mhd_ALIGNED_16 154 #endif 155 156 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 64 157 # define mhd_ALIGNED_64 mhd_ALIGNED (64) 158 #else 159 # define mhd_ALIGNED_64 mhd_ALIGNED_32 160 #endif 161 162 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 128 163 # define mhd_ALIGNED_128 mhd_ALIGNED (128) 164 #else 165 # define mhd_ALIGNED_128 mhd_ALIGNED_64 166 #endif 167 168 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 256 169 # define mhd_ALIGNED_256 mhd_ALIGNED (256) 170 #else 171 # define mhd_ALIGNED_256 mhd_ALIGNED_128 172 #endif 173 174 #endif /* ! MHD_ALIGN_H */