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



commit 93fca097efa6def8895abe6bbb338ed9329d6064
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.
    
    In jsapi-util-array.cpp there is more GArray code but it is not actually
    used anywhere in the GJS codebase. There, we simply insert a pragma
    ignoring the warnings.

 gi/object.cpp             |   42 +++++++++---------------------------------
 modules/cairo-context.cpp |   28 ++++++++++++----------------
 2 files changed, 21 insertions(+), 49 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index e7cd0b6..d66816c 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,7 +660,6 @@ 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
@@ -671,24 +671,14 @@ object_instance_props_to_g_parameters(JSContext   *context,
                                       unsigned     argc,
                                       JS::Value   *argv,
                                       GType        gtype,
-                                      GParameter **gparams_p,
-                                      int         *n_gparams_p)
+                                      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 0309783..ad8d54a 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -22,6 +22,8 @@
 
 #include <config.h>
 
+#include <vector>
+
 #include "gjs/compat.h"
 #include "gi/foreign.h"
 #include "gjs/jsapi-util-args.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]