]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Generate _dup() and _free() for structs in the posix profile
authorAaron Andersen <aaron@fosslib.net>
Sun, 25 Dec 2011 14:29:24 +0000 (15:29 +0100)
committerJürg Billeter <j@bitron.ch>
Tue, 31 Jan 2012 12:09:18 +0000 (13:09 +0100)
Fixes bug 665815.

codegen/valaccodestructmodule.vala

index b21829b5480dfb14c7198ae0d88c11aff5c9b91a..affb48ea2e885cfdaef1f18373a8a991119d277e 100644 (file)
@@ -155,7 +155,7 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
                        generate_struct_declaration (st, internal_header_file);
                }
 
-               if (context.profile == Profile.GOBJECT && !st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
+               if (!st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
                        if (st.is_disposable ()) {
                                begin_struct_destroy_function (st);
                        }
@@ -163,7 +163,7 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
 
                st.accept_children (this);
 
-               if (context.profile == Profile.GOBJECT && !st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
+               if (!st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {
                        if (st.is_disposable ()) {
                                add_struct_copy_function (st);
                                add_struct_destroy_function (st);
@@ -191,10 +191,17 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
 
                ccode.add_declaration (get_ccode_name (st) + "*", new CCodeVariableDeclarator ("dup"));
 
-               var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
-               creation_call.add_argument (new CCodeConstant (get_ccode_name (st)));
-               creation_call.add_argument (new CCodeConstant ("1"));
-               ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
+               if (context.profile == Profile.GOBJECT) {
+                       var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
+                       creation_call.add_argument (new CCodeConstant (get_ccode_name (st)));
+                       creation_call.add_argument (new CCodeConstant ("1"));
+                       ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
+               } else if (context.profile == Profile.POSIX) {
+                       var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("calloc"));
+                       creation_call.add_argument (new CCodeConstant ("1"));
+                       creation_call.add_argument (new CCodeIdentifier ("sizeof (%s*)".printf (get_ccode_name (st))));
+                       ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
+               }
 
                if (st.is_disposable ()) {
                        var copy_call = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_copy_function (st)));
@@ -237,9 +244,15 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
                        ccode.add_expression (destroy_call);
                }
 
-               var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_free"));
-               free_call.add_argument (new CCodeIdentifier ("self"));
-               ccode.add_expression (free_call);
+               if (context.profile == Profile.GOBJECT) {
+                       var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_free"));
+                       free_call.add_argument (new CCodeIdentifier ("self"));
+                       ccode.add_expression (free_call);
+               } else if (context.profile == Profile.POSIX) {
+                       var free_call = new CCodeFunctionCall (new CCodeIdentifier ("free"));
+                       free_call.add_argument (new CCodeIdentifier ("self"));
+                       ccode.add_expression (free_call);
+               }
 
                pop_function ();