[gjs/wip/ptomato/warnings: 4/10] js: Use std::vector instead of GArray



commit f076e7f7a47c1b59ec0d6d4d54330ce01d6673d3
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Oct 2 12:47:55 2016 -0700

    js: Use std::vector instead of GArray
    
    C++ does not approve of type-punning GArray's internal char *data
    pointer. This causes -Wcast-align because the alignment requirements on
    the underlying array may be different depending on which type it is.
    Casting char * to int * when the char array does not have int alignment
    may be slower on some architectures, or it may crash on some, for example
    older ARM architectures.
    
    We use std::vector instead since that does not have the same problem.
    In cairo-context.cpp we go ahead and switch to RAII style in the function
    where we replace GArray, since otherwise the gotos would cross the
    initialization boundary.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=773297

 gi/object.cpp             |   52 ++++++++++++--------------------------------
 modules/cairo-context.cpp |   28 ++++++++++-------------
 2 files changed, 26 insertions(+), 54 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 07dfe0a..d993442 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -25,6 +25,7 @@
 
 #include <memory>
 #include <string.h>
+#include <vector>
 
 #include "gjs/gi.h"
 #include "object.h"
@@ -659,36 +660,25 @@ free_g_params(GParameter *params,
     for (i = 0; i < n_params; ++i) {
         g_value_unset(&params[i].value);
     }
-    g_free(params);
 }
 
 /* Set properties from args to constructor (argv[0] is supposed to be
  * a hash)
  */
 static bool
-object_instance_props_to_g_parameters(JSContext   *context,
-                                      JSObject    *obj,
-                                      unsigned     argc,
-                                      JS::Value   *argv,
-                                      GType        gtype,
-                                      GParameter **gparams_p,
-                                      int         *n_gparams_p)
+object_instance_props_to_g_parameters(JSContext               *context,
+                                      JSObject                *obj,
+                                      unsigned                 argc,
+                                      JS::Value               *argv,
+                                      GType                    gtype,
+                                      std::vector<GParameter>& gparams)
 {
     JSObject *props;
     JSObject *iter;
     jsid prop_id;
-    GArray *gparams;
-
-    if (gparams_p)
-        *gparams_p = NULL;
-    if (n_gparams_p)
-        *n_gparams_p = 0;
-
-    gparams = g_array_new(/* nul term */ false, /* clear */ true,
-                          sizeof(GParameter));
 
     if (argc == 0 || argv[0].isUndefined())
-        goto out;
+        return true;
 
     if (!argv[0].isObject()) {
         gjs_throw(context, "argument should be a hash with props to set");
@@ -736,29 +726,17 @@ object_instance_props_to_g_parameters(JSContext   *context,
 
         g_free(name);
 
-        g_array_append_val(gparams, gparam);
+        gparams.push_back(gparam);
 
         prop_id = JSID_VOID;
         if (!JS_NextProperty(context, iter, &prop_id))
             goto free_array_and_fail;
     }
 
- out:
-    if (n_gparams_p)
-        *n_gparams_p = gparams->len;
-    if (gparams_p)
-        *gparams_p = (GParameter*) g_array_free(gparams, false);
-
     return true;
 
  free_array_and_fail:
-    {
-        GParameter *to_free;
-        int count;
-        count = gparams->len;
-        to_free = (GParameter*) g_array_free(gparams, false);
-        free_g_params(to_free, count);
-    }
+    free_g_params(&gparams[0], gparams.size());
     return false;
 }
 
@@ -1249,8 +1227,7 @@ object_instance_init (JSContext              *context,
 {
     ObjectInstance *priv;
     GType gtype;
-    GParameter *params;
-    int n_params;
+    std::vector<GParameter> params;
     GTypeQuery query;
     GObject *gobj;
 
@@ -1260,8 +1237,7 @@ object_instance_init (JSContext              *context,
     g_assert(gtype != G_TYPE_NONE);
 
     if (!object_instance_props_to_g_parameters(context, object, argc, argv,
-                                               gtype,
-                                               &params, &n_params)) {
+                                               gtype, params)) {
         return false;
     }
 
@@ -1276,9 +1252,9 @@ object_instance_init (JSContext              *context,
                               "object_init_list");
     }
 
-    gobj = (GObject*) g_object_newv(gtype, n_params, params);
+    gobj = (GObject*) g_object_newv(gtype, params.size(), &params[0]);
 
-    free_g_params(params, n_params);
+    free_g_params(&params[0], params.size());
 
     JS::RootedObject old_jsobj(context, peek_js_obj(gobj));
     if (old_jsobj != NULL && old_jsobj != object) {
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index 5355352..8ed57e0 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -22,6 +22,8 @@
 
 #include <config.h>
 
+#include <vector>
+
 #include "gi/foreign.h"
 #include "gjs/jsapi-util-args.h"
 #include "gjs/jsapi-wrapper.h"
@@ -532,55 +534,49 @@ setDash_func(JSContext *context,
     cairo_t *cr = priv ? priv->cr : NULL;
     JS::RootedObject dashes(context);
     double offset;
-    bool retval = false;
     guint len;
-    GArray *dashes_c = NULL;
 
     if (!gjs_parse_call_args(context, "setDash", argv, "of",
                              "dashes", &dashes,
                              "offset", &offset))
         return false;
 
-
     if (!JS_IsArrayObject(context, dashes)) {
         gjs_throw(context, "dashes must be an array");
-        goto out;
+        return false;
     }
 
     if (!JS_GetArrayLength(context, dashes, &len)) {
         gjs_throw(context, "Can't get length of dashes");
-        goto out;
+        return false;
     }
 
-    dashes_c = g_array_sized_new (false, false, sizeof(double), len);
+    std::vector<double> dashes_c;
+    dashes_c.reserve(len);
     for (i = 0; i < len; ++i) {
         JS::Value elem;
         double b;
 
         elem = JS::UndefinedValue();
         if (!JS_GetElement(context, dashes, i, &elem)) {
-            goto out;
+            return false;
         }
         if (elem.isUndefined())
             continue;
 
         if (!JS::ToNumber(context, elem, &b))
-            goto out;
+            return false;
         if (b <= 0) {
             gjs_throw(context, "Dash value must be positive");
-            goto out;
+            return false;
         }
 
-        g_array_append_val(dashes_c, b);
+        dashes_c.push_back(b);
     }
 
-    cairo_set_dash(cr, (double*)dashes_c->data, dashes_c->len, offset);
+    cairo_set_dash(cr, &dashes_c[0], dashes_c.size(), offset);
     argv.rval().setUndefined();
-    retval = true;
- out:
-    if (dashes_c != NULL)
-        g_array_free (dashes_c, true);
-    return retval;
+    return true;
 }
 
 static JSBool


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