constify pointer from RARRAY_PTR to detect potential write barrier bugs.

if you see compiler errors due to this commit, you'd better to use array-modifying
functions, e.g. mrb_ary_set() or mrb_ary_push(), otherwise you might see nasty
GC bugs in the future.  if you are sure what you are doing, replace `RARRAY_PTR(ary)`
by `mrb_ary_ptr(ary)->ptr`. but be warned.
parent 7ea8803e
......@@ -32,7 +32,7 @@ struct RArray {
#define RARRAY(v) ((struct RArray*)(mrb_ptr(v)))
#define RARRAY_LEN(a) (RARRAY(a)->len)
#define RARRAY_PTR(a) (RARRAY(a)->ptr)
#define RARRAY_PTR(a) ((const mrb_value*)RARRAY(a)->ptr)
#define MRB_ARY_SHARED 256
#define ARY_SHARED_P(a) ((a)->flags & MRB_ARY_SHARED)
#define ARY_SET_SHARED_FLAG(a) ((a)->flags |= MRB_ARY_SHARED)
......
......@@ -221,8 +221,8 @@ mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary)
j = mrb_fixnum(mrb_random_mt_rand(mrb, random, mrb_fixnum_value(RARRAY_LEN(ary))));
tmp = RARRAY_PTR(ary)[i];
RARRAY_PTR(ary)[i] = RARRAY_PTR(ary)[j];
RARRAY_PTR(ary)[j] = tmp;
mrb_ary_ptr(ary)->ptr[i] = RARRAY_PTR(ary)[j];
mrb_ary_ptr(ary)->ptr[j] = tmp;
}
}
......@@ -305,11 +305,10 @@ mrb_ary_sample(mrb_state *mrb, mrb_value ary)
}
break;
}
RARRAY_PTR(result)[i] = mrb_fixnum_value(r);
RARRAY_LEN(result)++;
mrb_ary_push(mrb, result, mrb_fixnum_value(r));
}
for (i=0; i<n; i++) {
RARRAY_PTR(result)[i] = RARRAY_PTR(ary)[mrb_fixnum(RARRAY_PTR(result)[i])];
mrb_ary_set(mrb, result, i, RARRAY_PTR(ary)[mrb_fixnum(RARRAY_PTR(result)[i])]);
}
return result;
}
......
......@@ -14,8 +14,8 @@
#include "mruby/hash.h"
#include "mruby/range.h"
#define RSTRUCT_LEN(st) RARRAY_LEN(st)
#define RSTRUCT_PTR(st) RARRAY_PTR(st)
#define RSTRUCT_LEN(st) mrb_ary_ptr(st)->len
#define RSTRUCT_PTR(st) mrb_ary_ptr(st)->ptr
static struct RClass *
struct_class(mrb_state *mrb)
......@@ -72,15 +72,10 @@ static mrb_value
mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
{
mrb_value members, ary;
mrb_value *p, *pend;
members = mrb_struct_s_members(mrb, klass);
ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members));
p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members);
while (p < pend) {
mrb_ary_push(mrb, ary, *p);
p++;
}
mrb_ary_replace(mrb, ary, members);
return ary;
}
......@@ -106,7 +101,8 @@ mrb_struct_members_m(mrb_state *mrb, mrb_value obj)
static mrb_value
mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id)
{
mrb_value members, slot, *ptr, *ptr_members;
mrb_value members, slot, *ptr;
const mrb_value *ptr_members;
mrb_int i, len;
ptr = RSTRUCT_PTR(obj);
......@@ -181,7 +177,8 @@ mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val)
const char *name;
mrb_int i, len, slen;
mrb_sym mid;
mrb_value members, slot, *ptr, *ptr_members;
mrb_value members, slot, *ptr;
const mrb_value *ptr_members;
/* get base id */
name = mrb_sym2name_len(mrb, mrb->c->ci->mid, &slen);
......@@ -227,7 +224,7 @@ is_const_id(mrb_state *mrb, const char *name)
static void
make_struct_define_accessors(mrb_state *mrb, mrb_value members, struct RClass *c)
{
mrb_value *ptr_members = RARRAY_PTR(members);
const mrb_value *ptr_members = RARRAY_PTR(members);
mrb_int i;
mrb_int len = RARRAY_LEN(members);
int ai = mrb_gc_arena_save(mrb);
......@@ -359,7 +356,7 @@ mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
}
for (i=0; i<RARRAY_LEN(rest); i++) {
id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]);
RARRAY_PTR(rest)[i] = mrb_symbol_value(id);
mrb_ary_set(mrb, rest, i, mrb_symbol_value(id));
}
}
st = make_struct(mrb, name, rest, struct_class(mrb));
......@@ -420,7 +417,8 @@ inspect_struct(mrb_state *mrb, mrb_value s, mrb_bool recur)
{
const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s));
mrb_value members, str = mrb_str_new_lit(mrb, "#<struct ");
mrb_value *ptr, *ptr_members;
mrb_value *ptr;
const mrb_value *ptr_members;
mrb_int i, len;
if (cn) {
......@@ -506,7 +504,8 @@ mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
static mrb_value
struct_aref_sym(mrb_state *mrb, mrb_value s, mrb_sym id)
{
mrb_value *ptr, members, *ptr_members;
mrb_value *ptr, members;
const mrb_value *ptr_members;
mrb_int i, len;
ptr = RSTRUCT_PTR(s);
......@@ -579,7 +578,8 @@ mrb_struct_aref(mrb_state *mrb, mrb_value s)
static mrb_value
mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
{
mrb_value members, *ptr, *ptr_members;
mrb_value members, *ptr;
const mrb_value *ptr_members;
mrb_int i, len;
members = mrb_struct_members(mrb, s);
......
......@@ -574,7 +574,7 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val
{
struct RArray *a = mrb_ary_ptr(ary);
mrb_int tail, size;
mrb_value *argv;
const mrb_value *argv;
mrb_int i, argc;
ary_modify(mrb, a);
......
......@@ -698,12 +698,13 @@ mrb_hash_keys(mrb_state *mrb, mrb_value hash)
{
khash_t(ht) *h = RHASH_TBL(hash);
khiter_t k;
mrb_value ary, *p;
mrb_value ary;
mrb_value *p;
if (!h || kh_size(h) == 0) return mrb_ary_new(mrb);
ary = mrb_ary_new_capa(mrb, kh_size(h));
mrb_ary_set(mrb, ary, kh_size(h)-1, mrb_nil_value());
p = RARRAY_PTR(ary);
p = mrb_ary_ptr(ary)->ptr;
for (k = kh_begin(h); k != kh_end(h); k++) {
if (kh_exist(h, k)) {
mrb_value kv = kh_key(h, k);
......
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