From: Rico Tzschichholz Date: Wed, 17 May 2017 09:49:27 +0000 (+0200) Subject: codegen: Use *_free_full to free GLib.List, GLib.SList and GLib.Queue X-Git-Tag: 0.37.1~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=270395dd2f8d6c76ca4738f064bb9c0ce579fe57;p=thirdparty%2Fvala.git 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. --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 09e8c9cb7..b6bb7708f 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 2ddd8b040..37bc653bd 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 000000000..c5ee1b9a4 --- /dev/null +++ b/tests/basic-types/glists.vala @@ -0,0 +1,47 @@ +void test_glist () { + var list = new GLib.List (); + list.prepend ("foo"); + list.prepend ("bar"); + assert (list.nth_data (1) == "foo"); + list = null; + + var list2 = new GLib.List (); + list2.prepend ("foo"); + list2.prepend ("bar"); + assert (list2.nth_data (1) == "foo"); + list2 = null; +} + +void test_gslist () { + var list = new GLib.SList (); + list.prepend ("foo"); + list.prepend ("bar"); + assert (list.nth_data (1) == "foo"); + list = null; + + var list2 = new GLib.SList (); + list2.prepend ("foo"); + list2.prepend ("bar"); + assert (list2.nth_data (1) == "foo"); + list2 = null; +} + +void test_gqueue () { + var queue = new GLib.Queue (); + queue.push_head ("foo"); + queue.push_head ("bar"); + assert (queue.peek_nth (1) == "foo"); + queue = null; + + var queue2 = new GLib.Queue (); + queue2.push_head ("foo"); + queue2.push_head ("bar"); + assert (queue2.peek_nth (1) == "foo"); + queue2 = null; +} + +void main () { + test_glist (); + test_gslist (); + test_gqueue (); +}