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);
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
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;
}
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);
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;
}
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;
}
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;
}
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;
}
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) {
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;
}