]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
use GSlice and generate free function for reference-type structs with a
authorJuerg Billeter <j@bitron.ch>
Thu, 26 Jul 2007 06:00:40 +0000 (06:00 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Thu, 26 Jul 2007 06:00:40 +0000 (06:00 +0000)
2007-07-26  Juerg Billeter  <j@bitron.ch>

* vala/valastruct.vala, gobject/valacodegeneratormethod.vala,
  gobject/valacodegeneratorstruct.vala: use GSlice and generate free
  function for reference-type structs with a creation method

svn path=/trunk/; revision=389

ChangeLog
gobject/valacodegeneratormethod.vala
gobject/valacodegeneratorstruct.vala
vala/valastruct.vala

index 3c0345203f99bc2b8e1e482ff49dbf42c5c3613e..ab9a829fa3b1997f4fa33b990739eb91649067e9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-07-26  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valastruct.vala, gobject/valacodegeneratormethod.vala,
+         gobject/valacodegeneratorstruct.vala: use GSlice and generate free
+         function for reference-type structs with a creation method
+
 2007-07-25  Jürg Billeter  <j@bitron.ch>
 
        * vala/valaarray.vala: remove comments of overridden methods
index a536292698f3af897102c3f7a919b7a2294259c4..8ca5622883d697b8dddfec00c3edd96d3b41cb0f 100644 (file)
@@ -294,9 +294,8 @@ public class Vala.CodeGenerator {
                                        } else {
                                                var st = (Struct) m.parent_symbol;
                                                var cdecl = new CCodeDeclaration (st.get_cname () + "*");
-                                               var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
-                                               ccall.add_argument (new CCodeConstant (st.get_cname ()));
-                                               ccall.add_argument (new CCodeConstant ("1"));
+                                               var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_new0"));
+                                               ccall.add_argument (new CCodeIdentifier (st.get_cname ()));
                                                cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer ("self", ccall));
                                                cinit.append (cdecl);
                                        }
index 4a4aeab450adc4a1cdfb06ed55b9accf2f12e266..2b9e6f3ed12337f936020ffb5ea1ac17bf5ffa58 100644 (file)
@@ -30,7 +30,7 @@ public class Vala.CodeGenerator {
                var old_instance_dispose_fragment = instance_dispose_fragment;
                current_type_symbol = st;
                instance_struct = new CCodeStruct ("_%s".printf (st.get_cname ()));
-               instance_dispose_fragment = null;
+               instance_dispose_fragment = new CCodeFragment ();
 
                CCodeFragment decl_frag;
                CCodeFragment def_frag;
@@ -53,6 +53,30 @@ public class Vala.CodeGenerator {
 
                st.accept_children (this);
 
+               if (st.default_construction_method != null) {
+                       var function = new CCodeFunction (st.get_lower_case_cprefix () + "free", "void");
+                       if (st.access == MemberAccessibility.PRIVATE) {
+                               function.modifiers = CCodeModifiers.STATIC;
+                       }
+
+                       function.add_parameter (new CCodeFormalParameter ("self", st.get_cname () + "*"));
+
+                       decl_frag.append (function.copy ());
+
+                       var cblock = new CCodeBlock ();
+
+                       cblock.add_statement (instance_dispose_fragment);
+
+                       var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
+                       ccall.add_argument (new CCodeIdentifier (st.get_cname ()));
+                       ccall.add_argument (new CCodeIdentifier ("self"));
+                       cblock.add_statement (new CCodeExpressionStatement (ccall));
+
+                       function.block = cblock;
+
+                       def_frag.append (function);
+               }
+
                current_type_symbol = old_type_symbol;
                instance_struct = old_instance_struct;
                instance_dispose_fragment = old_instance_dispose_fragment;
index f5841948b383d51611ef5a6a5591a5cd7b05071b..dcfdf4d4f274bb2a44644df389ca18b9b2a69d21 100644 (file)
@@ -333,7 +333,11 @@ public class Vala.Struct : DataType {
        
        public override string get_free_function () {
                if (free_function == null) {
-                       Report.error (source_reference, "The type `%s` doesn't contain a free function".printf (get_full_name ()));
+                       if (default_construction_method != null) {
+                               free_function = get_lower_case_cprefix () + "free";
+                       } else {
+                               Report.error (source_reference, "The type `%s` doesn't contain a free function".printf (get_full_name ()));
+                       }
                }
                return free_function;
        }