[vala/staging] codegen: Use *_free_full to free GLib.List, GLib.SList and GLib.Queue



commit 270395dd2f8d6c76ca4738f064bb9c0ce579fe57
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed May 17 11:49:27 2017 +0200

    codegen: Use *_free_full to free GLib.List, GLib.SList and GLib.Queue
    
    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 |   32 +++++++++++++------------
 tests/Makefile.am                |    1 +
 tests/basic-types/glists.vala    |   47 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 15 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 09e8c9c..b6bb770 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -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 ();
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2ddd8b0..37bc653 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 0000000..c5ee1b9
--- /dev/null
+++ b/tests/basic-types/glists.vala
@@ -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 ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]