[vala] posix: Fix array creation and destruction in POSIX profile



commit 2c25e8d6f2dc334cfbd34b72a3dd866085d6b68c
Author: Jürg Billeter <j bitron ch>
Date:   Mon Oct 12 21:25:49 2009 +0200

    posix: Fix array creation and destruction in POSIX profile
    
    Based on patch by pancake.

 codegen/valaccodearraymodule.vala |   17 +++++++++++++++--
 codegen/valaccodebasemodule.vala  |   12 ++++++++++--
 2 files changed, 25 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index da08cdf..0d745b0 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -65,8 +65,15 @@ internal class Vala.CCodeArrayModule : CCodeMethodCallModule {
 			return;
 		}
 
-		var gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
-		gnew.add_argument (new CCodeIdentifier (expr.element_type.get_cname ()));
+		CCodeFunctionCall gnew;
+		if (context.profile == Profile.POSIX) {
+			source_declarations.add_include ("stdlib.h");
+			gnew = new CCodeFunctionCall (new CCodeIdentifier ("calloc"));
+		} else {
+			gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
+			gnew.add_argument (new CCodeIdentifier (expr.element_type.get_cname ()));
+		}
+
 		bool first = true;
 		CCodeExpression cexpr = null;
 
@@ -99,6 +106,12 @@ internal class Vala.CCodeArrayModule : CCodeMethodCallModule {
 		
 		gnew.add_argument (cexpr);
 
+		if (context.profile == Profile.POSIX) {
+			var csizeof = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+			csizeof.add_argument (new CCodeIdentifier (expr.element_type.get_cname ()));
+			gnew.add_argument (csizeof);
+		}
+
 		if (expr.initializer_list != null) {
 			var ce = new CCodeCommaExpression ();
 			var temp_var = get_temp_variable (expr.value_type, true, expr);
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 5d440e7..f88bc27 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2470,9 +2470,17 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 				return new CCodeIdentifier (func_name);
 			}
 		} else if (type is ArrayType) {
-			return new CCodeIdentifier ("g_free");
+			if (context.profile == Profile.POSIX) {
+				return new CCodeIdentifier ("free");
+			} else {
+				return new CCodeIdentifier ("g_free");
+			}
 		} else if (type is PointerType) {
-			return new CCodeIdentifier ("g_free");
+			if (context.profile == Profile.POSIX) {
+				return new CCodeIdentifier ("free");
+			} else {
+				return new CCodeIdentifier ("g_free");
+			}
 		} else {
 			return new CCodeConstant ("NULL");
 		}



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