Ruby: include/ruby/random.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

random.h
Go to the documentation of this file.
1#ifndef RUBY_RANDOM_H
2#define RUBY_RANDOM_H 1
13#include "ruby/ruby.h"
14
15 RBIMPL_SYMBOL_EXPORT_BEGIN()
16
17struct rb_random_struct {
18 VALUE seed;
19};
20 typedef struct rb_random_struct rb_random_t;
21
22 typedef void rb_random_init_func(rb_random_t *, const uint32_t *, size_t);
23 typedef unsigned int rb_random_get_int32_func(rb_random_t *);
24 typedef void rb_random_get_bytes_func(rb_random_t *, void *, size_t);
25 typedef double rb_random_get_real_func(rb_random_t *, int);
26
27 typedef struct {
28 size_t default_seed_bits;
29 rb_random_init_func *init;
30 rb_random_get_int32_func *get_int32;
31 rb_random_get_bytes_func *get_bytes;
32 rb_random_get_real_func *get_real;
33} rb_random_interface_t;
34
35#define RB_RANDOM_INTERFACE_DECLARE(prefix) \
36 static void prefix##_init(rb_random_t *, const uint32_t *, size_t); \
37 static unsigned int prefix##_get_int32(rb_random_t *); \
38 static void prefix##_get_bytes(rb_random_t *, void *, size_t)
39
40#define RB_RANDOM_INTERFACE_DECLARE_WITH_REAL(prefix) \
41 RB_RANDOM_INTERFACE_DECLARE(prefix); \
42 static double prefix##_get_real(rb_random_t *, int)
43
44#define RB_RANDOM_INTERFACE_DEFINE(prefix) \
45 prefix##_init, \
46 prefix##_get_int32, \
47 prefix##_get_bytes
48
49#define RB_RANDOM_INTERFACE_DEFINE_WITH_REAL(prefix) \
50 RB_RANDOM_INTERFACE_DEFINE(prefix), \
51 prefix##_get_real
52
53#if defined _WIN32 && !defined __CYGWIN__
54typedef rb_data_type_t rb_random_data_type_t;
55# define RB_RANDOM_PARENT 0
56#else
57 typedef const rb_data_type_t rb_random_data_type_t;
58# define RB_RANDOM_PARENT &rb_random_data_type
59#endif
60
61#define RB_RANDOM_DATA_INIT_PARENT(random_data) \
62 rbimpl_random_data_init_parent(&random_data)
63
64 void rb_random_mark(void *ptr);
65void rb_random_base_init(rb_random_t *rnd);
66double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl);
67void rb_rand_bytes_int32(rb_random_get_int32_func *, rb_random_t *, void *, size_t);
68 RUBY_EXTERN const rb_data_type_t rb_random_data_type;
69
70 RBIMPL_SYMBOL_EXPORT_END()
71
72 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
73/* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
74static inline const rb_random_interface_t *
75rb_rand_if(VALUE obj)
76{
77 RBIMPL_ASSERT_OR_ASSUME(RTYPEDDATA_P(obj));
78 const struct rb_data_type_struct *t = RTYPEDDATA_TYPE(obj);
79 const void *ret = t->data;
80 return RBIMPL_CAST((const rb_random_interface_t *)ret);
81}
82
83 RBIMPL_ATTR_NOALIAS()
84static inline void
85rbimpl_random_data_init_parent(rb_random_data_type_t *random_data)
86{
87#if defined _WIN32 && !defined __CYGWIN__
88 random_data->parent = &rb_random_data_type;
89#endif
90}
91
92#endif /* RUBY_RANDOM_H */
RBIMPL_ASSERT_OR_ASSUME
#define RBIMPL_ASSERT_OR_ASSUME(expr)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition: assert.h:229
ptr
struct RIMemo * ptr
Definition: debug.c:88
RUBY_EXTERN
#define RUBY_EXTERN
Definition: dllexport.h:36
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
rb_random_init_func
void rb_random_init_func(rb_random_t *, const uint32_t *, size_t)
Definition: random.h:22
rb_random_data_type
const rb_data_type_t rb_random_data_type
Definition: random.c:249
rb_random_mark
void rb_random_mark(void *ptr)
rb_random_get_bytes_func
void rb_random_get_bytes_func(rb_random_t *, void *, size_t)
Definition: random.h:24
rb_random_data_type_t
const rb_data_type_t rb_random_data_type_t
Definition: random.h:57
rb_random_get_real_func
double rb_random_get_real_func(rb_random_t *, int)
Definition: random.h:25
rb_int_pair_to_real
double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl)
Definition: random.c:1036
rb_rand_bytes_int32
void rb_rand_bytes_int32(rb_random_get_int32_func *, rb_random_t *, void *, size_t)
Definition: random.c:1182
rb_random_get_int32_func
unsigned int rb_random_get_int32_func(rb_random_t *)
Definition: random.h:23
rb_random_base_init
void rb_random_base_init(rb_random_t *rnd)
Definition: random.c:333
RBIMPL_ATTR_NOALIAS
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition: noalias.h:55
RBIMPL_ATTR_PURE_UNLESS_DEBUG
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE iff.
Definition: pure.h:38
ruby.h
uint32_t
unsigned int uint32_t
Definition: sha2.h:101
const
#define const
Definition: strftime.c:108
rb_data_type_struct
Definition: rtypeddata.h:70
rb_random_interface_t
Definition: random.h:27
rb_random_interface_t::init
rb_random_init_func * init
Definition: random.h:29
rb_random_interface_t::default_seed_bits
size_t default_seed_bits
Definition: random.h:28
rb_random_interface_t::get_int32
rb_random_get_int32_func * get_int32
Definition: random.h:30
rb_random_interface_t::get_real
rb_random_get_real_func * get_real
Definition: random.h:32
rb_random_interface_t::get_bytes
rb_random_get_bytes_func * get_bytes
Definition: random.h:31
rb_random_struct
Definition: random.h:17
rb_random_struct::seed
VALUE seed
Definition: random.h:18
t
#define t
Definition: symbol.c:253
VALUE
unsigned long VALUE
Definition: value.h:38