From a5a3be43417f70953e7b56b0d82e8a1c774798d0 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 19 Oct 2021 06:24:54 +0000 Subject: [PATCH] Don't allocate a buffer to return a reference to a tuple element 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 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gen_type_mapping.py b/gen_type_mapping.py index e6585f97..d5a6d917 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -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 = ", ";") -- 2.39.5