Commit | Line | Data |
---|---|---|
de3dd40e PP |
1 | #ifndef BABELTRACE_CTF_IR_REF_H |
2 | #define BABELTRACE_CTF_IR_REF_H | |
3 | ||
4 | /* | |
5 | * BabelTrace - CTF IR: common reference counting | |
6 | * | |
7 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation | |
8 | * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <assert.h> | |
30 | #include <babeltrace/ref-internal.h> | |
31 | #include <babeltrace/ctf-ir/common-internal.h> | |
32 | ||
33 | /* | |
34 | * BT_CTF_PUT: calls bt_ctf_put() with a variable, then sets this | |
35 | * variable to NULL. | |
36 | * | |
37 | * A common action with CTF IR objects is to create or get one, do | |
38 | * something with it, and then put it. To avoid putting it a second time | |
39 | * later (if an error occurs, for example), the variable is often reset | |
40 | * to NULL after putting the object it points to. Since this is so | |
41 | * common, you can use the BT_CTF_PUT() macro, which does just that. | |
42 | * | |
43 | * It is safe to call this function with a NULL object. | |
44 | * | |
45 | * @param obj CTF IR object. | |
46 | */ | |
47 | #define BT_CTF_PUT(_obj) \ | |
48 | do { \ | |
49 | bt_ctf_put(_obj); \ | |
50 | (_obj) = NULL; \ | |
51 | } while (0) | |
52 | ||
53 | /* | |
54 | * bt_ctf_get: increments the reference count of a CTF IR object. | |
55 | * | |
56 | * The same number of bt_ctf_get() and bt_ctf_put() (plus one extra | |
57 | * bt_ctf_put() to release the initial reference done at creation) have | |
58 | * to be done to destroy a CTF IR object. | |
59 | * | |
60 | * It is safe to call this function with a NULL object. | |
61 | * | |
62 | * @param obj CTF IR object. | |
63 | */ | |
64 | static inline | |
65 | void bt_ctf_get(void *obj) | |
66 | { | |
67 | if (obj) { | |
68 | struct bt_ctf_base *base = obj; | |
69 | ||
70 | bt_ref_get(&base->ref_count); | |
71 | } | |
72 | } | |
73 | ||
74 | /* | |
75 | * bt_ctf_put: decrements the reference count of a CTF IR object. | |
76 | * | |
77 | * The same number of bt_ctf_get() and bt_ctf_put() (plus one extra | |
78 | * bt_ctf_put() to release the initial reference done at creation) have | |
79 | * to be done to destroy a CTF IR object. | |
80 | * | |
81 | * When the object's reference count is decremented to 0 by a call to | |
82 | * bt_ctf_put(), the object is freed. | |
83 | * | |
84 | * It is safe to call this function with a NULL object. | |
85 | * | |
86 | * @param obj CTF IR object. | |
87 | */ | |
88 | static inline | |
89 | void bt_ctf_put(void *obj) | |
90 | { | |
91 | if (obj) { | |
92 | struct bt_ctf_base *base = obj; | |
93 | ||
94 | bt_ref_put(&base->ref_count); | |
95 | } | |
96 | } | |
97 | ||
98 | #endif /* BABELTRACE_CTF_IR_REF_H */ |