[vala/staging] codegen: Use alternative for g_renew in POSIX profile



commit 7841739b199ac5f1901e32cd9c0e90e9d4f5afa3
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Oct 19 00:12:26 2019 +0200

    codegen: Use alternative for g_renew in POSIX profile

 codegen/valaccodearraymodule.vala | 24 +++++++++++++++++++-----
 vala/valaarraytype.vala           |  6 +++++-
 2 files changed, 24 insertions(+), 6 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 2d122035f..6a7ba9337 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -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);
diff --git a/vala/valaarraytype.vala b/vala/valaarraytype.vala
index e9b7192d7..bc5eb16d7 100644
--- a/vala/valaarraytype.vala
+++ b/vala/valaarraytype.vala
@@ -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));
 


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