From 9c259c501a619d7e1ca34f5092fbbe4e6c9d40b9 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Wed, 11 Jun 2014 01:00:35 +0900 Subject: Add NOFREE macros --- src/string.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/string.c b/src/string.c index 734bf739a..6d2799270 100644 --- a/src/string.c +++ b/src/string.c @@ -106,7 +106,7 @@ mrb_str_modify(mrb_state *mrb, struct RString *s) RSTR_UNSET_SHARED_FLAG(s); return; } - if (s->flags & MRB_STR_NOFREE) { + if (RSTR_NOFREE_P(s)) { char *p = s->as.heap.ptr; s->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)s->as.heap.len+1); @@ -115,7 +115,7 @@ mrb_str_modify(mrb_state *mrb, struct RString *s) } RSTR_PTR(s)[s->as.heap.len] = '\0'; s->as.heap.aux.capa = s->as.heap.len; - s->flags &= ~MRB_STR_NOFREE; + RSTR_UNSET_NOFREE_FLAG(s); return; } } @@ -302,7 +302,7 @@ mrb_gc_free_str(mrb_state *mrb, struct RString *str) /* no code */; else if (RSTR_SHARED_P(str)) str_decref(mrb, str->as.heap.aux.shared); - else if ((str->flags & MRB_STR_NOFREE) == 0) + else if (RSTR_NOFREE_P(str) == 0) mrb_free(mrb, str->as.heap.ptr); } @@ -340,10 +340,10 @@ str_make_shared(mrb_state *mrb, struct RString *s) shared->nofree = FALSE; shared->ptr = s->as.heap.ptr; } - else if (s->flags & MRB_STR_NOFREE) { + else if (RSTR_NOFREE_P(s)) { shared->nofree = TRUE; shared->ptr = s->as.heap.ptr; - s->flags &= ~MRB_STR_NOFREE; + RSTR_UNSET_NOFREE_FLAG(s); } else { shared->nofree = FALSE; @@ -1373,7 +1373,7 @@ str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2) if (RSTR_SHARED_P(s1)) { str_decref(mrb, s1->as.heap.aux.shared); } - else if (!RSTR_EMBED_P(s1) && !(s1->flags & MRB_STR_NOFREE)) { + else if (!RSTR_EMBED_P(s1) && !RSTR_NOFREE_P(s1)) { mrb_free(mrb, s1->as.heap.ptr); } RSTR_UNSET_EMBED_FLAG(s1); -- cgit v1.2.3 From 75d1412410b337293e884f4906bf743364d56e25 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Wed, 11 Jun 2014 23:09:18 +0900 Subject: Refactor how to use RSTR_NOFREE_P macro --- src/string.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/string.c b/src/string.c index 6d2799270..92a34e70f 100644 --- a/src/string.c +++ b/src/string.c @@ -302,7 +302,7 @@ mrb_gc_free_str(mrb_state *mrb, struct RString *str) /* no code */; else if (RSTR_SHARED_P(str)) str_decref(mrb, str->as.heap.aux.shared); - else if (RSTR_NOFREE_P(str) == 0) + else if (!RSTR_NOFREE_P(str)) mrb_free(mrb, str->as.heap.ptr); } -- cgit v1.2.3 From bd53818999f882e7509c797b7a9be4decef42c81 Mon Sep 17 00:00:00 2001 From: ksss Date: Wed, 11 Jun 2014 14:42:18 +0000 Subject: str_replace: self should not be shared and nofree --- src/string.c | 18 +++++++++++------- test/t/string.rb | 7 +++++++ 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/string.c b/src/string.c index 6d2799270..56fdf7b4c 100644 --- a/src/string.c +++ b/src/string.c @@ -1368,14 +1368,17 @@ str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2) long len; len = RSTR_LEN(s2); + if (RSTR_SHARED_P(s1)) { + str_decref(mrb, s1->as.heap.aux.shared); + } + else if (!RSTR_EMBED_P(s1) && !RSTR_NOFREE_P(s1)) { + mrb_free(mrb, s1->as.heap.ptr); + } + + RSTR_UNSET_NOFREE_FLAG(s1); + if (RSTR_SHARED_P(s2)) { - L_SHARE: - if (RSTR_SHARED_P(s1)) { - str_decref(mrb, s1->as.heap.aux.shared); - } - else if (!RSTR_EMBED_P(s1) && !RSTR_NOFREE_P(s1)) { - mrb_free(mrb, s1->as.heap.ptr); - } +L_SHARE: RSTR_UNSET_EMBED_FLAG(s1); s1->as.heap.ptr = s2->as.heap.ptr; s1->as.heap.len = len; @@ -1385,6 +1388,7 @@ str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2) } else { if (len <= RSTRING_EMBED_LEN_MAX) { + RSTR_UNSET_SHARED_FLAG(s1); RSTR_SET_EMBED_FLAG(s1); memcpy(s1->as.ary, RSTR_PTR(s2), len); RSTR_SET_EMBED_LEN(s1, len); diff --git a/test/t/string.rb b/test/t/string.rb index 5ecb51530..00e98f671 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -320,6 +320,13 @@ assert('String#replace', '15.2.10.5.28') do b.replace(c); c.replace(b); assert_equal c, b + + # shared string + s = "foo" * 100 + a = s[10, 90] # create shared string + assert_equal("", s.replace("")) # clear + assert_equal("", s) # s is cleared + assert_not_equal("", a) # a should not be affected end assert('String#reverse', '15.2.10.5.29') do -- cgit v1.2.3