From 46f96900352e2637892eff5e08107ffabd34d519 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sun, 22 Nov 2020 09:22:46 +0100 Subject: [PATCH] :art: Use generator for ref functions --- include/env/context.h | 4 +--- include/env/func.h | 4 +--- include/env/nspc.h | 4 +--- include/env/oo.h | 4 ++++ include/env/type.h | 4 +--- include/env/value.h | 5 +---- include/vm.h | 7 ++----- src/emit/emitter.c | 2 +- src/env/func.c | 2 +- src/gwion.c | 2 +- src/import/import_cdef.c | 2 +- src/import/import_special.c | 2 +- src/lib/ptr.c | 2 +- src/vm/vm_code.c | 4 ++-- 14 files changed, 19 insertions(+), 29 deletions(-) diff --git a/include/env/context.h b/include/env/context.h index 34b7b2e3..0fea1b74 100644 --- a/include/env/context.h +++ b/include/env/context.h @@ -11,9 +11,7 @@ struct Context_ { uint16_t ref; }; -ANN void free_context(const Context, struct Gwion_*const); -ANN static inline void context_addref(const Context c) { ++c->ref; } -ANN static inline void context_remref(const Context c, struct Gwion_ *const gwion) { if(!--c->ref) free_context(c, gwion); } +REF_FUNC(Context, context) ANN2(1,3) ANEW Context new_context(MemPool p, const Ast, const m_str); ANN void load_context(const Context, const Env); ANN void unload_context(const Context, const Env); diff --git a/include/env/func.h b/include/env/func.h index 026ab7c0..6f0e416a 100644 --- a/include/env/func.h +++ b/include/env/func.h @@ -22,9 +22,7 @@ struct Func_ { enum fflag fflag; }; -ANN void free_func(const Func, struct Gwion_*const); -ANN static inline void func_addref(const Func f) { ++f->ref; } -ANN static inline void func_remref(const Func f, struct Gwion_ *const gwion) { if(!--f->ref) free_func(f, gwion); } +REF_FUNC(Func, func) FLAG_FUNC(Func, f) ANEW ANN Func new_func(MemPool, const m_str, const Func_Def); diff --git a/include/env/nspc.h b/include/env/nspc.h index 1a70d732..1cf9b65a 100644 --- a/include/env/nspc.h +++ b/include/env/nspc.h @@ -20,9 +20,7 @@ struct Nspc_ { uint16_t ref; }; -ANN void free_nspc(const Nspc, struct Gwion_*const); -ANN static inline void nspc_addref(const Nspc n) { ++n->ref; } -ANN static inline void nspc_remref(const Nspc n, struct Gwion_ *const gwion) { if(!--n->ref) free_nspc(n, gwion); } +REF_FUNC(Nspc, nspc) extern ANEW ANN Nspc new_nspc(MemPool p, const m_str name); extern ANN void nspc_commit(const Nspc); diff --git a/include/env/oo.h b/include/env/oo.h index 92f80343..9ec8eeaa 100644 --- a/include/env/oo.h +++ b/include/env/oo.h @@ -8,4 +8,8 @@ typedef struct Nspc_ * Nspc; typedef struct Value_ * Value; typedef struct Func_ * Func; +#define REF_FUNC(A, b) \ +ANN void free_##b(const A, struct Gwion_*const); \ +ANN static inline void b##_addref(const A a) { ++a->ref; } \ +ANN static inline void b##_remref(const A a, struct Gwion_ *const gwion) { if(!--a->ref) free_##b(a, gwion); } #endif diff --git a/include/env/type.h b/include/env/type.h index 9e1abbaa..4a37b669 100644 --- a/include/env/type.h +++ b/include/env/type.h @@ -51,9 +51,7 @@ struct Type_ { enum tflag tflag; }; -ANN void free_type(const Type, struct Gwion_*const); -ANN static inline void type_addref(const Type t) { ++t->ref; } -ANN static inline void type_remref(const Type t, struct Gwion_ *const gwion) { if(!--t->ref) free_type(t, gwion); } +REF_FUNC(Type, type) FLAG_FUNC(Type, t) ANN2(1,3) ANEW Type new_type(MemPool, const m_uint xid, const m_str name, const Type); diff --git a/include/env/value.h b/include/env/value.h index b49cd631..c87da142 100644 --- a/include/env/value.h +++ b/include/env/value.h @@ -35,10 +35,7 @@ struct Value_ { enum vflag vflag; }; -ANN void free_value(const Value, struct Gwion_*const); -ANN static inline void value_addref(const Value v) { ++v->ref; } -ANN static inline void value_remref(const Value v, struct Gwion_ *const gwion) { if(!--v->ref) free_value(v, gwion); } - +REF_FUNC(Value, value) FLAG_FUNC(Value, v) ANEW ANN Value new_value(MemPool p, const Type type, const m_str name); diff --git a/include/vm.h b/include/vm.h index fe802a9e..156a6b52 100644 --- a/include/vm.h +++ b/include/vm.h @@ -70,11 +70,8 @@ struct VM_Shred_ { struct ShredTick_ * tick; struct ShredInfo_ * info; }; - -ANN void free_vm_code(const VM_Code, struct Gwion_*const); -ANN static inline void vmcode_addref(const VM_Code c) { ++c->ref; } -ANN static inline void vmcode_remref(const VM_Code c, struct Gwion_ *const gwion) { if(!--c->ref) free_vm_code(c, gwion); } -ANN2(1,5) ANEW VM_Code new_vm_code(MemPool p, const Vector instr, const m_uint stack_depth, const int builtin, const m_str name); +REF_FUNC(VM_Code, vmcode) +ANN2(1,5) ANEW VM_Code new_vmcode(MemPool p, const Vector instr, const m_uint stack_depth, const int builtin, const m_str name); ANN VM_Shred shreduler_get(const Shreduler s) __attribute__((hot)); ANN void shreduler_remove(const Shreduler s, const VM_Shred out, const m_bool erase)__attribute__((hot)); diff --git a/src/emit/emitter.c b/src/emit/emitter.c index 142e1b21..e88bc733 100644 --- a/src/emit/emitter.c +++ b/src/emit/emitter.c @@ -9,7 +9,7 @@ static ANEW ANN VM_Code emit_code(const Emitter emit) { Code* const c = emit->code; - const VM_Code code = new_vm_code(emit->gwion->mp, &c->instr, c->stack_depth, 0, c->name); + const VM_Code code = new_vmcode(emit->gwion->mp, &c->instr, c->stack_depth, 0, c->name); return code; } diff --git a/src/env/func.c b/src/env/func.c index c6289ccd..da91ce56 100644 --- a/src/env/func.c +++ b/src/env/func.c @@ -39,6 +39,6 @@ ANN2(1,2) Symbol func_symbol(const Env env, const m_str nspc, const m_str base, ANN void builtin_func(const MemPool mp, const Func f, void* func_ptr) { set_vflag(f->value_ref, vflag_builtin); - f->code = new_vm_code(mp, NULL, f->def->stack_depth, 1, f->name); + f->code = new_vmcode(mp, NULL, f->def->stack_depth, 1, f->name); f->code->native_func = (m_uint)func_ptr; } diff --git a/src/gwion.c b/src/gwion.c index f4284dcc..cc0486e7 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -28,7 +28,7 @@ ANN static inline m_bool gwion_engine(const Gwion gwion) { } ANN static void gwion_cleaner(const Gwion gwion) { - const VM_Code code = new_vm_code(gwion->mp, NULL, 0, 1, "in code dtor"); + const VM_Code code = new_vmcode(gwion->mp, NULL, 0, 1, "in code dtor"); gwion->vm->cleaner_shred = new_vm_shred(gwion->mp, code); vm_ini_shred(gwion->vm, gwion->vm->cleaner_shred); } diff --git a/src/import/import_cdef.c b/src/import/import_cdef.c index 27a95019..d4c12b91 100644 --- a/src/import/import_cdef.c +++ b/src/import/import_cdef.c @@ -20,7 +20,7 @@ ANN static m_bool mk_xtor(MemPool p, const Type type, const m_uint d, const enum tflag e) { VM_Code* code = e == tflag_ctor ? &type->nspc->pre_ctor : &type->nspc->dtor; const m_str name = type->name; - *code = new_vm_code(p, NULL, SZ_INT, 1, name); + *code = new_vmcode(p, NULL, SZ_INT, 1, name); (*code)->native_func = (m_uint)d; type->tflag |= e; return GW_OK; diff --git a/src/import/import_special.c b/src/import/import_special.c index 2c242db3..64458839 100644 --- a/src/import/import_special.c +++ b/src/import/import_special.c @@ -42,7 +42,7 @@ ANN void gwi_set_loc(const Gwi gwi, const m_str file, const uint line) { } ANN static m_bool mk_gack(MemPool p, const Type type, const f_gack d) { - const VM_Code code = new_vm_code(p, NULL, SZ_INT, 1, "@gack"); + const VM_Code code = new_vmcode(p, NULL, SZ_INT, 1, "@gack"); code->native_func = (m_uint)d; type->info->gack = code; return GW_OK; diff --git a/src/lib/ptr.c b/src/lib/ptr.c index bc8a94e6..38341deb 100644 --- a/src/lib/ptr.c +++ b/src/lib/ptr.c @@ -161,7 +161,7 @@ static OP_CHECK(opck_ptr_scan) { DECL_ON(const Type, t, = (Type)scan_class(env, ts->t, ts->td)) const Type base = known_type(env, t->info->cdef->base.tmpl->call->td); if(isa(base, env->gwion->type[et_compound]) > 0) { - t->nspc->dtor = new_vm_code(env->gwion->mp, NULL, SZ_INT, 1, "@PtrDtor"); + t->nspc->dtor = new_vmcode(env->gwion->mp, NULL, SZ_INT, 1, "@PtrDtor"); if(!tflag(base, tflag_struct)) t->nspc->dtor->native_func = (m_uint)ptr_object_dtor; else diff --git a/src/vm/vm_code.c b/src/vm/vm_code.c index 033a64e8..6e98a156 100644 --- a/src/vm/vm_code.c +++ b/src/vm/vm_code.c @@ -26,7 +26,7 @@ ANN static void _free_code_instr(const Vector v, const Gwion gwion) { free_vector(gwion->mp, v); } -ANN void free_vm_code(VM_Code a, Gwion gwion) { +ANN void free_vmcode(VM_Code a, Gwion gwion) { if(a->memoize) memoize_end(gwion->mp, a->memoize); if(!a->builtin) { @@ -59,7 +59,7 @@ ANN static m_bit* tobytecode(MemPool p, const VM_Code code) { } -VM_Code new_vm_code(MemPool p, const Vector instr, const m_uint stack_depth, +VM_Code new_vmcode(MemPool p, const Vector instr, const m_uint stack_depth, const int builtin, const m_str name) { VM_Code code = mp_calloc(p, VM_Code); if(instr) { -- 2.43.0