[gjs: 7/10] jsapi-util-root: Add debug_addr() method for pointer types



commit ba7fb121b94415128b8fe67bfd720a373d708d5f
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Jan 19 23:14:14 2019 -0800

    jsapi-util-root: Add debug_addr() method for pointer types
    
    For GjsMaybeOwned<T> where T is a pointer type, we add a debug_addr()
    method which returns the pointer address without triggering a read
    barrier. This is needed for debug methods because we want to be able to
    debug-log things even after they are marked for sweeping. (The read
    barrier will assert in debug mode if the object is already marked for
    sweeping.)

 gi/closure.cpp        | 10 +++++-----
 gjs/jsapi-util-root.h |  7 +++++++
 2 files changed, 12 insertions(+), 5 deletions(-)
---
diff --git a/gi/closure.cpp b/gi/closure.cpp
index 4a6645c3..6f3885e1 100644
--- a/gi/closure.cpp
+++ b/gi/closure.cpp
@@ -105,7 +105,7 @@ static void global_context_finalized(JS::HandleFunction func, void* data) {
     gjs_debug_closure(
         "Context global object destroy notifier on closure %p which calls "
         "object %p",
-        c, c->func.get());
+        c, c->func.debug_addr());
 
     if (c->func) {
         g_assert(c->func == func.get());
@@ -135,7 +135,7 @@ closure_invalidated(gpointer data,
 
     GJS_DEC_COUNTER(closure);
     gjs_debug_closure("Invalidating closure %p which calls function %p",
-                      closure, c->func.get());
+                      closure, c->func.debug_addr());
 
     if (!c->func) {
         gjs_debug_closure("   (closure %p already dead, nothing to do)",
@@ -165,7 +165,7 @@ closure_set_invalid(gpointer  data,
     Closure *self = &((GjsClosure*) closure)->priv;
 
     gjs_debug_closure("Invalidating signal closure %p which calls function %p",
-                      closure, self->func.get());
+                      closure, self->func.debug_addr());
 
     self->func.prevent_collection();
     self->func.reset();
@@ -217,7 +217,7 @@ gjs_closure_invoke(GClosure                   *closure,
         gjs_debug_closure(
             "Closure invocation failed (exception should have been thrown) "
             "closure %p function %p",
-            closure, c->func.get());
+            closure, c->func.debug_addr());
         /* If an exception has been thrown, log it, unless the caller
          * explicitly wants to handle it manually (for example to turn it
          * into a GError), in which case it replaces the return value
@@ -320,7 +320,7 @@ GClosure* gjs_closure_new(JSContext* context, JSFunction* callable,
     g_closure_add_finalize_notifier(&gc->base, NULL, closure_finalize);
 
     gjs_debug_closure("Create closure %p which calls function %p '%s'", gc,
-                      c->func.get(), description);
+                      c->func.debug_addr(), description);
 
     JS_EndRequest(context);
 
diff --git a/gjs/jsapi-util-root.h b/gjs/jsapi-util-root.h
index 427ff0c2..25b09db8 100644
--- a/gjs/jsapi-util-root.h
+++ b/gjs/jsapi-util-root.h
@@ -215,6 +215,13 @@ public:
     }
     operator const T(void) const { return get(); }
 
+    /* Use debug_addr() only for debug logging, because it is unbarriered. */
+    template <typename U = T>
+    GJS_USE const T
+    debug_addr(std::enable_if_t<std::is_pointer<U>::value>* = nullptr) const {
+        return m_rooted ? m_thing.root->get() : m_thing.heap.unbarrieredGet();
+    }
+
     bool
     operator==(const T& other) const
     {


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