]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Replace "g_memdup" with "_vala_memdup2" for target glib < 2.68
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 4 Feb 2021 20:07:17 +0000 (21:07 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 4 Feb 2021 20:33:07 +0000 (21:33 +0100)
"_vala_memdup2" is a copy of "g_memdup2" available in glib >= 2.68

codegen/valaccodearraymodule.vala
codegen/valaccodebasemodule.vala
codegen/valagvariantmodule.vala

index 9a010e360c08249c6f9aed69dac8493e855eb5dd..b9352ce7356aace0cc30c6e9ffee2ee395a0f713 100644 (file)
@@ -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));
index a5c42b01c9f19ed3f0f12909eaa45ae8f24e8171..db72d5a703256145590a1c25421270a7fc35bfdf 100644 (file)
@@ -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) {
index 4d3c692a8c2679d3ad60af6795b1cadeec9bd5c0..808af51b1f1b2328fc135641039df259b145d5bc 100644 (file)
@@ -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));