aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_align.h
blob: 47e9f199fe076ea39cba8faccc2276be9dc8cc38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
  This file is part of libmicrohttpd
  Copyright (C) 2021 Karlson2k (Evgeny Grin)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.
  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file microhttpd/mhd_align.h
 * @brief  types alignment macros
 * @author Karlson2k (Evgeny Grin)
 */

#ifndef MHD_ALIGN_H
#define MHD_ALIGN_H 1

#include "mhd_options.h"
#include <stdint.h>
#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif

#ifdef HAVE_C_ALIGNOF
#ifdef HAVE_STDALIGN_H
#include <stdalign.h>
#endif /* HAVE_STDALIGN_H */
#define _MHD_ALIGNOF(type) alignof(type)
#endif /* HAVE_C_ALIGNOF */

#ifndef _MHD_ALIGNOF
#if defined(_MSC_VER) && ! defined(__clang__) && _MSC_VER >= 1700
#define _MHD_ALIGNOF(type) __alignof(type)
#endif /* _MSC_VER >= 1900 */
#endif /* !_MHD_ALIGNOF */

#ifdef _MHD_ALIGNOF
#if (defined (__GNUC__) && __GNUC__ < 4 && __GNUC_MINOR__ < 9 && \
  ! defined(__clang__)) || \
  (defined (__clang__) && __clang_major__ < 8) || \
  (defined (__clang__) && __clang_major__ < 11 && \
  defined(__apple_build_version__))
/* GCC before 4.9 and clang before 8.0 have incorrect implementation of 'alignof()'
   which returns preferred alignment instead of minimal required alignment */
#define _MHD_ALIGNOF_UNRELIABLE 1
#endif

#if defined(_MSC_VER) && ! defined(__clang__) && _MSC_VER < 1900
/* MSVC has the same problem as old GCC versions:
   '__alignof()' may return "preferred" alignment instead of "required". */
#define _MHD_ALIGNOF_UNRELIABLE 1
#endif /* _MSC_VER < 1900 */
#endif /* _MHD_ALIGNOF */


#ifdef offsetof
#define _MHD_OFFSETOF(strct, membr) offsetof(strct, membr)
#else  /* ! offsetof */
#define _MHD_OFFSETOF(strct, membr) (size_t)(((char*)&(((strct*)0)->membr)) - \
                                     ((char*)((strct*)0)))
#endif /* ! offsetof */

/* Provide a limited set of alignment macros */
/* The set could be extended as needed */
#if defined(_MHD_ALIGNOF) && ! defined(_MHD_ALIGNOF_UNRELIABLE)
#define _MHD_UINT32_ALIGN _MHD_ALIGNOF(uint32_t)
#define _MHD_UINT64_ALIGN _MHD_ALIGNOF(uint64_t)
#else  /* ! _MHD_ALIGNOF */
struct _mhd_dummy_uint32_offset_test
{
  char dummy;
  uint32_t ui32;
};
#define _MHD_UINT32_ALIGN \
  _MHD_OFFSETOF(struct _mhd_dummy_uint32_offset_test, ui32)

struct _mhd_dummy_uint64_offset_test
{
  char dummy;
  uint64_t ui64;
};
#define _MHD_UINT64_ALIGN \
  _MHD_OFFSETOF(struct _mhd_dummy_uint64_offset_test, ui64)
#endif /* ! _MHD_ALIGNOF */

#endif /* ! MHD_ALIGN_H */