| 1 | #ifndef BABELTRACE_OBJECT_INTERNAL_H |
| 2 | #define BABELTRACE_OBJECT_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <babeltrace/babeltrace-internal.h> |
| 28 | #include <babeltrace/assert-internal.h> |
| 29 | #include <stdbool.h> |
| 30 | |
| 31 | struct bt_object; |
| 32 | |
| 33 | typedef void (*bt_object_release_func)(struct bt_object *); |
| 34 | typedef void (*bt_object_parent_is_owner_listener_func)( |
| 35 | struct bt_object *); |
| 36 | |
| 37 | static inline |
| 38 | void bt_object_get_no_null_check(const void *obj); |
| 39 | |
| 40 | static inline |
| 41 | void bt_object_put_no_null_check(const void *obj); |
| 42 | |
| 43 | /* |
| 44 | * Babeltrace object base. |
| 45 | * |
| 46 | * All objects publicly exposed by Babeltrace APIs must contain this |
| 47 | * object as their first member. |
| 48 | */ |
| 49 | struct bt_object { |
| 50 | /* |
| 51 | * True if this object is shared, that is, it has a reference |
| 52 | * count. |
| 53 | */ |
| 54 | bool is_shared; |
| 55 | |
| 56 | /* |
| 57 | * Current reference count. |
| 58 | */ |
| 59 | unsigned long long ref_count; |
| 60 | |
| 61 | /* |
| 62 | * Release function called when the object's reference count |
| 63 | * falls to zero. For an object with a parent, this function is |
| 64 | * bt_object_with_parent_release_func(), which calls |
| 65 | * `spec_release_func` below if there's no current parent. |
| 66 | */ |
| 67 | bt_object_release_func release_func; |
| 68 | |
| 69 | /* |
| 70 | * Specific release function called by |
| 71 | * bt_object_with_parent_release_func() or directly by a |
| 72 | * parent object. |
| 73 | */ |
| 74 | bt_object_release_func spec_release_func; |
| 75 | |
| 76 | /* |
| 77 | * Optional callback for an object with a parent, called by |
| 78 | * bt_object_with_parent_release_func() to indicate to the |
| 79 | * object that its parent is its owner. |
| 80 | */ |
| 81 | bt_object_parent_is_owner_listener_func |
| 82 | parent_is_owner_listener_func; |
| 83 | |
| 84 | /* |
| 85 | * Optional parent object. |
| 86 | */ |
| 87 | struct bt_object *parent; |
| 88 | }; |
| 89 | |
| 90 | static inline |
| 91 | unsigned long long bt_object_get_ref_count(const struct bt_object *c_obj) |
| 92 | { |
| 93 | struct bt_object *obj = (void *) c_obj; |
| 94 | |
| 95 | BT_ASSERT(obj); |
| 96 | BT_ASSERT(obj->is_shared); |
| 97 | return obj->ref_count; |
| 98 | } |
| 99 | |
| 100 | static inline |
| 101 | struct bt_object *bt_object_borrow_parent(const struct bt_object *c_obj) |
| 102 | { |
| 103 | struct bt_object *obj = (void *) c_obj; |
| 104 | |
| 105 | BT_ASSERT(obj); |
| 106 | BT_ASSERT(obj->is_shared); |
| 107 | return obj->parent; |
| 108 | } |
| 109 | |
| 110 | static inline |
| 111 | struct bt_object *bt_object_get_parent(const struct bt_object *c_obj) |
| 112 | { |
| 113 | struct bt_object *obj = (void *) c_obj; |
| 114 | struct bt_object *parent = bt_object_borrow_parent(obj); |
| 115 | |
| 116 | if (parent) { |
| 117 | bt_object_get_no_null_check(parent); |
| 118 | } |
| 119 | |
| 120 | return parent; |
| 121 | } |
| 122 | |
| 123 | static inline |
| 124 | void bt_object_set_parent(struct bt_object *child, struct bt_object *parent) |
| 125 | { |
| 126 | BT_ASSERT(child); |
| 127 | BT_ASSERT(child->is_shared); |
| 128 | |
| 129 | #ifdef BT_LOGV |
| 130 | BT_LOGV("Setting object's parent: addr=%p, parent-addr=%p", |
| 131 | child, parent); |
| 132 | #endif |
| 133 | |
| 134 | /* |
| 135 | * It is assumed that a "child" having a parent is publicly |
| 136 | * reachable. Therefore, a reference to its parent must be |
| 137 | * taken. The reference to the parent will be released once the |
| 138 | * object's reference count falls to zero. |
| 139 | */ |
| 140 | if (parent) { |
| 141 | BT_ASSERT(!child->parent); |
| 142 | child->parent = parent; |
| 143 | bt_object_get_no_null_check(parent); |
| 144 | } else { |
| 145 | if (child->parent) { |
| 146 | bt_object_put_no_null_check(child->parent); |
| 147 | } |
| 148 | |
| 149 | child->parent = NULL; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static inline |
| 154 | void bt_object_try_spec_release(struct bt_object *obj) |
| 155 | { |
| 156 | BT_ASSERT(obj); |
| 157 | BT_ASSERT(obj->is_shared); |
| 158 | BT_ASSERT(obj->spec_release_func); |
| 159 | |
| 160 | if (bt_object_get_ref_count(obj) == 0) { |
| 161 | obj->spec_release_func(obj); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static inline |
| 166 | void bt_object_with_parent_release_func(struct bt_object *obj) |
| 167 | { |
| 168 | if (obj->parent) { |
| 169 | /* |
| 170 | * Keep our own copy of the parent address because `obj` |
| 171 | * could be destroyed in |
| 172 | * obj->parent_is_owner_listener_func(). |
| 173 | */ |
| 174 | struct bt_object *parent = obj->parent; |
| 175 | |
| 176 | #ifdef BT_LOGV |
| 177 | BT_LOGV("Releasing parented object: addr=%p, ref-count=%llu, " |
| 178 | "parent-addr=%p, parent-ref-count=%llu", |
| 179 | obj, obj->ref_count, |
| 180 | parent, parent->ref_count); |
| 181 | #endif |
| 182 | |
| 183 | if (obj->parent_is_owner_listener_func) { |
| 184 | /* |
| 185 | * Object has a chance to destroy itself here |
| 186 | * under certain conditions and notify its |
| 187 | * parent. At this point the parent is |
| 188 | * guaranteed to exist because it's not put yet. |
| 189 | */ |
| 190 | obj->parent_is_owner_listener_func(obj); |
| 191 | } |
| 192 | |
| 193 | /* The release function will be invoked by the parent. */ |
| 194 | bt_object_put_no_null_check(parent); |
| 195 | } else { |
| 196 | bt_object_try_spec_release(obj); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static inline |
| 201 | void bt_object_init(struct bt_object *obj, bool is_shared, |
| 202 | bt_object_release_func release_func) |
| 203 | { |
| 204 | BT_ASSERT(obj); |
| 205 | BT_ASSERT(!is_shared || release_func); |
| 206 | obj->is_shared = is_shared; |
| 207 | obj->release_func = release_func; |
| 208 | obj->parent_is_owner_listener_func = NULL; |
| 209 | obj->spec_release_func = NULL; |
| 210 | obj->parent = NULL; |
| 211 | obj->ref_count = 1; |
| 212 | } |
| 213 | |
| 214 | static inline |
| 215 | void bt_object_init_shared(struct bt_object *obj, |
| 216 | bt_object_release_func release_func) |
| 217 | { |
| 218 | bt_object_init(obj, true, release_func); |
| 219 | } |
| 220 | |
| 221 | static inline |
| 222 | void bt_object_init_unique(struct bt_object *obj) |
| 223 | { |
| 224 | bt_object_init(obj, false, NULL); |
| 225 | } |
| 226 | |
| 227 | static inline |
| 228 | void bt_object_init_shared_with_parent(struct bt_object *obj, |
| 229 | bt_object_release_func spec_release_func) |
| 230 | { |
| 231 | BT_ASSERT(obj); |
| 232 | BT_ASSERT(spec_release_func); |
| 233 | bt_object_init_shared(obj, bt_object_with_parent_release_func); |
| 234 | obj->spec_release_func = spec_release_func; |
| 235 | } |
| 236 | |
| 237 | static inline |
| 238 | void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj, |
| 239 | bt_object_parent_is_owner_listener_func func) |
| 240 | { |
| 241 | BT_ASSERT(obj); |
| 242 | BT_ASSERT(obj->is_shared); |
| 243 | BT_ASSERT(obj->spec_release_func); |
| 244 | ((struct bt_object *) obj)->parent_is_owner_listener_func = func; |
| 245 | } |
| 246 | |
| 247 | static inline |
| 248 | void bt_object_inc_ref_count(const struct bt_object *c_obj) |
| 249 | { |
| 250 | struct bt_object *obj = (void *) c_obj; |
| 251 | |
| 252 | BT_ASSERT(obj); |
| 253 | BT_ASSERT(obj->is_shared); |
| 254 | obj->ref_count++; |
| 255 | BT_ASSERT(obj->ref_count != 0); |
| 256 | } |
| 257 | |
| 258 | static inline |
| 259 | void bt_object_get_no_null_check_no_parent_check(const struct bt_object *c_obj) |
| 260 | { |
| 261 | struct bt_object *obj = (void *) c_obj; |
| 262 | |
| 263 | BT_ASSERT(obj); |
| 264 | BT_ASSERT(obj->is_shared); |
| 265 | |
| 266 | #ifdef BT_LOGV |
| 267 | BT_LOGV("Incrementing object's reference count: %llu -> %llu: " |
| 268 | "addr=%p, cur-count=%llu, new-count=%llu", |
| 269 | obj->ref_count, obj->ref_count + 1, |
| 270 | obj, obj->ref_count, obj->ref_count + 1); |
| 271 | #endif |
| 272 | |
| 273 | bt_object_inc_ref_count(obj); |
| 274 | } |
| 275 | |
| 276 | static inline |
| 277 | void bt_object_get_no_null_check(const void *c_obj) |
| 278 | { |
| 279 | struct bt_object *obj = (void *) c_obj; |
| 280 | |
| 281 | BT_ASSERT(obj); |
| 282 | BT_ASSERT(obj->is_shared); |
| 283 | |
| 284 | if (unlikely(obj->parent && bt_object_get_ref_count(obj) == 0)) { |
| 285 | #ifdef BT_LOGV |
| 286 | BT_LOGV("Incrementing object's parent's reference count: " |
| 287 | "addr=%p, parent-addr=%p", obj, obj->parent); |
| 288 | #endif |
| 289 | |
| 290 | bt_object_get_no_null_check(obj->parent); |
| 291 | } |
| 292 | |
| 293 | #ifdef BT_LOGV |
| 294 | BT_LOGV("Incrementing object's reference count: %llu -> %llu: " |
| 295 | "addr=%p, cur-count=%llu, new-count=%llu", |
| 296 | obj->ref_count, obj->ref_count + 1, |
| 297 | obj, obj->ref_count, obj->ref_count + 1); |
| 298 | #endif |
| 299 | |
| 300 | bt_object_inc_ref_count(obj); |
| 301 | } |
| 302 | |
| 303 | static inline |
| 304 | void bt_object_put_no_null_check(const void *c_obj) |
| 305 | { |
| 306 | struct bt_object *obj = (void *) c_obj; |
| 307 | |
| 308 | BT_ASSERT(obj); |
| 309 | BT_ASSERT(obj->is_shared); |
| 310 | BT_ASSERT(obj->ref_count > 0); |
| 311 | |
| 312 | #ifdef BT_LOGV |
| 313 | BT_LOGV("Decrementing object's reference count: %llu -> %llu: " |
| 314 | "addr=%p, cur-count=%llu, new-count=%llu", |
| 315 | obj->ref_count, obj->ref_count - 1, |
| 316 | obj, obj->ref_count, obj->ref_count - 1); |
| 317 | #endif |
| 318 | |
| 319 | obj->ref_count--; |
| 320 | |
| 321 | if (obj->ref_count == 0) { |
| 322 | BT_ASSERT(obj->release_func); |
| 323 | obj->release_func(obj); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | static inline |
| 328 | void bt_object_get_ref(const void *ptr) |
| 329 | { |
| 330 | struct bt_object *obj = (void *) ptr; |
| 331 | |
| 332 | if (unlikely(!obj)) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | #ifdef BT_ASSERT_PRE |
| 337 | BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj); |
| 338 | #endif |
| 339 | |
| 340 | bt_object_get_no_null_check(obj); |
| 341 | } |
| 342 | |
| 343 | static inline |
| 344 | void bt_object_put_ref(const void *ptr) |
| 345 | { |
| 346 | struct bt_object *obj = (void *) ptr; |
| 347 | |
| 348 | if (unlikely(!obj)) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | #ifdef BT_ASSERT_PRE |
| 353 | BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj); |
| 354 | BT_ASSERT_PRE(bt_object_get_ref_count(obj) > 0, |
| 355 | "Decrementing a reference count set to 0: %!+O", ptr); |
| 356 | #endif |
| 357 | |
| 358 | bt_object_put_no_null_check(obj); |
| 359 | } |
| 360 | |
| 361 | #define BT_OBJECT_PUT_REF_AND_RESET(_var) \ |
| 362 | do { \ |
| 363 | bt_object_put_ref(_var); \ |
| 364 | (_var) = NULL; \ |
| 365 | } while (0) |
| 366 | |
| 367 | #define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \ |
| 368 | do { \ |
| 369 | bt_object_put_ref(_var_dst); \ |
| 370 | (_var_dst) = (_var_src); \ |
| 371 | (_var_src) = NULL; \ |
| 372 | } while (0) |
| 373 | |
| 374 | #endif /* BABELTRACE_OBJECT_INTERNAL_H */ |