1 /************************************************
2
3 defines.h -
4
5 $Author: ko1 $
6 created at: Wed May 18 00:21:44 JST 1994
7
8 ************************************************/
9
10 #ifndef RUBY_DEFINES_H
11 #define RUBY_DEFINES_H 1
12
13 #if defined(__cplusplus)
14 extern "C" {
15 #if 0
16 } /* satisfy cc-mode */
17 #endif
18 #endif
19
20 #include "ruby/config.h"
21 #ifdef RUBY_EXTCONF_H
22 #include RUBY_EXTCONF_H
23 #endif
24
25 /* function attributes */
26 #ifndef CONSTFUNC
27 # define CONSTFUNC(x) x
28 #endif
29 #ifndef PUREFUNC
30 # define PUREFUNC(x) x
31 #endif
32 #ifndef DEPRECATED
33 # define DEPRECATED(x) x
34 #endif
35 #ifndef DEPRECATED_BY
36 # define DEPRECATED_BY(n,x) DEPRECATED(x)
37 #endif
38 #ifndef DEPRECATED_TYPE
39 # define DEPRECATED_TYPE(mesg, decl) decl
40 #endif
41 #ifndef NOINLINE
42 # define NOINLINE(x) x
43 #endif
44 #ifndef ALWAYS_INLINE
45 # define ALWAYS_INLINE(x) x
46 #endif
47 #ifndef ERRORFUNC
48 # define HAVE_ATTRIBUTE_ERRORFUNC 0
49 # define ERRORFUNC(mesg, x) x
50 #else
51 # define HAVE_ATTRIBUTE_ERRORFUNC 1
52 #endif
53 #ifndef WARNINGFUNC
54 # define HAVE_ATTRIBUTE_WARNINGFUNC 0
55 # define WARNINGFUNC(mesg, x) x
56 #else
57 # define HAVE_ATTRIBUTE_WARNINGFUNC 1
58 #endif
59
60 #ifndef GCC_VERSION_SINCE
61 # if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
62 # define GCC_VERSION_SINCE(major, minor, patchlevel) \
63 ((__GNUC__ > (major)) || \
64 ((__GNUC__ == (major) && \
65 ((__GNUC_MINOR__ > (minor)) || \
66 (__GNUC_MINOR__ == (minor) && __GNUC_PATCHLEVEL__ >= (patchlevel))))))
67 # else
68 # define GCC_VERSION_SINCE(major, minor, patchlevel) 0
69 # endif
70 #endif
71 #ifndef GCC_VERSION_BEFORE
72 # if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
73 # define GCC_VERSION_BEFORE(major, minor, patchlevel) \
74 ((__GNUC__ < (major)) || \
75 ((__GNUC__ == (major) && \
76 ((__GNUC_MINOR__ < (minor)) || \
77 (__GNUC_MINOR__ == (minor) && __GNUC_PATCHLEVEL__ <= (patchlevel))))))
78 # else
79 # define GCC_VERSION_BEFORE(major, minor, patchlevel) 0
80 # endif
81 #endif
82
83 /* likely */
84 #if __GNUC__ >= 3
85 #define RB_LIKELY(x) (__builtin_expect(!!(x), 1))
86 #define RB_UNLIKELY(x) (__builtin_expect(!!(x), 0))
87 #else /* __GNUC__ >= 3 */
88 #define RB_LIKELY(x) (x)
89 #define RB_UNLIKELY(x) (x)
90 #endif /* __GNUC__ >= 3 */
91
92 /*
93 cold attribute for code layout improvements
94 RUBY_FUNC_ATTRIBUTE not used because MSVC does not like nested func macros
95 */
96 #if defined(__clang__) || GCC_VERSION_SINCE(4, 3, 0)
97 #define COLDFUNC __attribute__((cold))
98 #else
99 #define COLDFUNC
100 #endif
101
102 #ifdef __GNUC__
103 #if defined __MINGW_PRINTF_FORMAT
104 #define PRINTF_ARGS(decl, string_index, first_to_check) \
105 decl __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, first_to_check)))
106 #else
107 #define PRINTF_ARGS(decl, string_index, first_to_check) \
108 decl __attribute__((format(printf, string_index, first_to_check)))
109 #endif
110 #else
111 #define PRINTF_ARGS(decl, string_index, first_to_check) decl
112 #endif
113
114 #ifdef __GNUC__
115 #define RB_GNUC_EXTENSION __extension__
116 #define RB_GNUC_EXTENSION_BLOCK(x) __extension__ ({ x; })
117 #else
118 #define RB_GNUC_EXTENSION
119 #define RB_GNUC_EXTENSION_BLOCK(x) (x)
120 #endif
121
122 /* AC_INCLUDES_DEFAULT */
123 #include <stdio.h>
124 #ifdef HAVE_SYS_TYPES_H
125 # include <sys/types.h>
126 #endif
127 #ifdef HAVE_SYS_STAT_H
128 # include <sys/stat.h>
129 #endif
130 #ifdef STDC_HEADERS
131 # include <stdlib.h>
132 # include <stddef.h>
133 #else
134 # ifdef HAVE_STDLIB_H
135 # include <stdlib.h>
136 # endif
137 #endif
138 #ifdef HAVE_STRING_H
139 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
140 # include <memory.h>
141 # endif
142 # include <string.h>
143 #endif
144 #ifdef HAVE_STRINGS_H
145 # include <strings.h>
146 #endif
147 #ifdef HAVE_INTTYPES_H
148 # include <inttypes.h>
149 #endif
150 #ifdef HAVE_STDINT_H
151 # include <stdint.h>
152 #endif
153 #ifdef HAVE_STDALIGN_H
154 # include <stdalign.h>
155 #endif
156 #ifdef HAVE_UNISTD_H
157 # include <unistd.h>
158 #endif
159
160 #ifdef HAVE_SYS_SELECT_H
161 # include <sys/select.h>
162 #endif
163
164 #ifdef RUBY_USE_SETJMPEX
165 #include <setjmpex.h>
166 #endif
167
168 #include "ruby/missing.h"
169
170 #define RUBY
171
172 #ifdef __cplusplus
173 # ifndef HAVE_PROTOTYPES
174 # define HAVE_PROTOTYPES 1
175 # endif
176 # ifndef HAVE_STDARG_PROTOTYPES
177 # define HAVE_STDARG_PROTOTYPES 1
178 # endif
179 #endif
180
181 #undef _
182 #ifdef HAVE_PROTOTYPES
183 # define _(args) args
184 #else
185 # define _(args) ()
186 #endif
187
188 #undef __
189 #ifdef HAVE_STDARG_PROTOTYPES
190 # define __(args) args
191 #else
192 # define __(args) ()
193 #endif
194
195 #ifdef __cplusplus
196 #define ANYARGS ...
197 #else
198 #define ANYARGS
199 #endif
200
201 #ifndef RUBY_SYMBOL_EXPORT_BEGIN
202 # define RUBY_SYMBOL_EXPORT_BEGIN /* begin */
203 # define RUBY_SYMBOL_EXPORT_END /* end */
204 #endif
205
206 RUBY_SYMBOL_EXPORT_BEGIN
207
208 #define xmalloc ruby_xmalloc
209 #define xmalloc2 ruby_xmalloc2
210 #define xcalloc ruby_xcalloc
211 #define xrealloc ruby_xrealloc
212 #define xrealloc2 ruby_xrealloc2
213 #define xfree ruby_xfree
214
215 #if GCC_VERSION_SINCE(4,3,0)
216 # define RUBY_ATTR_ALLOC_SIZE(params) __attribute__ ((alloc_size params))
217 #else
218 # define RUBY_ATTR_ALLOC_SIZE(params)
219 #endif
220
221 void *ruby_xmalloc(size_t) RUBY_ATTR_ALLOC_SIZE((1));
222 void *ruby_xmalloc2(size_t,size_t) RUBY_ATTR_ALLOC_SIZE((1,2));
223 void *ruby_xcalloc(size_t,size_t) RUBY_ATTR_ALLOC_SIZE((1,2));
224 void *ruby_xrealloc(void*,size_t) RUBY_ATTR_ALLOC_SIZE((2));
225 void *ruby_xrealloc2(void*,size_t,size_t) RUBY_ATTR_ALLOC_SIZE((2,3));
226 void ruby_xfree(void*);
227
228 #ifndef USE_GC_MALLOC_OBJ_INFO_DETAILS
229 #define USE_GC_MALLOC_OBJ_INFO_DETAILS 0
230 #endif
231
232 #if USE_GC_MALLOC_OBJ_INFO_DETAILS
233
234 void *ruby_xmalloc_body(size_t) RUBY_ATTR_ALLOC_SIZE((1));
235 void *ruby_xmalloc2_body(size_t,size_t) RUBY_ATTR_ALLOC_SIZE((1,2));
236 void *ruby_xcalloc_body(size_t,size_t) RUBY_ATTR_ALLOC_SIZE((1,2));
237 void *ruby_xrealloc_body(void*,size_t) RUBY_ATTR_ALLOC_SIZE((2));
238 void *ruby_xrealloc2_body(void*,size_t,size_t) RUBY_ATTR_ALLOC_SIZE((2,3));
239
240 #define ruby_xmalloc(s1) ruby_xmalloc_with_location(s1, __FILE__, __LINE__)
241 #define ruby_xmalloc2(s1, s2) ruby_xmalloc2_with_location(s1, s2, __FILE__, __LINE__)
242 #define ruby_xcalloc(s1, s2) ruby_xcalloc_with_location(s1, s2, __FILE__, __LINE__)
243 #define ruby_xrealloc(ptr, s1) ruby_xrealloc_with_location(ptr, s1, __FILE__, __LINE__)
244 #define ruby_xrealloc2(ptr, s1, s2) ruby_xrealloc2_with_location(ptr, s1, s2, __FILE__, __LINE__)
245
246 extern const char *ruby_malloc_info_file;
247 extern int ruby_malloc_info_line;
248
249 static inline void *
ruby_xmalloc_with_location(size_t s,const char * file,int line)250 ruby_xmalloc_with_location(size_t s, const char *file, int line)
251 {
252 void *ptr;
253 ruby_malloc_info_file = file;
254 ruby_malloc_info_line = line;
255 ptr = ruby_xmalloc_body(s);
256 ruby_malloc_info_file = NULL;
257 return ptr;
258 }
259
260 static inline void *
ruby_xmalloc2_with_location(size_t s1,size_t s2,const char * file,int line)261 ruby_xmalloc2_with_location(size_t s1, size_t s2, const char *file, int line)
262 {
263 void *ptr;
264 ruby_malloc_info_file = file;
265 ruby_malloc_info_line = line;
266 ptr = ruby_xmalloc2_body(s1, s2);
267 ruby_malloc_info_file = NULL;
268 return ptr;
269 }
270
271 static inline void *
ruby_xcalloc_with_location(size_t s1,size_t s2,const char * file,int line)272 ruby_xcalloc_with_location(size_t s1, size_t s2, const char *file, int line)
273 {
274 void *ptr;
275 ruby_malloc_info_file = file;
276 ruby_malloc_info_line = line;
277 ptr = ruby_xcalloc_body(s1, s2);
278 ruby_malloc_info_file = NULL;
279 return ptr;
280 }
281
282 static inline void *
ruby_xrealloc_with_location(void * ptr,size_t s,const char * file,int line)283 ruby_xrealloc_with_location(void *ptr, size_t s, const char *file, int line)
284 {
285 void *rptr;
286 ruby_malloc_info_file = file;
287 ruby_malloc_info_line = line;
288 rptr = ruby_xrealloc_body(ptr, s);
289 ruby_malloc_info_file = NULL;
290 return rptr;
291 }
292
293 static inline void *
ruby_xrealloc2_with_location(void * ptr,size_t s1,size_t s2,const char * file,int line)294 ruby_xrealloc2_with_location(void *ptr, size_t s1, size_t s2, const char *file, int line)
295 {
296 void *rptr;
297 ruby_malloc_info_file = file;
298 ruby_malloc_info_line = line;
299 rptr = ruby_xrealloc2_body(ptr, s1, s2);
300 ruby_malloc_info_file = NULL;
301 return rptr;
302 }
303 #endif
304
305 #define STRINGIZE(expr) STRINGIZE0(expr)
306 #ifndef STRINGIZE0
307 #define STRINGIZE0(expr) #expr
308 #endif
309
310 #ifdef HAVE_LONG_LONG
311 # define HAVE_TRUE_LONG_LONG 1
312 #endif
313
314 #if SIZEOF_LONG_LONG > 0
315 # define LONG_LONG long long
316 #elif SIZEOF___INT64 > 0
317 # define HAVE_LONG_LONG 1
318 # define LONG_LONG __int64
319 # undef SIZEOF_LONG_LONG
320 # define SIZEOF_LONG_LONG SIZEOF___INT64
321 #endif
322
323 #ifdef __CYGWIN__
324 #undef _WIN32
325 #endif
326
327 #if defined(_WIN32)
328 /*
329 DOSISH mean MS-Windows style filesystem.
330 But you should use more precise macros like DOSISH_DRIVE_LETTER, PATH_SEP,
331 ENV_IGNORECASE or CASEFOLD_FILESYSTEM.
332 */
333 #define DOSISH 1
334 # define DOSISH_DRIVE_LETTER
335 #endif
336
337 #ifdef AC_APPLE_UNIVERSAL_BUILD
338 #undef WORDS_BIGENDIAN
339 #ifdef __BIG_ENDIAN__
340 #define WORDS_BIGENDIAN
341 #endif
342 #endif
343
344 #ifdef _WIN32
345 #include "ruby/win32.h"
346 #endif
347
348 #ifdef RUBY_EXPORT
349 #undef RUBY_EXTERN
350
351 #ifndef FALSE
352 # define FALSE 0
353 #elif FALSE
354 # error FALSE must be false
355 #endif
356 #ifndef TRUE
357 # define TRUE 1
358 #elif !TRUE
359 # error TRUE must be true
360 #endif
361
362 #endif
363
364 #ifndef RUBY_FUNC_EXPORTED
365 #define RUBY_FUNC_EXPORTED
366 #endif
367
368 /* These macros are used for functions which are exported only for MJIT
369 and NOT ensured to be exported in future versions. */
370 #define MJIT_FUNC_EXPORTED RUBY_FUNC_EXPORTED
371 #define MJIT_SYMBOL_EXPORT_BEGIN RUBY_SYMBOL_EXPORT_BEGIN
372 #define MJIT_SYMBOL_EXPORT_END RUBY_SYMBOL_EXPORT_END
373
374 #if defined(MJIT_HEADER) && defined(_MSC_VER)
375 # undef MJIT_FUNC_EXPORTED
376 # define MJIT_FUNC_EXPORTED static
377 #endif
378
379 #ifndef RUBY_EXTERN
380 #define RUBY_EXTERN extern
381 #endif
382
383 #ifndef EXTERN
384 # if defined __GNUC__
385 # define EXTERN _Pragma("message \"EXTERN is deprecated, use RUBY_EXTERN instead\""); \
386 RUBY_EXTERN
387 # elif defined _MSC_VER
388 # define EXTERN __pragma(message(__FILE__"("STRINGIZE(__LINE__)"): warning: "\
389 "EXTERN is deprecated, use RUBY_EXTERN instead")); \
390 RUBY_EXTERN
391 # else
392 # define EXTERN <-<-"EXTERN is deprecated, use RUBY_EXTERN instead"->->
393 # endif
394 #endif
395
396 #ifndef RUBY_MBCHAR_MAXSIZE
397 #define RUBY_MBCHAR_MAXSIZE INT_MAX
398 /* MB_CUR_MAX will not work well in C locale */
399 #endif
400
401 #if defined(__sparc)
402 void rb_sparc_flush_register_windows(void);
403 # define FLUSH_REGISTER_WINDOWS rb_sparc_flush_register_windows()
404 #elif defined(__ia64)
405 void *rb_ia64_bsp(void);
406 void rb_ia64_flushrs(void);
407 # define FLUSH_REGISTER_WINDOWS rb_ia64_flushrs()
408 #else
409 # define FLUSH_REGISTER_WINDOWS ((void)0)
410 #endif
411
412 #if defined(DOSISH)
413 #define PATH_SEP ";"
414 #else
415 #define PATH_SEP ":"
416 #endif
417 #define PATH_SEP_CHAR PATH_SEP[0]
418
419 #define PATH_ENV "PATH"
420
421 #if defined(DOSISH)
422 #define ENV_IGNORECASE
423 #endif
424
425 #ifndef CASEFOLD_FILESYSTEM
426 # if defined DOSISH
427 # define CASEFOLD_FILESYSTEM 1
428 # else
429 # define CASEFOLD_FILESYSTEM 0
430 # endif
431 #endif
432
433 #ifndef DLEXT_MAXLEN
434 #define DLEXT_MAXLEN 4
435 #endif
436
437 #ifndef RUBY_PLATFORM
438 #define RUBY_PLATFORM "unknown-unknown"
439 #endif
440
441 #ifndef FUNC_MINIMIZED
442 #define FUNC_MINIMIZED(x) x
443 #endif
444 #ifndef FUNC_UNOPTIMIZED
445 #define FUNC_UNOPTIMIZED(x) x
446 #endif
447 #ifndef RUBY_ALIAS_FUNCTION_TYPE
448 #define RUBY_ALIAS_FUNCTION_TYPE(type, prot, name, args) \
449 FUNC_MINIMIZED(type prot) {return (type)name args;}
450 #endif
451 #ifndef RUBY_ALIAS_FUNCTION_VOID
452 #define RUBY_ALIAS_FUNCTION_VOID(prot, name, args) \
453 FUNC_MINIMIZED(void prot) {name args;}
454 #endif
455 #ifndef RUBY_ALIAS_FUNCTION
456 #define RUBY_ALIAS_FUNCTION(prot, name, args) \
457 RUBY_ALIAS_FUNCTION_TYPE(VALUE, prot, name, args)
458 #endif
459
460 #ifndef UNALIGNED_WORD_ACCESS
461 # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
462 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
463 defined(__powerpc64__) || \
464 defined(__mc68020__)
465 # define UNALIGNED_WORD_ACCESS 1
466 # else
467 # define UNALIGNED_WORD_ACCESS 0
468 # endif
469 #endif
470 #ifndef PACKED_STRUCT
471 # define PACKED_STRUCT(x) x
472 #endif
473 #ifndef PACKED_STRUCT_UNALIGNED
474 # if UNALIGNED_WORD_ACCESS
475 # define PACKED_STRUCT_UNALIGNED(x) PACKED_STRUCT(x)
476 # else
477 # define PACKED_STRUCT_UNALIGNED(x) x
478 # endif
479 #endif
480
481 #ifndef RUBY_ALIGNAS
482 #define RUBY_ALIGNAS(x) /* x */
483 #endif
484
485 #ifdef RUBY_ALIGNOF
486 /* OK, take that definition */
487 #elif defined(__cplusplus) && (__cplusplus >= 201103L)
488 #define RUBY_ALIGNOF alignof
489 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
490 #define RUBY_ALIGNOF _Alignof
491 #else
492 #define RUBY_ALIGNOF(type) ((size_t)offsetof(struct { char f1; type f2; }, f2))
493 #endif
494
495 #define NORETURN_STYLE_NEW 1
496 #ifdef NORETURN
497 /* OK, take that definition */
498 #elif defined(__cplusplus) && (__cplusplus >= 201103L)
499 #define NORETURN(x) [[ noreturn ]] x
500 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
501 #define NORETURN(x) _Noreturn x
502 #else
503 #define NORETURN(x) x
504 #endif
505
506 RUBY_SYMBOL_EXPORT_END
507
508 #if defined(__cplusplus)
509 #if 0
510 { /* satisfy cc-mode */
511 #endif
512 } /* extern "C" { */
513 #endif
514
515 #endif /* RUBY_DEFINES_H */
516