From: fennecdjay Date: Sun, 19 May 2019 00:52:11 +0000 (+0200) Subject: :art: Rename VM_Object => RefCount X-Git-Tag: nightly~2453^2~3 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=6b108ebe290c591eb0016730952b9f3987a9a074;p=gwion.git :art: Rename VM_Object => RefCount --- diff --git a/include/nspc.h b/include/nspc.h index 8ef12eaa..f0cd52fa 100644 --- a/include/nspc.h +++ b/include/nspc.h @@ -17,8 +17,8 @@ struct Nspc_ { struct VM_Code_* pre_ctor; struct VM_Code_* dtor; struct NspcInfo_* info; - uint ref; HAS_OBJ + uint is_union; }; extern ANEW ANN Nspc new_nspc(MemPool p, const m_str name); diff --git a/include/oo.h b/include/oo.h index b2af7536..6ca6a63b 100644 --- a/include/oo.h +++ b/include/oo.h @@ -6,19 +6,25 @@ typedef struct Nspc_ * Nspc; typedef struct Value_ * Value; typedef struct Func_ * Func; -struct VM_Object_ { +typedef struct RefCount_ { void (*free)(void*,void*); - uint16_t ref_count; // could be an unsigned short -}; + uint16_t count; // could be an unsigned short +} RefCount; -#define HAS_OBJ struct VM_Object_* obj; -#define INIT_OO(mp, a, b) { (a)->obj = mp_alloc(mp, VM_Object); (a)->obj->ref_count = 1; (a)->obj->free= (void(*)(void*,void*))b; } -ANN static inline void rem_ref(MemPool mp, struct VM_Object_* a, void* ptr, void *gwion) { - if(--a->ref_count) +#define HAS_OBJ RefCount* ref; +ANN static inline RefCount* new_refcount(MemPool mp, void(*free)(void*,void*)) { + RefCount *ref = mp_alloc(mp, RefCount); + ref->count = 1; + ref->free= free; + return ref; +} +#define new_refcount(a, b) new_refcount(a, (void(*)(void*,void*))b) +ANN static inline void rem_ref(MemPool mp, RefCount* a, void* ptr, void *gwion) { + if(--a->count) return; a->free(ptr, gwion); - mp_free(mp, VM_Object, a); + mp_free(mp, RefCount, a); } -#define ADD_REF(a) { ++(a)->obj->ref_count; } -#define REM_REF(a, b) { rem_ref(((Gwion)(b))->mp, (a)->obj, (a), (b)); } +#define ADD_REF(a) { ++(a)->ref->count; } +#define REM_REF(a, b) { rem_ref(((Gwion)(b))->mp, (a)->ref, (a), (b)); } #endif diff --git a/src/oo/context.c b/src/oo/context.c index 7d7aca38..b940aa90 100644 --- a/src/oo/context.c +++ b/src/oo/context.c @@ -18,7 +18,7 @@ ANN2(2) Context new_context(MemPool p, const Ast ast, const m_str str) { context->nspc = new_nspc(p, str); context->tree = ast; context->name = str; - INIT_OO(p, context, free_context); + context->ref = new_refcount(p, free_context); return context; } diff --git a/src/oo/nspc.c b/src/oo/nspc.c index 16cb8673..9d8477a8 100644 --- a/src/oo/nspc.c +++ b/src/oo/nspc.c @@ -30,7 +30,7 @@ ANN static inline void nspc_release_object(const Nspc a, Value value, Gwion gwio ANN static void free_nspc_value(const Nspc a, Gwion gwion) { struct scope_iter iter = { a->info->value, 0, 0 }; Value v; - if(!a->ref) { + if(!a->is_union) { while(scope_iter(&iter, &v) > 0) { if(isa(v->type, t_object) > 0) nspc_release_object(a, v, gwion); @@ -78,6 +78,6 @@ ANN Nspc new_nspc(MemPool p, const m_str name) { a->info->value = new_scope(p); a->info->type = new_scope(p); a->info->func = new_scope(p); - INIT_OO(p, a, free_nspc); + a->ref = new_refcount(p, free_nspc); return a; } diff --git a/src/oo/type.c b/src/oo/type.c index a31d611a..3c0071a1 100644 --- a/src/oo/type.c +++ b/src/oo/type.c @@ -25,7 +25,7 @@ Type new_type(MemPool p, const m_uint xid, const m_str name, const Type parent) type->e->parent = parent; if(type->e->parent) type->size = parent->size; - INIT_OO(p, type, free_type); + type->ref = new_refcount(p, free_type); return type; } diff --git a/src/oo/value.c b/src/oo/value.c index 7da32239..07a0f949 100644 --- a/src/oo/value.c +++ b/src/oo/value.c @@ -22,6 +22,6 @@ ANN Value new_value(MemPool p, const Type type, const m_str name) { const Value a = mp_alloc(p, Value); a->type = type; a->name = name; - INIT_OO(p, a, free_value); + a->ref = new_refcount(p, free_value); return a; } diff --git a/src/parse/func.c b/src/parse/func.c index 40871c2f..0ee82dd0 100644 --- a/src/parse/func.c +++ b/src/parse/func.c @@ -25,7 +25,7 @@ ANN Func new_func(MemPool p, const m_str name, const Func_Def def) { Func func = mp_alloc(p, Func); func->name = name; func->def = def; - INIT_OO(p, func, free_func); + func->ref = new_refcount(p, free_func); return func; } diff --git a/src/parse/scan0.c b/src/parse/scan0.c index dfdf7803..9c8ebbdf 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -100,7 +100,7 @@ ANN static Type union_type(const Env env, const Nspc nspc, const Symbol s, const t->name = name; t->nspc = new_nspc(env->gwion->mp, name); t->nspc->parent = nspc; - t->nspc->ref = 1; + t->nspc->is_union = 1; t->e->owner = nspc; t->e->parent = t_union; if(add) { diff --git a/src/vm/vm_code.c b/src/vm/vm_code.c index 802c981c..092caadb 100644 --- a/src/vm/vm_code.c +++ b/src/vm/vm_code.c @@ -46,7 +46,7 @@ VM_Code new_vm_code(MemPool p, const Vector instr, const m_uint stack_depth, code->name = strdup(name); code->stack_depth = stack_depth; code->flag = flag; - INIT_OO(p, code, free_vm_code) + code->ref = new_refcount(p, free_vm_code); return code; }