lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / include / babeltrace / object-internal.h
index 545a20aadd21a7f9f9bd3b78971f9e64b0c5182b..4efba28e8278f8b721c7f06a45794a307dc61ec9 100644 (file)
  * SOFTWARE.
  */
 
+#include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/ref-internal.h>
 #include <babeltrace/ref.h>
+#include <babeltrace/assert-internal.h>
+#include <stdbool.h>
 
 /**
  * All objects publicly exposed by Babeltrace APIs must contain this structure
@@ -44,6 +47,12 @@ struct bt_object {
        bt_object_release_func parent_is_owner_listener;
        /* @see doc/ref-counting.md */
        struct bt_object *parent;
+
+       /*
+        * True if this object is shared, that is, it uses reference
+        * counting. Only used in developer mode.
+        */
+       bool is_shared;
 };
 
 static inline
@@ -59,7 +68,12 @@ void bt_object_release(void *ptr)
 {
        struct bt_object *obj = ptr;
 
-       if (obj && obj->release && !bt_object_get_ref_count(obj)) {
+#ifdef BT_LOGV
+       BT_LOGV("Releasing object: addr=%p, ref-count=%lu", ptr,
+               obj->ref_count.count);
+#endif
+
+       if (obj && obj->release && bt_object_get_ref_count(obj) == 0) {
                obj->release(obj);
        }
 }
@@ -68,7 +82,14 @@ static inline
 void generic_release(struct bt_object *obj)
 {
        if (obj->parent) {
-               void *parent = obj->parent;
+               struct bt_object *parent = obj->parent;
+
+#ifdef BT_LOGV
+               BT_LOGV("Releasing parented object: addr=%p, ref-count=%lu, "
+                       "parent-addr=%p, parent-ref-count=%lu",
+                       obj, obj->ref_count.count,
+                       parent, parent->ref_count.count);
+#endif
 
                if (obj->parent_is_owner_listener) {
                        /*
@@ -110,6 +131,11 @@ void bt_object_set_parent(void *child_ptr, void *parent)
                return;
        }
 
+#ifdef BT_LOGV
+       BT_LOGV("Setting object's parent: addr=%p, parent-addr=%p",
+               child_ptr, parent);
+#endif
+
        /*
         * It is assumed that a "child" being "parented" is publicly reachable.
         * Therefore, a reference to its parent must be taken. The reference
@@ -120,6 +146,18 @@ void bt_object_set_parent(void *child_ptr, void *parent)
        child->parent = bt_get(parent);
 }
 
+#ifdef BT_DEV_MODE
+static inline
+void _bt_object_set_is_shared(struct bt_object *obj, bool is_shared)
+{
+       obj->is_shared = is_shared;
+}
+
+# define bt_object_set_is_shared       _bt_object_set_is_shared
+#else
+# define bt_object_set_is_shared(_obj, _is_shared)
+#endif
+
 static inline
 void bt_object_init(void *ptr, bt_object_release_func release)
 {
@@ -127,6 +165,7 @@ void bt_object_init(void *ptr, bt_object_release_func release)
 
        obj->release = release;
        obj->parent = NULL;
+       bt_object_set_is_shared(obj, true);
        bt_ref_init(&obj->ref_count, generic_release);
 }
 
@@ -134,7 +173,7 @@ static inline
 void bt_object_set_parent_is_owner_listener(void *obj,
                bt_object_release_func cb)
 {
-       assert(obj);
+       BT_ASSERT(obj);
        ((struct bt_object *) obj)->parent_is_owner_listener = cb;
 }
 
This page took 0.024407 seconds and 4 git commands to generate.