[gjs: 8/18] boxed: Use an unique_ptr to store the FieldMap




commit f0ecb36c9cdc2b13ba5c94a70dc713cd5077895b
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Sep 30 13:37:11 2020 +0200

    boxed: Use an unique_ptr to store the FieldMap
    
    There's no need to do manual memory management here, given that
    unique_ptr will not cost us anything but will handle it for free

 gi/boxed.cpp | 8 ++------
 gi/boxed.h   | 7 +++++--
 2 files changed, 7 insertions(+), 8 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index f3974b64..6874d06b 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -150,12 +150,12 @@ void BoxedInstance::allocate_directly(void) {
  * to do n O(n) lookups, so put put the fields into a hash table and store it on proto->priv
  * for fast lookup. 
  */
-BoxedPrototype::FieldMap* BoxedPrototype::create_field_map(
+std::unique_ptr<BoxedPrototype::FieldMap> BoxedPrototype::create_field_map(
     JSContext* cx, GIStructInfo* struct_info) {
     int n_fields;
     int i;
 
-    auto* result = new BoxedPrototype::FieldMap();
+    auto result = std::make_unique<BoxedPrototype::FieldMap>();
     n_fields = g_struct_info_get_n_fields(struct_info);
     if (!result->reserve(n_fields)) {
         JS_ReportOutOfMemory(cx);
@@ -439,9 +439,6 @@ BoxedInstance::~BoxedInstance() {
 BoxedPrototype::~BoxedPrototype(void) {
     g_clear_pointer(&m_info, g_base_info_unref);
 
-    if (m_field_map)
-        delete m_field_map;
-
     GJS_DEC_COUNTER(boxed_prototype);
 }
 
@@ -880,7 +877,6 @@ BoxedPrototype::BoxedPrototype(GIStructInfo* info, GType gtype)
       m_zero_args_constructor(-1),
       m_default_constructor(-1),
       m_default_constructor_name(JSID_VOID),
-      m_field_map(nullptr),
       m_can_allocate_directly(struct_is_simple(info)) {
     GJS_INC_COUNTER(boxed_prototype);
 }
diff --git a/gi/boxed.h b/gi/boxed.h
index 98b192a8..6bb1f180 100644
--- a/gi/boxed.h
+++ b/gi/boxed.h
@@ -9,6 +9,8 @@
 
 #include <stdint.h>
 
+#include <memory>  // for unique_ptr
+
 #include <girepository.h>
 #include <glib-object.h>
 #include <glib.h>
@@ -87,7 +89,7 @@ class BoxedPrototype : public GIWrapperPrototype<BoxedBase, BoxedPrototype,
     int m_zero_args_constructor;  // -1 if none
     int m_default_constructor;  // -1 if none
     JS::Heap<jsid> m_default_constructor_name;
-    FieldMap* m_field_map;
+    std::unique_ptr<FieldMap> m_field_map;
     bool m_can_allocate_directly : 1;
 
     explicit BoxedPrototype(GIStructInfo* info, GType gtype);
@@ -134,7 +136,8 @@ class BoxedPrototype : public GIWrapperPrototype<BoxedBase, BoxedPrototype,
     // Helper methods
 
     GJS_JSAPI_RETURN_CONVENTION
-    static FieldMap* create_field_map(JSContext* cx, GIStructInfo* struct_info);
+    static std::unique_ptr<FieldMap> create_field_map(
+        JSContext* cx, GIStructInfo* struct_info);
     GJS_JSAPI_RETURN_CONVENTION
     bool ensure_field_map(JSContext* cx);
     GJS_JSAPI_RETURN_CONVENTION


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