[gjs: 13/25] object: Use "associated memory" API instead of malloc counter



commit 8343b7aee4018e878019fad5d7735f06ca68a635
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon May 20 17:52:16 2019 -0700

    object: Use "associated memory" API instead of malloc counter
    
    This new API replaces the old malloc counter API. The calls to
    JS::AddAssociatedMemory() and JS::RemoveAssociatedMemory() must be
    exactly matched, so we have to move things around a bit in order to
    ensure that every object gets associated memory. Previously, we did it
    sort of hand-wavingly in init_impl().
    
    SpiderMonkey provides 5 slots in the JS::MemoryUse enum for use by
    embedders so we start defining aliases for those in wrapperutils.h.
    
    We should really add this to wrapperutils.h so that boxed types, etc.
    also get memory associated with them, but I don't think we can do that
    generally until https://gitlab.gnome.org/GNOME/glib/issues/623 is fixed
    because for custom JS types we need the workaround in
    ObjectBase::type_query_dynamic_safe().

 gi/object.cpp     | 22 ++++++++++++++++------
 gi/object.h       |  1 +
 gi/wrapperutils.h |  5 +++++
 3 files changed, 22 insertions(+), 6 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index affac701..7e5c5ce2 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1276,6 +1276,12 @@ void ObjectInstance::prepare_shutdown(void) {
 
 ObjectInstance::ObjectInstance(JSContext* cx, JS::HandleObject object)
     : GIWrapperInstance(cx, object) {
+    GTypeQuery query;
+    type_query_dynamic_safe(&query);
+    if (G_LIKELY(query.type))
+        JS::AddAssociatedMemory(object, query.instance_size,
+                                MemoryUse::GObjectInstanceStruct);
+
     GJS_INC_COUNTER(object_instance);
 }
 
@@ -1430,8 +1436,6 @@ ObjectInstance::init_impl(JSContext              *context,
                           const JS::CallArgs&     args,
                           JS::MutableHandleObject object)
 {
-    GTypeQuery query;
-
     g_assert(gtype() != G_TYPE_NONE);
 
     if (args.length() > 1 &&
@@ -1498,10 +1502,6 @@ ObjectInstance::init_impl(JSContext              *context,
         return true;
     }
 
-    type_query_dynamic_safe(&query);
-    if (G_LIKELY (query.type))
-        JS_updateMallocCounter(context, query.instance_size);
-
     if (G_IS_INITIALLY_UNOWNED(gobj) &&
         !g_object_is_floating(gobj)) {
         /* GtkWindow does not return a ref to caller of g_object_new.
@@ -1564,6 +1564,16 @@ void ObjectPrototype::trace_impl(JSTracer* tracer) {
     m_field_cache.trace(tracer);
 }
 
+void ObjectInstance::finalize_impl(JSFreeOp* fop, JSObject* obj) {
+    GTypeQuery query;
+    type_query_dynamic_safe(&query);
+    if (G_LIKELY(query.type))
+        JS::RemoveAssociatedMemory(obj, query.instance_size,
+                                   MemoryUse::GObjectInstanceStruct);
+
+    GIWrapperInstance::finalize_impl(fop, obj);
+}
+
 ObjectInstance::~ObjectInstance() {
     TRACE(GJS_OBJECT_WRAPPER_FINALIZE(this, m_ptr, ns(), name()));
 
diff --git a/gi/object.h b/gi/object.h
index 1ba36f9d..3108a3bb 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -405,6 +405,7 @@ class ObjectInstance : public GIWrapperInstance<ObjectBase, ObjectPrototype,
     GJS_JSAPI_RETURN_CONVENTION
     bool add_property_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
                            JS::HandleValue value);
+    void finalize_impl(JSFreeOp* fop, JSObject* obj);
 
     /* JS property getters/setters */
 
diff --git a/gi/wrapperutils.h b/gi/wrapperutils.h
index 11201fb2..67bb576f 100644
--- a/gi/wrapperutils.h
+++ b/gi/wrapperutils.h
@@ -35,6 +35,7 @@
 #include <glib.h>
 
 #include "gjs/jsapi-wrapper.h"
+#include "js/MemoryFunctions.h"
 
 #include "gjs/atoms.h"
 #include "gjs/context-private.h"
@@ -63,6 +64,10 @@ namespace InfoType {
 enum Tag { Enum, Interface, Object, Struct, Union };
 }
 
+namespace MemoryUse {
+constexpr JS::MemoryUse GObjectInstanceStruct = JS::MemoryUse::Embedding1;
+}
+
 struct GjsTypecheckNoThrow {};
 
 /*


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