| 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 | #include <assert.h> |
| 14 | |
| 15 | typedef void (*lttng_release_func)(void *); |
| 16 | |
| 17 | struct lttng_ref { |
| 18 | unsigned long count; |
| 19 | lttng_release_func release; |
| 20 | }; |
| 21 | |
| 22 | static inline |
| 23 | void lttng_ref_init(struct lttng_ref *ref, lttng_release_func release) |
| 24 | { |
| 25 | assert(ref); |
| 26 | ref->count = 1; |
| 27 | ref->release = release; |
| 28 | } |
| 29 | |
| 30 | static inline |
| 31 | void lttng_ref_get(struct lttng_ref *ref) |
| 32 | { |
| 33 | assert(ref); |
| 34 | ref->count++; |
| 35 | /* Overflow check. */ |
| 36 | assert(ref->count); |
| 37 | } |
| 38 | |
| 39 | static inline |
| 40 | void lttng_ref_put(struct lttng_ref *ref) |
| 41 | { |
| 42 | assert(ref); |
| 43 | /* Underflow check. */ |
| 44 | assert(ref->count); |
| 45 | if (caa_unlikely((--ref->count) == 0)) { |
| 46 | ref->release(ref); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #endif /* LTTNG_REF_INTERNAL_H */ |