lib: private functions: do not repeat `private` word
[babeltrace.git] / include / babeltrace / object.h
CommitLineData
65300d60
PP
1#ifndef BABELTRACE_OBJECT_H
2#define BABELTRACE_OBJECT_H
de3dd40e
PP
3
4/*
83509119 5 * BabelTrace: common reference counting
de3dd40e
PP
6 *
7 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
8 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
83509119 9 * Copyright (c) 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
de3dd40e
PP
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
26162993
PP
30#ifdef __cplusplus
31extern "C" {
32#endif
33
b62d3f21
PP
34/**
35@defgroup refs Reference counting management
36@ingroup apiref
37@brief Common reference counting management for all Babeltrace objects.
38
9197569d 39@code
65300d60 40#include <babeltrace/object.h>
9197569d
PP
41@endcode
42
b62d3f21
PP
43The macros and functions of this module are everything that is needed
44to handle the <strong><em>reference counting</em></strong> of
45Babeltrace objects.
46
223f6c6a 47Any Babeltrace object can be shared by multiple owners thanks to
b62d3f21 48<a href="https://en.wikipedia.org/wiki/Reference_counting">reference
223f6c6a 49counting</a>.
b62d3f21 50
223f6c6a
PP
51The Babeltrace C API complies with the following key principles:
52
531. When you call an API function which accepts a Babeltrace object
54 pointer as a parameter, the API function <strong>borrows the
55 reference</strong> for the <strong>duration of the function</strong>.
56
57 @image html ref-count-user-calls.png
58
59 The API function can also get a new reference if the system needs a
60 more persistent reference, but the ownership is <strong>never
61 transferred</strong> from the caller to the API function.
62
63 In other words, the caller still owns the object after calling any
64 API function: no function "steals" the user's reference (except
65300d60 65 bt_object_put_ref()).
223f6c6a
PP
66
672. An API function which \em returns a Babeltrace object pointer to the
68 user returns a <strong>new reference</strong>. The caller becomes an
69 owner of the object.
70
71 @image html ref-count-api-returns.png
72
73 It is your responsibility to discard the object when you don't
65300d60 74 need it anymore with bt_object_put_ref().
223f6c6a
PP
75
76 For example, see bt_value_array_get().
77
783. A Babeltrace object pointer received as a parameter in a user
79 function called back from an API function is a
80 <strong>borrowed</strong>, or <strong>weak reference</strong>: if you
09ac5ce7 81 need a reference which is more persistent than the duration of the
65300d60 82 user function, call bt_object_get_ref() on the pointer.
223f6c6a
PP
83
84 @image html ref-count-callback.png
85
07208d85 86 For example, see bt_value_map_foreach_entry().
b62d3f21 87
65300d60
PP
88The two macros BT_OBJECT_PUT_REF_AND_RESET() and BT_OBJECT_MOVE_REF() operate on \em variables rather
89than pointer values. You should use BT_OBJECT_PUT_REF_AND_RESET() instead of bt_object_put_ref() when
b62d3f21 90possible to avoid "double puts". For the same reason, you should use use
65300d60 91BT_OBJECT_MOVE_REF() instead of performing manual reference moves between
b62d3f21
PP
92variables.
93
94@file
95@brief Reference counting management macros and functions.
96@sa refs
97
98@addtogroup refs
99@{
100*/
101
102/**
65300d60 103@brief Calls bt_object_put_ref() on a variable named \p _var, then
34d0da31 104 sets \p _var to \c NULL.
b62d3f21 105
65300d60 106Using this macro is considered safer than calling bt_object_put_ref() because it
b62d3f21 107makes sure that the variable which used to contain a reference to a
65300d60
PP
108Babeltrace object is set to \c NULL so that a future BT_OBJECT_PUT_REF_AND_RESET() or
109bt_object_put_ref() call will not cause another, unwanted reference decrementation.
b62d3f21 110
34d0da31
PP
111@param[in,out] _var Name of a variable containing a
112 Babeltrace object's address (this address
113 can be \c NULL).
b62d3f21 114
34d0da31
PP
115@post <strong>If \p _var does not contain \p NULL</strong>,
116 its reference count is decremented.
117@post \p _var contains \c NULL.
b62d3f21 118
65300d60 119@sa BT_OBJECT_MOVE_REF(): Transfers the ownership of a Babeltrace object from a
b62d3f21
PP
120 variable to another.
121*/
65300d60
PP
122#define BT_OBJECT_PUT_REF_AND_RESET(_var) \
123 do { \
124 bt_object_put_ref(_var); \
125 (_var) = NULL; \
de3dd40e
PP
126 } while (0)
127
b62d3f21 128/**
34d0da31
PP
129@brief Transfers the ownership of a Babeltrace object from a variable
130 named \p _var_src to a variable named \p _var_dst.
b62d3f21
PP
131
132This macro implements the following common pattern:
133
65300d60 134 1. Call bt_object_put_ref() on \p _var_dst to make sure the previous reference
b62d3f21
PP
135 held by \p _var_dst is discarded.
136 2. Assign \p _var_src to \p _var_dst.
137 3. Set \p _var_src to \c NULL to avoid future, unwanted reference
138 decrementation of \p _var_src.
139
140@warning
141You must \em not use this macro when both \p _var_dst and
142\p _var_src contain the same Babeltrace object address and the reference
65300d60 143count of this object is 1. The initial call to bt_object_put_ref() on \p _var_dst
b62d3f21
PP
144would destroy the object and leave a dangling pointer in \p _var_dst.
145
34d0da31
PP
146@param[in,out] _var_dst Name of the destination variable, containing
147 either the address of a Babeltrace object to
148 put first, or \c NULL.
149@param[in,out] _var_src Name of the source variable, containing
150 either the address of a Babeltrace object to
151 move, or \c NULL.
b62d3f21
PP
152
153@pre <strong>If \p _var_dst and \p _var_src contain the same
34d0da31
PP
154 value which is not \c NULL</strong>, this object's reference
155 count is greater than 1.
b62d3f21
PP
156@post <strong>If \c _var_dst is not \c NULL</strong>, its reference
157 count is decremented.
34d0da31
PP
158@post \p _var_dst is equal to the value of \p _var_src \em before
159 you called this macro.
b62d3f21
PP
160@post \p _var_src is \c NULL.
161
65300d60 162@sa BT_OBJECT_PUT_REF_AND_RESET(): Calls bt_object_put_ref() on a variable, then sets it to \c NULL.
b62d3f21 163*/
65300d60 164#define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \
b62d3f21 165 do { \
65300d60 166 bt_object_put_ref(_var_dst); \
b62d3f21
PP
167 (_var_dst) = (_var_src); \
168 (_var_src) = NULL; \
e693822d
PP
169 } while (0)
170
b62d3f21
PP
171/**
172@brief Increments the reference count of the Babeltrace object \p obj.
173
174@param[in] obj Babeltrace object of which to get a new reference
175 (can be \c NULL).
176@returns \p obj
177
178@post <strong>If \c obj is not \c NULL</strong>, its reference
179 count is incremented.
180
65300d60 181@sa bt_object_put_ref(): Decrements the reference count of a Babeltrace object.
b62d3f21 182*/
65300d60 183void *bt_object_get_ref(void *obj);
de3dd40e 184
b62d3f21
PP
185/**
186@brief Decrements the reference count of the Babeltrace object
187 \p obj.
188
189When the object's reference count reaches 0, the object can no longer
190be accessed and is considered \em destroyed.
191
192@remarks
65300d60 193You should use the BT_OBJECT_PUT_REF_AND_RESET() macro instead of calling bt_object_put_ref() since the
b62d3f21
PP
194former is generally safer.
195
196@param[in] obj Babeltrace object of which to drop a reference
197 (can be \c NULL).
198
199@post <strong>If \c obj is not \c NULL</strong>, its reference
200 count is decremented.
201
65300d60
PP
202@sa BT_OBJECT_PUT_REF_AND_RESET(): Calls bt_object_put_ref() on a variable, then sets it to \c NULL.
203@sa BT_OBJECT_MOVE_REF(): Transfers the ownership of a Babeltrace object from a
b62d3f21 204 variable to another.
65300d60 205@sa bt_object_get_ref(): Increments the reference count of a Babeltrace object.
b62d3f21 206*/
65300d60 207void bt_object_put_ref(void *obj);
de3dd40e 208
b62d3f21
PP
209/**
210@}
211*/
212
26162993
PP
213#ifdef __cplusplus
214}
215#endif
216
65300d60 217#endif /* BABELTRACE_OBJECT_H */
This page took 0.078176 seconds and 4 git commands to generate.