[gjs: 4/16] maint: Replace use of g_newa() and g_alloca()




commit 7f7c1ed0a78b120ae46cf11a1a219a384f0159b2
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Nov 20 17:07:27 2020 -0800

    maint: Replace use of g_newa() and g_alloca()
    
    We do use stack allocation with g_newa() for performance reasons in some
    places, but these are not hot paths, so there is no need to do it here.
    Cppcheck now warns about this, so just use GjsAutoPointer and g_new()
    instead.

 gi/object.cpp  |  6 ++----
 gi/private.cpp |  7 +++----
 gi/value.cpp   | 29 +++++++++--------------------
 3 files changed, 14 insertions(+), 28 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 3d58f930..0e5853cc 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1904,7 +1904,6 @@ ObjectInstance::emit_impl(JSContext          *context,
     guint signal_id;
     GQuark signal_detail;
     GSignalQuery signal_query;
-    GValue *instance_and_args;
     GValue rvalue = G_VALUE_INIT;
     unsigned int i;
     bool failed;
@@ -1940,9 +1939,8 @@ ObjectInstance::emit_impl(JSContext          *context,
         g_value_init(&rvalue, signal_query.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
     }
 
-    instance_and_args = g_newa(GValue, signal_query.n_params + 1);
-    memset(instance_and_args, 0, sizeof(GValue) * (signal_query.n_params + 1));
-
+    GjsAutoPointer<GValue> instance_and_args =
+        g_new0(GValue, signal_query.n_params + 1);
     g_value_init(&instance_and_args[0], gtype());
     g_value_set_instance(&instance_and_args[0], m_ptr);
 
diff --git a/gi/private.cpp b/gi/private.cpp
index c1ad5e6b..f3c911d3 100644
--- a/gi/private.cpp
+++ b/gi/private.cpp
@@ -192,7 +192,7 @@ static bool gjs_register_interface(JSContext* cx, unsigned argc,
                                                  &n_interfaces, &n_properties))
         return false;
 
-    GType* iface_types = g_newa(GType, n_interfaces);
+    GjsAutoPointer<GType> iface_types = g_new(GType, n_interfaces);
 
     /* We do interface addition in two passes so that any failure
        is caught early, before registering the GType (which we can't undo) */
@@ -266,8 +266,7 @@ static bool gjs_register_type(JSContext* cx, unsigned argc, JS::Value* vp) {
                                                  &n_interfaces, &n_properties))
         return false;
 
-    auto* iface_types =
-        static_cast<GType*>(g_alloca(sizeof(GType) * n_interfaces));
+    GjsAutoPointer<GType> iface_types = g_new(GType, n_interfaces);
 
     /* We do interface addition in two passes so that any failure
        is caught early, before registering the GType (which we can't undo) */
@@ -369,7 +368,7 @@ static bool gjs_signal_new(JSContext* cx, unsigned argc, JS::Value* vp) {
     if (!JS::GetArrayLength(cx, params_obj, &n_parameters))
         return false;
 
-    GType* params = g_newa(GType, n_parameters);
+    GjsAutoPointer<GType> params = g_new(GType, n_parameters);
     JS::RootedValue gtype_val(cx);
     for (uint32_t ix = 0; ix < n_parameters; ix++) {
         if (!JS_GetElement(cx, params_obj, ix, &gtype_val) ||
diff --git a/gi/value.cpp b/gi/value.cpp
index bca3aec7..7aabaa5a 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -124,9 +124,6 @@ closure_marshal(GClosure        *closure,
     unsigned i;
     GSignalQuery signal_query = { 0, };
     GISignalInfo *signal_info;
-    bool *skip;
-    int *array_len_indices_for;
-    GITypeInfo **type_info_for;
 
     gjs_debug_marshal(GJS_DEBUG_GCLOSURE,
                       "Marshal closure %p",
@@ -187,13 +184,12 @@ closure_marshal(GClosure        *closure,
     /* Check if any parameters, such as array lengths, need to be eliminated
      * before we invoke the closure.
      */
-    skip = g_newa(bool, n_param_values);
-    memset(skip, 0, sizeof (bool) * n_param_values);
-    array_len_indices_for = g_newa(int, n_param_values);
+    GjsAutoPointer<bool> skip = g_new0(bool, n_param_values);
+    GjsAutoPointer<int> array_len_indices_for = g_new(int, n_param_values);
     for(i = 0; i < n_param_values; i++)
         array_len_indices_for[i] = -1;
-    type_info_for = g_newa(GITypeInfo *, n_param_values);
-    memset(type_info_for, 0, sizeof (gpointer) * n_param_values);
+    GjsAutoPointer<GITypeInfo> type_info_for =
+        g_new0(GITypeInfo, n_param_values);
 
     signal_info = get_signal_info_if_available(&signal_query);
     if (signal_info) {
@@ -203,9 +199,9 @@ closure_marshal(GClosure        *closure,
             int array_len_pos;
 
             arg_info = g_callable_info_get_arg(signal_info, i - 1);
-            type_info_for[i] = g_arg_info_get_type(arg_info);
+            g_arg_info_load_type(arg_info, &type_info_for[i]);
 
-            array_len_pos = g_type_info_get_array_length(type_info_for[i]);
+            array_len_pos = g_type_info_get_array_length(&type_info_for[i]);
             if (array_len_pos != -1) {
                 skip[array_len_pos + 1] = true;
                 array_len_indices_for[i] = array_len_pos + 1;
@@ -240,12 +236,9 @@ closure_marshal(GClosure        *closure,
         array_len_index = array_len_indices_for[i];
         if (array_len_index != -1) {
             const GValue *array_len_gval = &param_values[array_len_index];
-            res = gjs_value_from_array_and_length_values(context,
-                                                         &argv_to_append,
-                                                         type_info_for[i],
-                                                         gval, array_len_gval,
-                                                         no_copy, &signal_query,
-                                                         array_len_index);
+            res = gjs_value_from_array_and_length_values(
+                context, &argv_to_append, &type_info_for[i], gval,
+                array_len_gval, no_copy, &signal_query, array_len_index);
         } else {
             res = gjs_value_from_g_value_internal(context,
                                                   &argv_to_append,
@@ -264,10 +257,6 @@ closure_marshal(GClosure        *closure,
         argv.infallibleAppend(argv_to_append);
     }
 
-    for (i = 1; i < n_param_values; i++)
-        if (type_info_for[i])
-            g_base_info_unref((GIBaseInfo *)type_info_for[i]);
-
     JS::RootedValue rval(context);
     mozilla::Unused << gjs_closure_invoke(closure, nullptr, argv, &rval, false);
     // Any exception now pending, is handled when returning control to JS


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