X-Git-Url: http://nitlanguage.org diff --git a/src/compiler_ffi.nit b/src/compiler_ffi.nit index b885bc7..9a83ba4 100644 --- a/src/compiler_ffi.nit +++ b/src/compiler_ffi.nit @@ -45,12 +45,25 @@ redef class MModule ensure_compile_nitni_base(v) nitni_ccu.header_c_types.add("#include \"{name}._ffi.h\"\n") + nitni_ccu.header_c_types.add """ +extern void nitni_global_ref_incr(void*); +extern void nitni_global_ref_decr(void*); +""" nitni_ccu.write_as_nitni(self, v.compiler.modelbuilder.compile_dir) for file in nitni_ccu.files do v.compiler.extern_bodies.add(new ExternCFile(file, c_compiler_options)) end + + # reset FFI things so the next compilation job, if any, starts with a clean context + # FIXME clean and rationalize this + nitni_ccu = null + compiled_ffi_methods.clear + ffi_ccu = null + ffi_files.clear + compiled_callbacks.clear + #Do not reset `foreign_callbacks` and `ffi_callbacks` because they are computed in previous phases end private fun ensure_compile_nitni_base(v: AbstractCompilerVisitor) @@ -169,7 +182,7 @@ redef class AMethPropdef arguments_for_c.add(arg.name) else v.add("struct nitni_instance* var_for_c_{a};") - v.add("var_for_c_{a} = malloc(sizeof(struct nitni_instance));") + v.add("var_for_c_{a} = nit_alloc(sizeof(struct nitni_instance));") v.add("var_for_c_{a}->value = {arg.name};") arguments_for_c.add("var_for_c_{a}") end @@ -226,7 +239,7 @@ redef class AMethPropdef arguments_for_c.add(arg.name) else v.add("struct nitni_instance* var_for_c_{a};") - v.add("var_for_c_{a} = malloc(sizeof(struct nitni_instance));") + v.add("var_for_c_{a} = nit_alloc(sizeof(struct nitni_instance));") v.add("var_for_c_{a}->value = {arg.name};") arguments_for_c.add("var_for_c_{a}") end @@ -285,7 +298,7 @@ redef class AbstractCompilerVisitor add("return {src};") else add("struct nitni_instance* ret_for_c;") - add("ret_for_c = malloc(sizeof(struct nitni_instance));") + add("ret_for_c = nit_alloc(sizeof(struct nitni_instance));") add("ret_for_c->value = {src};") add("return ret_for_c;") end @@ -307,14 +320,17 @@ redef class MType private fun compile_extern_helper_functions(v: AbstractCompilerVisitor, ccu: CCompilationUnit) do # actually, we do not need to do anything when using the bohem garbage collector + var call_context = from_c_call_context # incr_ref - var nitni_visitor = v.compiler.new_visitor - ccu.header_decl.add("#define {mangled_cname}_incr_ref(from) while(0)\{\}\n") - - # incr_ref - nitni_visitor = v.compiler.new_visitor - ccu.header_decl.add("#define {mangled_cname}_decr_ref(from) while(0)\{\}\n") + ccu.header_decl.add "#ifndef {mangled_cname}_incr_ref\n" + ccu.header_decl.add " #define {mangled_cname}_incr_ref(from) nitni_global_ref_incr(({call_context.name_mtype(self)})(from))\n" + ccu.header_decl.add "#endif\n" + + # decr_ref + ccu.header_decl.add "#ifndef {mangled_cname}_decr_ref\n" + ccu.header_decl.add " #define {mangled_cname}_decr_ref(from) nitni_global_ref_decr(({call_context.name_mtype(self)})(from))\n" + ccu.header_decl.add "#endif\n" end end @@ -345,7 +361,7 @@ redef class MNullableType var full_internal_csignature = "{cname_blind} {full_cname}()" nitni_visitor.add("{full_internal_csignature} \{") nitni_visitor.add("struct nitni_instance* ret_for_c;") - nitni_visitor.add("ret_for_c = malloc(sizeof(struct nitni_instance));") + nitni_visitor.add("ret_for_c = nit_alloc(sizeof(struct nitni_instance));") nitni_visitor.add("ret_for_c->value = NULL;") nitni_visitor.add("return ret_for_c;") nitni_visitor.add("\}")