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 | ||
e693822d PP |
53 | /* |
54 | * BT_CTF_MOVE: moves the ownership of a CTF object, setting the old | |
55 | * owner to NULL. | |
56 | * | |
57 | * This macro sets the variable _dst to the value of the variable _src, | |
58 | * then sets _src to NULL, effectively moving the ownership of a CTF | |
59 | * object from one variable to the other. | |
60 | * | |
61 | * @param obj CTF IR object. | |
62 | */ | |
63 | #define BT_CTF_MOVE(_dst, _src) \ | |
64 | do { \ | |
65 | (_dst) = (_src); \ | |
66 | (_src) = NULL; \ | |
67 | } while (0) | |
68 | ||
de3dd40e PP |
69 | /* |
70 | * bt_ctf_get: increments the reference count of a CTF IR object. | |
71 | * | |
72 | * The same number of bt_ctf_get() and bt_ctf_put() (plus one extra | |
73 | * bt_ctf_put() to release the initial reference done at creation) have | |
74 | * to be done to destroy a CTF IR object. | |
75 | * | |
76 | * It is safe to call this function with a NULL object. | |
77 | * | |
78 | * @param obj CTF IR object. | |
79 | */ | |
80 | static inline | |
81 | void bt_ctf_get(void *obj) | |
82 | { | |
83 | if (obj) { | |
84 | struct bt_ctf_base *base = obj; | |
85 | ||
86 | bt_ref_get(&base->ref_count); | |
87 | } | |
88 | } | |
89 | ||
90 | /* | |
91 | * bt_ctf_put: decrements the reference count of a CTF IR object. | |
92 | * | |
93 | * The same number of bt_ctf_get() and bt_ctf_put() (plus one extra | |
94 | * bt_ctf_put() to release the initial reference done at creation) have | |
95 | * to be done to destroy a CTF IR object. | |
96 | * | |
97 | * When the object's reference count is decremented to 0 by a call to | |
98 | * bt_ctf_put(), the object is freed. | |
99 | * | |
100 | * It is safe to call this function with a NULL object. | |
101 | * | |
102 | * @param obj CTF IR object. | |
103 | */ | |
104 | static inline | |
105 | void bt_ctf_put(void *obj) | |
106 | { | |
107 | if (obj) { | |
108 | struct bt_ctf_base *base = obj; | |
109 | ||
110 | bt_ref_put(&base->ref_count); | |
111 | } | |
112 | } | |
113 | ||
114 | #endif /* BABELTRACE_CTF_IR_REF_H */ |