Use `MRB_TT_ISTRUCT` for `Random` to reduce memory.

When the size of Xorshift128 seed (`sizeof(uint32)*4`) is bigger than
ISTRUCT_DATA_SIZE, `Random` uses Xorshift96 instead.
parent a57a9bce
......@@ -9,17 +9,19 @@
#include <mruby/class.h>
#include <mruby/data.h>
#include <mruby/array.h>
#include <mruby/istruct.h>
#if INT32_MAX <= INTPTR_MAX
# define XORSHIFT96
# define NSEEDS 3
#else
# define NSEEDS 4
#endif
#define LASTSEED (NSEEDS-1)
#include <time.h>
static char const RAND_STATE_KEY[] = "$mrb_i_rand_state";
static const struct mrb_data_type rand_state_type = {
RAND_STATE_KEY, mrb_free,
};
typedef struct rand_state {
uint32_t seed[4];
uint32_t seed[NSEEDS];
} rand_state;
static void
......@@ -28,18 +30,39 @@ rand_init(rand_state *t)
t->seed[0] = 123456789;
t->seed[1] = 362436069;
t->seed[2] = 521288629;
#ifndef XORSHIFT96
t->seed[3] = 88675123;
#endif
}
static uint32_t
rand_seed(rand_state *t, uint32_t seed)
{
uint32_t old_seed = t->seed[3];
uint32_t old_seed = t->seed[LASTSEED];
rand_init(t);
t->seed[3] = seed;
t->seed[LASTSEED] = seed;
return old_seed;
}
#ifdef XORSHIFT96
static uint32_t
rand_uint32(rand_state *state)
{
uint32_t *seed = state->seed;
uint32_t x = seed[0];
uint32_t y = seed[1];
uint32_t z = seed[2];
uint32_t t;
t = (x ^ (x << 3)) ^ (y ^ (y >> 19)) ^ (z ^ (z << 6));
x = y; y = z; z = t;
seed[0] = x;
seed[1] = y;
seed[2] = z;
return z;
}
#else /* XORSHIFT96 */
static uint32_t
rand_uint32(rand_state *state)
{
......@@ -60,6 +83,7 @@ rand_uint32(rand_state *state)
return w;
}
#endif /* XORSHIFT96 */
#ifndef MRB_WITHOUT_FLOAT
static double
......@@ -109,19 +133,26 @@ get_opt(mrb_state* mrb)
return arg;
}
static void
random_check(mrb_state *mrb, mrb_value random) {
struct RClass *c = mrb_class_get(mrb, "Random");
if (!mrb_obj_is_kind_of(mrb, random, c) || mrb_type(random) != MRB_TT_ISTRUCT) {
mrb_raise(mrb, E_TYPE_ERROR, "Random instance required");
}
}
static mrb_value
random_default(mrb_state *mrb) {
return mrb_const_get(mrb,
mrb_obj_value(mrb_class_get(mrb, "Random")),
mrb_intern_lit(mrb, "DEFAULT"));
struct RClass *c = mrb_class_get(mrb, "Random");
mrb_value d = mrb_const_get(mrb, mrb_obj_value(c), mrb_intern_lit(mrb, "DEFAULT"));
if (!mrb_obj_is_kind_of(mrb, d, c)) {
mrb_raise(mrb, E_TYPE_ERROR, "Random::DEFAULT replaced");
}
return d;
}
static rand_state *
random_state(mrb_state *mrb)
{
mrb_value random_val = random_default(mrb);
return DATA_GET_PTR(mrb, random_val, &rand_state_type, rand_state);
}
#define random_ptr(v) (rand_state*)mrb_istruct_ptr(v)
#define random_default_state(mrb) random_ptr(random_default(mrb))
static mrb_value
random_m_init(mrb_state *mrb, mrb_value self)
......@@ -131,11 +162,7 @@ random_m_init(mrb_state *mrb, mrb_value self)
seed = get_opt(mrb);
/* avoid memory leaks */
t = (rand_state*)DATA_PTR(self);
if (t == NULL) {
t = (rand_state *)mrb_malloc(mrb, sizeof(rand_state));
mrb_data_init(self, t, &rand_state_type);
}
t = random_ptr(self);
if (mrb_nil_p(seed)) {
rand_init(t);
}
......@@ -150,7 +177,7 @@ static mrb_value
random_m_rand(mrb_state *mrb, mrb_value self)
{
mrb_value max;
rand_state *t = DATA_GET_PTR(mrb, self, &rand_state_type, rand_state);
rand_state *t = random_ptr(self);
max = get_opt(mrb);
return random_rand(mrb, t, max);
......@@ -162,7 +189,7 @@ random_m_srand(mrb_state *mrb, mrb_value self)
uint32_t seed;
uint32_t old_seed;
mrb_value sv;
rand_state *t = DATA_GET_PTR(mrb, self, &rand_state_type, rand_state);
rand_state *t = random_ptr(self);
sv = get_opt(mrb);
if (mrb_nil_p(sv)) {
......@@ -188,13 +215,18 @@ mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary)
{
mrb_int i;
mrb_value max;
rand_state *random = NULL;
mrb_value r = mrb_nil_value();
rand_state *random;
if (RARRAY_LEN(ary) > 1) {
mrb_get_args(mrb, "|d", &random, &rand_state_type);
mrb_get_args(mrb, "|o", &r);
if (random == NULL) {
random = random_state(mrb);
if (mrb_nil_p(r)) {
random = random_default_state(mrb);
}
else {
random_check(mrb, r);
random = random_ptr(r);
}
mrb_ary_modify(mrb, mrb_ary_ptr(ary));
max = mrb_fixnum_value(RARRAY_LEN(ary));
......@@ -250,12 +282,17 @@ mrb_ary_sample(mrb_state *mrb, mrb_value ary)
{
mrb_int n = 0;
mrb_bool given;
rand_state *random = NULL;
mrb_value r = mrb_nil_value();
rand_state *random;
mrb_int len;
mrb_get_args(mrb, "|i?d", &n, &given, &random, &rand_state_type);
if (random == NULL) {
random = random_state(mrb);
mrb_get_args(mrb, "|i?o", &n, &given, &r);
if (mrb_nil_p(r)) {
random = random_default_state(mrb);
}
else {
random_check(mrb, r);
random = random_ptr(r);
}
len = RARRAY_LEN(ary);
if (!given) { /* pick one element */
......@@ -301,7 +338,7 @@ mrb_ary_sample(mrb_state *mrb, mrb_value ary)
static mrb_value
random_f_rand(mrb_state *mrb, mrb_value self)
{
rand_state *t = random_state(mrb);
rand_state *t = random_default_state(mrb);
return random_rand(mrb, t, get_opt(mrb));
}
......@@ -318,11 +355,13 @@ void mrb_mruby_random_gem_init(mrb_state *mrb)
struct RClass *random;
struct RClass *array = mrb->array_class;
mrb_assert(sizeof(rand_state) < ISTRUCT_DATA_SIZE);
mrb_define_method(mrb, mrb->kernel_module, "rand", random_f_rand, MRB_ARGS_OPT(1));
mrb_define_method(mrb, mrb->kernel_module, "srand", random_f_srand, MRB_ARGS_OPT(1));
random = mrb_define_class(mrb, "Random", mrb->object_class);
MRB_SET_INSTANCE_TT(random, MRB_TT_DATA);
MRB_SET_INSTANCE_TT(random, MRB_TT_ISTRUCT);
mrb_define_class_method(mrb, random, "rand", random_f_rand, MRB_ARGS_OPT(1));
mrb_define_class_method(mrb, random, "srand", random_f_srand, MRB_ARGS_OPT(1));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment