From: fennecdjay Date: Wed, 7 Aug 2019 20:46:08 +0000 (+0200) Subject: :art: Fixes some memory problems X-Git-Tag: nightly~2297 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=3f3fc5ed2304ebd1e8b6c3cc48fe4653582c0492;p=gwion.git :art: Fixes some memory problems --- diff --git a/include/cpy_ast.h b/include/cpy_ast.h index 54344060..eaf91151 100644 --- a/include/cpy_ast.h +++ b/include/cpy_ast.h @@ -3,4 +3,5 @@ ANN Func_Def cpy_func_def(MemPool, Func_Def); ANN struct Func_Base_* cpy_func_base(MemPool, struct Func_Base_*); ANN Class_Def cpy_class_def(MemPool, Class_Def); +ANN Type_List cpy_type_list(MemPool p, const Type_List src); #endif diff --git a/src/lib/object.c b/src/lib/object.c index d3e09a93..932ccff4 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -146,6 +146,8 @@ static OP_EMIT(opem_at_object) { static inline Type new_force_type(MemPool p, const Type t, const Symbol sym) { const Type ret = type_copy(p, t); + if(ret->nspc) + ADD_REF(ret->nspc) ret->name = s_name(sym); ret->flag = t->flag | ae_flag_force; map_set(&t->e->owner->info->type->map, (vtype)sym, (vtype)ret); diff --git a/src/lib/shred.c b/src/lib/shred.c index 9aa6783e..64409ed9 100644 --- a/src/lib/shred.c +++ b/src/lib/shred.c @@ -139,9 +139,12 @@ describe_path_and_dir(_code, s->code->name) static DTOR(shred_dtor) { VM *vm = ME(o)->info->vm; free_vm_shred(ME(o)); - if(isa(o->type_ref, t_fork) > 0) + if(isa(o->type_ref, t_fork) > 0) { +//gwion_end(vm->gwion); +// mp_free(shred->info->mp, Gwion, vm->gwion); free_vm(vm); } +} static MFUN(shred_lock) { MUTEX_LOCK(ME(o)->tick->shreduler->mutex); @@ -153,7 +156,7 @@ static MFUN(shred_unlock) { static DTOR(fork_dtor) { THREAD_JOIN(FORK_THREAD(o)); - mp_free(shred->info->mp, Gwion, ME(o)->info->vm->gwion); +// mp_free(shred->info->mp, Gwion, ME(o)->info->vm->gwion); } static MFUN(fork_join) { diff --git a/src/oo/env.c b/src/oo/env.c index 2550d820..e27284ce 100644 --- a/src/oo/env.c +++ b/src/oo/env.c @@ -93,10 +93,9 @@ ANN void env_add_type(const Env env, const Type type) { const Type v_type = type_copy(env->gwion->mp, t_class); v_type->e->d.base_type = type; SET_FLAG(type, builtin); - nspc_add_type(env->curr, insert_symbol(type->name), type); -// map_set(&env->curr->info->type->map, insert_symbol(type->name), type); -// map_set(vector_back(&env->curr->info->type->ptr), insert_symbol(type->name), type); - const Value v = new_value(env->gwion->mp, v_type, type->name); + const Symbol sym = insert_symbol(type->name); + map_set(&env->curr->info->type->map, (vtype)sym, (vtype)type); + const Value v = new_value(env->gwion->mp, v_type, s_name(sym)); SET_FLAG(v, checked | ae_flag_const | ae_flag_global | ae_flag_builtin); nspc_add_value(env->curr, insert_symbol(type->name), v); type->e->owner = env->curr; diff --git a/src/oo/nspc.c b/src/oo/nspc.c index 4ecba648..4da25de4 100644 --- a/src/oo/nspc.c +++ b/src/oo/nspc.c @@ -58,7 +58,6 @@ ANN static void free_nspc(Nspc a, Gwion gwion) { if(a->info->op_map.ptr) free_op_map(&a->info->op_map, gwion); nspc_free_type(a, gwion); - if(a->info->class_data) mp_free2(gwion->mp, a->info->class_data_size, a->info->class_data); if(a->info->vtable.ptr) diff --git a/src/parse/check.c b/src/parse/check.c index fa986be0..cbedfca5 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -220,11 +220,17 @@ ANN static Type check_exp_prim_this(const Env env, const Exp_Primary* primary) { ANN static Type prim_str(const Env env, Exp_Primary *const prim) { if(!prim->value) { const m_str str = prim->d.str; - const Value v = new_value(env->gwion->mp, t_string, str); char c[strlen(str) + 8]; sprintf(c, "%s:string", str); - nspc_add_value(env_nspc(env), insert_symbol(c), v); - prim->value = v; + const Symbol sym = insert_symbol(c); + const Value exist = nspc_lookup_value0(env->global_nspc, sym); + if(exist) + prim->value = exist; + else { + const Value v = new_value(env->gwion->mp, t_string, s_name(sym)); + nspc_add_value(env->global_nspc, sym, v); + prim->value = v; + } } return t_string; } @@ -480,7 +486,7 @@ ANN static m_bool check_func_args(const Env env, Arg_List arg_list) { } ANN static Func _find_template_match(const Env env, const Value v, const Exp_Call* exp) { -CHECK_BO(check_call(env, exp)) + CHECK_BO(check_call(env, exp)) const Type_List types = exp->tmpl->call; Func m_func = NULL, former = env->func; DECL_OO(const m_str, tmpl_name, = tl2str(env, types)) @@ -492,7 +498,7 @@ CHECK_BO(check_call(env, exp)) Func_Base *fbase = cpy_func_base(env->gwion->mp, base->base); fbase->xid = sym; fbase->tmpl->base = 0; - fbase->tmpl->call = types; + fbase->tmpl->call = cpy_type_list(env->gwion->mp, types); if(template_push_types(env, fbase->tmpl) > 0) { const Fptr_Def fptr = new_fptr_def(env->gwion->mp, fbase, base->flag); if(value) { @@ -528,7 +534,7 @@ CHECK_BO(check_call(env, exp)) continue; const Func_Def fdef = (Func_Def)cpy_func_def(env->gwion->mp, value->d.func_ref->def); SET_FLAG(fdef, template); - fdef->base->tmpl->call = types; + fdef->base->tmpl->call = cpy_type_list(env->gwion->mp, types); fdef->base->tmpl->base = i; if((m_func = ensure_tmpl(env, fdef, exp))) break; @@ -598,6 +604,7 @@ ANN static Func get_template_func(const Env env, const Exp_Call* func, const Val if(f) { Tmpl* tmpl = new_tmpl_call(env->gwion->mp, func->tmpl->call); tmpl->list = v->d.func_ref ? v->d.func_ref->def->base->tmpl->list : func->func->type->e->d.func->def->base->tmpl->list; +// tmpl->list = cpy_id_list(env->gwion->mp, tmpl->list); ((Exp_Call*)func)->tmpl = tmpl; return ((Exp_Call*)func)->m_func = f; } @@ -694,10 +701,6 @@ ANN Type check_exp_call1(const Env env, const Exp_Call *exp) { CHECK_BO(check_exp_call1_check(env, exp->func)) if(exp->func->type == t_lambda) return check_lambda_call(env, exp); - if(GET_FLAG(exp->func->type->e->d.func, ref)) { - const Value value = exp->func->type->e->d.func->value_ref; - CHECK_BO(traverse_class_def(env, value->owner_class->e->def)) - } if(exp->args) CHECK_OO(check_exp(env, exp->args)) if(GET_FLAG(exp->func->type, func)) @@ -941,6 +944,7 @@ ANN static m_bool do_stmt_auto(const Env env, const Stmt_Auto stmt) { ptr = type_decl_resolve(env, &td); if(!GET_FLAG(ptr, checked)) check_class_def(env, ptr->e->def); +// CHECK_BB(traverse_class_def(env, ptr->e->def)) } t = depth ? array_type(env, ptr, depth) : ptr; stmt->v = new_value(env->gwion->mp, t, s_name(stmt->sym)); diff --git a/src/parse/cpy_ast.c b/src/parse/cpy_ast.c index 0f455553..a92b6dc3 100644 --- a/src/parse/cpy_ast.c +++ b/src/parse/cpy_ast.c @@ -4,7 +4,7 @@ ANN static Stmt cpy_stmt(MemPool p, const Stmt src); ANN static Exp cpy_exp(MemPool p, const Exp src); ANN static ID_List cpy_id_list(MemPool p, const ID_List src); -ANN static Type_List cpy_type_list(MemPool p, const Type_List src); +ANN Type_List cpy_type_list(MemPool p, const Type_List src); ANN static Arg_List cpy_arg_list(MemPool p, const Arg_List src); ANN Class_Def cpy_class_def(MemPool p, const Class_Def src); ANN static Stmt_List cpy_stmt_list(MemPool p, const Stmt_List src); @@ -74,7 +74,7 @@ ANN static ID_List cpy_id_list(MemPool p, const ID_List src) { return a; } -ANN static Type_List cpy_type_list(MemPool p, const Type_List src) { +ANN Type_List cpy_type_list(MemPool p, const Type_List src) { Type_List a = mp_calloc(p, Type_List); if(src->td) a->td = cpy_type_decl(p, src->td); // 1 diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 4bee421d..fb63c5b4 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -14,16 +14,16 @@ static inline void add_type(const Env env, const Nspc nspc, const Type t) { map_set(&nspc->info->type->map, (m_uint)insert_symbol(t->name), (m_uint)t); -// map_set((Map)vector_front((Vector)&nspc->info->type->ptr), (m_uint)insert_symbol(t->name), (m_uint)t); } ANN static Value mk_class(const Env env, const Type base) { + const Symbol sym = insert_symbol(base->name); const Type t = type_copy(env->gwion->mp, t_class); - const Value v = new_value(env->gwion->mp, t, base->name); + const Value v = new_value(env->gwion->mp, t, s_name(sym)); t->e->d.base_type = base; v->owner = base->e->owner; SET_FLAG(v, const | ae_flag_checked); - nspc_add_value(base->e->owner, insert_symbol(base->name), v); + map_set(&base->e->owner->info->value->map, sym, v); return v; } @@ -114,6 +114,7 @@ ANN static m_bool typedef_complex(const Env env, const Type_Def tdef, const Type ANN static void typedef_fptr(const Env env, const Type_Def tdef, const Type base) { tdef->type = type_copy(env->gwion->mp, base); + ADD_REF(tdef->type->nspc) tdef->type->name = s_name(tdef->xid); tdef->type->e->parent = base; add_type(env, env->curr, tdef->type); @@ -208,15 +209,15 @@ ANN m_bool scan0_union_def(const Env env, const Union_Def udef) { } else { const Nspc nspc = !GET_FLAG(udef, global) ? env->curr : env->global_nspc; -// TODO make unique names - const Type t = union_type(env, nspc, insert_symbol("union"), 1); - udef->value = new_value(env->gwion->mp, t, "union"); + char name[7 + strlen(env->name) + 1]; // add pos + sprintf(name, "@union:%s", env->name); + const Symbol sym = insert_symbol(name); + const Type t = union_type(env, nspc, sym, 1); + udef->value = new_value(env->gwion->mp, t, s_name(sym)); udef->value->owner_class = env->class_def; udef->value->owner = nspc; nspc_add_value(nspc, udef->xid, udef->value); - char c[16]; - sprintf(c, "%p", udef); - nspc_add_type(nspc, insert_symbol(c), t); + add_type(env, nspc, t); SET_FLAG(udef->value, checked | udef->flag); SET_FLAG(t, scan1); } @@ -256,7 +257,8 @@ ANN static Type scan0_class_def_init(const Env env, const Class_Def cdef) { const Type t = new_type(env->gwion->mp, ++env->scope->type_xid, s_name(cdef->base.xid), t_object); t->e->owner = env->curr; t->nspc = new_nspc(env->gwion->mp, t->name); - t->nspc->parent = (GET_FLAG(cdef, global) ? env_nspc(env) : env->curr); +// t->nspc->parent = GET_FLAG(cdef, global) ? env_nspc(env) : env->curr; + t->nspc->parent = GET_FLAG(cdef, global) ? env->global_nspc : env->curr; t->e->def = cdef; t->flag = cdef->flag; if(!strstr(t->name, "<")) diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 66471747..d717fca9 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -86,7 +86,6 @@ ANN m_bool scan2_fptr_def(const Env env, const Fptr_Def fptr) { CHECK_BB(scan2_args(env, def)) } else SET_FLAG(fptr->type, func); - nspc_add_func(fptr->type->e->owner, fptr->base->xid, fptr->base->func); return GW_OK; } @@ -309,7 +308,7 @@ ANN static Type func_type(const Env env, const Func func) { ANN2(1,2) static Value func_value(const Env env, const Func f, const Value overload) { const Type t = func_type(env, f); - const Value v = new_value(env->gwion->mp, t, t->name); + const Value v = new_value(env->gwion->mp, t, s_name(insert_symbol(t->name))); CHECK_OO(scan2_func_assign(env, f->def, f, v)) if(!overload) { ADD_REF(v); diff --git a/src/parse/scanx.c b/src/parse/scanx.c index 512eeafc..3dc5882b 100644 --- a/src/parse/scanx.c +++ b/src/parse/scanx.c @@ -72,7 +72,7 @@ scanx_parent(const Type t, const _exp_func f, void* d) { ANN m_bool scanx_cdef(const Env env, void* opt, const Class_Def cdef, const _exp_func f_cdef, const _exp_func f_union) { - if(cdef->base.type->e->parent != t_union) + if(cdef->base.type && cdef->base.type->e->parent != t_union) return f_cdef(opt, cdef); CHECK_BB(template_push_types(env, cdef->base.tmpl)) const m_bool ret = f_union(opt, cdef->union_def); diff --git a/src/parse/template.c b/src/parse/template.c index d65916b5..d279925f 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -123,7 +123,7 @@ ANN static Class_Def template_class(const Env env, const Class_Def def, const Ty DECL_OO(const Symbol, name, = template_id(env, def, call)) if(env->class_def && name == insert_symbol(env->class_def->name)) return env->class_def->e->def; - const Type t = nspc_lookup_type1(env->curr, name); + const Type t = nspc_lookup_type0(env->curr, name); if(t) return t->e->def; const Class_Def c = cpy_class_def(env->gwion->mp, def); @@ -157,7 +157,7 @@ extern ANN m_bool traverse_class_def(const Env, const Class_Def); ANN Tmpl* mk_tmpl(const Env env, const Type t, const Tmpl *tm, const Type_List types) { Tmpl *tmpl = new_tmpl(env->gwion->mp, get_total_type_list(env, t, tm), 0); - tmpl->call = types; + tmpl->call = cpy_type_list(env->gwion->mp, types); return tmpl; } @@ -177,8 +177,7 @@ ANN Type scan_type(const Env env, const Type t, const Type_Decl* type) { a->base.tmpl = mk_tmpl(env, t, t->e->def->base.tmpl, type->types); if(t->e->parent != t_union) { CHECK_BO(scan0_class_def(env, a)) - map_set(&t->e->owner->info->type->map, (vtype)a->base.xid, (vtype)a->base.type); - map_set((Map)vector_front((Vector)&t->e->owner->info->type->ptr), (vtype)a->base.xid, (vtype)a->base.type); + map_set(&env->curr->info->type->map, (vtype)a->base.xid, (vtype)a->base.type); } else { a->union_def = new_union_def(env->gwion->mp, a->list, t->e->def->pos); a->union_def->type_xid = a->base.xid; @@ -202,10 +201,11 @@ ANN Type scan_type(const Env env, const Type t, const Type_Decl* type) { if(base_type) return base_type; const Type ret = type_copy(env->gwion->mp, t); + ADD_REF(ret->nspc) ret->e->parent = t; ret->name = s_name(sym); SET_FLAG(ret, func); - nspc_add_type(env->curr, sym, ret); + map_set(&t->e->owner->info->type->map, sym, ret); const Func_Def def = cpy_func_def(env->gwion->mp, t->e->d.func->def); const Func func = ret->e->d.func = new_func(env->gwion->mp, s_name(sym), def); const Value value = new_value(env->gwion->mp, ret, s_name(sym)); @@ -216,7 +216,7 @@ ANN Type scan_type(const Env env, const Type t, const Type_Decl* type) { func->value_ref = value; func->def->base->tmpl = mk_tmpl(env, t, t->e->d.func->def->base->tmpl, type->types); def->base->func = func; - nspc_add_value(env->curr, sym, value); + map_set(&t->e->owner->info->value->map, sym, value); // to base ? return ret; } ERR_O(type->xid->pos, diff --git a/src/parse/traverse.c b/src/parse/traverse.c index 4f9da80c..6f8a8507 100644 --- a/src/parse/traverse.c +++ b/src/parse/traverse.c @@ -58,6 +58,7 @@ ANN m_bool traverse_type_def(const Env env, const Type_Def def) { ANN m_bool traverse_class_def(const Env env, const Class_Def def) { if(!GET_FLAG(def, scan1)) CHECK_BB(scan1_class_def(env, def)) - CHECK_BB(scan2_class_def(env, def)) + if(!GET_FLAG(def, scan2)) + CHECK_BB(scan2_class_def(env, def)) return check_class_def(env, def); } diff --git a/src/parse/type_decl.c b/src/parse/type_decl.c index 78cd46d5..649ad590 100644 --- a/src/parse/type_decl.c +++ b/src/parse/type_decl.c @@ -25,6 +25,8 @@ ANN Type type_decl_resolve(const Env env, const Type_Decl* td) { if(exist) return exist; const Type t = type_copy(env->gwion->mp, ret); + if(t->nspc) + ADD_REF(t->nspc) t->name = s_name(sym); t->e->parent = ret; SET_FLAG(t, nonnull);