Ruby: include/ruby/internal/stdalign.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

stdalign.h
Go to the documentation of this file.
1#ifndef RBIMPL_STDALIGN_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_STDALIGN_H
23#include "ruby/internal/config.h"
24
25#ifdef STDC_HEADERS
26# include <stddef.h>
27#endif
28
29#include "ruby/internal/compiler_is.h"
30#include "ruby/internal/has/attribute.h"
31#include "ruby/internal/has/declspec_attribute.h"
32#include "ruby/internal/has/feature.h"
33
53#if defined(__cplusplus) && RBIMPL_HAS_FEATURE(cxx_alignas)
54# define RBIMPL_ALIGNAS alignas
55
56#elif defined(__cplusplus) && (__cplusplus >= 201103L)
57# define RBIMPL_ALIGNAS alignas
58
59#elif defined(__INTEL_CXX11_MODE__)
60# define RBIMPL_ALIGNAS alignas
61
62#elif defined(__GXX_EXPERIMENTAL_CXX0X__)
63# define RBIMPL_ALIGNAS alignas
64
65#elif RBIMPL_HAS_DECLSPEC_ATTRIBUTE(align)
66# define RBIMPL_ALIGNAS(_) __declspec(align(_))
67
68#elif RBIMPL_HAS_ATTRIBUTE(aligned)
69# define RBIMPL_ALIGNAS(_) __attribute__((__aligned__(_)))
70
71#else
72# define RBIMPL_ALIGNAS(_) /* void */
73#endif
74
86#if defined(__DOXYGEN__)
87# define RBIMPL_ALIGNOF alignof
88#elif defined(__cplusplus)
89# /* C++11 `alignof()` can be buggy. */
90# /* see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69560 */
91# /* But don't worry, we can use templates. */
92# define RBIMPL_ALIGNOF(T) (static_cast<size_t>(ruby::rbimpl_alignof<T>::value))
93
94namespace ruby {
95template<typename T>
96struct rbimpl_alignof {
97 typedef struct {
98 char _;
99 T t;
100 } type;
101
102 enum {
103 value = offsetof(type, t)
104 };
105};
106}
107
108#elif RBIMPL_COMPILER_IS(MSVC)
109# /* Windows have no alignment glitch.*/
110# define RBIMPL_ALIGNOF __alignof
111
112#elif defined(HAVE__ALIGNOF)
113# /* Autoconf detected availability of a sane `_Alignof()`. */
114# define RBIMPL_ALIGNOF(T) RB_GNUC_EXTENSION(_Alignof(T))
115
116#else
117# /* :BEWARE: This is the last resort. If your compiler somehow supports
118# * querying the alignment of a type, you definitely should use that instead.
119# * There are 2 known pitfalls for this fallback implementation:
120# *
121# * First, it is either an undefined behaviour (C) or an explicit error (C++)
122# * to define a struct inside of `offsetof`. C compilers tend to accept such
123# * things, but AFAIK C++ has no room to allow.
124# *
125# * Second, there exist T such that `struct { char _; T t; }` is invalid. A
126# * known example is when T is a struct with a flexible array member. Such
127# * struct cannot be enclosed into another one.
128# */
129# /* see: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2083.htm */
130# /* see: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm */
131# define RBIMPL_ALIGNOF(T) offsetof(struct { char _; T t; }, t)
132
133#endif
134
135#endif /* RBIMPL_STDALIGN_H */
attribute.h
Defines RBIMPL_HAS_ATTRIBUTE.
compiler_is.h
Defines RBIMPL_COMPILER_IS.
declspec_attribute.h
Defines RBIMPL_HAS_DECLSPEC_ATTRIBUTE.
feature.h
Defines RBIMPL_HAS_FEATURE.
ruby
The main namespace.
Definition cxxanyargs.hpp:37
_
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35