avoid copying when the original string comes with MRB_STR_NOFREE

parent 9b7b18aa
......@@ -32,6 +32,9 @@ struct RString {
#define RSTRING_CAPA(s) (RSTRING(s)->aux.capa)
#define RSTRING_END(s) (RSTRING(s)->ptr + RSTRING(s)->len)
#define MRB_STR_SHARED 1
#define MRB_STR_NOFREE 2
void mrb_gc_free_str(mrb_state*, struct RString*);
void mrb_str_modify(mrb_state*, struct RString*);
mrb_value mrb_str_literal(mrb_state*, mrb_value);
......
......@@ -136,7 +136,9 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
mrb_free(mrb, irep->iseq);
for (i=0; i<irep->plen; i++) {
if (mrb_type(irep->pool[i]) == MRB_TT_STRING) {
if (mrb_str_ptr(irep->pool[i])->flags & MRB_STR_NOFREE == 0) {
mrb_free(mrb, mrb_str_ptr(irep->pool[i])->ptr);
}
mrb_free(mrb, mrb_obj_ptr(irep->pool[i]));
}
#ifdef MRB_WORD_BOXING
......@@ -170,12 +172,17 @@ mrb_str_pool(mrb_state *mrb, mrb_value str)
len = s->len;
ns->len = len;
if (s->flags & MRB_STR_NOFREE) {
ns->ptr = s->ptr;
ns->flags = MRB_STR_NOFREE;
}
else {
ns->ptr = (char *)mrb_malloc(mrb, (size_t)len+1);
if (s->ptr) {
memcpy(ns->ptr, s->ptr, len);
}
ns->ptr[len] = '\0';
}
return mrb_obj_value(ns);
}
......
......@@ -30,9 +30,6 @@ typedef struct mrb_shared_string {
mrb_int len;
} mrb_shared_string;
#define MRB_STR_SHARED 1
#define MRB_STR_NOFREE 2
static mrb_value str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2);
static mrb_value mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
......
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