From: Jérémie Astor Date: Wed, 6 Jan 2021 20:21:51 +0000 (+0100) Subject: :art: Improve global alloc X-Git-Tag: nightly~1053 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=5e3a63dc5aeafce5b03617063040fd2d7137425a;p=gwion.git :art: Improve global alloc --- diff --git a/include/import/item.h b/include/import/item.h index 5090ad05..1c9622e6 100644 --- a/include/import/item.h +++ b/include/import/item.h @@ -3,6 +3,6 @@ ANN m_int gwi_item_ini(const Gwi gwi, const m_str type, const m_str name); ANN2(1) m_int gwi_item_end(const Gwi gwi, const ae_flag flag, union value_data); -#define gwi_item_end(a, b, c) gwi_item_end(a, (const ae_flag)(b), (m_uint*const)c) +#define gwi_item_end(a, b, c) gwi_item_end(a, (const ae_flag)(b), c) ANN void ck_clean_item(MemPool, ImportCK*); #endif diff --git a/src/emit/emit.c b/src/emit/emit.c index 615b0afc..8b54391b 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -391,19 +391,14 @@ ANN static m_bool emit_symbol_builtin(const Emitter emit, const Symbol *data) { if(vflag(v, vflag_direct)) { const m_uint size = v->type->size; const Instr instr = emit_kind(emit, size, exp_getvar(prim_exp(data)), dotstatic); - instr->m_val = (m_uint)v->d.ptr; + instr->m_val = (m_uint)&v->d.ptr; } else { const m_uint size = v->type->size; const Instr instr = emit_kind(emit, size, exp_getvar(prim_exp(data)), regpushimm); - if((!exp_getvar(prim_exp(data)) && size == SZ_INT)) { - if(isa(v->type, emit->gwion->type[et_object]) > 0 || vflag(v, vflag_enum)) - instr->m_val = (m_uint)v->d.ptr; - else if(v->d.ptr) - instr->m_val = *(m_uint*)v->d.ptr; - } else { - assert(v->d.ptr); // instr->m_val = v->d.ptr; - memcpy(&instr->m_val, v->d.ptr, v->type->size); - } + if(v->type->size == SZ_FLOAT) + instr->f = v->d.fnum; + else + instr->m_val = v->d.num; instr->m_val2 = size; } return GW_OK; @@ -786,13 +781,6 @@ ANN static m_bool emit_exp_decl_non_static(const Emitter emit, const Exp_Decl *d const Instr instr = emit_add_instr(emit, Reg2Reg); instr->m_val = -SZ_INT; } -/* - const size_t missing_depth = type->array_depth - (array ? array->depth : 0) - (decl->td->array ? decl->td->array->depth : 0); - if(missing_depth) { - const Instr push = emit_add_instr(emit, Reg2Reg); - push->m_val = -(missing_depth) * SZ_INT; - } -*/ } else if(struct_ctor(v)) emit_struct_decl_finish(emit, v->type, emit_addr); return GW_OK; @@ -809,21 +797,14 @@ ANN static m_bool emit_exp_decl_global(const Emitter emit, const Exp_Decl *decl, if(is_obj && (is_array || !is_ref)) CHECK_BB(emit_instantiate_decl(emit, type, decl->td, array, is_ref)) const Instr instr = emit_kind(emit, v->type->size, !struct_ctor(v) ? emit_addr : 1, dotstatic); - v->d.ptr = mp_calloc2(emit->gwion->mp, v->type->size); - if(isa(type, emit->gwion->type[et_union]) < 0) - set_vflag(v, vflag_direct);// mpalloc - instr->m_val = (m_uint)v->d.ptr; + if(type->size > SZ_INT) //{ + v->d.ptr = mp_calloc2(emit->gwion->mp, v->type->size); + instr->m_val = (m_uint)&v->d.ptr; + set_vflag(v, vflag_direct);// mpalloc instr->m_val2 = v->type->size; if(is_obj && (is_array || !is_ref || emit_addr)) { const Instr assign = emit_add_instr(emit, Assign); assign->m_val = emit_var; -/* - const size_t missing_depth = type->array_depth - (array ? array->depth : 0) - (decl->td->array ? decl->td->array->depth : 0); - if(missing_depth) { - const Instr push = emit_add_instr(emit, Reg2Reg); - push->m_val = -(missing_depth) * SZ_INT; - } -*/ (void)emit_object_addref(emit, -SZ_INT, emit_var); } else if(struct_ctor(v)) emit_struct_decl_finish(emit, v->type, emit_addr); diff --git a/src/env/value.c b/src/env/value.c index fc52ef7c..eb34932e 100644 --- a/src/env/value.c +++ b/src/env/value.c @@ -4,17 +4,12 @@ #include "vm.h" #include "gwion.h" +#define MAX(a, b) (a >= b ? a : b) ANN void free_value(Value a, Gwion gwion) { const Type t = a->type; -if(!vflag(a, vflag_enum)) { - if(!vflag(a, vflag_func) && a->d.ptr && !vflag(a, vflag_direct) && - !(vflag(a, vflag_builtin) && a->from->owner_class) - && isa(t, gwion->type[et_object]) < 0) - _mp_free(gwion->mp, t->size, a->d.ptr); - else if(vflag(a, vflag_freeme)) - xfree(a->d.ptr); -} - if(is_class(gwion, t)) + if(t->size > SZ_INT && !vflag(a, vflag_func) && a->d.ptr) + _mp_free(gwion->mp, t->size, a->d.ptr); + else if(is_class(gwion, t)) type_remref(t, gwion); mp_free(gwion->mp, ValueFrom, a->from); mp_free(gwion->mp, Value, a); diff --git a/src/lib/prim_values.c b/src/lib/prim_values.c index 9a49f76e..c5ab56d9 100644 --- a/src/lib/prim_values.c +++ b/src/lib/prim_values.c @@ -18,32 +18,24 @@ GWION_IMPORT(values) { VM* vm = gwi_vm(gwi); - ALLOC_PTR(gwi->gwion->mp, d_zero, m_float, 0.0); - ALLOC_PTR(gwi->gwion->mp, sr, m_float, (m_float)vm->bbq->si->sr); - ALLOC_PTR(gwi->gwion->mp, samp, m_float, 1.0); - ALLOC_PTR(gwi->gwion->mp, ms, m_float, (m_float)(*sr / 1000.)); - ALLOC_PTR(gwi->gwion->mp, second, m_float, (m_float)*sr); - ALLOC_PTR(gwi->gwion->mp, minute, m_float, (m_float)(*sr * 60.0)); - ALLOC_PTR(gwi->gwion->mp, hour, m_float, (m_float)(*minute * 60.0)); - ALLOC_PTR(gwi->gwion->mp, t_zero, m_float, 0.0); - ALLOC_PTR(gwi->gwion->mp, pi, m_float, (m_float)M_PI); + const m_float sr = vm->bbq->si->sr; gwi_item_ini(gwi, "float", "samplerate"); gwi_item_end(gwi, ae_flag_const, sr); gwi_item_ini(gwi, "float", "pi"); - gwi_item_end(gwi, ae_flag_const, pi); + gwi_item_end(gwi, ae_flag_const, (m_float)M_PI); gwi_item_ini(gwi, "dur", "d_zero"); - gwi_item_end(gwi, ae_flag_const, d_zero); + gwi_item_end(gwi, ae_flag_const, (m_float)0.0); gwi_item_ini(gwi, "dur", "samp"); - gwi_item_end(gwi, ae_flag_const, samp); + gwi_item_end(gwi, ae_flag_const, (m_float)1.0); gwi_item_ini(gwi, "dur", "ms"); - gwi_item_end(gwi, ae_flag_const, ms); + gwi_item_end(gwi, ae_flag_const, (m_float)(sr/1000.0)); gwi_item_ini(gwi, "dur", "second"); - gwi_item_end(gwi, ae_flag_const, second); + gwi_item_end(gwi, ae_flag_const, sr); gwi_item_ini(gwi, "dur", "minute"); - gwi_item_end(gwi, ae_flag_const, minute); + gwi_item_end(gwi, ae_flag_const, (m_float)(sr*60.0)); gwi_item_ini(gwi, "dur", "hour"); - gwi_item_end(gwi, ae_flag_const, hour); + gwi_item_end(gwi, ae_flag_const, (m_float)(sr*60*60)); gwi_item_ini(gwi, "time", "t_zero"); - gwi_item_end(gwi, ae_flag_const, t_zero); + gwi_item_end(gwi, ae_flag_const, (m_float)0.0); return GW_OK; } diff --git a/src/lib/string.c b/src/lib/string.c index d40cce24..c87854b3 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -101,13 +101,19 @@ static MFUN(string_ltrim) { static MFUN(string_rtrim) { const m_str str = STRING(o); - m_uint len = strlen(str) - 1; - while(str[len] == ' ') - len--; - char c[len + 2]; - strncpy(c, str, len + 1); - c[len + 1] = '\0'; - *(M_Object*)RETURN = new_string(shred->info->vm->gwion->mp, shred, c); + const size_t sz = strlen(str); + if(sz) { + m_uint len = strlen(str) - 1; + while(str[len] == ' ') + len--; + char c[len + 2]; + strncpy(c, str, len + 1); + c[len + 1] = '\0'; + *(M_Object*)RETURN = new_string(shred->info->vm->gwion->mp, shred, c); + } else { + ++o->ref; + *(M_Object*)RETURN = o; + } } static MFUN(string_trim) { diff --git a/tests/plug/global_var.c b/tests/plug/global_var.c index 1032deaf..e9487b60 100644 --- a/tests/plug/global_var.c +++ b/tests/plug/global_var.c @@ -11,13 +11,11 @@ GWION_IMPORT(global_var_test) { -// ALLOC_PTR(i, m_uint, 1); const M_Object obj = new_object(gwi->gwion->mp, NULL, gwi->gwion->type[et_string]); STRING(obj) = s_name(insert_symbol(gwi->gwion->st, "test")); GWI_BB(gwi_item_ini(gwi,"string", "i")) GWI_BB(gwi_item_end(gwi, 0, obj)) - ALLOC_PTR(gwi->gwion->mp, cf, m_float, 2.1); GWI_BB(gwi_item_ini(gwi,"float", "f")) - GWI_BB(gwi_item_end(gwi, 0, cf)) + GWI_BB(gwi_item_end(gwi, 0, (m_float)2.1)) return GW_OK; }