[vala/staging] codegen: Replace "g_memdup" with "_vala_memdup2" for target glib < 2.68



commit 05966d512de99b562807f8b186c285b46da93f0c
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Feb 4 21:07:17 2021 +0100

    codegen: Replace "g_memdup" with "_vala_memdup2" for target glib < 2.68
    
    "_vala_memdup2" is a copy of "g_memdup2" available in glib >= 2.68

 codegen/valaccodearraymodule.vala |  3 ++-
 codegen/valaccodebasemodule.vala  | 42 +++++++++++++++++++++++++++++++++++++++
 codegen/valagvariantmodule.vala   |  9 ++++++---
 3 files changed, 50 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 9a010e360..b9352ce73 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -640,7 +640,8 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
                                if (context.require_glib_version (2, 68)) {
                                        dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup2"));
                                } else {
-                                       dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
+                                       requires_memdup2 = true;
+                                       dup_call = new CCodeFunctionCall (new CCodeIdentifier 
("_vala_memdup2"));
                                }
                                dup_call.add_argument (new CCodeIdentifier ("self"));
                                dup_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, 
length_expr, sizeof_call));
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index a5c42b01c..db72d5a70 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -364,6 +364,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        public bool requires_array_length;
        public bool requires_array_n_elements;
        public bool requires_clear_mutex;
+       public bool requires_memdup2;
 
        public Set<string> wrappers;
        Set<Symbol> generated_external_symbols;
@@ -756,6 +757,44 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                cfile.add_function (fun);
        }
 
+       void append_vala_memdup2 () {
+               // g_malloc
+               cfile.add_include ("glib.h");
+
+               var fun = new CCodeFunction ("_vala_memdup2", "gpointer");
+               fun.modifiers = CCodeModifiers.STATIC | CCodeModifiers.INLINE;
+               fun.add_parameter (new CCodeParameter ("mem", "gconstpointer"));
+               fun.add_parameter (new CCodeParameter ("byte_size", "gsize"));
+
+               push_function (fun);
+
+               ccode.add_declaration ("gpointer", new CCodeVariableDeclarator ("new_mem"));
+
+               ccode.open_if (new CCodeIdentifier ("mem && byte_size != 0"));
+
+               var malloc = new CCodeFunctionCall (new CCodeIdentifier ("g_malloc"));
+               malloc.add_argument (new CCodeIdentifier ("byte_size"));
+               ccode.add_assignment (new CCodeIdentifier ("new_mem"), malloc);
+               var mcpy = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
+               mcpy.add_argument (new CCodeIdentifier ("new_mem"));
+               mcpy.add_argument (new CCodeIdentifier ("mem"));
+               mcpy.add_argument (new CCodeIdentifier ("byte_size"));
+               ccode.add_expression (mcpy);
+
+               ccode.add_else ();
+
+               ccode.add_assignment (new CCodeIdentifier ("new_mem"), new CCodeIdentifier ("NULL"));
+
+               ccode.close ();
+
+               ccode.add_return (new CCodeIdentifier ("new_mem"));
+
+               pop_function ();
+
+               cfile.add_function_declaration (fun);
+               cfile.add_function (fun);
+       }
+
        public override void visit_source_file (SourceFile source_file) {
                cfile = new CCodeFile (CCodeFileType.SOURCE, source_file);
 
@@ -811,6 +850,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        append_vala_clear_mutex ("GRWLock", "g_rw_lock");
                        append_vala_clear_mutex ("GCond", "g_cond");
                }
+               if (requires_memdup2) {
+                       append_vala_memdup2 ();
+               }
 
                var comments = source_file.get_comments();
                if (comments != null) {
diff --git a/codegen/valagvariantmodule.vala b/codegen/valagvariantmodule.vala
index 4d3c692a8..808af51b1 100644
--- a/codegen/valagvariantmodule.vala
+++ b/codegen/valagvariantmodule.vala
@@ -422,7 +422,8 @@ public class Vala.GVariantModule : GValueModule {
                if (context.require_glib_version (2, 68)) {
                        dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup2"));
                } else {
-                       dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
+                       requires_memdup2 = true;
+                       dup_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_memdup2"));
                }
                dup_call.add_argument (get_data_call);
                dup_call.add_argument (length);
@@ -568,7 +569,8 @@ public class Vala.GVariantModule : GValueModule {
                                if (context.require_glib_version (2, 68)) {
                                        cdup = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup2"));
                                } else {
-                                       cdup = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
+                                       requires_memdup2 = true;
+                                       cdup = new CCodeFunctionCall (new CCodeIdentifier ("_vala_memdup2"));
                                }
                                cdup.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, 
result));
                                cdup.add_argument (csizeof);
@@ -745,7 +747,8 @@ public class Vala.GVariantModule : GValueModule {
                if (context.require_glib_version (2, 68)) {
                        dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup2"));
                } else {
-                       dup_call = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
+                       requires_memdup2 = true;
+                       dup_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_memdup2"));
                }
                dup_call.add_argument (array_expr);
                dup_call.add_argument (get_array_length (array_expr, 1));


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