| 1 | #ifndef LTTNG_REF_INTERNAL_H |
| 2 | #define LTTNG_REF_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * LTTng - Non thread-safe reference counting |
| 6 | * |
| 7 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * |
| 9 | * SPDX-License-Identifier: LGPL-2.1-only |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | typedef void (*lttng_release_func)(void *); |
| 15 | |
| 16 | struct lttng_ref { |
| 17 | unsigned long count; |
| 18 | lttng_release_func release; |
| 19 | }; |
| 20 | |
| 21 | static inline |
| 22 | void lttng_ref_init(struct lttng_ref *ref, lttng_release_func release) |
| 23 | { |
| 24 | LTTNG_ASSERT(ref); |
| 25 | ref->count = 1; |
| 26 | ref->release = release; |
| 27 | } |
| 28 | |
| 29 | static inline |
| 30 | void lttng_ref_get(struct lttng_ref *ref) |
| 31 | { |
| 32 | LTTNG_ASSERT(ref); |
| 33 | ref->count++; |
| 34 | /* Overflow check. */ |
| 35 | LTTNG_ASSERT(ref->count); |
| 36 | } |
| 37 | |
| 38 | static inline |
| 39 | void lttng_ref_put(struct lttng_ref *ref) |
| 40 | { |
| 41 | LTTNG_ASSERT(ref); |
| 42 | /* Underflow check. */ |
| 43 | LTTNG_ASSERT(ref->count); |
| 44 | if (caa_unlikely((--ref->count) == 0)) { |
| 45 | ref->release(ref); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #endif /* LTTNG_REF_INTERNAL_H */ |