]> git.bitcoin.ninja Git - ldk-java/commitdiff
Don't allocate a buffer to return a reference to a tuple element
authorMatt Corallo <git@bluematt.me>
Tue, 19 Oct 2021 06:24:54 +0000 (06:24 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 19 Oct 2021 06:24:54 +0000 (06:24 +0000)
If we're returning a reference to an object because we don't have a
clone function available, we'll set the reference flag implying no
free'ing will occur. In that case, we don't need to allocate a
buffer to copy the object's memory, we might as well just return a
pointer to the original.

After this commit, test leaks are:
    73 allocations remained for 1137376 bytes.

gen_type_mapping.py

index e6585f9715cff5897bd48941c6b797e54780ed41..d5a6d917202591a400b05605dd647b4d9058d17e 100644 (file)
@@ -378,12 +378,12 @@ class TypeMappingGenerator:
                     ret_conv_name = "((uint64_t)" + ty_info.var_name + "_conv)"
                     if holds_ref:
                         # If we're trying to return a ref, we have to clone.
-                        # We just blindly assume its implemented and let the compiler fail if its not.
-                        ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_conv = ", ";")
                         if (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns:
-                            ret_conv = (ret_conv[0], ret_conv[1] + "\n// Warning: we really need to clone here, but no clone is available for " + ty_info.rust_obj)
+                            ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = &", "")
+                            ret_conv = (ret_conv[0], ";\n// Warning: we really need to clone here, but no clone is available for " + ty_info.rust_obj)
                             ret_conv_name += " | 1"
                         else:
+                            ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_conv = ", ";")
                             ret_conv = (ret_conv[0], ret_conv[1] + "\n*" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(" + ty_info.var_name + "_conv);")
                     else:
                         ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_conv = ", ";")