libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

mhd_align.h (3013B)


      1 /*
      2   This file is part of libmicrohttpd
      3   Copyright (C) 2021-2022 Karlson2k (Evgeny Grin)
      4 
      5   This library is free software; you can redistribute it and/or
      6   modify it under the terms of the GNU Lesser General Public
      7   License as published by the Free Software Foundation; either
      8   version 2.1 of the License, or (at your option) any later version.
      9 
     10   This library is distributed in the hope that it will be useful,
     11   but WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13   Lesser General Public License for more details.
     14 
     15   You should have received a copy of the GNU Lesser General Public
     16   License along with this library.
     17   If not, see <http://www.gnu.org/licenses/>.
     18 */
     19 
     20 /**
     21  * @file microhttpd/mhd_align.h
     22  * @brief  types alignment macros
     23  * @author Karlson2k (Evgeny Grin)
     24  */
     25 
     26 #ifndef MHD_ALIGN_H
     27 #define MHD_ALIGN_H 1
     28 
     29 #include "mhd_options.h"
     30 #include <stdint.h>
     31 #ifdef HAVE_STDDEF_H
     32 #include <stddef.h>
     33 #endif
     34 
     35 #ifdef HAVE_C_ALIGNOF
     36 #ifdef HAVE_STDALIGN_H
     37 #include <stdalign.h>
     38 #endif /* HAVE_STDALIGN_H */
     39 #define _MHD_ALIGNOF(type) alignof(type)
     40 #endif /* HAVE_C_ALIGNOF */
     41 
     42 #ifndef _MHD_ALIGNOF
     43 #if defined(_MSC_VER) && ! defined(__clang__) && _MSC_VER >= 1700
     44 #define _MHD_ALIGNOF(type) __alignof(type)
     45 #endif /* _MSC_VER >= 1700 */
     46 #endif /* !_MHD_ALIGNOF */
     47 
     48 #ifdef _MHD_ALIGNOF
     49 #if (defined(__GNUC__) && __GNUC__ < 4 && __GNUC_MINOR__ < 9 && \
     50   ! defined(__clang__)) || \
     51   (defined(__clang__) && __clang_major__ < 8) || \
     52   (defined(__clang__) && __clang_major__ < 11 && \
     53   defined(__apple_build_version__))
     54 /* GCC before 4.9 and clang before 8.0 have incorrect implementation of 'alignof()'
     55    which returns preferred alignment instead of minimal required alignment */
     56 #define _MHD_ALIGNOF_UNRELIABLE 1
     57 #endif
     58 
     59 #if defined(_MSC_VER) && ! defined(__clang__) && _MSC_VER < 1900
     60 /* MSVC has the same problem as old GCC versions:
     61    '__alignof()' may return "preferred" alignment instead of "required". */
     62 #define _MHD_ALIGNOF_UNRELIABLE 1
     63 #endif /* _MSC_VER < 1900 */
     64 #endif /* _MHD_ALIGNOF */
     65 
     66 
     67 #ifdef offsetof
     68 #define _MHD_OFFSETOF(strct, membr) offsetof(strct, membr)
     69 #else  /* ! offsetof */
     70 #define _MHD_OFFSETOF(strct, membr) (size_t)(((char*)&(((strct*)0)->membr)) - \
     71                                      ((char*)((strct*)0)))
     72 #endif /* ! offsetof */
     73 
     74 /* Provide a limited set of alignment macros */
     75 /* The set could be extended as needed */
     76 #if defined(_MHD_ALIGNOF) && ! defined(_MHD_ALIGNOF_UNRELIABLE)
     77 #define _MHD_UINT32_ALIGN _MHD_ALIGNOF(uint32_t)
     78 #define _MHD_UINT64_ALIGN _MHD_ALIGNOF(uint64_t)
     79 #else  /* ! _MHD_ALIGNOF */
     80 struct _mhd_dummy_uint32_offset_test
     81 {
     82   char dummy;
     83   uint32_t ui32;
     84 };
     85 #define _MHD_UINT32_ALIGN \
     86   _MHD_OFFSETOF(struct _mhd_dummy_uint32_offset_test, ui32)
     87 
     88 struct _mhd_dummy_uint64_offset_test
     89 {
     90   char dummy;
     91   uint64_t ui64;
     92 };
     93 #define _MHD_UINT64_ALIGN \
     94   _MHD_OFFSETOF(struct _mhd_dummy_uint64_offset_test, ui64)
     95 #endif /* ! _MHD_ALIGNOF */
     96 
     97 #endif /* ! MHD_ALIGN_H */