mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
add macro ARY_SHARED_P
This commit is contained in:
+8
-7
@@ -15,6 +15,7 @@
|
||||
#define ARY_SHRINK_RATIO 5 /* must be larger than 2 */
|
||||
#define ARY_C_MAX_SIZE (SIZE_MAX / sizeof(mrb_value))
|
||||
#define ARY_MAX_SIZE ((ARY_C_MAX_SIZE < (size_t)MRB_INT_MAX) ? (mrb_int)ARY_C_MAX_SIZE : MRB_INT_MAX-1)
|
||||
#define ARY_SHARED_P(a) ((a)->flags & MRB_ARY_SHARED)
|
||||
|
||||
static inline mrb_value
|
||||
ary_elt(mrb_value ary, mrb_int offset)
|
||||
@@ -124,7 +125,7 @@ ary_fill_with_nil(mrb_value *ptr, mrb_int size)
|
||||
static void
|
||||
ary_modify(mrb_state *mrb, struct RArray *a)
|
||||
{
|
||||
if (a->flags & MRB_ARY_SHARED) {
|
||||
if (ARY_SHARED_P(a)) {
|
||||
mrb_shared_array *shared = a->aux.shared;
|
||||
|
||||
if (shared->refcnt == 1 && a->ptr == shared->ptr) {
|
||||
@@ -160,7 +161,7 @@ mrb_ary_modify(mrb_state *mrb, struct RArray* a)
|
||||
static void
|
||||
ary_make_shared(mrb_state *mrb, struct RArray *a)
|
||||
{
|
||||
if (!(a->flags & MRB_ARY_SHARED)) {
|
||||
if (!ARY_SHARED_P(a)) {
|
||||
mrb_shared_array *shared = (mrb_shared_array *)mrb_malloc(mrb, sizeof(mrb_shared_array));
|
||||
|
||||
shared->refcnt = 1;
|
||||
@@ -481,7 +482,7 @@ mrb_ary_shift(mrb_state *mrb, mrb_value self)
|
||||
mrb_value val;
|
||||
|
||||
if (a->len == 0) return mrb_nil_value();
|
||||
if (a->flags & MRB_ARY_SHARED) {
|
||||
if (ARY_SHARED_P(a)) {
|
||||
L_SHIFT:
|
||||
val = a->ptr[0];
|
||||
a->ptr++;
|
||||
@@ -515,7 +516,7 @@ mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
|
||||
if ((a->flags & MRB_ARY_SHARED)
|
||||
if (ARY_SHARED_P(a)
|
||||
&& a->aux.shared->refcnt == 1 /* shared only referenced from this array */
|
||||
&& a->ptr - a->aux.shared->ptr >= 1) /* there's room for unshifted item */ {
|
||||
a->ptr--;
|
||||
@@ -542,7 +543,7 @@ mrb_ary_unshift_m(mrb_state *mrb, mrb_value self)
|
||||
int len;
|
||||
|
||||
mrb_get_args(mrb, "*", &vals, &len);
|
||||
if ((a->flags & MRB_ARY_SHARED)
|
||||
if (ARY_SHARED_P(a)
|
||||
&& a->aux.shared->refcnt == 1 /* shared only referenced from this array */
|
||||
&& a->ptr - a->aux.shared->ptr >= len) /* there's room for unshifted item */ {
|
||||
a->ptr -= len;
|
||||
@@ -858,7 +859,7 @@ mrb_ary_first(mrb_state *mrb, mrb_value self)
|
||||
}
|
||||
|
||||
if (size > a->len) size = a->len;
|
||||
if (a->flags & MRB_ARY_SHARED) {
|
||||
if (ARY_SHARED_P(a)) {
|
||||
return ary_subseq(mrb, a, 0, size);
|
||||
}
|
||||
return mrb_ary_new_from_values(mrb, size, a->ptr);
|
||||
@@ -885,7 +886,7 @@ mrb_ary_last(mrb_state *mrb, mrb_value self)
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size");
|
||||
}
|
||||
if (size > a->len) size = a->len;
|
||||
if ((a->flags & MRB_ARY_SHARED) || size > ARY_DEFAULT_LEN) {
|
||||
if (ARY_SHARED_P(a) || size > ARY_DEFAULT_LEN) {
|
||||
return ary_subseq(mrb, a, a->len - size, size);
|
||||
}
|
||||
return mrb_ary_new_from_values(mrb, size, a->ptr + a->len - size);
|
||||
|
||||
Reference in New Issue
Block a user