Skip to content

Commit

Permalink
🎨 Fixes some memory problems
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Aug 7, 2019
1 parent 3c9d6e9 commit 3f3fc5e
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 39 deletions.
1 change: 1 addition & 0 deletions include/cpy_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/lib/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/lib/shred.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions src/oo/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/oo/nspc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 14 additions & 10 deletions src/parse/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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))
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/parse/cpy_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions src/parse/scan0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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, "<"))
Expand Down
3 changes: 1 addition & 2 deletions src/parse/scan2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/parse/scanx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/parse/template.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/parse/traverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions src/parse/type_decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3f3fc5e

Please sign in to comment.