Fix: headers: add missing end of `extern "C"` curly brackets (for C++)
[babeltrace.git] / include / babeltrace / object-internal.h
index 70391b3b95b003ddee8b9952027a05d355490a8b..00f48ef675e6bdd3cb0b4ebca47ca85c6a1ddc95 100644 (file)
@@ -2,12 +2,9 @@
 #define BABELTRACE_OBJECT_INTERNAL_H
 
 /*
- * Babeltrace - Base object
- *
+ * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
  * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
@@ -38,10 +35,10 @@ typedef void (*bt_object_parent_is_owner_listener_func)(
                struct bt_object *);
 
 static inline
-void bt_object_get_no_null_check(struct bt_object *obj);
+void bt_object_get_no_null_check(const void *obj);
 
 static inline
-void bt_object_put_no_null_check(struct bt_object *obj);
+void bt_object_put_no_null_check(const void *obj);
 
 /*
  * Babeltrace object base.
@@ -91,24 +88,29 @@ struct bt_object {
 };
 
 static inline
-unsigned long long bt_object_get_ref_count(struct bt_object *obj)
+unsigned long long bt_object_get_ref_count(const struct bt_object *c_obj)
 {
+       struct bt_object *obj = (void *) c_obj;
+
        BT_ASSERT(obj);
        BT_ASSERT(obj->is_shared);
        return obj->ref_count;
 }
 
 static inline
-struct bt_object *bt_object_borrow_parent(struct bt_object *obj)
+struct bt_object *bt_object_borrow_parent(const struct bt_object *c_obj)
 {
+       struct bt_object *obj = (void *) c_obj;
+
        BT_ASSERT(obj);
        BT_ASSERT(obj->is_shared);
        return obj->parent;
 }
 
 static inline
-struct bt_object *bt_object_get_parent(struct bt_object *obj)
+struct bt_object *bt_object_get_parent(const struct bt_object *c_obj)
 {
+       struct bt_object *obj = (void *) c_obj;
        struct bt_object *parent = bt_object_borrow_parent(obj);
 
        if (parent) {
@@ -243,8 +245,10 @@ void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj,
 }
 
 static inline
-void bt_object_inc_ref_count(struct bt_object *obj)
+void bt_object_inc_ref_count(const struct bt_object *c_obj)
 {
+       struct bt_object *obj = (void *) c_obj;
+
        BT_ASSERT(obj);
        BT_ASSERT(obj->is_shared);
        obj->ref_count++;
@@ -252,8 +256,28 @@ void bt_object_inc_ref_count(struct bt_object *obj)
 }
 
 static inline
-void bt_object_get_no_null_check(struct bt_object *obj)
+void bt_object_get_no_null_check_no_parent_check(const struct bt_object *c_obj)
+{
+       struct bt_object *obj = (void *) c_obj;
+
+       BT_ASSERT(obj);
+       BT_ASSERT(obj->is_shared);
+
+#ifdef BT_LOGV
+       BT_LOGV("Incrementing object's reference count: %llu -> %llu: "
+               "addr=%p, cur-count=%llu, new-count=%llu",
+               obj->ref_count, obj->ref_count + 1,
+               obj, obj->ref_count, obj->ref_count + 1);
+#endif
+
+       bt_object_inc_ref_count(obj);
+}
+
+static inline
+void bt_object_get_no_null_check(const void *c_obj)
 {
+       struct bt_object *obj = (void *) c_obj;
+
        BT_ASSERT(obj);
        BT_ASSERT(obj->is_shared);
 
@@ -277,8 +301,10 @@ void bt_object_get_no_null_check(struct bt_object *obj)
 }
 
 static inline
-void bt_object_put_no_null_check(struct bt_object *obj)
+void bt_object_put_no_null_check(const void *c_obj)
 {
+       struct bt_object *obj = (void *) c_obj;
+
        BT_ASSERT(obj);
        BT_ASSERT(obj->is_shared);
        BT_ASSERT(obj->ref_count > 0);
@@ -298,4 +324,51 @@ void bt_object_put_no_null_check(struct bt_object *obj)
        }
 }
 
+static inline
+void bt_object_get_ref(const void *ptr)
+{
+       struct bt_object *obj = (void *) ptr;
+
+       if (unlikely(!obj)) {
+               return;
+       }
+
+#ifdef BT_ASSERT_PRE
+       BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj);
+#endif
+
+       bt_object_get_no_null_check(obj);
+}
+
+static inline
+void bt_object_put_ref(const void *ptr)
+{
+       struct bt_object *obj = (void *) ptr;
+
+       if (unlikely(!obj)) {
+               return;
+       }
+
+#ifdef BT_ASSERT_PRE
+       BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj);
+       BT_ASSERT_PRE(bt_object_get_ref_count(obj) > 0,
+               "Decrementing a reference count set to 0: %!+O", ptr);
+#endif
+
+       bt_object_put_no_null_check(obj);
+}
+
+#define BT_OBJECT_PUT_REF_AND_RESET(_var)      \
+       do {                                    \
+               bt_object_put_ref(_var);        \
+               (_var) = NULL;                  \
+       } while (0)
+
+#define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \
+       do {                                    \
+               bt_object_put_ref(_var_dst);    \
+               (_var_dst) = (_var_src);        \
+               (_var_src) = NULL;              \
+       } while (0)
+
 #endif /* BABELTRACE_OBJECT_INTERNAL_H */
This page took 0.042557 seconds and 4 git commands to generate.