API doc: add missing @postrefcountsame conditions
[babeltrace.git] / include / babeltrace / ref.h
1 #ifndef BABELTRACE_REF_H
2 #define BABELTRACE_REF_H
3
4 /*
5 * BabelTrace: common reference counting
6 *
7 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
8 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
9 * Copyright (c) 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /**
35 @defgroup refs Reference counting management
36 @ingroup apiref
37 @brief Common reference counting management for all Babeltrace objects.
38
39 The macros and functions of this module are everything that is needed
40 to handle the <strong><em>reference counting</em></strong> of
41 Babeltrace objects.
42
43 All Babeltrace objects can be shared by multiple owners thanks to
44 <a href="https://en.wikipedia.org/wiki/Reference_counting">reference
45 counting</a>. A function which returns a Babeltrace object owned
46 by another one increments its reference count so that the caller
47 becomes an owner too.
48
49 When a Babeltrace object is created, its reference count is initialized
50 to 1. It is your responsibility to discard the object when you don't
51 need it anymore with bt_put().
52
53 The two macros BT_PUT() and BT_MOVE() operate on \em variables rather
54 than pointer values. You should use BT_PUT() instead of bt_put() when
55 possible to avoid "double puts". For the same reason, you should use use
56 BT_MOVE() instead of performing manual reference moves between
57 variables.
58
59 @file
60 @brief Reference counting management macros and functions.
61 @sa refs
62
63 @addtogroup refs
64 @{
65 */
66
67 /**
68 @brief Calls bt_put() on variable \p _var, then sets \p _var to \c NULL.
69
70 Using this macro is considered safer than calling bt_put() because it
71 makes sure that the variable which used to contain a reference to a
72 Babeltrace object is set to \c NULL so that a future BT_PUT() or
73 bt_put() call will not cause another, unwanted reference decrementation.
74
75 @param[in,out] _var Variable containing a Babeltrace object's
76 address (can be \c NULL).
77
78 @post <strong>If \c _var is not \c NULL</strong>, its reference
79 count is decremented.
80 @post \p _var is \c NULL.
81
82 @sa BT_MOVE(): Transfers the ownership of a Babeltrace object from a
83 variable to another.
84 */
85 #define BT_PUT(_var) \
86 do { \
87 bt_put(_var); \
88 (_var) = NULL; \
89 } while (0)
90
91 /**
92 @brief Transfers the ownership of a Babeltrace object from variable
93 \p _var_src to variable \p _var_dst.
94
95 This macro implements the following common pattern:
96
97 1. Call bt_put() on \p _var_dst to make sure the previous reference
98 held by \p _var_dst is discarded.
99 2. Assign \p _var_src to \p _var_dst.
100 3. Set \p _var_src to \c NULL to avoid future, unwanted reference
101 decrementation of \p _var_src.
102
103 @warning
104 You must \em not use this macro when both \p _var_dst and
105 \p _var_src contain the same Babeltrace object address and the reference
106 count of this object is 1. The initial call to bt_put() on \p _var_dst
107 would destroy the object and leave a dangling pointer in \p _var_dst.
108
109 @param[in,out] _var_dst Destination variable, possibly containing the
110 address of a Babeltrace object to put first.
111 @param[in,out] _var_src Source variable containing the address of a
112 Babeltrace object to move.
113
114 @pre <strong>If \p _var_dst and \p _var_src contain the same
115 value</strong>, this object's reference count is greater than 1.
116 @post <strong>If \c _var_dst is not \c NULL</strong>, its reference
117 count is decremented.
118 @post \p _var_dst is equal to \p _var_src.
119 @post \p _var_src is \c NULL.
120
121 @sa BT_PUT(): Calls bt_put() on a variable, then sets it to \c NULL.
122 */
123 #define BT_MOVE(_var_dst, _var_src) \
124 do { \
125 bt_put(_var_dst); \
126 (_var_dst) = (_var_src); \
127 (_var_src) = NULL; \
128 } while (0)
129
130 /**
131 @brief Increments the reference count of the Babeltrace object \p obj.
132
133 @param[in] obj Babeltrace object of which to get a new reference
134 (can be \c NULL).
135 @returns \p obj
136
137 @post <strong>If \c obj is not \c NULL</strong>, its reference
138 count is incremented.
139
140 @sa bt_put(): Decrements the reference count of a Babeltrace object.
141 */
142 void *bt_get(void *obj);
143
144 /**
145 @brief Decrements the reference count of the Babeltrace object
146 \p obj.
147
148 When the object's reference count reaches 0, the object can no longer
149 be accessed and is considered \em destroyed.
150
151 @remarks
152 You should use the BT_PUT() macro instead of calling bt_put() since the
153 former is generally safer.
154
155 @param[in] obj Babeltrace object of which to drop a reference
156 (can be \c NULL).
157
158 @post <strong>If \c obj is not \c NULL</strong>, its reference
159 count is decremented.
160
161 @sa BT_PUT(): Calls bt_put() on a variable, then sets it to \c NULL.
162 @sa BT_MOVE(): Transfers the ownership of a Babeltrace object from a
163 variable to another.
164 @sa bt_get(): Increments the reference count of a Babeltrace object.
165 */
166 void bt_put(void *obj);
167
168 /**
169 @}
170 */
171
172 #ifdef __cplusplus
173 }
174 #endif
175
176 #endif /* BABELTRACE_REF_H */
This page took 0.031816 seconds and 4 git commands to generate.