Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)
sizes.c
1#include "ruby/ruby.h"
2
3#if defined(HAVE_TYPE_SIG_ATOMIC_T)
4# include <signal.h>
5#endif
6
7#if defined(HAVE_TYPE_WINT_T) || defined(HAVE_TYPE_WCTRANS_T) || defined(HAVE_TYPE_WCTYPE_T)
8# include <wctype.h>
9#endif
10
11
12/*
13 * Document-const: SIZEOF
14 *
15 * A Hash with the byte size of \C types available to the compiler
16 * used to build Ruby. To access this constant, first run
17 * <code>require 'rbconfig/sizeof'</code>.
18 *
19 * require 'rbconfig/sizeof'
20 * RUBY_PLATFORM # => "x64-mingw-ucrt"
21 * RbConfig::SIZEOF.fetch_values('long', 'void*') # => [4, 8]
22 *
23 */
24
25extern void Init_limits(void);
26void
27Init_sizeof(void)
28{
29 VALUE s = rb_hash_new();
30 VALUE mRbConfig = rb_define_module("RbConfig");
31 rb_define_const(mRbConfig, "SIZEOF", s);
32
33#define DEFINE(type, size) rb_hash_aset(s, rb_usascii_str_new_lit(#type), INT2FIX(SIZEOF_##size))
34#define DEFINE_SIZE(type) rb_hash_aset(s, rb_usascii_str_new_lit(#type), INT2FIX(sizeof(type)))
35
36#if defined(SIZEOF_INT) && SIZEOF_INT != 0
37 DEFINE(int, INT);
38#endif
39#if defined(SIZEOF_SHORT) && SIZEOF_SHORT != 0
40 DEFINE(short, SHORT);
41#endif
42#if defined(SIZEOF_LONG) && SIZEOF_LONG != 0
43 DEFINE(long, LONG);
44#endif
45#if defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG != 0 && defined(HAVE_TRUE_LONG_LONG)
46 DEFINE(long long, LONG_LONG);
47#endif
48#if defined(SIZEOF___INT64) && SIZEOF___INT64 != 0
49 DEFINE(__int64, __INT64);
50#endif
51#ifdef HAVE_TYPE___INT128
52 DEFINE_SIZE(__int128);
53#endif
54#if defined(SIZEOF_OFF_T) && SIZEOF_OFF_T != 0
55 DEFINE(off_t, OFF_T);
56#endif
57#if defined(SIZEOF_VOIDP) && SIZEOF_VOIDP != 0
58 DEFINE(void*, VOIDP);
59#endif
60#if defined(SIZEOF_FLOAT) && SIZEOF_FLOAT != 0
61 DEFINE(float, FLOAT);
62#endif
63#if defined(SIZEOF_DOUBLE) && SIZEOF_DOUBLE != 0
64 DEFINE(double, DOUBLE);
65#endif
66#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T != 0
67 DEFINE(time_t, TIME_T);
68#endif
69#if defined(SIZEOF_CLOCK_T) && SIZEOF_CLOCK_T != 0
70 DEFINE(clock_t, CLOCK_T);
71#endif
72#if defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T != 0
73 DEFINE(size_t, SIZE_T);
74#endif
75#if defined(SIZEOF_PTRDIFF_T) && SIZEOF_PTRDIFF_T != 0
76 DEFINE(ptrdiff_t, PTRDIFF_T);
77#endif
78#if defined(SIZEOF_DEV_T) && SIZEOF_DEV_T != 0
79 DEFINE(dev_t, DEV_T);
80#endif
81#if defined(SIZEOF_INT8_T) && SIZEOF_INT8_T != 0
82 DEFINE(int8_t, INT8_T);
83#endif
84#if defined(SIZEOF_UINT8_T) && SIZEOF_UINT8_T != 0
85 DEFINE(uint8_t, UINT8_T);
86#endif
87#if defined(SIZEOF_INT16_T) && SIZEOF_INT16_T != 0
88 DEFINE(int16_t, INT16_T);
89#endif
90#if defined(SIZEOF_UINT16_T) && SIZEOF_UINT16_T != 0
91 DEFINE(uint16_t, UINT16_T);
92#endif
93#if defined(SIZEOF_INT32_T) && SIZEOF_INT32_T != 0
94 DEFINE(int32_t, INT32_T);
95#endif
96#if defined(SIZEOF_UINT32_T) && SIZEOF_UINT32_T != 0
97 DEFINE(uint32_t, UINT32_T);
98#endif
99#if defined(SIZEOF_INT64_T) && SIZEOF_INT64_T != 0
100 DEFINE(int64_t, INT64_T);
101#endif
102#if defined(SIZEOF_UINT64_T) && SIZEOF_UINT64_T != 0
103 DEFINE(uint64_t, UINT64_T);
104#endif
105#if defined(SIZEOF_INT128_T) && SIZEOF_INT128_T != 0
106 DEFINE(int128_t, INT128_T);
107#endif
108#if defined(SIZEOF_UINT128_T) && SIZEOF_UINT128_T != 0
109 DEFINE(uint128_t, UINT128_T);
110#endif
111#if defined(SIZEOF_INTPTR_T) && SIZEOF_INTPTR_T != 0
112 DEFINE(intptr_t, INTPTR_T);
113#endif
114#if defined(SIZEOF_UINTPTR_T) && SIZEOF_UINTPTR_T != 0
115 DEFINE(uintptr_t, UINTPTR_T);
116#endif
117#if defined(SIZEOF_SSIZE_T) && SIZEOF_SSIZE_T != 0
118 DEFINE(ssize_t, SSIZE_T);
119#endif
120#ifdef HAVE_TYPE_INT_LEAST8_T
121 DEFINE_SIZE(int_least8_t);
122#endif
123#ifdef HAVE_TYPE_INT_LEAST16_T
124 DEFINE_SIZE(int_least16_t);
125#endif
126#ifdef HAVE_TYPE_INT_LEAST32_T
127 DEFINE_SIZE(int_least32_t);
128#endif
129#ifdef HAVE_TYPE_INT_LEAST64_T
130 DEFINE_SIZE(int_least64_t);
131#endif
132#ifdef HAVE_TYPE_INT_FAST8_T
133 DEFINE_SIZE(int_fast8_t);
134#endif
135#ifdef HAVE_TYPE_INT_FAST16_T
136 DEFINE_SIZE(int_fast16_t);
137#endif
138#ifdef HAVE_TYPE_INT_FAST32_T
139 DEFINE_SIZE(int_fast32_t);
140#endif
141#ifdef HAVE_TYPE_INT_FAST64_T
142 DEFINE_SIZE(int_fast64_t);
143#endif
144#ifdef HAVE_TYPE_INTMAX_T
145 DEFINE_SIZE(intmax_t);
146#endif
147#ifdef HAVE_TYPE_SIG_ATOMIC_T
148 DEFINE_SIZE(sig_atomic_t);
149#endif
150#ifdef HAVE_TYPE_WCHAR_T
151 DEFINE_SIZE(wchar_t);
152#endif
153#ifdef HAVE_TYPE_WINT_T
154 DEFINE_SIZE(wint_t);
155#endif
156#ifdef HAVE_TYPE_WCTRANS_T
157 DEFINE_SIZE(wctrans_t);
158#endif
159#ifdef HAVE_TYPE_WCTYPE_T
160 DEFINE_SIZE(wctype_t);
161#endif
162#ifdef HAVE_TYPE__BOOL
163 DEFINE_SIZE(_Bool);
164#endif
165#ifdef HAVE_TYPE_LONG_DOUBLE
166 DEFINE_SIZE(long double);
167#endif
168#ifdef HAVE_TYPE_FLOAT__COMPLEX
169 DEFINE_SIZE(float _Complex);
170#endif
171#ifdef HAVE_TYPE_DOUBLE__COMPLEX
172 DEFINE_SIZE(double _Complex);
173#endif
174#ifdef HAVE_TYPE_LONG_DOUBLE__COMPLEX
175 DEFINE_SIZE(long double _Complex);
176#endif
177#ifdef HAVE_TYPE_FLOAT__IMAGINARY
178 DEFINE_SIZE(float _Imaginary);
179#endif
180#ifdef HAVE_TYPE_DOUBLE__IMAGINARY
181 DEFINE_SIZE(double _Imaginary);
182#endif
183#ifdef HAVE_TYPE_LONG_DOUBLE__IMAGINARY
184 DEFINE_SIZE(long double _Imaginary);
185#endif
186#ifdef HAVE_TYPE___INT128
187 DEFINE_SIZE(__int128);
188#endif
189#ifdef HAVE_TYPE___FLOAT128
190 DEFINE_SIZE(__float128);
191#endif
192#ifdef HAVE_TYPE__DECIMAL32
193 DEFINE_SIZE(_Decimal32);
194#endif
195#ifdef HAVE_TYPE__DECIMAL64
196 DEFINE_SIZE(_Decimal64);
197#endif
198#ifdef HAVE_TYPE__DECIMAL128
199 DEFINE_SIZE(_Decimal128);
200#endif
201#ifdef HAVE_TYPE___M64
202 DEFINE_SIZE(__m64);
203#endif
204#ifdef HAVE_TYPE___M128
205 DEFINE_SIZE(__m128);
206#endif
207#ifdef HAVE_TYPE___FLOAT80
208 DEFINE_SIZE(__float80);
209#endif
210 OBJ_FREEZE(s);
211
212#undef DEFINE
213 Init_limits();
214}
#define LONG_LONG
Definition long_long.h:38
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1095
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:135
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
Definition variable.c:3729
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40