[vala/staging] codegen: Use alternative for g_memdup in POSIX profile
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging] codegen: Use alternative for g_memdup in POSIX profile
- Date: Sat, 19 Oct 2019 08:39:20 +0000 (UTC)
commit 6ac9b05833c369603a61b0152f04c80a39a3b42a
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Sat Oct 19 00:12:26 2019 +0200
codegen: Use alternative for g_memdup in POSIX profile
codegen/valaccodearraymodule.vala | 30 +++++++++++++++++++++++++-----
1 file changed, 25 insertions(+), 5 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 6a7ba9337..4e295c7c6 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -555,14 +555,34 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
ccode.add_return (new CCodeIdentifier ("result"));
} else {
- var dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
- dup_call.add_argument (new CCodeIdentifier ("self"));
-
var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
sizeof_call.add_argument (new CCodeIdentifier (get_ccode_name
(array_type.element_type)));
- dup_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new
CCodeIdentifier ("length"), sizeof_call));
+ var length_expr = new CCodeIdentifier ("length");
+
+ if (context.profile == Profile.POSIX) {
+ cfile.add_include ("stdlib.h");
+ var alloc = new CCodeFunctionCall (new CCodeIdentifier ("calloc"));
+ alloc.add_argument (length_expr);
+ alloc.add_argument (sizeof_call);
- ccode.add_return (dup_call);
+ var cvardecl = new CCodeVariableDeclarator ("result");
+ ccode.add_declaration (get_ccode_name (array_type), cvardecl);
+ ccode.add_assignment (new CCodeIdentifier ("result"), alloc);
+
+ var dup_call = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
+ dup_call.add_argument (new CCodeIdentifier ("result"));
+ dup_call.add_argument (new CCodeIdentifier ("self"));
+ dup_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL,
length_expr, sizeof_call));
+ ccode.add_expression (dup_call);
+
+ ccode.add_return (new CCodeIdentifier ("result"));
+ } else {
+ var dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
+ dup_call.add_argument (new CCodeIdentifier ("self"));
+ dup_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL,
length_expr, sizeof_call));
+
+ ccode.add_return (dup_call);
+ }
}
// append to file
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]