+static inline
+void bt_object_init_shared(struct bt_object *obj,
+ bt_object_release_func release_func)
+{
+ bt_object_init(obj, true, release_func);
+}
+
+static inline
+void bt_object_init_unique(struct bt_object *obj)
+{
+ bt_object_init(obj, false, NULL);
+}
+
+static inline
+void bt_object_init_shared_with_parent(struct bt_object *obj,
+ bt_object_release_func spec_release_func)
+{
+ BT_ASSERT(obj);
+ BT_ASSERT(spec_release_func);
+ bt_object_init_shared(obj, bt_object_with_parent_release_func);
+ obj->spec_release_func = spec_release_func;
+}
+
+static inline
+void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj,
+ bt_object_parent_is_owner_listener_func func)
+{
+ BT_ASSERT(obj);
+ BT_ASSERT(obj->is_shared);
+ BT_ASSERT(obj->spec_release_func);
+ ((struct bt_object *) obj)->parent_is_owner_listener_func = func;
+}
+
+static inline
+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++;
+ BT_ASSERT(obj->ref_count != 0);
+}
+
+static inline
+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);
+
+ if (unlikely(obj->parent && bt_object_get_ref_count(obj) == 0)) {
+#ifdef BT_LOGV
+ BT_LOGV("Incrementing object's parent's reference count: "
+ "addr=%p, parent-addr=%p", obj, obj->parent);
+#endif
+
+ bt_object_get_no_null_check(obj->parent);
+ }
+
+#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_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);
+
+#ifdef BT_LOGV
+ BT_LOGV("Decrementing 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
+
+ obj->ref_count--;
+
+ if (obj->ref_count == 0) {
+ BT_ASSERT(obj->release_func);
+ obj->release_func(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)
+