From 47601e5f2145c7a23260a4cbdfc4286b56a77365 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Tue, 19 Jan 2016 13:22:59 -0500 Subject: [PATCH] Object: Acquire reference to parent on first acquired reference MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit See doc/ref-counting.md Signed-off-by: Jérémie Galarneau --- lib/ref.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/ref.c b/lib/ref.c index e642e266..97460e93 100644 --- a/lib/ref.c +++ b/lib/ref.c @@ -27,13 +27,19 @@ #include #include -void *bt_get(void *obj) +void *bt_get(void *ptr) { - if (obj) { - struct bt_object *base = obj; + struct bt_object *obj = ptr; + + if (!obj) { + goto end; + } - bt_ref_get(&base->ref_count); + if (obj->parent && bt_object_get_ref_count(obj) == 0) { + bt_get(obj->parent); } + bt_ref_get(&obj->ref_count); +end: return obj; } -- 2.34.1