docs: Add supported versions and fix-backport policy
[lttng-tools.git] / include / lttng / ref-internal.hpp
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 using lttng_release_func = void (*)(void *);
14
15 struct lttng_ref {
16 unsigned long count;
17 lttng_release_func release;
18 };
19
20 static inline void lttng_ref_init(struct lttng_ref *ref, lttng_release_func release)
21 {
22 LTTNG_ASSERT(ref);
23 ref->count = 1;
24 ref->release = release;
25 }
26
27 static inline void lttng_ref_get(struct lttng_ref *ref)
28 {
29 LTTNG_ASSERT(ref);
30 ref->count++;
31 /* Overflow check. */
32 LTTNG_ASSERT(ref->count);
33 }
34
35 static inline void lttng_ref_put(struct lttng_ref *ref)
36 {
37 LTTNG_ASSERT(ref);
38 /* Underflow check. */
39 LTTNG_ASSERT(ref->count);
40 if (caa_unlikely((--ref->count) == 0)) {
41 ref->release(ref);
42 }
43 }
44
45 #endif /* LTTNG_REF_INTERNAL_H */
This page took 0.029467 seconds and 4 git commands to generate.