]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Use alternative for g_renew in POSIX profile 7841739b199ac5f1901e32cd9c0e90e9d4f5afa3
authorRico Tzschichholz <ricotz@ubuntu.com>
Fri, 18 Oct 2019 22:12:26 +0000 (00:12 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 19 Oct 2019 08:20:52 +0000 (10:20 +0200)
codegen/valaccodearraymodule.vala
vala/valaarraytype.vala

index 2d122035f98aafd9d2cb7a9f4d68b80bedd81d20..6a7ba933748298c2e0efb9b6a2ae6a5501aa7b65 100644 (file)
@@ -663,15 +663,29 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
                var length = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("length"));
                var size = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("size"));
 
-               var renew_call = new CCodeFunctionCall (new CCodeIdentifier ("g_renew"));
-               renew_call.add_argument (new CCodeIdentifier (get_ccode_name (array_type.element_type)));
-               renew_call.add_argument (array);
+               CCodeFunctionCall renew_call;
+               if (context.profile == Profile.POSIX) {
+                       cfile.add_include ("stdlib.h");
+                       renew_call = new CCodeFunctionCall (new CCodeIdentifier ("realloc"));
+                       renew_call.add_argument (array);
+               } else {
+                       renew_call = new CCodeFunctionCall (new CCodeIdentifier ("g_renew"));
+                       renew_call.add_argument (new CCodeIdentifier (get_ccode_name (array_type.element_type)));
+                       renew_call.add_argument (array);
+               }
+               CCodeExpression renew_call_size;
                if (array_type.element_type.is_reference_type_or_type_parameter ()) {
                        // NULL terminate array
-                       renew_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, size, new CCodeConstant ("1")));
+                       renew_call_size = new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, size, new CCodeConstant ("1"));
                } else {
-                       renew_call.add_argument (size);
+                       renew_call_size = size;
+               }
+               if (context.profile == Profile.POSIX) {
+                       var csizeof = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+                       csizeof.add_argument (new CCodeIdentifier (get_ccode_name (array_type.element_type)));
+                       renew_call_size = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, size, csizeof);
                }
+               renew_call.add_argument (renew_call_size);
 
                var csizecheck = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, length, size);
                ccode.open_if (csizecheck);
index e9b7192d7e7e95ce03266fe50eff8033e80d98ba..bc5eb16d78edea6bf952240d8680d88c52eec813 100644 (file)
@@ -130,7 +130,11 @@ public class Vala.ArrayType : ReferenceType {
                        resize_method.return_type = new VoidType ();
                        resize_method.access = SymbolAccessibility.PUBLIC;
 
-                       resize_method.set_attribute_string ("CCode", "cname", "g_renew");
+                       if (CodeContext.get ().profile == Profile.POSIX) {
+                               resize_method.set_attribute_string ("CCode", "cname", "realloc");
+                       } else {
+                               resize_method.set_attribute_string ("CCode", "cname", "g_renew");
+                       }
 
                        resize_method.add_parameter (new Parameter ("length", length_type));