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

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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(__cplusplus)
87# /* C++11 `alignof()` can be buggy. */
88# /* see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69560 */
89# /* But don't worry, we can use templates. */
90# define RBIMPL_ALIGNOF(T) (static_cast<size_t>(ruby::rbimpl_alignof<T>::value))
91
92namespace ruby {
93template<typename T>
94 struct rbimpl_alignof {
95 typedef struct {
96 char _;
97 T t;
98 } type;
99
100 enum {
101 value = offsetof(type, t)
102 };
103};
104}
105
106#elif RBIMPL_COMPILER_IS(MSVC)
107# /* Windows have no alignment glitch.*/
108# define RBIMPL_ALIGNOF __alignof
109
110#elif defined(HAVE__ALIGNOF)
111# /* Autoconf detected availability of a sane `_Alignof()`. */
112# define RBIMPL_ALIGNOF(T) RB_GNUC_EXTENSION(_Alignof(T))
113
114#else
115# /* :BEWARE: This is the last resort. If your compiler somehow supports
116# * querying the alignment of a type, you definitely should use that instead.
117# * There are 2 known pitfalls for this fallback implementation:
118# *
119# * First, it is either an undefined behaviour (C) or an explicit error (C++)
120# * to define a struct inside of `offsetof`. C compilers tend to accept such
121# * things, but AFAIK C++ has no room to allow.
122# *
123# * Second, there exist T such that `struct { char _; T t; }` is invalid. A
124# * known example is when T is a struct with a flexible array member. Such
125# * struct cannot be enclosed into another one.
126# */
127# /* see: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2083.htm */
128# /* see: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm */
129# define RBIMPL_ALIGNOF(T) offsetof(struct { char _; T t; }, t)
130
131#endif
132
133#endif /* RBIMPL_STDALIGN_H */
offsetof
#define offsetof(p_type, field)
Definition: addrinfo.h:186
attribute.h
Defines RBIMPL_HAS_ATTRIBUTE.
T
unsigned char T
Definition: cls_uchar_va.c:10
compiler_is.h
Defines RBIMPL_COMPILER_IS.
declspec_attribute.h
Defines RBIMPL_HAS_DECLSPEC_ATTRIBUTE.
feature.h
Defines RBIMPL_HAS_FEATURE.
config.h
Thin wrapper to ruby/config.h.
ruby
The main namespace.
Definition: cxxanyargs.hpp:37
ruby::rbimpl_alignof::type
Definition: stdalign.h:95
ruby::rbimpl_alignof::type::_
char _
Definition: stdalign.h:96
ruby::rbimpl_alignof::type::t
T t
Definition: stdalign.h:97
ruby::rbimpl_alignof
Definition: stdalign.h:94
ruby::rbimpl_alignof::value
@ value
Definition: stdalign.h:101
t
#define t
Definition: symbol.c:253