Skip to content

Commit

Permalink
Add rb_ary_reset
Browse files Browse the repository at this point in the history
rb_ary_reset will free heap allocated arrays and unshare shared arrays.
  • Loading branch information
peterzhu2118 committed Mar 11, 2022
1 parent 09186f3 commit 9a4bddd
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions array.c
Expand Up @@ -528,11 +528,14 @@ rb_ary_unshare(VALUE ary)
FL_UNSET_SHARED(ary);
}

static inline void
rb_ary_unshare_safe(VALUE ary)
static void
rb_ary_reset(VALUE ary)
{
if (ARY_SHARED_P(ary) && !ARY_EMBED_P(ary)) {
rb_ary_unshare(ary);
if (ARY_OWNS_HEAP_P(ary)) {
ary_heap_free(ary);
}
else if (ARY_SHARED_P(ary)) {
rb_ary_unshare(ary);
}
}

Expand Down Expand Up @@ -1075,10 +1078,7 @@ rb_ary_initialize(int argc, VALUE *argv, VALUE ary)

rb_ary_modify(ary);
if (argc == 0) {
if (ARY_OWNS_HEAP_P(ary) && ARY_HEAP_PTR(ary) != NULL) {
ary_heap_free(ary);
}
rb_ary_unshare_safe(ary);
rb_ary_reset(ary);
FL_SET_EMBED(ary);
ARY_SET_EMBED_LEN(ary, 0);
if (rb_block_given_p()) {
Expand Down Expand Up @@ -4391,12 +4391,7 @@ rb_ary_replace(VALUE copy, VALUE orig)
orig = to_ary(orig);
if (copy == orig) return copy;

if (ARY_OWNS_HEAP_P(copy)) {
ary_heap_free(copy);
}
else if (ARY_SHARED_P(copy)) {
rb_ary_unshare(copy);
}
rb_ary_reset(copy);

if (RARRAY_LEN(orig) <= RARRAY_EMBED_LEN_MAX) {
FL_SET_EMBED(copy);
Expand Down

0 comments on commit 9a4bddd

Please sign in to comment.