[gjs: 2/6] jsapi-util: add definition for gstrfreev auto-pointer



commit 0824b6cd85aa7ec74ac8d8473488e771797cdbae
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Oct 10 04:03:06 2018 +0200

    jsapi-util: add definition for gstrfreev auto-pointer
    
    And use it where convenient

 gjs/coverage.cpp      | 3 +--
 gjs/importer.cpp      | 7 ++-----
 gjs/jsapi-util-args.h | 4 +---
 gjs/jsapi-util.h      | 2 ++
 gjs/profiler.cpp      | 8 +++-----
 5 files changed, 9 insertions(+), 15 deletions(-)
---
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 539a0ea4..9ac2232b 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -200,7 +200,6 @@ write_statistics_internal(GjsCoverage *coverage,
                           GError     **error)
 {
     using AutoCChar = std::unique_ptr<char, decltype(&free)>;
-    using AutoStrv = std::unique_ptr<char *, decltype(&g_strfreev)>;
 
     GjsCoveragePrivate *priv = (GjsCoveragePrivate *) gjs_coverage_get_instance_private(coverage);
 
@@ -224,7 +223,7 @@ write_statistics_internal(GjsCoverage *coverage,
     if (!ostream)
         return nullptr;
 
-    AutoStrv lcov_lines(g_strsplit(lcov.get(), "\n", -1), g_strfreev);
+    GjsAutoStrv lcov_lines = g_strsplit(lcov, "\n", -1);
     GjsAutoChar test_name;
     bool ignoring_file = false;
 
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 6f5a7daf..c9f8400c 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -897,7 +897,6 @@ gjs_create_importer(JSContext          *context,
                     JS::HandleObject    in_object)
 {
     char **paths[2] = {0};
-    char **search_path;
 
     paths[0] = (char**)initial_search_path;
     if (add_standard_search_path) {
@@ -905,19 +904,17 @@ gjs_create_importer(JSContext          *context,
         paths[1] = (char**)gjs_get_search_path();
     }
 
-    search_path = gjs_g_strv_concat(paths, 2);
+    GjsAutoStrv search_path = gjs_g_strv_concat(paths, 2);
 
     JS::RootedObject importer(context, importer_new(context, is_root));
 
     /* API users can replace this property from JS, is the idea */
     if (!gjs_define_string_array(context, importer,
-                                 "searchPath", -1, (const char **)search_path,
+                                 "searchPath", -1, search_path.as<const char *>(),
                                  /* settable (no READONLY) but not deleteable (PERMANENT) */
                                  JSPROP_PERMANENT | JSPROP_RESOLVING))
         g_error("no memory to define importer search path prop");
 
-    g_strfreev(search_path);
-
     if (!define_meta_properties(context, importer, NULL, importer_name, in_object))
         g_error("failed to define meta properties on importer");
 
diff --git a/gjs/jsapi-util-args.h b/gjs/jsapi-util-args.h
index cf12b842..377feed7 100644
--- a/gjs/jsapi-util-args.h
+++ b/gjs/jsapi-util-args.h
@@ -379,7 +379,6 @@ gjs_parse_call_args(JSContext          *cx,
     const char *fmt_iter, *fmt_required, *fmt_optional;
     unsigned n_required = 0, n_total = 0;
     bool optional_args = false, ignore_trailing_args = false, retval;
-    char **parts;
 
     if (*format == '!') {
         ignore_trailing_args = true;
@@ -423,7 +422,7 @@ gjs_parse_call_args(JSContext          *cx,
         return false;
     }
 
-    parts = g_strsplit(format, "|", 2);
+    GjsAutoStrv parts = g_strsplit(format, "|", 2);
     fmt_required = parts[0];
     fmt_optional = parts[1];  /* may be NULL */
 
@@ -431,6 +430,5 @@ gjs_parse_call_args(JSContext          *cx,
                                     ignore_trailing_args, fmt_required,
                                     fmt_optional, 0, params...);
 
-    g_strfreev(parts);
     return retval;
 }
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index b9f9053d..319a2c69 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -70,6 +70,8 @@ struct GjsAutoCharFuncs {
 using GjsAutoChar =
     GjsAutoPointer<char, char, GjsAutoCharFuncs::free, GjsAutoCharFuncs::dup>;
 
+using GjsAutoStrv = GjsAutoPointer<char*, char*, g_strfreev>;
+
 template <typename T>
 using GjsAutoUnref = GjsAutoPointer<T, void, g_object_unref, g_object_ref>;
 
diff --git a/gjs/profiler.cpp b/gjs/profiler.cpp
index 41fc1b79..a23c85b7 100644
--- a/gjs/profiler.cpp
+++ b/gjs/profiler.cpp
@@ -126,8 +126,6 @@ static GjsContext *profiling_context;
 static bool
 gjs_profiler_extract_maps(GjsProfiler *self)
 {
-    using AutoStrv = std::unique_ptr<char *, decltype(&g_strfreev)>;
-
     int64_t now = g_get_monotonic_time() * 1000L;
 
     g_assert(((void) "Profiler must be set up before extracting maps", self));
@@ -140,9 +138,9 @@ gjs_profiler_extract_maps(GjsProfiler *self)
       return false;
     GjsAutoChar content = content_tmp;
 
-    AutoStrv lines(g_strsplit(content, "\n", 0), g_strfreev);
+    GjsAutoStrv lines = g_strsplit(content, "\n", 0);
 
-    for (size_t ix = 0; lines.get()[ix]; ix++) {
+    for (size_t ix = 0; lines[ix]; ix++) {
         char file[256];
         unsigned long start;
         unsigned long end;
@@ -151,7 +149,7 @@ gjs_profiler_extract_maps(GjsProfiler *self)
 
         file[sizeof file - 1] = '\0';
 
-        int r = sscanf(lines.get()[ix], "%lx-%lx %*15s %lx %*x:%*x %lu %255s",
+        int r = sscanf(lines[ix], "%lx-%lx %*15s %lx %*x:%*x %lu %255s",
                        &start, &end, &offset, &inode, file);
         if (r != 5)
             continue;


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