12 #include "ruby/internal/config.h"
24 #include <sys/types.h>
31 #if defined(HAVE_SYS_TIME_H)
32 # include <sys/time.h>
37 #elif defined HAVE_SYS_SYSCALL_H
38 # include <sys/syscall.h>
42 # include <winsock2.h>
44 # include <wincrypt.h>
48 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
50 # include <sys/param.h>
53 #if defined HAVE_GETRANDOM || defined HAVE_GETENTROPY
54 # if defined(HAVE_SYS_RANDOM_H)
55 # include <sys/random.h>
57 #elif defined __linux__ && defined __NR_getrandom
58 # include <linux/random.h>
62 # include <AvailabilityMacros.h>
66 #include "internal/array.h"
67 #include "internal/compilers.h"
68 #include "internal/numeric.h"
69 #include "internal/random.h"
70 #include "internal/sanitizers.h"
71 #include "internal/variable.h"
72 #include "ruby_atomic.h"
76 STATIC_ASSERT(int_must_be_32bit_at_least,
sizeof(
int) * CHAR_BIT >= 32);
78 #include "missing/mt19937.c"
81 static double int_pair_to_real_exclusive(uint32_t a, uint32_t b);
83 genrand_real(
struct MT *mt)
86 unsigned int a = genrand_int32(mt), b = genrand_int32(mt);
87 return int_pair_to_real_exclusive(a, b);
90 static const double dbl_reduce_scale =
92 / (double)(DBL_MANT_DIG > 2*31 ? (1ul<<31) : 1.0)
93 / (
double)(DBL_MANT_DIG > 1*31 ? (1ul<<31) : 1.0)
94 / (double)(1ul<<(DBL_MANT_DIG%31)));
97 int_pair_to_real_exclusive(uint32_t a, uint32_t b)
99 static const int a_shift = DBL_MANT_DIG < 64 ?
100 (64-DBL_MANT_DIG)/2 : 0;
101 static const int b_shift = DBL_MANT_DIG < 64 ?
102 (65-DBL_MANT_DIG)/2 : 0;
105 return (a*(
double)(1ul<<(32-b_shift))+b)*dbl_reduce_scale;
109 static double int_pair_to_real_inclusive(uint32_t a, uint32_t b);
112 genrand_real2(
struct MT *mt)
115 uint32_t a = genrand_int32(mt), b = genrand_int32(mt);
116 return int_pair_to_real_inclusive(a, b);
130 #define DEFAULT_SEED_CNT 4
134 static void fill_random_seed(uint32_t *seed,
size_t cnt,
bool try_bytes);
135 static VALUE make_seed_value(uint32_t *
ptr,
size_t len);
136 #define fill_random_bytes ruby_fill_random_bytes
140 DEFAULT_SEED_CNT * 32,
147 if (!genrand_initialized(&r->mt)) {
148 r->base.
seed = rand_init(&random_mt_if, &r->base, random_seed(
Qundef));
156 return &rand_mt_start(r)->base;
162 rb_free_default_rand_key(
void)
164 xfree(default_rand_key);
168 default_rand_mark(
void *
ptr)
195 return rand_mt_start(default_rand());
201 struct MT *mt = &default_mt()->mt;
202 return genrand_int32(mt);
208 struct MT *mt = &default_mt()->mt;
209 return genrand_real(mt);
212 #define SIZEOF_INT32 (31/CHAR_BIT + 1)
215 int_pair_to_real_inclusive(uint32_t a, uint32_t b)
218 enum {dig = DBL_MANT_DIG};
219 enum {dig_u = dig-32, dig_r64 = 64-dig, bmask = ~(~0u<<(dig_r64))};
220 #if defined HAVE_UINT128_T
221 const uint128_t m = ((uint128_t)1 << dig) | 1;
222 uint128_t x = ((uint128_t)a << 32) | b;
223 r = (double)(uint64_t)((x * m) >> 64);
224 #elif defined HAVE_UINT64_T && !MSC_VERSION_BEFORE(1300)
225 uint64_t x = ((uint64_t)a << dig_u) +
226 (((uint64_t)b + (a >> dig_u)) >> dig_r64);
230 b = (b >> dig_r64) + (((a >> dig_u) + (b & bmask)) >> dig_r64);
231 r = (double)a * (1 << dig_u) + b;
233 return r * dbl_reduce_scale;
239 static ID id_rand, id_bytes;
240 NORETURN(
static void domain_error(
void));
243 #define random_mark rb_random_mark
246 random_mark(
void *
ptr)
251 #define random_free RUBY_TYPED_DEFAULT_FREE
254 random_memsize(
const void *
ptr)
266 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
269 #define random_mt_mark rb_random_mark
270 #define random_mt_free RUBY_TYPED_DEFAULT_FREE
273 random_mt_memsize(
const void *
ptr)
286 (
void *)&random_mt_if,
287 RUBY_TYPED_FREE_IMMEDIATELY
301 get_rnd_mt(
VALUE obj)
309 try_get_rnd(
VALUE obj)
312 return rand_start(default_rand());
328 if (rnd == &default_rand()->base) {
329 return &random_mt_if;
343 random_alloc(
VALUE klass)
354 VALUE seed, buf0 = 0;
358 fill_random_seed(buf,
len,
true);
360 seed = make_seed_value(buf,
len);
361 explicit_bzero(buf,
len *
sizeof(*buf));
385 if (sign != 2 && buf[
len-1] == 1)
389 explicit_bzero(buf,
len *
sizeof(*buf));
414 unsigned int major = rng->
version.major;
415 unsigned int minor = rng->
version.minor;
416 if (major != RUBY_RANDOM_INTERFACE_VERSION_MAJOR) {
418 STRINGIZE(RUBY_RANDOM_INTERFACE_VERSION_MAJOR)
"."
419 STRINGIZE(RUBY_RANDOM_INTERFACE_VERSION_MINOR)
" "
420 "expected: %d.%d", major, minor);
423 rb_check_frozen(obj);
425 rnd->
seed = rand_init_default(rng, rnd);
433 #define DEFAULT_SEED_LEN (DEFAULT_SEED_CNT * (int)sizeof(int32_t))
435 #if defined(S_ISCHR) && !defined(DOSISH)
436 # define USE_DEV_URANDOM 1
438 # define USE_DEV_URANDOM 0
441 #ifdef HAVE_GETENTROPY
442 # define MAX_SEED_LEN_PER_READ 256
444 fill_random_bytes_urandom(
void *seed,
size_t size)
446 unsigned char *p = (
unsigned char *)seed;
448 size_t len = size < MAX_SEED_LEN_PER_READ ? size : MAX_SEED_LEN_PER_READ;
449 if (getentropy(p,
len) != 0) {
457 #elif USE_DEV_URANDOM
459 fill_random_bytes_urandom(
void *seed,
size_t size)
478 if (fd < 0)
return -1;
480 if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) {
482 ret = read(fd, ((
char*)seed) + offset, size - offset);
487 offset += (size_t)ret;
488 }
while (offset < size);
494 # define fill_random_bytes_urandom(seed, size) -1
497 #if ! defined HAVE_GETRANDOM && defined __linux__ && defined __NR_getrandom
498 # ifndef GRND_NONBLOCK
499 # define GRND_NONBLOCK 0x0001
501 # define getrandom(ptr, size, flags) \
502 (ssize_t)syscall(__NR_getrandom, (ptr), (size), (flags))
503 # define HAVE_GETRANDOM 1
507 #elif defined MAC_OS_X_VERSION_10_7 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
509 # if defined(USE_COMMON_RANDOM)
510 # elif defined MAC_OS_X_VERSION_10_10 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10
511 # define USE_COMMON_RANDOM 1
513 # define USE_COMMON_RANDOM 0
515 # if USE_COMMON_RANDOM
516 # include <CommonCrypto/CommonCryptoError.h>
517 # include <CommonCrypto/CommonRandom.h>
519 # include <Security/SecRandom.h>
523 fill_random_bytes_syscall(
void *seed,
size_t size,
int unused)
525 #if USE_COMMON_RANDOM
526 CCRNGStatus status = CCRandomGenerateBytes(seed, size);
527 int failed = status != kCCSuccess;
529 int status = SecRandomCopyBytes(kSecRandomDefault, size, seed);
530 int failed = status != errSecSuccess;
535 # if USE_COMMON_RANDOM
537 fprintf(stderr,
"CCRandomGenerateBytes failed: %d\n", status);
539 CFStringRef s = SecCopyErrorMessageString(status, NULL);
540 const char *m = s ? CFStringGetCStringPtr(s, kCFStringEncodingUTF8) : NULL;
541 fprintf(stderr,
"SecRandomCopyBytes failed: %d: %s\n", status,
550 #elif defined(HAVE_ARC4RANDOM_BUF)
552 fill_random_bytes_syscall(
void *buf,
size_t size,
int unused)
554 #if (defined(__OpenBSD__) && OpenBSD >= 201411) || \
555 (defined(__NetBSD__) && __NetBSD_Version__ >= 700000000) || \
556 (defined(__FreeBSD__) && __FreeBSD_version >= 1200079)
557 arc4random_buf(buf, size);
563 #elif defined(_WIN32)
566 # define DWORD_MAX (~(DWORD)0UL)
569 # if defined(CRYPT_VERIFYCONTEXT)
572 static const HCRYPTPROV INVALID_HCRYPTPROV = (HCRYPTPROV)INVALID_HANDLE_VALUE;
575 release_crypt(
void *p)
578 HCRYPTPROV prov = (HCRYPTPROV)ATOMIC_PTR_EXCHANGE(*
ptr, INVALID_HCRYPTPROV);
579 if (prov && prov != INVALID_HCRYPTPROV) {
580 CryptReleaseContext(prov, 0);
587 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
591 fill_random_bytes_crypt(
void *seed,
size_t size)
593 static HCRYPTPROV perm_prov;
594 HCRYPTPROV prov = perm_prov, old_prov;
597 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
598 prov = INVALID_HCRYPTPROV;
600 old_prov = (HCRYPTPROV)ATOMIC_PTR_CAS(perm_prov, 0, prov);
601 if (LIKELY(!old_prov)) {
602 if (prov != INVALID_HCRYPTPROV) {
604 rb_vm_register_global_object(wrapper);
608 if (prov != INVALID_HCRYPTPROV) {
609 CryptReleaseContext(prov, 0);
614 if (prov == INVALID_HCRYPTPROV)
return -1;
616 DWORD n = (size > (size_t)DWORD_MAX) ? DWORD_MAX : (DWORD)size;
617 if (!CryptGenRandom(prov, n, seed))
return -1;
618 seed = (
char *)seed + n;
624 # define fill_random_bytes_crypt(seed, size) -1
628 fill_random_bytes_bcrypt(
void *seed,
size_t size)
631 ULONG n = (size > (size_t)ULONG_MAX) ? LONG_MAX : (ULONG)size;
632 if (BCryptGenRandom(NULL, seed, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG))
634 seed = (
char *)seed + n;
641 fill_random_bytes_syscall(
void *seed,
size_t size,
int unused)
643 if (fill_random_bytes_bcrypt(seed, size) == 0)
return 0;
644 return fill_random_bytes_crypt(seed, size);
646 #elif defined HAVE_GETRANDOM
648 fill_random_bytes_syscall(
void *seed,
size_t size,
int need_secure)
655 flags = GRND_NONBLOCK;
658 ssize_t ret = getrandom(((
char*)seed) + offset, size - offset, flags);
660 ATOMIC_SET(try_syscall, 0);
663 offset += (size_t)ret;
664 }
while (offset < size);
670 # define fill_random_bytes_syscall(seed, size, need_secure) -1
674 ruby_fill_random_bytes(
void *seed,
size_t size,
int need_secure)
676 int ret = fill_random_bytes_syscall(seed, size, need_secure);
677 if (ret == 0)
return ret;
678 return fill_random_bytes_urandom(seed, size);
683 fill_random_seed(uint32_t *seed,
size_t cnt,
bool try_bytes)
686 #if defined HAVE_CLOCK_GETTIME
688 #elif defined HAVE_GETTIMEOFDAY
691 size_t len = cnt *
sizeof(*seed);
694 fill_random_bytes(seed,
len, FALSE);
698 memset(seed, 0,
len);
699 #if defined HAVE_CLOCK_GETTIME
700 clock_gettime(CLOCK_REALTIME, &tv);
701 seed[0] ^= tv.tv_nsec;
702 #elif defined HAVE_GETTIMEOFDAY
703 gettimeofday(&tv, 0);
704 seed[0] ^= tv.tv_usec;
706 seed[1] ^= (uint32_t)tv.tv_sec;
707 #
if SIZEOF_TIME_T > SIZEOF_INT
708 seed[0] ^= (uint32_t)((time_t)tv.tv_sec >> SIZEOF_INT * CHAR_BIT);
710 seed[2] ^= getpid() ^ (ATOMIC_FETCH_ADD(n, 1) << 16);
711 seed[3] ^= (uint32_t)(
VALUE)&seed;
712 #if SIZEOF_VOIDP > SIZEOF_INT
713 seed[2] ^= (uint32_t)((
VALUE)&seed >> SIZEOF_INT * CHAR_BIT);
718 make_seed_value(uint32_t *
ptr,
size_t len)
733 #define with_random_seed(size, add, try_bytes) \
734 for (uint32_t seedbuf[(size)+(add)], loop = (fill_random_seed(seedbuf, (size), try_bytes), 1); \
735 loop; explicit_bzero(seedbuf, (size)*sizeof(seedbuf[0])), loop = 0)
749 with_random_seed(DEFAULT_SEED_CNT, 1,
true) {
750 v = make_seed_value(seedbuf, DEFAULT_SEED_CNT);
776 if (n == 0)
return buf;
797 random_get_seed(
VALUE obj)
799 return get_rnd(obj)->
seed;
811 rnd1 = get_rnd_mt(obj);
812 rnd2 = get_rnd_mt(orig);
816 mt->next = mt->state + numberof(mt->state) - mt->left + 1;
821 mt_state(
const struct MT *mt)
824 sizeof(*mt->state), 0,
830 rand_mt_state(
VALUE obj)
833 return mt_state(&rnd->mt);
838 random_s_state(
VALUE klass)
840 return mt_state(&default_rand()->mt);
845 rand_mt_left(
VALUE obj)
853 random_s_left(
VALUE klass)
855 return INT2FIX(default_rand()->mt.left);
860 rand_mt_dump(
VALUE obj)
877 struct MT *mt = &rnd->mt;
895 sizeof(*mt->state), 0,
898 if (x > numberof(mt->state)) {
901 mt->left = (
unsigned int)x;
902 mt->next = mt->state + numberof(mt->state) - x + 1;
909 rand_mt_init_int32(
rb_random_t *rnd, uint32_t data)
912 init_genrand(mt, data);
919 init_by_array(mt, buf, (
int)
len);
926 return genrand_int32(mt);
965 seed = random_seed(obj);
971 rand_init(&random_mt_if, &r->base, seed);
978 make_mask(
unsigned long x)
995 unsigned long val, mask;
997 if (!limit)
return 0;
998 mask = make_mask(limit);
1001 if (0xffffffff < limit) {
1005 for (i = SIZEOF_LONG/SIZEOF_INT32-1; 0 <= i; i--) {
1006 if ((mask >> (i * 32)) & 0xffffffff) {
1007 val |= (
unsigned long)rng->
get_int32(rnd) << (i * 32);
1019 }
while (limit < val);
1033 uint32_t *tmp, *lim_array, *rnd_array;
1040 rnd_array = tmp +
len;
1047 for (i =
len-1; 0 <= i; i--) {
1049 uint32_t lim = lim_array[i];
1050 mask = mask ? 0xffffffff : (uint32_t)make_mask(lim);
1079 return limited_rand(&random_mt_if, &mt->base, limit);
1083 obj_random_bytes(
VALUE obj,
void *p,
long n)
1110 obj_random_bytes(obj, &x,
sizeof(x));
1111 return (
unsigned int)x;
1113 return random_int32(try_rand_if(obj, rnd), rnd);
1122 uint32_t x[2] = {0, 0};
1123 obj_random_bytes(obj, x,
sizeof(x));
1130 a = random_int32(rng, rnd);
1131 b = random_int32(rng, rnd);
1140 return int_pair_to_real_exclusive(a, b);
1143 return int_pair_to_real_inclusive(a, b);
1157 else if (d >= 1.0) {
1162 return random_real(obj, rnd, TRUE);
1166 ulong_to_num_plus_1(
unsigned long n)
1171 if (n >= ULONG_MAX) {
1178 static unsigned long
1181 if (!limit)
return 0;
1183 const int w =
sizeof(limit) * CHAR_BIT - nlz_long(limit);
1184 const int n = w > 32 ?
sizeof(
unsigned long) :
sizeof(uint32_t);
1185 const unsigned long mask = ~(~0UL << w);
1186 const unsigned long full =
1187 (size_t)n >=
sizeof(
unsigned long) ? ~0UL :
1188 ~(~0UL << n * CHAR_BIT);
1189 unsigned long val, bits = 0, rest = 0;
1192 union {uint32_t u32;
unsigned long ul;} buf;
1193 obj_random_bytes(obj, &buf, n);
1195 bits = (n ==
sizeof(uint32_t)) ? buf.u32 : buf.ul;
1201 }
while (limit < val);
1204 return limited_rand(try_rand_if(obj, rnd), rnd, limit);
1212 VALUE lim = ulong_to_num_plus_1(limit);
1215 if (rb_num_negative_p(v)) {
1223 return limited_rand(try_rand_if(obj, rnd), rnd, limit);
1232 uint32_t *tmp =
ALLOCV_N(uint32_t, vtmp,
len * 2);
1233 uint32_t mask = (uint32_t)~0 >> nlz;
1234 uint32_t *lim_array = tmp;
1235 uint32_t *rnd_array = tmp +
len;
1240 obj_random_bytes(obj, rnd_array,
len *
sizeof(uint32_t));
1241 rnd_array[0] &= mask;
1242 for (i = 0; i <
len; ++i) {
1243 if (lim_array[i] < rnd_array[i])
1245 if (rnd_array[i] < lim_array[i])
1252 return limited_big_rand(try_rand_if(obj, rnd), rnd, vmax);
1288 for (; n >= SIZEOF_INT32; n -= SIZEOF_INT32) {
1310 return obj_random_bytes(obj, NULL, n);
1312 return rand_bytes(try_rand_if(obj, rnd), rnd, n);
1344 random_s_seed(
VALUE obj)
1347 return rnd->base.
seed;
1356 if (begp) *begp = beg;
1358 if (endp) *endp = end;
1360 return rb_check_funcall_default(end, id_minus, 1, begp,
Qfalse);
1371 if (!max)
return Qnil;
1373 if (restrictive)
return Qnil;
1376 r = random_ulong_limited(obj, rnd, (
unsigned long)max - 1);
1382 if (!BIGNUM_SIGN(vmax)) {
1383 if (restrictive)
return Qnil;
1384 vmax = rb_big_uminus(vmax);
1389 if (max == -1)
return Qnil;
1390 r = random_ulong_limited(obj, rnd, max);
1393 ret = random_ulong_limited_big(obj, rnd, vmax);
1406 NORETURN(
static void invalid_argument(
VALUE));
1408 invalid_argument(
VALUE arg0)
1414 check_random_number(
VALUE v,
const VALUE *argv)
1421 invalid_argument(argv[0]);
1426 static inline double
1427 float_value(
VALUE v)
1442 if ((v = vmax = range_values(range, &beg, &end, &excl)) ==
Qfalse)
1444 if (
NIL_P(v)) domain_error();
1451 if ((max =
FIX2LONG(vmax) - excl) >= 0) {
1452 unsigned long r = random_ulong_limited(obj, rnd, (
unsigned long)max);
1462 v = random_ulong_limited_big(obj, rnd, vmax);
1475 else if (isnan(max)) {
1480 r = random_real(obj, rnd, excl);
1482 return rb_float_new(+(+(+(r - 0.5) * max) * scale) + mid);
1486 else if (max == 0.0 && !excl) {
1546 random_rand(
int argc,
VALUE *argv,
VALUE obj)
1548 VALUE v = rand_random(argc, argv, obj, try_get_rnd(obj));
1549 check_random_number(v, argv);
1565 if (!
NIL_P(v))
return rand_int(obj, rnd, v, 1);
1569 const double max = float_value(v);
1574 double r = random_real(obj, rnd, TRUE);
1575 if (max > 0.0) r *= max;
1579 return rand_range(obj, rnd, vmax);
1595 rand_random_number(
int argc,
VALUE *argv,
VALUE obj)
1598 VALUE v = rand_random(argc, argv, obj, rnd);
1599 if (
NIL_P(v)) v = rand_random(0, 0, obj, rnd);
1600 else if (!v) invalid_argument(argv[0]);
1633 r1 = get_rnd_mt(
self);
1634 r2 = get_rnd_mt(other);
1635 if (memcmp(r1->mt.state, r2->mt.state,
sizeof(r1->mt.state)))
return Qfalse;
1636 if ((r1->mt.next - r1->mt.state) != (r2->mt.next - r2->mt.state))
return Qfalse;
1637 if (r1->mt.left != r2->mt.left)
return Qfalse;
1638 return rb_equal(r1->base.seed, r2->base.seed);
1679 VALUE v = rand_range(obj, rnd, vmax);
1680 if (v !=
Qfalse)
return v;
1683 v = rand_int(obj, rnd, vmax, 0);
1684 if (!
NIL_P(v))
return v;
1687 return DBL2NUM(random_real(obj, rnd, TRUE));
1701 random_s_rand(
int argc,
VALUE *argv,
VALUE obj)
1703 VALUE v = rand_random(argc, argv,
Qnil, rand_start(default_rand()));
1704 check_random_number(v, argv);
1708 #define SIP_HASH_STREAMING 0
1709 #define sip_hash13 ruby_sip_hash13
1710 #if !defined _WIN32 && !defined BYTE_ORDER
1711 # ifdef WORDS_BIGENDIAN
1712 # define BYTE_ORDER BIG_ENDIAN
1714 # define BYTE_ORDER LITTLE_ENDIAN
1716 # ifndef LITTLE_ENDIAN
1717 # define LITTLE_ENDIAN 1234
1720 # define BIG_ENDIAN 4321
1723 #include "siphash.c"
1736 init_hash_salt(
struct MT *mt)
1740 for (i = 0; i < numberof(hash_salt.u32); ++i)
1741 hash_salt.u32[i] = genrand_int32(mt);
1744 NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t
rb_hash_start(st_index_t h));
1748 return st_hash_start(hash_salt.key.hash + h);
1755 #ifdef HAVE_UINT64_T
1756 return (st_index_t)h;
1758 return (st_index_t)(h.u32[0] ^ h.u32[1]);
1765 Init_RandomSeedCore(
void)
1767 if (!fill_random_bytes(&hash_salt,
sizeof(hash_salt), FALSE))
return;
1778 with_random_seed(DEFAULT_SEED_CNT, 0,
false) {
1779 init_by_array(&mt, seedbuf, DEFAULT_SEED_CNT);
1782 init_hash_salt(&mt);
1783 explicit_bzero(&mt,
sizeof(mt));
1790 uninit_genrand(&r->mt);
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#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.
VALUE rb_float_new(double d)
Converts a C's double into an instance of rb_cFloat.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
VALUE rb_define_class_id(ID id, VALUE super)
This is a very badly designed API that creates an anonymous class.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
#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_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.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
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.
VALUE rb_eArgError
ArgumentError exception.
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_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.
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.
void rb_gc_mark(VALUE obj)
Marks an object.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Exports an integer into a buffer.
VALUE rb_big_minus(VALUE x, VALUE y)
Performs subtraction of the passed two objects.
int rb_bigzero_p(VALUE x)
Queries if the passed bignum instance is a "bigzero".
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
VALUE rb_big_plus(VALUE x, VALUE y)
Performs addition of the passed two objects.
size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
Calculates the number of words needed represent the absolute value of the passed integer.
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Import an integer from a buffer.
#define INTEGER_PACK_MSWORD_FIRST
Stores/interprets the most significant word as the first word.
VALUE rb_big_norm(VALUE x)
Normalises the passed bignum.
#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.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
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().
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
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().
void * rb_ractor_local_storage_ptr(rb_ractor_local_key_t key)
Identical to rb_ractor_local_storage_value() except the return type.
static const rb_random_interface_t * rb_rand_if(VALUE obj)
Queries the interface of the passed random object.
const rb_data_type_t rb_random_data_type
The data that holds the backend type of rb_cRandom.
#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.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define DATA_PTR(obj)
Convenient getter macro.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
static const struct rb_data_type_struct * 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.
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.
rb_random_get_bytes_func * get_bytes
Function to obtain a series of random bytes.
struct rb_random_interface_t::@55 version
Major/minor versions of this interface.
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.
void ruby_xfree(void *ptr)
Deallocates a storage instance.