[gjs: 10/18] gi: Use more GjsAutoPointer's in wrapper classes




commit 868922992398a8f809b3505c995fe695e9a76cca
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Sep 30 13:56:23 2020 +0200

    gi: Use more GjsAutoPointer's in wrapper classes
    
    Being now the autopointer as heavy as having a normal pointer, we can
    just use it more without being worried of the extra memory allocation,
    as it in fact has none.

 gi/boxed.cpp       | 24 +++++++-----------------
 gi/enumeration.cpp | 10 ++--------
 gi/fundamental.cpp |  7 ++-----
 gi/fundamental.h   |  3 ++-
 gi/gerror.cpp      | 11 +++--------
 gi/object.cpp      |  1 -
 gi/wrapperutils.h  |  4 +---
 7 files changed, 17 insertions(+), 43 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 6874d06b..2b000280 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -437,8 +437,6 @@ BoxedInstance::~BoxedInstance() {
 }
 
 BoxedPrototype::~BoxedPrototype(void) {
-    g_clear_pointer(&m_info, g_base_info_unref);
-
     GJS_DEC_COUNTER(boxed_prototype);
 }
 
@@ -764,12 +762,9 @@ const struct JSClass BoxedBase::klass = {
     if (g_type_info_is_pointer(type_info)) {
         if (g_type_info_get_tag(type_info) == GI_TYPE_TAG_ARRAY &&
             g_type_info_get_array_type(type_info) == GI_ARRAY_TYPE_C) {
-            GITypeInfo *param_info;
-
-            param_info = g_type_info_get_param_type(type_info, 0);
+            GjsAutoBaseInfo param_info =
+                g_type_info_get_param_type(type_info, 0);
             is_simple = type_can_be_allocated_directly(param_info);
-
-            g_base_info_unref((GIBaseInfo*)param_info);
         } else if (g_type_info_get_tag(type_info) == GI_TYPE_TAG_VOID) {
             return true;
         } else {
@@ -779,8 +774,8 @@ const struct JSClass BoxedBase::klass = {
         switch (g_type_info_get_tag(type_info)) {
         case GI_TYPE_TAG_INTERFACE:
             {
-                GIBaseInfo *interface = g_type_info_get_interface(type_info);
-                switch (g_base_info_get_type(interface)) {
+            GjsAutoBaseInfo interface = g_type_info_get_interface(type_info);
+            switch (g_base_info_get_type(interface)) {
                 case GI_INFO_TYPE_BOXED:
                 case GI_INFO_TYPE_STRUCT:
                     if (!struct_is_simple((GIStructInfo *)interface))
@@ -813,9 +808,7 @@ const struct JSClass BoxedBase::klass = {
                 case GI_INFO_TYPE_FLAGS:
                 default:
                     break;
-                }
-
-                g_base_info_unref(interface);
+            }
                 break;
             }
         case GI_TYPE_TAG_BOOLEAN:
@@ -860,13 +853,10 @@ const struct JSClass BoxedBase::klass = {
         return false;
 
     for (i = 0; i < n_fields && is_simple; i++) {
-        GIFieldInfo *field_info = g_struct_info_get_field(info, i);
-        GITypeInfo *type_info = g_field_info_get_type(field_info);
+        GjsAutoBaseInfo field_info = g_struct_info_get_field(info, i);
+        GjsAutoBaseInfo type_info = g_field_info_get_type(field_info);
 
         is_simple = type_can_be_allocated_directly(type_info);
-
-        g_base_info_unref((GIBaseInfo *)field_info);
-        g_base_info_unref((GIBaseInfo *)type_info);
     }
 
     return is_simple;
diff --git a/gi/enumeration.cpp b/gi/enumeration.cpp
index fa8dffb4..b3ae6f3c 100644
--- a/gi/enumeration.cpp
+++ b/gi/enumeration.cpp
@@ -72,16 +72,10 @@ gjs_define_enum_values(JSContext       *context,
      */
     n_values = g_enum_info_get_n_values(info);
     for (i = 0; i < n_values; ++i) {
-        GIValueInfo *value_info = g_enum_info_get_value(info, i);
-        bool failed;
+        GjsAutoBaseInfo value_info = g_enum_info_get_value(info, i);
 
-        failed = !gjs_define_enum_value(context, in_object, value_info);
-
-        g_base_info_unref( (GIBaseInfo*) value_info);
-
-        if (failed) {
+        if (!gjs_define_enum_value(context, in_object, value_info))
             return false;
-        }
     }
     return true;
 }
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 8e690a46..70419785 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -70,16 +70,14 @@ bool FundamentalInstance::associate_js_instance(JSContext* cx, JSObject* object,
     n_methods = g_object_info_get_n_methods(info);
 
     for (i = 0; i < n_methods; ++i) {
-        GIFunctionInfo *func_info;
+        GjsAutoFunctionInfo func_info;
         GIFunctionInfoFlags flags;
 
         func_info = g_object_info_get_method(info, i);
 
         flags = g_function_info_get_flags(func_info);
         if ((flags & GI_FUNCTION_IS_CONSTRUCTOR) != 0)
-            return func_info;
-
-        g_base_info_unref((GIBaseInfo *) func_info);
+            return func_info.release();
     }
 
     return nullptr;
@@ -231,7 +229,6 @@ FundamentalPrototype::FundamentalPrototype(GIObjectInfo* info, GType gtype)
 }
 
 FundamentalPrototype::~FundamentalPrototype(void) {
-    g_clear_pointer(&m_constructor_info, g_base_info_unref);
     GJS_DEC_COUNTER(fundamental_prototype);
 }
 
diff --git a/gi/fundamental.h b/gi/fundamental.h
index d3429783..a3faa783 100644
--- a/gi/fundamental.h
+++ b/gi/fundamental.h
@@ -14,6 +14,7 @@
 #include <js/TypeDecls.h>
 
 #include "gi/wrapperutils.h"
+#include "gjs/jsapi-util.h"  // for GjsAutoCallableInfo
 #include "gjs/macros.h"
 #include "util/log.h"
 
@@ -67,7 +68,7 @@ class FundamentalPrototype
     GIObjectInfoUnrefFunction m_unref_function;
     GIObjectInfoGetValueFunction m_get_value_function;
     GIObjectInfoSetValueFunction m_set_value_function;
-    GICallableInfo* m_constructor_info;
+    GjsAutoCallableInfo m_constructor_info;
 
     explicit FundamentalPrototype(GIObjectInfo* info, GType gtype);
     ~FundamentalPrototype(void);
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index c5f146a5..04e88f7c 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -352,14 +352,9 @@ JSObject* ErrorInstance::object_for_c_ptr(JSContext* context, GError* gerror) {
     if (!info) {
         /* We don't have error domain metadata */
         /* Marshal the error as a plain GError */
-        GIBaseInfo *glib_boxed;
-        JSObject *retval;
-
-        glib_boxed = g_irepository_find_by_name(nullptr, "GLib", "Error");
-        retval = BoxedInstance::new_for_c_struct(context, glib_boxed, gerror);
-
-        g_base_info_unref(glib_boxed);
-        return retval;
+        GjsAutoBaseInfo glib_boxed =
+            g_irepository_find_by_name(nullptr, "GLib", "Error");
+        return BoxedInstance::new_for_c_struct(context, glib_boxed, gerror);
     }
 
     gjs_debug_marshal(GJS_DEBUG_GBOXED,
diff --git a/gi/object.cpp b/gi/object.cpp
index 1d7d6df6..17b1bedd 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1685,7 +1685,6 @@ ObjectInstance::~ObjectInstance() {
 ObjectPrototype::~ObjectPrototype() {
     invalidate_closure_list(&m_vfuncs);
 
-    g_clear_pointer(&m_info, g_base_info_unref);
     g_type_class_unref(g_type_class_peek(m_gtype));
 
     GJS_DEC_COUNTER(object_prototype);
diff --git a/gi/wrapperutils.h b/gi/wrapperutils.h
index 544bae3b..2745c5bc 100644
--- a/gi/wrapperutils.h
+++ b/gi/wrapperutils.h
@@ -712,7 +712,7 @@ class GIWrapperPrototype : public Base {
     // not exposed through introspection, such as GLocalFile. Not all subclasses
     // of GIWrapperPrototype support this. Object and Interface support it in
     // any case.
-    Info* m_info;
+    GjsAutoBaseInfo m_info;
     GType m_gtype;
 
     explicit GIWrapperPrototype(Info* info, GType gtype)
@@ -720,8 +720,6 @@ class GIWrapperPrototype : public Base {
         Base::debug_lifecycle("Prototype constructor");
     }
 
-    ~GIWrapperPrototype(void) { g_clear_pointer(&m_info, g_base_info_unref); }
-
     /*
      * GIWrapperPrototype::init:
      *


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