#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
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.
};
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) {
}
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++;
}
static inline
-void bt_object_get_no_null_check_no_parent_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);
}
static inline
-void bt_object_get_no_null_check(struct bt_object *obj)
+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);
}
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);
}
}
+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 */