12#include "ruby/internal/config.h"
31#if defined(HAVE_SYS_TIME_H)
37#elif defined HAVE_SYS_SYSCALL_H
38# include <sys/syscall.h>
47#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
49# include <sys/param.h>
52#if defined HAVE_GETRANDOM || defined HAVE_GETENTROPY
53# if defined(HAVE_SYS_RANDOM_H)
54# include <sys/random.h>
56#elif defined __linux__ && defined __NR_getrandom
57# include <linux/random.h>
61# include <AvailabilityMacros.h>
65#include "internal/array.h"
66#include "internal/compilers.h"
67#include "internal/numeric.h"
68#include "internal/random.h"
69#include "internal/sanitizers.h"
70#include "internal/variable.h"
71#include "ruby_atomic.h"
75STATIC_ASSERT(int_must_be_32bit_at_least,
sizeof(
int) * CHAR_BIT >= 32);
77#include "missing/mt19937.c"
80static double int_pair_to_real_exclusive(uint32_t a, uint32_t b);
82genrand_real(
struct MT *mt)
85 unsigned int a = genrand_int32(mt), b = genrand_int32(mt);
86 return int_pair_to_real_exclusive(a, b);
89static const double dbl_reduce_scale =
91 / (double)(DBL_MANT_DIG > 2*31 ? (1ul<<31) : 1.0)
92 / (
double)(DBL_MANT_DIG > 1*31 ? (1ul<<31) : 1.0)
93 / (double)(1ul<<(DBL_MANT_DIG%31)));
96int_pair_to_real_exclusive(uint32_t a, uint32_t b)
98 static const int a_shift = DBL_MANT_DIG < 64 ?
99 (64-DBL_MANT_DIG)/2 : 0;
100 static const int b_shift = DBL_MANT_DIG < 64 ?
101 (65-DBL_MANT_DIG)/2 : 0;
104 return (a*(
double)(1ul<<(32-b_shift))+b)*dbl_reduce_scale;
108static double int_pair_to_real_inclusive(uint32_t a, uint32_t b);
111genrand_real2(
struct MT *mt)
114 uint32_t a = genrand_int32(mt), b = genrand_int32(mt);
115 return int_pair_to_real_inclusive(a, b);
129#define DEFAULT_SEED_CNT 4
133static void fill_random_seed(uint32_t *seed,
size_t cnt,
bool try_bytes);
134static VALUE make_seed_value(uint32_t *ptr,
size_t len);
135#define fill_random_bytes ruby_fill_random_bytes
139 DEFAULT_SEED_CNT * 32,
146 if (!genrand_initialized(&r->mt)) {
147 r->base.
seed = rand_init(&random_mt_if, &r->base, random_seed(
Qundef));
158 return &rand_mt_start(r, obj)->base;
164rb_free_default_rand_key(
void)
166 xfree(default_rand_key);
170default_rand_mark(
void *ptr)
173 rb_gc_mark(rnd->base.
seed);
197 return rand_mt_start(default_rand(),
Qfalse);
201default_rand_start(
void)
203 return &default_mt()->base;
209 struct MT *mt = &default_mt()->mt;
210 return genrand_int32(mt);
216 struct MT *mt = &default_mt()->mt;
217 return genrand_real(mt);
220#define SIZEOF_INT32 (31/CHAR_BIT + 1)
223int_pair_to_real_inclusive(uint32_t a, uint32_t b)
226 enum {dig = DBL_MANT_DIG};
227 enum {dig_u = dig-32, dig_r64 = 64-dig, bmask = ~(~0u<<(dig_r64))};
228#if defined HAVE_UINT128_T
229 const uint128_t m = ((uint128_t)1 << dig) | 1;
230 uint128_t x = ((uint128_t)a << 32) | b;
231 r = (double)(uint64_t)((x * m) >> 64);
232#elif defined HAVE_UINT64_T
233 uint64_t x = ((uint64_t)a << dig_u) +
234 (((uint64_t)b + (a >> dig_u)) >> dig_r64);
238 b = (b >> dig_r64) + (((a >> dig_u) + (b & bmask)) >> dig_r64);
239 r = (double)a * (1 << dig_u) + b;
241 return r * dbl_reduce_scale;
247static ID id_rand, id_bytes;
248NORETURN(
static void domain_error(
void));
251#define random_mark rb_random_mark
254random_mark(
void *ptr)
259#define random_free RUBY_TYPED_DEFAULT_FREE
262random_memsize(
const void *ptr)
274 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED
277#define random_mt_mark rb_random_mark
278#define random_mt_free RUBY_TYPED_DEFAULT_FREE
281random_mt_memsize(
const void *ptr)
293 &rb_random_data_type,
294 (
void *)&random_mt_if,
295 RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED
320 *rng_p = &random_mt_if;
321 return default_rand_start();
323 if (!rb_typeddata_is_kind_of(obj, &rb_random_data_type))
return NULL;
328 rb_raise(rb_eArgError,
"uninitialized random: %s",
331 if (
type == &random_mt_type) rnd = rand_start(rnd, obj);
344random_alloc(
VALUE klass)
361 fill_random_seed(buf,
len,
true);
363 seed = make_seed_value(buf,
len);
364 explicit_bzero(buf,
len *
sizeof(*buf));
368 uint32_t minimul[1] = {0};
369 rng->
init(rnd, minimul, 0);
383 len = rb_absint_numwords(seed, 32, NULL);
386 sign = rb_integer_pack(seed, buf,
len,
sizeof(uint32_t), 0,
394 if (sign != 2 && buf[
len-1] == 1)
398 explicit_bzero(buf,
len *
sizeof(*buf));
423 unsigned int major = rng->
version.major;
424 unsigned int minor = rng->
version.minor;
425 if (major != RUBY_RANDOM_INTERFACE_VERSION_MAJOR) {
427 STRINGIZE(RUBY_RANDOM_INTERFACE_VERSION_MAJOR)
"."
428 STRINGIZE(RUBY_RANDOM_INTERFACE_VERSION_MINOR)
" "
429 "expected: %d.%d", major, minor);
432 rb_check_frozen(obj);
442#define DEFAULT_SEED_LEN (DEFAULT_SEED_CNT * (int)sizeof(int32_t))
444#if defined(S_ISCHR) && !defined(DOSISH)
445# define USE_DEV_URANDOM 1
447# define USE_DEV_URANDOM 0
450#if ! defined HAVE_GETRANDOM && defined __linux__ && defined __NR_getrandom
451# ifndef GRND_NONBLOCK
452# define GRND_NONBLOCK 0x0001
454# define getrandom(ptr, size, flags) \
455 (ssize_t)syscall(__NR_getrandom, (ptr), (size), (flags))
456# define HAVE_GETRANDOM 1
462fill_random_bytes_urandom(
void *seed,
size_t size)
481 if (fd < 0)
return -1;
483 if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) {
485 ret = read(fd, ((
char*)seed) + offset, size - offset);
490 offset += (size_t)ret;
491 }
while (offset < size);
497# define fill_random_bytes_urandom(seed, size) -1
502#elif defined MAC_OS_X_VERSION_10_7 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
504# if defined(USE_COMMON_RANDOM)
505# elif defined MAC_OS_X_VERSION_10_10 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10
506# define USE_COMMON_RANDOM 1
508# define USE_COMMON_RANDOM 0
510# if USE_COMMON_RANDOM
511# include <CommonCrypto/CommonCryptoError.h>
512# include <CommonCrypto/CommonRandom.h>
514# include <Security/SecRandom.h>
518fill_random_bytes_lib(
void *seed,
size_t size)
521 CCRNGStatus status = CCRandomGenerateBytes(seed, size);
522 int failed = status != kCCSuccess;
524 int status = SecRandomCopyBytes(kSecRandomDefault, size, seed);
525 int failed = status != errSecSuccess;
530# if USE_COMMON_RANDOM
532 fprintf(stderr,
"CCRandomGenerateBytes failed: %d\n", status);
534 CFStringRef s = SecCopyErrorMessageString(status, NULL);
535 const char *m = s ? CFStringGetCStringPtr(s, kCFStringEncodingUTF8) : NULL;
536 fprintf(stderr,
"SecRandomCopyBytes failed: %d: %s\n", status,
545#elif defined(HAVE_ARC4RANDOM_BUF) && \
546 ((defined(__OpenBSD__) && OpenBSD >= 201411) || \
547 (defined(__NetBSD__) && __NetBSD_Version__ >= 700000000) || \
548 (defined(__FreeBSD__) && __FreeBSD_version >= 1200079))
551fill_random_bytes_lib(
void *buf,
size_t size)
553 arc4random_buf(buf, size);
558fill_random_bytes_lib(
void *seed,
size_t size)
561 ULONG n = (size > (size_t)ULONG_MAX) ? ULONG_MAX/2+1 : (ULONG)size;
562 if (BCryptGenRandom(NULL, seed, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG))
564 seed = (
char *)seed + n;
570# define fill_random_bytes_lib(seed, size) -1
575#elif defined HAVE_GETRANDOM
577fill_random_bytes_syscall(
void *seed,
size_t size,
int need_secure)
584 flags = GRND_NONBLOCK;
587 ssize_t ret = getrandom(((
char*)seed) + offset, size - offset, flags);
589 ATOMIC_SET(try_syscall, 0);
592 offset += (size_t)ret;
593 }
while (offset < size);
598#elif defined(HAVE_GETENTROPY)
606#ifndef GETENTROPY_MAX
607# define GETENTROPY_MAX 256
610fill_random_bytes_syscall(
void *seed,
size_t size,
int need_secure)
612 unsigned char *p = (
unsigned char *)seed;
614 size_t len = size < GETENTROPY_MAX ? size : GETENTROPY_MAX;
615 if (getentropy(p,
len) != 0) {
624# define fill_random_bytes_syscall(seed, size, need_secure) -1
628ruby_fill_random_bytes(
void *seed,
size_t size,
int need_secure)
630 int ret = fill_random_bytes_syscall(seed, size, need_secure);
631 if (ret == 0)
return ret;
632 if (fill_random_bytes_lib(seed, size) == 0)
return 0;
633 return fill_random_bytes_urandom(seed, size);
638fill_random_seed(uint32_t *seed,
size_t cnt,
bool try_bytes)
641#if defined HAVE_CLOCK_GETTIME
643#elif defined HAVE_GETTIMEOFDAY
646 size_t len = cnt *
sizeof(*seed);
649 fill_random_bytes(seed,
len, FALSE);
653 memset(seed, 0,
len);
654#if defined HAVE_CLOCK_GETTIME
655 clock_gettime(CLOCK_REALTIME, &tv);
656 seed[0] ^= tv.tv_nsec;
657#elif defined HAVE_GETTIMEOFDAY
658 gettimeofday(&tv, 0);
659 seed[0] ^= tv.tv_usec;
661 seed[1] ^= (uint32_t)tv.tv_sec;
662#if SIZEOF_TIME_T > SIZEOF_INT
663 seed[0] ^= (uint32_t)((time_t)tv.tv_sec >> SIZEOF_INT * CHAR_BIT);
665 seed[2] ^= getpid() ^ (ATOMIC_FETCH_ADD(n, 1) << 16);
666 seed[3] ^= (uint32_t)(
VALUE)&seed;
667#if SIZEOF_VOIDP > SIZEOF_INT
668 seed[2] ^= (uint32_t)((
VALUE)&seed >> SIZEOF_INT * CHAR_BIT);
673make_seed_value(uint32_t *ptr,
size_t len)
677 if (ptr[
len-1] <= 1) {
682 seed = rb_integer_unpack(ptr,
len,
sizeof(uint32_t), 0,
688#define with_random_seed(size, add, try_bytes) \
689 for (uint32_t seedbuf[(size)+(add)], loop = (fill_random_seed(seedbuf, (size), try_bytes), 1); \
690 loop; explicit_bzero(seedbuf, (size)*sizeof(seedbuf[0])), loop = 0)
704 with_random_seed(DEFAULT_SEED_CNT, 1,
true) {
705 v = make_seed_value(seedbuf, DEFAULT_SEED_CNT);
731 if (n == 0)
return buf;
732 if (fill_random_bytes(RSTRING_PTR(buf), n, TRUE))
752random_get_seed(
VALUE obj)
754 return get_rnd(obj)->
seed;
766 rnd1 = get_rnd_mt(obj);
767 rnd2 = get_rnd_mt(orig);
772 mt->next = mt->state + numberof(mt->state) - mt->left + 1;
777mt_state(
const struct MT *mt)
779 return rb_integer_unpack(mt->state, numberof(mt->state),
780 sizeof(*mt->state), 0,
786rand_mt_state(
VALUE obj)
789 return mt_state(&rnd->mt);
794random_s_state(
VALUE klass)
796 return mt_state(&default_rand()->mt);
801rand_mt_left(
VALUE obj)
809random_s_left(
VALUE klass)
811 return INT2FIX(default_rand()->mt.left);
816rand_mt_dump(
VALUE obj)
833 struct MT *mt = &rnd->mt;
848 rb_raise(rb_eArgError,
"wrong dump data");
850 rb_integer_pack(state, mt->state, numberof(mt->state),
851 sizeof(*mt->state), 0,
854 if (x > numberof(mt->state) || x == 0) {
855 rb_raise(rb_eArgError,
"wrong value");
857 mt->left = (
unsigned int)x;
858 mt->next = mt->state + numberof(mt->state) - x + 1;
868 init_genrand(mt, data);
875 init_by_array(mt, buf, (
int)
len);
882 return genrand_int32(mt);
886rand_mt_get_bytes(
rb_random_t *rnd,
void *ptr,
size_t n)
921 seed = random_seed(obj);
927 rand_init(&random_mt_if, &r->base, seed);
934make_mask(
unsigned long x)
951 unsigned long val, mask;
953 if (!limit)
return 0;
954 mask = make_mask(limit);
957 if (0xffffffff < limit) {
961 for (i = SIZEOF_LONG/SIZEOF_INT32-1; 0 <= i; i--) {
962 if ((mask >> (i * 32)) & 0xffffffff) {
963 val |= (
unsigned long)rng->
get_int32(rnd) << (i * 32);
975 }
while (limit < val);
989 uint32_t *tmp, *lim_array, *rnd_array;
993 len = rb_absint_numwords(limit, 32, NULL);
996 rnd_array = tmp +
len;
997 rb_integer_pack(limit, lim_array,
len,
sizeof(uint32_t), 0,
1003 for (i =
len-1; 0 <= i; i--) {
1005 uint32_t lim = lim_array[i];
1006 mask = mask ? 0xffffffff : (uint32_t)make_mask(lim);
1018 val = rb_integer_unpack(rnd_array,
len,
sizeof(uint32_t), 0,
1035 return limited_rand(&random_mt_if, &mt->base, limit);
1039obj_random_bytes(
VALUE obj,
void *p,
long n)
1050 if (p) memcpy(p, RSTRING_PTR(v), n);
1067 obj_random_bytes(obj, &x,
sizeof(x));
1068 return (
unsigned int)x;
1070 return random_int32(rng, rnd);
1079 uint32_t x[2] = {0, 0};
1080 obj_random_bytes(obj, x,
sizeof(x));
1086 a = random_int32(rng, rnd);
1087 b = random_int32(rng, rnd);
1096 return int_pair_to_real_exclusive(a, b);
1099 return int_pair_to_real_inclusive(a, b);
1114 else if (d >= 1.0) {
1119 return random_real(obj, rng, rnd, TRUE);
1123ulong_to_num_plus_1(
unsigned long n)
1128 if (n >= ULONG_MAX) {
1138 if (!limit)
return 0;
1140 const int w =
sizeof(limit) * CHAR_BIT - nlz_long(limit);
1141 const int n = w > 32 ?
sizeof(
unsigned long) : sizeof(uint32_t);
1142 const unsigned long mask = ~(~0UL << w);
1143 const unsigned long full =
1144 (size_t)n >=
sizeof(
unsigned long) ? ~0UL :
1145 ~(~0UL << n * CHAR_BIT);
1146 unsigned long val, bits = 0, rest = 0;
1149 union {uint32_t u32;
unsigned long ul;} buf;
1150 obj_random_bytes(obj, &buf, n);
1152 bits = (n ==
sizeof(uint32_t)) ? buf.u32 : buf.ul;
1158 }
while (limit < val);
1161 return limited_rand(rng, rnd, limit);
1170 VALUE lim = ulong_to_num_plus_1(limit);
1173 if (rb_num_negative_p(v)) {
1181 return limited_rand(rng, rnd, limit);
1189 size_t i, nlz,
len = rb_absint_numwords(vmax, 32, &nlz);
1190 uint32_t *tmp =
ALLOCV_N(uint32_t, vtmp,
len * 2);
1191 uint32_t mask = (uint32_t)~0 >> nlz;
1192 uint32_t *lim_array = tmp;
1193 uint32_t *rnd_array = tmp +
len;
1195 rb_integer_pack(vmax, lim_array,
len,
sizeof(uint32_t), 0, flag);
1198 obj_random_bytes(obj, rnd_array,
len *
sizeof(uint32_t));
1199 rnd_array[0] &= mask;
1200 for (i = 0; i <
len; ++i) {
1201 if (lim_array[i] < rnd_array[i])
1203 if (rnd_array[i] < lim_array[i])
1206 v = rb_integer_unpack(rnd_array,
len,
sizeof(uint32_t), 0, flag);
1210 return limited_big_rand(rng, rnd, vmax);
1220 ptr = RSTRING_PTR(bytes);
1247 for (; n >= SIZEOF_INT32; n -= SIZEOF_INT32) {
1270 return obj_random_bytes(obj, NULL, n);
1272 return rand_bytes(rng, rnd, n);
1304random_s_seed(
VALUE obj)
1307 return rnd->base.
seed;
1316 if (begp) *begp = beg;
1318 if (endp) *endp = end;
1320 return rb_check_funcall_default(end, id_minus, 1, begp,
Qfalse);
1331 if (!max)
return Qnil;
1333 if (restrictive)
return Qnil;
1336 r = random_ulong_limited(obj, rng, rnd, (
unsigned long)max - 1);
1341 if (rb_bigzero_p(vmax))
return Qnil;
1342 if (!BIGNUM_SIGN(vmax)) {
1343 if (restrictive)
return Qnil;
1344 vmax = rb_big_uminus(vmax);
1346 vmax = rb_big_minus(vmax,
INT2FIX(1));
1349 if (max == -1)
return Qnil;
1350 r = random_ulong_limited(obj, rng, rnd, max);
1353 ret = random_ulong_limited_big(obj, rng, rnd, vmax);
1366NORETURN(
static void invalid_argument(
VALUE));
1368invalid_argument(
VALUE arg0)
1370 rb_raise(rb_eArgError,
"invalid argument - %"PRIsVALUE, arg0);
1374check_random_number(
VALUE v,
const VALUE *argv)
1381 invalid_argument(argv[0]);
1402 if ((v = vmax = range_values(range, &beg, &end, &excl)) ==
Qfalse)
1404 if (
NIL_P(v)) domain_error();
1411 if ((max =
FIX2LONG(vmax) - excl) >= 0) {
1412 unsigned long r = random_ulong_limited(obj, rng, rnd, (
unsigned long)max);
1417 vmax = excl ? rb_big_minus(vmax,
INT2FIX(1)) : rb_big_norm(vmax);
1422 v = random_ulong_limited_big(obj, rng, rnd, vmax);
1435 else if (isnan(max)) {
1440 r = random_real(obj, rng, rnd, excl);
1442 return rb_float_new(+(+(+(r - 0.5) * max) * scale) + mid);
1444 v = rb_float_new(r * max);
1446 else if (max == 0.0 && !excl) {
1447 v = rb_float_new(0.0);
1459 return rb_big_plus(v, beg);
1512 VALUE v = rand_random(argc, argv, obj, rng, rnd);
1513 check_random_number(v, argv);
1523 return rb_float_new(random_real(obj, rng, rnd, TRUE));
1529 if (!
NIL_P(v))
return rand_int(obj, rng, rnd, v, 1);
1533 const double max = float_value(v);
1538 double r = random_real(obj, rng, rnd, TRUE);
1539 if (max > 0.0) r *= max;
1540 return rb_float_new(r);
1543 return rand_range(obj, rng, rnd, vmax);
1559rand_random_number(
int argc,
VALUE *argv,
VALUE obj)
1563 VALUE v = rand_random(argc, argv, obj, rng, rnd);
1564 if (
NIL_P(v)) v = rand_random(0, 0, obj, rng, rnd);
1565 else if (!v) invalid_argument(argv[0]);
1598 r1 = get_rnd_mt(self);
1599 r2 = get_rnd_mt(other);
1600 if (memcmp(r1->mt.state, r2->mt.state,
sizeof(r1->mt.state)))
return Qfalse;
1601 if ((r1->mt.next - r1->mt.state) != (r2->mt.next - r2->mt.state))
return Qfalse;
1602 if (r1->mt.left != r2->mt.left)
return Qfalse;
1603 return rb_equal(r1->base.seed, r2->base.seed);
1647 VALUE v = rand_range(obj, rng, rnd, vmax);
1648 if (v !=
Qfalse)
return v;
1651 v = rand_int(obj, rng, rnd, vmax, 0);
1652 if (!
NIL_P(v))
return v;
1655 return DBL2NUM(random_real(obj, rng, rnd, TRUE));
1669random_s_rand(
int argc,
VALUE *argv,
VALUE obj)
1671 VALUE v = rand_random(argc, argv,
Qnil, &random_mt_if, default_rand_start());
1672 check_random_number(v, argv);
1676#define SIP_HASH_STREAMING 0
1677#define sip_hash13 ruby_sip_hash13
1678#if !defined _WIN32 && !defined BYTE_ORDER
1679# ifdef WORDS_BIGENDIAN
1680# define BYTE_ORDER BIG_ENDIAN
1682# define BYTE_ORDER LITTLE_ENDIAN
1684# ifndef LITTLE_ENDIAN
1685# define LITTLE_ENDIAN 1234
1688# define BIG_ENDIAN 4321
1704init_hash_salt(
struct MT *mt)
1708 for (i = 0; i < numberof(hash_salt.u32); ++i)
1709 hash_salt.u32[i] = genrand_int32(mt);
1712NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t
rb_hash_start(st_index_t h));
1716 return st_hash_start(hash_salt.key.hash + h);
1723#if SIZEOF_ST_INDEX_T >= 8
1724 return (st_index_t)h;
1725#elif defined HAVE_UINT64_T
1726 return (st_index_t)((h >> 32) ^ h);
1728 return (st_index_t)(h.u32[0] ^ h.u32[1]);
1735Init_RandomSeedCore(
void)
1737 if (!fill_random_bytes(&hash_salt,
sizeof(hash_salt), FALSE))
return;
1748 with_random_seed(DEFAULT_SEED_CNT, 0,
false) {
1749 init_by_array(&mt, seedbuf, DEFAULT_SEED_CNT);
1752 init_hash_salt(&mt);
1753 explicit_bzero(&mt,
sizeof(mt));
1760 uninit_genrand(&r->mt);
1803 rb_cRandom = rb_define_class(
"Random", base);
1858 id_rand = rb_intern(
"rand");
1859 id_bytes = rb_intern(
"bytes");
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
#define TYPE(_)
Old name of rb_type.
#define NUM2ULONG
Old name of RB_NUM2ULONG.
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define NUM2DBL
Old name of rb_num2dbl.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define DBL2NUM
Old name of rb_float_new.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_check_copyable(VALUE obj, VALUE orig)
Ensures that the passed object can be initialize_copy relationship.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
VALUE rb_eSystemCallError
SystemCallError exception.
VALUE rb_check_to_int(VALUE val)
Identical to rb_check_to_integer(), except it uses #to_int for conversion.
VALUE rb_cObject
Object class.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_check_to_float(VALUE val)
This is complicated.
VALUE rb_cRandom
Random class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_to_float(VALUE val)
Identical to rb_check_to_float(), except it raises on error.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it only takes public methods into account.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_MSWORD_FIRST
Stores/interprets the most significant word as the first word.
#define INTEGER_PACK_LSWORD_FIRST
Stores/interprets the least significant word as the first word.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
unsigned long rb_genrand_ulong_limited(unsigned long i)
Generates a random number whose upper limit is i.
double rb_random_real(VALUE rnd)
Identical to rb_genrand_real(), except it generates using the passed RNG.
unsigned int rb_random_int32(VALUE rnd)
Identical to rb_genrand_int32(), except it generates using the passed RNG.
void rb_reset_random_seed(void)
Resets the RNG behind rb_genrand_int32()/rb_genrand_real().
VALUE rb_random_bytes(VALUE rnd, long n)
Generates a String of random bytes.
double rb_genrand_real(void)
Generates a double random number.
unsigned long rb_random_ulong_limited(VALUE rnd, unsigned long limit)
Identical to rb_genrand_ulong_limited(), except it generates using the passed RNG.
unsigned int rb_genrand_int32(void)
Generates a 32 bit random number.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
int len
Length of the buffer.
void * rb_ractor_local_storage_ptr(rb_ractor_local_key_t key)
Identical to rb_ractor_local_storage_value() except the return type.
void rb_ractor_local_storage_ptr_set(rb_ractor_local_key_t key, void *ptr)
Identical to rb_ractor_local_storage_value_set() except the parameter type.
rb_ractor_local_key_t rb_ractor_local_storage_ptr_newkey(const struct rb_ractor_local_storage_type *type)
Extended version of rb_ractor_local_storage_value_newkey().
#define RB_RANDOM_INTERFACE_DEFINE(prefix)
This utility macro expands to the names declared using RB_RANDOM_INTERFACE_DECLARE.
struct rb_random_struct rb_random_t
#define RB_RANDOM_INTERFACE_DECLARE(prefix)
This utility macro defines 4 functions named prefix_init, prefix_init_int32, prefix_get_int32,...
void rb_rand_bytes_int32(rb_random_get_int32_func *func, rb_random_t *prng, void *buff, size_t size)
Repeatedly calls the passed function over and over again until the passed buffer is filled with rando...
unsigned int rb_random_get_int32_func(rb_random_t *rng)
This is the type of functions called from your object's #rand method.
double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl)
Generates a 64 bit floating point number by concatenating two 32bit unsigned integers.
void rb_random_base_init(rb_random_t *rnd)
Initialises an allocated rb_random_t instance.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
static const rb_data_type_t * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define errno
Ractor-aware version of errno.
#define InitVM(ext)
This macro is for internal use.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
Type that defines a ractor-local storage.
PRNG algorithmic interface, analogous to Ruby level classes.
rb_random_init_func * init
Function to initialize from uint32_t array.
rb_random_init_int32_func * init_int32
Function to initialize from single uint32_t.
size_t default_seed_bits
Number of bits of seed numbers.
rb_random_get_int32_func * get_int32
Function to obtain a random integer.
rb_random_get_real_func * get_real
Function to obtain a random double.
struct rb_random_interface_t::@66 version
Major/minor versions of this interface.
rb_random_get_bytes_func * get_bytes
Function to obtain a series of random bytes.
Base components of the random interface.
VALUE seed
Seed, passed through e.g.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.