[gjs: 1/4] wrapperutils: Use native C++ allocation and deletion for wrappers




commit 6d90b3fcfd9a5c8378225e5aabba15234cf90f6f
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Sep 2 22:59:54 2020 +0200

    wrapperutils: Use native C++ allocation and deletion for wrappers
    
    When allocating a wrapped object we've some unnecessarily complicated
    initializiation logic, where we initially allocate the memory (zero-ing
    it) and then calling the constructor for the allocated memory.
    The same process is repeatead (in inverted order) when deallocating it.
    
    While this may have the benefit of zero-ing the memory for us, it's just
    unncessary as C++ constructors and destructors can handle this for us,
    and we can avoid duplicate writes on the same memory space, leaving the
    initialization fully to the constructors.
    
    So, using simple new/delete and adjusting the few places where we didn't
    initialize the private flags at construction.

 gi/boxed.cpp      | 1 -
 gi/object.cpp     | 5 ++++-
 gi/object.h       | 4 ++--
 gi/wrapperutils.h | 8 +++-----
 4 files changed, 9 insertions(+), 9 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index ed879406..f28b3a07 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -42,7 +42,6 @@ BoxedInstance::BoxedInstance(JSContext* cx, JS::HandleObject obj)
     : GIWrapperInstance(cx, obj),
       m_allocated_directly(false),
       m_owning_ptr(false) {
-    m_ptr = nullptr;
     GJS_INC_COUNTER(boxed_instance);
 }
 
diff --git a/gi/object.cpp b/gi/object.cpp
index fd338090..88bfc231 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1319,7 +1319,10 @@ void ObjectInstance::prepare_shutdown(void) {
 }
 
 ObjectInstance::ObjectInstance(JSContext* cx, JS::HandleObject object)
-    : GIWrapperInstance(cx, object) {
+    : GIWrapperInstance(cx, object),
+      m_wrapper_finalized(false),
+      m_gobj_disposed(false),
+      m_uses_toggle_ref(false) {
     GTypeQuery query;
     type_query_dynamic_safe(&query);
     if (G_LIKELY(query.type))
diff --git a/gi/object.h b/gi/object.h
index e5614072..2fc1cdd8 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -46,8 +46,8 @@ class ObjectPrototype;
 
 class GjsListLink {
  private:
-    ObjectInstance* m_prev;
-    ObjectInstance* m_next;
+    ObjectInstance* m_prev = nullptr;
+    ObjectInstance* m_next = nullptr;
 
  public:
     [[nodiscard]] ObjectInstance* prev() const { return m_prev; }
diff --git a/gi/wrapperutils.h b/gi/wrapperutils.h
index 89b77342..def68332 100644
--- a/gi/wrapperutils.h
+++ b/gi/wrapperutils.h
@@ -1024,7 +1024,7 @@ class GIWrapperPrototype : public Base {
 template <class Base, class Prototype, class Instance, typename Wrapped = void>
 class GIWrapperInstance : public Base {
  protected:
-    Wrapped* m_ptr;
+    Wrapped* m_ptr = nullptr;
 
     explicit GIWrapperInstance(JSContext* cx, JS::HandleObject obj)
         : Base(Prototype::for_js_prototype(cx, obj)) {
@@ -1043,8 +1043,7 @@ class GIWrapperInstance : public Base {
     [[nodiscard]] static Instance* new_for_js_object(JSContext* cx,
                                                      JS::HandleObject obj) {
         g_assert(!JS_GetPrivate(obj));
-        auto* priv = g_new0(Instance, 1);
-        new (priv) Instance(cx, obj);
+        auto* priv = new Instance(cx, obj);
 
         // Init the private variable before we do anything else. If a garbage
         // collection happens when calling the constructor, then this object
@@ -1083,8 +1082,7 @@ class GIWrapperInstance : public Base {
 
  protected:
     void finalize_impl(JSFreeOp*, JSObject*) {
-        static_cast<Instance*>(this)->~Instance();
-        g_free(this);
+        delete static_cast<Instance*>(this);
     }
 
     // Override if necessary


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