41#if defined(_MSC_VER) && defined(_WIN64)
43# pragma intrinsic(_umul128)
68#elif RBIMPL_HAS_BUILTIN(__builtin_alloca)
69# define alloca __builtin_alloca
72#elif defined(__cplusplus)
78#if defined(HAVE_INT128_T) && SIZEOF_SIZE_T <= 8
79# define DSIZE_T uint128_t
80#elif SIZEOF_SIZE_T * 2 <= SIZEOF_LONG_LONG
81# define DSIZE_T unsigned LONG_LONG
85# define RUBY_ALLOCV_LIMIT 0
87# define RUBY_ALLOCV_LIMIT 1024
91#define RB_GC_GUARD(v) \
93 volatile VALUE *rb_gc_guarded_ptr = &(v); \
94 __asm__("" : : "m"(rb_gc_guarded_ptr)); \
98#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr(&(v)))
100#define HAVE_RB_GC_GUARDED_PTR_VAL 1
101#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr_val(&(v),(v)))
105#define RB_ALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))
106#define RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))
107#define RB_ZALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))
108#define RB_ZALLOC(type) (RB_ZALLOC_N(type, 1))
109#define RB_REALLOC_N(var,type,n) \
110 ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))
112#define ALLOCA_N(type,n) \
113 RBIMPL_CAST((type *)(!(n) ? NULL : alloca(rbimpl_size_mul_or_raise(sizeof(type), (n)))))
117#define RB_ALLOCV(v, n) \
118 ((n) < RUBY_ALLOCV_LIMIT ? \
119 ((v) = 0, !(n) ? NULL : alloca(n)) : \
120 rb_alloc_tmp_buffer(&(v), (n)))
121#define RB_ALLOCV_N(type, v, n) \
122 RBIMPL_CAST((type *) \
123 (((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
124 ((v) = 0, !(n) ? NULL : alloca((n) * sizeof(type))) : \
125 rb_alloc_tmp_buffer2(&(v), (n), sizeof(type))))
126#define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
128#define MEMZERO(p,type,n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))
129#define MEMCPY(p1,p2,type,n) memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
130#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
131#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
133#define ALLOC_N RB_ALLOC_N
134#define ALLOC RB_ALLOC
135#define ZALLOC_N RB_ZALLOC_N
136#define ZALLOC RB_ZALLOC
137#define REALLOC_N RB_REALLOC_N
138#define ALLOCV RB_ALLOCV
139#define ALLOCV_N RB_ALLOCV_N
140#define ALLOCV_END RB_ALLOCV_END
164#ifdef HAVE_RB_GC_GUARDED_PTR_VAL
170# pragma optimize("", off)
172static inline volatile VALUE *
173rb_gc_guarded_ptr(
volatile VALUE *
ptr)
178# pragma optimize("", on)
183rb_mul_size_overflow(
size_t a,
size_t b,
size_t max,
size_t *c)
190 if (c2 >
max)
return 1;
191 *c = RBIMPL_CAST((
size_t)c2);
193 if (b != 0 && a >
max / b)
return 1;
199#if RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
201#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
206rbimpl_size_mul_overflow(
size_t x,
size_t y)
210#if RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
211 ret.
left = __builtin_mul_overflow(x, y, &ret.
right);
213#elif defined(DSIZE_T)
218 ret.
right = RBIMPL_CAST((
size_t)dz);
220#elif defined(_MSC_VER) && defined(_WIN64)
221 unsigned __int64
dp = 0;
222 unsigned __int64 dz = _umul128(x, y, &
dp);
223 ret.
left = RBIMPL_CAST((
bool)
dp);
224 ret.
right = RBIMPL_CAST((
size_t)dz);
236rbimpl_size_mul_or_raise(
size_t x,
size_t y)
239 rbimpl_size_mul_overflow(x, y);
251rb_alloc_tmp_buffer2(
volatile VALUE *store,
long count,
size_t elsize)
253 const size_t total_size = rbimpl_size_mul_or_raise(
count, elsize);
254 const size_t cnt = (total_size +
sizeof(
VALUE) - 1) /
sizeof(
VALUE);
267ruby_nonempty_memcpy(
void *dest,
const void *src,
size_t n)
270 return memcpy(dest, src, n);
278#define memcpy ruby_nonempty_memcpy
Defines RBIMPL_ATTR_ALLOC_SIZE.
#define RBIMPL_ATTR_ALLOC_SIZE(tuple)
Wraps (or simulates) __attribute__((alloc_size))
Defines ASSUME / RB_LIKELY / UNREACHABLE.
Defines RBIMPL_ATTR_CONST.
#define RBIMPL_ATTR_CONST()
Wraps (or simulates) __attribute__((const))
#define RBIMPL_ATTR_CONSTEXPR(_)
Wraps (or simulates) C++11 constexpr.
#define RB_GNUC_EXTENSION
Tewaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Thin wrapper to ruby/config.h.
Defines RBIMPL_HAS_BUILTIN.
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Defines RBIMPL_ALIGNAS / RBIMPL_ALIGNOF.
Historical shim for <limits.h>.
void * rb_alloc_tmp_buffer(volatile VALUE *store, long len)
void ruby_malloc_size_overflow(size_t, size_t)
void * rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t len, size_t count)
void rb_free_tmp_buffer(volatile VALUE *store)
Defines RBIMPL_ATTR_NOALIAS.
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Defines RBIMPL_ATTR_NORETURN.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Defines RBIMPL_ATTR_RESTRICT.
#define RBIMPL_ATTR_RESTRICT()
Wraps (or simulates) __declspec(restrict)
Defines RBIMPL_ATTR_RETURNS_NONNULL.
#define RBIMPL_ATTR_RETURNS_NONNULL()
Wraps (or simulates) __attribute__((returns_nonnull))
rb_atomic_t cnt[RUBY_NSIG]