mhd_align.h (5944B)
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 #ifndef mhd_ALIGNOF 89 # include "sys_offsetof.h" 90 # define mhd_ALIGNOF(TYPE) \ 91 offsetof (struct { char mhd__s; TYPE mhd__m; }, mhd__m) 92 #endif 93 94 /* Provide a limited set of alignment macros */ 95 /* The set could be extended as needed */ 96 #define mhd_UINT32_ALIGN mhd_ALIGNOF (uint_least32_t) 97 #define mhd_UINT64_ALIGN mhd_ALIGNOF (uint_least32_t) 98 #define mhd_UINT_FAST32_ALIGN mhd_ALIGNOF (uint_fast32_t) 99 100 101 #if defined(HAVE_ATTR_ALIGNED) 102 # define mhd_ALIGNED(num) __attribute__((aligned (num))) 103 #elif defined(HAVE_DECLSPEC_ALIGN) 104 # define mhd_ALIGNED(num) __declspec(align (num))) 105 #endif 106 107 #ifdef HAVE_C_ALIGNAS 108 # if ! defined(__STDC_VERSION__) || (__STDC_VERSION__ + 0) < 202311 109 # ifdef HAVE_STDALIGN_H 110 # include <stdalign.h> 111 # endif /* HAVE_STDALIGN_H */ 112 # endif /* before C23 */ 113 # if defined(alignas) || defined(__alignas_is_defined) || \ 114 (defined(__STDC_VERSION__) && (__STDC_VERSION__ + 0) >= 202311) 115 # define mhd_ALIGNED_AS(type) alignas (type) 116 # ifndef mhd_ALIGNED 117 # define mhd_ALIGNED(num) alignas (num) 118 # endif /* ! mhd_ALIGNED */ 119 # endif /* alignas || C23 or later */ 120 #endif /* ! HAVE_C_ALIGNAS */ 121 122 #ifndef mhd_ALIGNED 123 # define mhd_ALIGNED(num) /* Empty fallback */ 124 #endif 125 126 #ifndef mhd_ALIGNED_AS 127 # define mhd_ALIGNED_AS(type) mhd_ALIGNED (mhd_ALIGNOF (type)) 128 #endif 129 130 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 8 131 # define mhd_ALIGNED_8 mhd_ALIGNED (8) 132 #else 133 # define mhd_ALIGNED_8 mhd_ALIGNED_AS (void*) 134 #endif 135 136 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 16 137 # define mhd_ALIGNED_16 mhd_ALIGNED (16) 138 #else 139 # define mhd_ALIGNED_16 mhd_ALIGNED_8 140 #endif 141 142 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 32 143 # define mhd_ALIGNED_32 mhd_ALIGNED (32) 144 #else 145 # define mhd_ALIGNED_32 mhd_ALIGNED_16 146 #endif 147 148 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 64 149 # define mhd_ALIGNED_64 mhd_ALIGNED (64) 150 #else 151 # define mhd_ALIGNED_64 mhd_ALIGNED_32 152 #endif 153 154 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 128 155 # define mhd_ALIGNED_128 mhd_ALIGNED (128) 156 #else 157 # define mhd_ALIGNED_128 mhd_ALIGNED_64 158 #endif 159 160 #if defined(MHD_ALIGNMENT_LARGE_NUM) && (MHD_ALIGNMENT_LARGE_NUM + 0) >= 256 161 # define mhd_ALIGNED_256 mhd_ALIGNED (256) 162 #else 163 # define mhd_ALIGNED_256 mhd_ALIGNED_128 164 #endif 165 166 #endif /* ! MHD_ALIGN_H */