From: Matt Corallo Date: Tue, 2 Feb 2021 02:18:29 +0000 (-0500) Subject: Properly free C tuples when the inner fields are dropped X-Git-Tag: v0.0.1~5 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=ceda1e3041ff42b4aadc1245f6da26fd511b51ff;p=ldk-java Properly free C tuples when the inner fields are dropped Previously for the full test suite (incl 64 HumanObjectPeerTest runs): `991 allocations remained for 32165 bytes.` After for the full test suite (incl 64 HumanObjectPeerTest runs): `179 allocations remained for 5369 bytes.` --- diff --git a/gen_type_mapping.py b/gen_type_mapping.py index cba51d51..da23440f 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -343,6 +343,7 @@ class TypeMappingGenerator: from_hu_conv = "bindings." + self.tuple_types[ty_info.rust_obj][1].replace("LDK", "") + "_new(" to_hu_conv_pfx = "" to_hu_conv_sfx = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " + ty_info.java_hu_ty + "(" + to_hu_conv_refs = "" for idx, conv in enumerate(self.tuple_types[ty_info.rust_obj][0]): if idx != 0: to_hu_conv_sfx = to_hu_conv_sfx + ", " @@ -359,6 +360,11 @@ class TypeMappingGenerator: if conv_map.to_hu_conv is not None: to_hu_conv_pfx = to_hu_conv_pfx + conv_map.to_hu_conv + ";\n" to_hu_conv_sfx = to_hu_conv_sfx + conv_map.to_hu_conv_name + if to_hu_conv_refs is not None: + if conv_map.c_ty.endswith("Array"): + to_hu_conv_refs = None + else: + to_hu_conv_refs = to_hu_conv_refs + "\n" + conv_map.to_hu_conv_name + ".ptrs_to.add(" + ty_info.var_name + "_conv);" else: to_hu_conv_sfx = to_hu_conv_sfx + ty_info.var_name + "_" + chr(idx + ord("a")) if conv_map.from_hu_conv is not None: @@ -368,17 +374,23 @@ class TypeMappingGenerator: else: from_hu_conv = from_hu_conv + ty_info.var_name + "." + chr(idx + ord("a")) + if to_hu_conv_refs is None: + to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ");\n// Warning: We may not free the C tuple object!" + else: + to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ", () -> {\n" + to_hu_conv = to_hu_conv + "\tbindings." + ty_info.rust_obj.replace("LDK", "") + "_free(" + ty_info.var_name + ");\n" + to_hu_conv = to_hu_conv + "});" + to_hu_conv_refs if not ty_info.is_ptr and not holds_ref: ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_ref = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_ref = ", ";") return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, ret_conv = ret_conv, ret_conv_name = "(long)" + ty_info.var_name + "_ref", - to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ");", to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (from_hu_conv + ")", from_hu_conv_sfx)) + to_hu_conv = to_hu_conv, to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (from_hu_conv + ")", from_hu_conv_sfx)) return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, ret_conv = ("long " + ty_info.var_name + "_ref = (long)(&", ") | 1;"), ret_conv_name = ty_info.var_name + "_ref", - to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ");", to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (from_hu_conv + ")", from_hu_conv_sfx)) + to_hu_conv = to_hu_conv, to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (from_hu_conv + ")", from_hu_conv_sfx)) # The manually-defined types - TxOut and Transaction assert ty_info.rust_obj == "LDKTxOut" diff --git a/src/main/java/org/ldk/util/ThreeTuple.java b/src/main/java/org/ldk/util/ThreeTuple.java index ff63e71f..13722658 100644 --- a/src/main/java/org/ldk/util/ThreeTuple.java +++ b/src/main/java/org/ldk/util/ThreeTuple.java @@ -1,6 +1,7 @@ package org.ldk.util; public class ThreeTuple { + private Runnable finalize_run; public A a; public B b; public C c; @@ -9,4 +10,13 @@ public class ThreeTuple { this.b = b; this.c = c; } + public ThreeTuple(A a, B b, C c, Runnable finalize_run) { + this(a, b, c); + this.finalize_run = finalize_run; + } + @Override + public void finalize() throws Throwable { + if (finalize_run != null) finalize_run.run(); + super.finalize(); + } } diff --git a/src/main/java/org/ldk/util/TwoTuple.java b/src/main/java/org/ldk/util/TwoTuple.java index 32b4a910..627e3373 100644 --- a/src/main/java/org/ldk/util/TwoTuple.java +++ b/src/main/java/org/ldk/util/TwoTuple.java @@ -1,10 +1,20 @@ package org.ldk.util; public class TwoTuple { + private Runnable finalize_run; public A a; public B b; public TwoTuple(A a, B b) { this.a = a; this.b = b; } + public TwoTuple(A a, B b, Runnable finalize_run) { + this(a, b); + this.finalize_run = finalize_run; + } + @Override + public void finalize() throws Throwable { + if (finalize_run != null) finalize_run.run(); + super.finalize(); + } }