Commit 0debca9b authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #500 from monaka/pr-avoid-memcpy-on-copying-structure

Avoid memcpy() on copying structures.
parents 7fe6d00f d5c44390
......@@ -63,6 +63,17 @@ mrb_ary_new(mrb_state *mrb)
return mrb_ary_new_capa(mrb, 0);
}
static inline void
array_copy(mrb_value *dst, const mrb_value *src, size_t size)
{
int i;
for (i = 0; i < size; i++) {
dst[i] = src[i];
}
}
mrb_value
mrb_ary_new_from_values(mrb_state *mrb, int size, mrb_value *vals)
{
......@@ -71,7 +82,7 @@ mrb_ary_new_from_values(mrb_state *mrb, int size, mrb_value *vals)
ary = mrb_ary_new_capa(mrb, size);
a = mrb_ary_ptr(ary);
memcpy(a->ptr, vals, sizeof(mrb_value)*size);
array_copy(a->ptr, vals, size);
a->len = size;
return ary;
......@@ -115,7 +126,7 @@ ary_modify(mrb_state *mrb, struct RArray *a)
len = a->len * sizeof(mrb_value);
ptr = (mrb_value *)mrb_malloc(mrb, len);
if (p) {
memcpy(ptr, p, len);
array_copy(ptr, p, a->len);
}
a->ptr = ptr;
a->aux.capa = a->len;
......@@ -213,7 +224,7 @@ ary_concat(mrb_state *mrb, struct RArray *a, mrb_value *ptr, int blen)
ary_modify(mrb, a);
if (a->aux.capa < len) ary_expand_capa(mrb, a, len);
memcpy(a->ptr+a->len, ptr, sizeof(mrb_value)*blen);
array_copy(a->ptr+a->len, ptr, blen);
mrb_write_barrier(mrb, (struct RBasic*)a);
a->len = len;
}
......@@ -249,8 +260,8 @@ mrb_ary_plus(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "a", &ptr, &blen);
ary = mrb_ary_new_capa(mrb, a1->len + blen);
a2 = mrb_ary_ptr(ary);
memcpy(a2->ptr, a1->ptr, sizeof(mrb_value)*a1->len);
memcpy(a2->ptr + a1->len, ptr, sizeof(mrb_value)*blen);
array_copy(a2->ptr, a1->ptr, a1->len);
array_copy(a2->ptr + a1->len, ptr, blen);
a2->len = a1->len + blen;
return ary;
......@@ -309,7 +320,7 @@ ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, int len)
ary_modify(mrb, a);
if (a->aux.capa < len)
ary_expand_capa(mrb, a, len);
memcpy(a->ptr, argv, sizeof(mrb_value)*len);
array_copy(a->ptr, argv, len);
mrb_write_barrier(mrb, (struct RBasic*)a);
a->len = len;
}
......@@ -352,7 +363,7 @@ mrb_ary_times(mrb_state *mrb, mrb_value self)
a2 = mrb_ary_ptr(ary);
ptr = a2->ptr;
while(times--) {
memcpy(ptr, a1->ptr, sizeof(mrb_value)*(a1->len));
array_copy(ptr, a1->ptr, a1->len);
ptr += a1->len;
a2->len += a1->len;
}
......@@ -410,7 +421,7 @@ mrb_ary_new4(mrb_state *mrb, int n, const mrb_value *elts)
ary = mrb_ary_new_capa(mrb, n);
if (n > 0 && elts) {
memcpy(RARRAY_PTR(ary), elts, sizeof(mrb_value)*n);
array_copy(RARRAY_PTR(ary), elts, n);
RARRAY_LEN(ary) = n;
}
......@@ -540,7 +551,7 @@ mrb_ary_unshift_m(mrb_state *mrb, mrb_value self)
ary_expand_capa(mrb, a, a->len + len);
memmove(a->ptr + len, a->ptr, sizeof(mrb_value)*a->len);
}
memcpy(a->ptr, vals, sizeof(mrb_value)*len);
array_copy(a->ptr, vals, len);
a->len += len;
mrb_write_barrier(mrb, (struct RBasic*)a);
......
......@@ -38,6 +38,16 @@
#define STACK_INIT_SIZE 128
#define CALLINFO_INIT_SIZE 32
static inline void
stack_copy(mrb_value *dst, const mrb_value *src, size_t size)
{
int i;
for (i = 0; i < size; i++) {
dst[i] = src[i];
}
}
static void
stack_init(mrb_state *mrb)
{
......@@ -170,7 +180,7 @@ cipop(mrb_state *mrb)
mrb_value *p = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value)*len);
e->cioff = -1;
memcpy(p, e->stack, sizeof(mrb_value)*len);
stack_copy(p, e->stack, len);
e->stack = p;
}
......@@ -296,10 +306,10 @@ mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, int argc, mr
mrb->stack[0] = self;
if (undef) {
mrb->stack[1] = mrb_symbol_value(undef);
memcpy(mrb->stack+2, argv, sizeof(mrb_value)*(argc-1));
stack_copy(mrb->stack+2, argv, argc-1);
}
else if (argc > 0) {
memcpy(mrb->stack+1, argv, sizeof(mrb_value)*argc);
stack_copy(mrb->stack+1, argv, argc);
}
mrb->stack[argc+1] = blk;
......@@ -351,7 +361,7 @@ mrb_yield_internal(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv, mrb_v
stack_extend(mrb, ci->nregs, 0);
mrb->stack[0] = self;
if (argc > 0) {
memcpy(mrb->stack+1, argv, sizeof(mrb_value)*argc);
stack_copy(mrb->stack+1, argv, argc);
}
mrb->stack[argc+1] = mrb_nil_value();
......@@ -989,12 +999,12 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self)
}
regs[a] = mrb_ary_new_capa(mrb, m1+len+m2);
rest = mrb_ary_ptr(regs[a]);
memcpy(rest->ptr, stack, sizeof(mrb_value)*m1);
stack_copy(rest->ptr, stack, m1);
if (len > 0) {
memcpy(rest->ptr+m1, pp, sizeof(mrb_value)*len);
stack_copy(rest->ptr+m1, pp, len);
}
if (m2 > 0) {
memcpy(rest->ptr+m1+len, stack+m1+1, sizeof(mrb_value)*m2);
stack_copy(rest->ptr+m1+len, stack+m1+1, m2);
}
rest->len = m1+len+m2;
}
......
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