src: introduce new constructors
[nit.git] / src / compiler_ffi.nit
index 2ebf76d..9a83ba4 100644 (file)
@@ -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
@@ -261,6 +274,12 @@ redef class CCompilationUnit
        end
 end
 
+redef class AbstractCompiler
+       # Cache to avoid multiple compilation of NULL values
+       # see FIXME in `MNullableType#compile_extern_helper_functions`
+       private var compiled_null_types = new Array[MNullableType]
+end
+
 redef class AbstractCompilerVisitor
        # Create a `RuntimeVariable` for this C variable originating from C user code
        private fun var_from_c(name: String, mtype: MType): RuntimeVariable
@@ -279,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
@@ -301,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
 
@@ -330,8 +352,8 @@ redef class MNullableType
                # FIXME: This is ugly an broke the separate compilation principle
                # The real function MUST be compiled only once, #define pragma only protect the compiler, not the loader
                # However, I am not sure of the right approach here (eg. week refs are ugly)
-               if is_already_compiled then return
-               is_already_compiled = true
+               if v.compiler.compiled_null_types.has(self) then return
+               v.compiler.compiled_null_types.add self
 
                # Internally, implement internal function
                var nitni_visitor = v.compiler.new_visitor
@@ -339,13 +361,11 @@ 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("\}")
        end
-
-       private var is_already_compiled = false # FIXME to remove, show above
 end
 
 redef class MExplicitCall