Properly free C tuples when the inner fields are dropped
authorMatt Corallo <git@bluematt.me>
Tue, 2 Feb 2021 02:18:29 +0000 (21:18 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 2 Feb 2021 03:02:42 +0000 (22:02 -0500)
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.`

gen_type_mapping.py
src/main/java/org/ldk/util/ThreeTuple.java
src/main/java/org/ldk/util/TwoTuple.java

index cba51d51bfddf31ead763cb3d9bec6d7f1a2de30..da23440f413115471842b3098ecbbed8eb1cd790 100644 (file)
@@ -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"
index ff63e71ff39842a952b2df724897c3be5598164f..13722658045be4d4c6029b42886539018e79e079 100644 (file)
@@ -1,6 +1,7 @@
 package org.ldk.util;
 
 public class ThreeTuple<A, B, C> {
+    private Runnable finalize_run;
     public A a;
     public B b;
     public C c;
@@ -9,4 +10,13 @@ public class ThreeTuple<A, B, C> {
         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();
+    }
 }
index 32b4a910ba8bba7bb671dae875ef9a90ebd43d29..627e33735a31cd908432433f1f3e58d76c7ea349 100644 (file)
@@ -1,10 +1,20 @@
 package org.ldk.util;
 
 public class TwoTuple<A, B> {
+    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();
+    }
 }