[gjs: 3/9] gtype: reuse heap wrapper from cached ones
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 3/9] gtype: reuse heap wrapper from cached ones
- Date: Tue, 6 Nov 2018 04:06:07 +0000 (UTC)
commit d174bb8f86f3827454b1ff3d9bdc4b0bdeace8c1
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date: Sat Oct 13 10:30:51 2018 -0500
gtype: reuse heap wrapper from cached ones
gi/gtype.cpp | 44 ++++++++++++--------------------------------
1 file changed, 12 insertions(+), 32 deletions(-)
---
diff --git a/gi/gtype.cpp b/gi/gtype.cpp
index c211f65c..5ef79579 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -47,36 +47,19 @@ GJS_DEFINE_PROTO_ABSTRACT("GIRepositoryGType", gtype,
/* priv_from_js adds a "*", so this returns "void *" */
GJS_DEFINE_PRIV_FROM_JS(void, gjs_gtype_class);
-GJS_USE
-static GQuark
-gjs_get_gtype_wrapper_quark(void)
-{
- static gsize once_init = 0;
- static GQuark value = 0;
- if (g_once_init_enter(&once_init)) {
- value = g_quark_from_string("gjs-gtype-wrapper");
- g_once_init_leave(&once_init, 1);
- }
- return value;
-}
-
static void
update_gtype_weak_pointers(JSContext *cx,
JSCompartment *compartment,
void *data)
{
for (auto iter = weak_pointer_list.begin(); iter != weak_pointer_list.end(); ) {
- GType gtype = iter->first;
- auto heap_wrapper = static_cast<JS::Heap<JSObject *> *>(
- g_type_get_qdata(gtype, gjs_get_gtype_wrapper_quark()));
+ JS::Heap<JSObject *> *heap_wrapper = iter->second.get();
JS_UpdateWeakPointerAfterGC(heap_wrapper);
/* No read barriers are needed if the only thing we are doing with the
* pointer is comparing it to nullptr. */
- if (heap_wrapper->unbarrieredGet() == nullptr) {
- g_type_set_qdata(gtype, gjs_get_gtype_wrapper_quark(), nullptr);
+ if (heap_wrapper->unbarrieredGet() == nullptr)
iter = weak_pointer_list.erase(iter);
- }
else
iter++;
}
@@ -102,7 +85,6 @@ gjs_gtype_finalize(JSFreeOp *fop,
if (G_UNLIKELY(gtype == 0))
return;
- g_type_set_qdata(gtype, gjs_get_gtype_wrapper_quark(), NULL);
weak_pointer_list.erase(gtype);
}
@@ -168,30 +150,28 @@ gjs_gtype_create_gtype_wrapper (JSContext *context,
JSAutoRequest ar(context);
- auto heap_wrapper =
- static_cast<JS::Heap<JSObject *> *>(g_type_get_qdata(gtype, gjs_get_gtype_wrapper_quark()));
- if (heap_wrapper != nullptr)
- return *heap_wrapper;
+ auto heap_wrapper_it = weak_pointer_list.find(gtype);
+ if (heap_wrapper_it != std::end(weak_pointer_list))
+ return *heap_wrapper_it->second;
JS::RootedObject proto(context);
if (!gjs_gtype_define_proto(context, nullptr, &proto))
return nullptr;
- auto unique_wrapper = std::make_unique<JS::Heap<JSObject *>>();
- *unique_wrapper = JS_NewObjectWithGivenProto(context, &gjs_gtype_class, proto);
- if (*unique_wrapper == nullptr)
+ auto heap_wrapper = std::make_unique<JS::Heap<JSObject *>>();
+ if (!(*heap_wrapper =
+ JS_NewObjectWithGivenProto(context, &gjs_gtype_class, proto)))
return nullptr;
- JS_SetPrivate(*unique_wrapper, GSIZE_TO_POINTER(gtype));
+ JS_SetPrivate(*heap_wrapper, GSIZE_TO_POINTER(gtype));
ensure_weak_pointer_callback(context);
/* Saving a reference to the wrapper pointer, as heap_wrapper will be
* nullified by std::move */
- heap_wrapper = unique_wrapper.get();
- g_type_set_qdata(gtype, gjs_get_gtype_wrapper_quark(), heap_wrapper);
- weak_pointer_list.insert({gtype, std::move(unique_wrapper)});
+ JSObject *gtype_wrapper = *heap_wrapper;
+ weak_pointer_list.insert({gtype, std::move(heap_wrapper)});
- return *heap_wrapper;
+ return gtype_wrapper;
}
GJS_JSAPI_RETURN_CONVENTION
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]