]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Use *_free_full to free GLib.List, GLib.SList and GLib.Queue
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 17 May 2017 09:49:27 +0000 (11:49 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 17 May 2017 10:32:02 +0000 (12:32 +0200)
g_list_free_full and g_slist_free_full are available since 2.28.
g_queue_free_full is available since 2.32.

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/basic-types/glists.vala [new file with mode: 0644]

index 09e8c9cb7b2c99aaae66d51ec723eca02bf23b08..b6bb7708f484ca23180ae5d0549a519236b1a82a 100644 (file)
@@ -3217,14 +3217,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                }
 
                var function = new CCodeFunction (destroy_func, "void");
-               function.modifiers = CCodeModifiers.STATIC;
-
                function.add_parameter (new CCodeParameter ("self", get_ccode_name (collection_type)));
 
                push_function (function);
 
-               CCodeFunctionCall element_free_call;
                if (collection_type.data_type == gnode_type) {
+                       CCodeFunctionCall element_free_call;
                        /* A wrapper which converts GNodeTraverseFunc into GDestroyNotify */
                        string destroy_node_func = "%s_node".printf (destroy_func);
                        var wrapper = new CCodeFunction (destroy_node_func, "gboolean");
@@ -3250,25 +3248,29 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        element_free_call.add_argument (new CCodeConstant ("-1"));
                        element_free_call.add_argument (new CCodeIdentifier (destroy_node_func));
                        element_free_call.add_argument (new CCodeConstant ("NULL"));
+                       ccode.add_expression (element_free_call);
+
+                       var cfreecall = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_free_function (gnode_type)));
+                       cfreecall.add_argument (new CCodeIdentifier ("self"));
+                       ccode.add_expression (cfreecall);
+
+                       function.modifiers = CCodeModifiers.STATIC;
                } else {
+                       CCodeFunctionCall collection_free_call;
                        if (collection_type.data_type == glist_type) {
-                               element_free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_list_foreach"));
+                               collection_free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_list_free_full"));
                        } else if (collection_type.data_type == gslist_type) {
-                               element_free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_slist_foreach"));
+                               collection_free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_slist_free_full"));
                        } else {
-                               element_free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_queue_foreach"));
+                               collection_free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_queue_free_full"));
                        }
 
-                       element_free_call.add_argument (new CCodeIdentifier ("self"));
-                       element_free_call.add_argument (new CCodeCastExpression (element_destroy_func_expression, "GFunc"));
-                       element_free_call.add_argument (new CCodeConstant ("NULL"));
-               }
+                       collection_free_call.add_argument (new CCodeIdentifier ("self"));
+                       collection_free_call.add_argument (new CCodeCastExpression (element_destroy_func_expression, "GDestroyNotify"));
+                       ccode.add_expression (collection_free_call);
 
-               ccode.add_expression (element_free_call);
-
-               var cfreecall = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_free_function (collection_type.data_type)));
-               cfreecall.add_argument (new CCodeIdentifier ("self"));
-               ccode.add_expression (cfreecall);
+                       function.modifiers = CCodeModifiers.STATIC | CCodeModifiers.INLINE;
+               }
 
                pop_function ();
 
index 2ddd8b0402b0cfaf9557d762c9ab136c829c60bd..37bc653bdedc829c3b57e848c97a261ad3fba400 100644 (file)
@@ -22,6 +22,7 @@ TESTS = \
        basic-types/arrays.vala \
        basic-types/pointers.vala \
        basic-types/sizeof.vala \
+       basic-types/glists.vala \
        basic-types/bug571486.vala \
        basic-types/bug591552.vala \
        basic-types/bug595751.vala \
diff --git a/tests/basic-types/glists.vala b/tests/basic-types/glists.vala
new file mode 100644 (file)
index 0000000..c5ee1b9
--- /dev/null
@@ -0,0 +1,47 @@
+void test_glist () {
+       var list = new GLib.List<string> ();
+       list.prepend ("foo");
+       list.prepend ("bar");
+       assert (list.nth_data (1) == "foo");
+       list = null;
+
+       var list2 = new GLib.List<unowned string> ();
+       list2.prepend ("foo");
+       list2.prepend ("bar");
+       assert (list2.nth_data (1) == "foo");
+       list2 = null;
+}
+
+void test_gslist () {
+       var list = new GLib.SList<string> ();
+       list.prepend ("foo");
+       list.prepend ("bar");
+       assert (list.nth_data (1) == "foo");
+       list = null;
+
+       var list2 = new GLib.SList<unowned string> ();
+       list2.prepend ("foo");
+       list2.prepend ("bar");
+       assert (list2.nth_data (1) == "foo");
+       list2 = null;
+}
+
+void test_gqueue () {
+       var queue = new GLib.Queue<string> ();
+       queue.push_head ("foo");
+       queue.push_head ("bar");
+       assert (queue.peek_nth (1) == "foo");
+       queue = null;
+
+       var queue2 = new GLib.Queue<unowned string> ();
+       queue2.push_head ("foo");
+       queue2.push_head ("bar");
+       assert (queue2.peek_nth (1) == "foo");
+       queue2 = null;
+}
+
+void main () {
+       test_glist ();
+       test_gslist ();
+       test_gqueue ();
+}