summaryrefslogtreecommitdiffhomepage
path: root/src/state.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/state.c')
-rw-r--r--src/state.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/state.c b/src/state.c
index 48e75c817..f05dbda34 100644
--- a/src/state.c
+++ b/src/state.c
@@ -135,8 +135,8 @@ 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])->as.heap.ptr);
+ if ((mrb_str_ptr(irep->pool[i])->flags & (MRB_STR_NOFREE|MRB_STR_EMBED)) == 0) {
+ mrb_free(mrb, RSTRING_PTR(irep->pool[i]));
}
mrb_free(mrb, mrb_obj_ptr(irep->pool[i]));
}
@@ -163,13 +163,17 @@ mrb_str_pool(mrb_state *mrb, mrb_value str)
{
struct RString *s = mrb_str_ptr(str);
struct RString *ns;
+ char *ptr;
mrb_int len;
ns = (struct RString *)mrb_malloc(mrb, sizeof(struct RString));
ns->tt = MRB_TT_STRING;
ns->c = mrb->string_class;
- len = s->as.heap.len;
+ if (s->flags & MRB_STR_EMBED)
+ len = (mrb_int)((s->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT);
+ else
+ len = s->as.heap.len;
ns->as.heap.len = len;
if (s->flags & MRB_STR_NOFREE) {
ns->as.heap.ptr = s->as.heap.ptr;
@@ -178,8 +182,9 @@ mrb_str_pool(mrb_state *mrb, mrb_value str)
else {
ns->flags = 0;
ns->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)len+1);
- if (s->as.heap.ptr) {
- memcpy(ns->as.heap.ptr, s->as.heap.ptr, len);
+ ptr = (s->flags & MRB_STR_EMBED) ? s->as.ary : s->as.heap.ptr;
+ if (ptr) {
+ memcpy(ns->as.heap.ptr, ptr, len);
}
ns->as.heap.ptr[len] = '\0';
}