Commit | Line | Data |
---|---|---|
8abe313a JG |
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 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
13 | * as published by the Free Software Foundation. | |
14 | * | |
15 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
18 | * for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU Lesser General Public License | |
21 | * along with this library; if not, write to the Free Software Foundation, | |
22 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
23 | */ | |
24 | ||
25 | #include <assert.h> | |
26 | ||
27 | typedef void (*lttng_release_func)(void *); | |
28 | ||
29 | struct lttng_ref { | |
30 | unsigned long count; | |
31 | lttng_release_func release; | |
32 | }; | |
33 | ||
34 | static inline | |
35 | void lttng_ref_init(struct lttng_ref *ref, lttng_release_func release) | |
36 | { | |
37 | assert(ref); | |
38 | ref->count = 1; | |
39 | ref->release = release; | |
40 | } | |
41 | ||
42 | static inline | |
43 | void lttng_ref_get(struct lttng_ref *ref) | |
44 | { | |
45 | assert(ref); | |
46 | ref->count++; | |
47 | /* Overflow check. */ | |
48 | assert(ref->count); | |
49 | } | |
50 | ||
51 | static inline | |
52 | void lttng_ref_put(struct lttng_ref *ref) | |
53 | { | |
54 | assert(ref); | |
55 | /* Underflow check. */ | |
56 | assert(ref->count); | |
57 | if (caa_unlikely((--ref->count) == 0)) { | |
58 | ref->release(ref); | |
59 | } | |
60 | } | |
61 | ||
62 | #endif /* LTTNG_REF_INTERNAL_H */ |