00001 /* 00002 * astobj2 - replacement containers for asterisk data structures. 00003 * 00004 * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy 00005 * 00006 * See http://www.asterisk.org for more information about 00007 * the Asterisk project. Please do not directly contact 00008 * any of the maintainers of this project for assistance; 00009 * the project provides a web site, mailing lists and IRC 00010 * channels for your use. 00011 * 00012 * This program is free software, distributed under the terms of 00013 * the GNU General Public License Version 2. See the LICENSE file 00014 * at the top of the source tree. 00015 */ 00016 00017 #ifndef _ASTERISK_ASTOBJ2_H 00018 #define _ASTERISK_ASTOBJ2_H 00019 00020 #include "asterisk/compat.h" 00021 #include "asterisk/lock.h" 00022 00023 /*! \file 00024 * \ref AstObj2 00025 * 00026 * \page AstObj2 Object Model implementing objects and containers. 00027 00028 This module implements an abstraction for objects (with locks and 00029 reference counts), and containers for these user-defined objects, 00030 also supporting locking, reference counting and callbacks. 00031 00032 The internal implementation of objects and containers is opaque to the user, 00033 so we can use different data structures as needs arise. 00034 00035 \section AstObj2_UsageObjects USAGE - OBJECTS 00036 00037 An ao2 object is a block of memory that the user code can access, 00038 and for which the system keeps track (with a bit of help from the 00039 programmer) of the number of references around. When an object has 00040 no more references (refcount == 0), it is destroyed, by first 00041 invoking whatever 'destructor' function the programmer specifies 00042 (it can be NULL if none is necessary), and then freeing the memory. 00043 This way objects can be shared without worrying who is in charge 00044 of freeing them. 00045 As an additional feature, ao2 objects are associated to individual 00046 locks. 00047 00048 Creating an object requires the size of the object and 00049 a pointer to the destructor function: 00050 00051 struct foo *o; 00052 00053 o = ao2_alloc(sizeof(struct foo), my_destructor_fn); 00054 00055 The value returned points to the user-visible portion of the objects 00056 (user-data), but is also used as an identifier for all object-related 00057 operations such as refcount and lock manipulations. 00058 00059 On return from ao2_alloc(): 00060 00061 - the object has a refcount = 1; 00062 - the memory for the object is allocated dynamically and zeroed; 00063 - we cannot realloc() the object itself; 00064 - we cannot call free(o) to dispose of the object. Rather, we 00065 tell the system that we do not need the reference anymore: 00066 00067 ao2_ref(o, -1) 00068 00069 causing the destructor to be called (and then memory freed) when 00070 the refcount goes to 0. 00071 00072 - ao2_ref(o, +1) can be used to modify the refcount on the 00073 object in case we want to pass it around. 00074 00075 - ao2_lock(obj), ao2_unlock(obj), ao2_trylock(obj) can be used 00076 to manipulate the lock associated with the object. 00077 00078 00079 \section AstObj2_UsageContainers USAGE - CONTAINERS 00080 00081 An ao2 container is an abstract data structure where we can store 00082 ao2 objects, search them (hopefully in an efficient way), and iterate 00083 or apply a callback function to them. A container is just an ao2 object 00084 itself. 00085 00086 A container must first be allocated, specifying the initial 00087 parameters. At the moment, this is done as follows: 00088 00089 <b>Sample Usage:</b> 00090 \code 00091 00092 struct ao2_container *c; 00093 00094 c = ao2_container_alloc(MAX_BUCKETS, my_hash_fn, my_cmp_fn); 00095 \endcode 00096 00097 where 00098 00099 - MAX_BUCKETS is the number of buckets in the hash table, 00100 - my_hash_fn() is the (user-supplied) function that returns a 00101 hash key for the object (further reduced modulo MAX_BUCKETS 00102 by the container's code); 00103 - my_cmp_fn() is the default comparison function used when doing 00104 searches on the container, 00105 00106 A container knows little or nothing about the objects it stores, 00107 other than the fact that they have been created by ao2_alloc(). 00108 All knowledge of the (user-defined) internals of the objects 00109 is left to the (user-supplied) functions passed as arguments 00110 to ao2_container_alloc(). 00111 00112 If we want to insert an object in a container, we should 00113 initialize its fields -- especially, those used by my_hash_fn() -- 00114 to compute the bucket to use. 00115 Once done, we can link an object to a container with 00116 00117 ao2_link(c, o); 00118 00119 The function returns NULL in case of errors (and the object 00120 is not inserted in the container). Other values mean success 00121 (we are not supposed to use the value as a pointer to anything). 00122 Linking an object to a container increases its refcount by 1 00123 automatically. 00124 00125 \note While an object o is in a container, we expect that 00126 my_hash_fn(o) will always return the same value. The function 00127 does not lock the object to be computed, so modifications of 00128 those fields that affect the computation of the hash should 00129 be done by extracting the object from the container, and 00130 re-inserting it after the change (this is not terribly expensive). 00131 00132 \note A container with a single buckets is effectively a linked 00133 list. However there is no ordering among elements. 00134 00135 - \ref AstObj2_Containers 00136 - \ref astobj2.h All documentation for functions and data structures 00137 00138 */ 00139 00140 /* 00141 \note DEBUGGING REF COUNTS BIBLE: 00142 An interface to help debug refcounting is provided 00143 in this package. It is dependent on the REF_DEBUG macro being 00144 defined via menuselect and in using variants of the normal ao2_xxxx 00145 function that are named ao2_t_xxxx instead, with an extra argument, 00146 a string that will be printed out into the refs log file when the 00147 refcount for an object is changed. 00148 00149 these ao2_t_xxx variants are provided: 00150 00151 ao2_t_alloc(arg1, arg2, arg3) 00152 ao2_t_ref(arg1,arg2,arg3) 00153 ao2_t_container_alloc(arg1,arg2,arg3,arg4) 00154 ao2_t_link(arg1, arg2, arg3) 00155 ao2_t_unlink(arg1, arg2, arg3) 00156 ao2_t_callback(arg1,arg2,arg3,arg4,arg5) 00157 ao2_t_find(arg1,arg2,arg3,arg4) 00158 ao2_t_iterator_next(arg1, arg2) 00159 00160 If you study each argument list, you will see that these functions all have 00161 one extra argument than their ao2_xxx counterpart. The last argument in 00162 each case is supposed to be a string pointer, a "tag", that should contain 00163 enough of an explanation, that you can pair operations that increment the 00164 ref count, with operations that are meant to decrement the refcount. 00165 00166 Each of these calls will generate at least one line of output in in the refs 00167 log files. These lines look like this: 00168 ... 00169 0x8756f00,+1,1234,chan_sip.c,22240,load_module,**constructor**,allocate users 00170 0x86e3408,+1,1234,chan_sip.c,22241,load_module,**constructor**,allocate peers 00171 0x86dd380,+1,1234,chan_sip.c,22242,load_module,**constructor**,allocate peers_by_ip 00172 0x822d020,+1,1234,chan_sip.c,22243,load_module,**constructor**,allocate dialogs 00173 0x8930fd8,+1,1234,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct 00174 0x8930fd8,+1,1234,chan_sip.c,21467,reload_config,1,link peer into peer table 00175 0x8930fd8,-1,1234,chan_sip.c,2370,unref_peer,2,unref_peer: from reload_config 00176 0x89318b0,1,5678,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct 00177 0x89318b0,+1,5678,chan_sip.c,21467,reload_config,1,link peer into peer table 00178 0x89318b0,-1,1234,chan_sip.c,2370,unref_peer,2,unref_peer: from reload_config 00179 0x8930218,+1,1234,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct 00180 0x8930218,+1,1234,chan_sip.c,21539,reload_config,1,link peer into peers table 00181 0x868c040,-1,1234,chan_sip.c,2424,dialog_unlink_all,2,unset the relatedpeer->call field in tandem with relatedpeer field itself 00182 0x868c040,-1,1234,chan_sip.c,2443,dialog_unlink_all,1,Let's unbump the count in the unlink so the poor pvt can disappear if it is time 00183 0x868c040,-1,1234,chan_sip.c,2443,dialog_unlink_all,**destructor**,Let's unbump the count in the unlink so the poor pvt can disappear if it is time 00184 0x8cc07e8,-1,1234,chan_sip.c,2370,unref_peer,3,unsetting a dialog relatedpeer field in sip_destroy 00185 0x8cc07e8,+1,1234,chan_sip.c,3876,find_peer,2,ao2_find in peers table 00186 0x8cc07e8,-1,1234,chan_sip.c,2370,unref_peer,3,unref_peer, from sip_devicestate, release ref from find_peer 00187 ... 00188 00189 This uses a comma delineated format. The columns in the format are as 00190 follows: 00191 - The first column is the object address. 00192 - The second column reflects how the operation affected the ref count 00193 for that object. A change in the ref count is reflected either as 00194 an increment (+) or decrement (-), as well as the amount it changed 00195 by. 00196 - The third column is the ID of the thread that modified the reference 00197 count. 00198 - The fourth column is the source file that the change in reference was 00199 issued from. 00200 - The fifth column is the line number of the source file that the ref 00201 change was issued from. 00202 - The sixth column is the name of the function that the ref change was 00203 issued from. 00204 - The seventh column indicates either (a) construction of the object via 00205 the special tag **constructor**; (b) destruction of the object via 00206 the special tag **destructor**; (c) the previous reference count 00207 prior to this reference change. 00208 - The eighth column is a special tag added by the developer to provide 00209 context for the ref change. Note that any subsequent columns are 00210 considered to be part of this tag. 00211 00212 Sometimes you have some helper functions to do object ref/unref 00213 operations. Using these normally hides the place where these 00214 functions were called. To get the location where these functions 00215 were called to appear in /refs, you can do this sort of thing: 00216 00217 #ifdef REF_DEBUG 00218 #define dialog_ref(arg1,arg2) dialog_ref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00219 #define dialog_unref(arg1,arg2) dialog_unref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00220 static struct sip_pvt *dialog_ref_debug(struct sip_pvt *p, const char *tag, const char *file, int line, const char *func) 00221 { 00222 if (p) { 00223 ao2_ref_debug(p, 1, tag, file, line, func); 00224 } else { 00225 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n"); 00226 } 00227 return p; 00228 } 00229 00230 static struct sip_pvt *dialog_unref_debug(struct sip_pvt *p, const char *tag, const char *file, int line, const char *func) 00231 { 00232 if (p) { 00233 ao2_ref_debug(p, -1, tag, file, line, func); 00234 } 00235 return NULL; 00236 } 00237 #else 00238 static struct sip_pvt *dialog_ref(struct sip_pvt *p, const char *tag) 00239 { 00240 if (p) { 00241 ao2_ref(p, 1); 00242 } else { 00243 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n"); 00244 } 00245 return p; 00246 } 00247 00248 static struct sip_pvt *dialog_unref(struct sip_pvt *p, const char *tag) 00249 { 00250 if (p) { 00251 ao2_ref(p, -1); 00252 } 00253 return NULL; 00254 } 00255 #endif 00256 00257 In the above code, note that the "normal" helper funcs call ao2_ref() as 00258 normal, and the "helper" functions call ao2_ref_debug directly with the 00259 file, function, and line number info provided. You might find this 00260 well worth the effort to help track these function calls in the code. 00261 00262 To find out why objects are not destroyed (a common bug), you can 00263 edit the source file to use the ao2_t_* variants, enable REF_DEBUG 00264 in menuselect, and add a descriptive tag to each call. Recompile, 00265 and run Asterisk, exit asterisk with "core stop gracefully", which should 00266 result in every object being destroyed. 00267 00268 Then, you can "sort -k 1 {AST_LOG_DIR}/refs > x1" to get a sorted list of 00269 all the objects, or you can use "contrib/script/refcounter.py" to scan 00270 the file for you and output any problems it finds. 00271 00272 The above may seem astronomically more work than it is worth to debug 00273 reference counts, which may be true in "simple" situations, but for 00274 more complex situations, it is easily worth 100 times this effort to 00275 help find problems. 00276 00277 To debug, pair all calls so that each call that increments the 00278 refcount is paired with a corresponding call that decrements the 00279 count for the same reason. Hopefully, you will be left with one 00280 or more unpaired calls. This is where you start your search! 00281 00282 For instance, here is an example of this for a dialog object in 00283 chan_sip, that was not getting destroyed, after I moved the lines around 00284 to pair operations: 00285 00286 0x83787a0,+1,1234,chan_sip.c,5733,sip_alloc,**constructor**,(allocate a dialog(pvt) struct) 00287 0x83787a0,-1,1234,chan_sip.c,19173,sip_poke_peer,4,(unref dialog at end of sip_poke_peer, obtained from sip_alloc, just before it goes out of scope) 00288 00289 0x83787a0,+1,1234,chan_sip.c,5854,sip_alloc,1,(link pvt into dialogs table) 00290 0x83787a0,-1,1234,chan_sip.c,19150,sip_poke_peer,3,(About to change the callid -- remove the old name) 00291 0x83787a0,+1,1234,chan_sip.c,19152,sip_poke_peer,2,(Linking in under new name) 00292 0x83787a0,-1,1234,chan_sip.c,2399,dialog_unlink_all,5,(unlinking dialog via ao2_unlink) 00293 00294 0x83787a0,+1,1234,chan_sip.c,19130,sip_poke_peer,2,(copy sip alloc from p to peer->call) 00295 00296 00297 0x83787a0,+1,1234,chan_sip.c,2996,__sip_reliable_xmit,3,(__sip_reliable_xmit: setting pkt->owner) 00298 0x83787a0,-1,1234,chan_sip.c,2425,dialog_unlink_all,4,(remove all current packets in this dialog, and the pointer to the dialog too as part of __sip_destroy) 00299 00300 0x83787a0,+1,1234,chan_sip.c,22356,unload_module,4,(iterate thru dialogs) 00301 0x83787a0,-1,1234,chan_sip.c,22359,unload_module,5,(toss dialog ptr from iterator_next) 00302 00303 00304 0x83787a0,+1,1234,chan_sip.c,22373,unload_module,3,(iterate thru dialogs) 00305 0x83787a0,-1,1234,chan_sip.c,22375,unload_module,2,(throw away iterator result) 00306 00307 0x83787a0,+1,1234,chan_sip.c,2397,dialog_unlink_all,4,(Let's bump the count in the unlink so it doesn't accidentally become dead before we are done) 00308 0x83787a0,-1,1234,chan_sip.c,2436,dialog_unlink_all,3,(Let's unbump the count in the unlink so the poor pvt can disappear if it is time) 00309 00310 As you can see, only one unbalanced operation is in the list, a ref count increment when 00311 the peer->call was set, but no corresponding decrement was made... 00312 00313 Hopefully this helps you narrow your search and find those bugs. 00314 00315 THE ART OF REFERENCE COUNTING 00316 (by Steve Murphy) 00317 SOME TIPS for complicated code, and ref counting: 00318 00319 1. Theoretically, passing a refcounted object pointer into a function 00320 call is an act of copying the reference, and could be refcounted. 00321 But, upon examination, this sort of refcounting will explode the amount 00322 of code you have to enter, and for no tangible benefit, beyond 00323 creating more possible failure points/bugs. It will even 00324 complicate your code and make debugging harder, slow down your program 00325 doing useless increments and decrements of the ref counts. 00326 00327 2. It is better to track places where a ref counted pointer 00328 is copied into a structure or stored. Make sure to decrement the refcount 00329 of any previous pointer that might have been there, if setting 00330 this field might erase a previous pointer. ao2_find and iterate_next 00331 internally increment the ref count when they return a pointer, so 00332 you need to decrement the count before the pointer goes out of scope. 00333 00334 3. Any time you decrement a ref count, it may be possible that the 00335 object will be destroyed (freed) immediately by that call. If you 00336 are destroying a series of fields in a refcounted object, and 00337 any of the unref calls might possibly result in immediate destruction, 00338 you can first increment the count to prevent such behavior, then 00339 after the last test, decrement the pointer to allow the object 00340 to be destroyed, if the refcount would be zero. 00341 00342 Example: 00343 00344 dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done"); 00345 00346 ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink"); 00347 00348 *//* Unlink us from the owner (channel) if we have one *//* 00349 if (dialog->owner) { 00350 if (lockowner) { 00351 ast_channel_lock(dialog->owner); 00352 } 00353 ast_debug(1, "Detaching from channel %s\n", dialog->owner->name); 00354 dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all"); 00355 if (lockowner) { 00356 ast_channel_unlock(dialog->owner); 00357 } 00358 } 00359 if (dialog->registry) { 00360 if (dialog->registry->call == dialog) { 00361 dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all"); 00362 } 00363 dialog->registry = registry_unref(dialog->registry, "delete dialog->registry"); 00364 } 00365 ... 00366 dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time"); 00367 00368 In the above code, the ao2_t_unlink could end up destroying the dialog 00369 object; if this happens, then the subsequent usages of the dialog 00370 pointer could result in a core dump. So, we 'bump' the 00371 count upwards before beginning, and then decrementing the count when 00372 we are finished. This is analogous to 'locking' or 'protecting' operations 00373 for a short while. 00374 00375 4. One of the most insidious problems I've run into when converting 00376 code to do ref counted automatic destruction, is in the destruction 00377 routines. Where a "destroy" routine had previously been called to 00378 get rid of an object in non-refcounted code, the new regime demands 00379 that you tear that "destroy" routine into two pieces, one that will 00380 tear down the links and 'unref' them, and the other to actually free 00381 and reset fields. A destroy routine that does any reference deletion 00382 for its own object, will never be called. Another insidious problem 00383 occurs in mutually referenced structures. As an example, a dialog contains 00384 a pointer to a peer, and a peer contains a pointer to a dialog. Watch 00385 out that the destruction of one doesn't depend on the destruction of the 00386 other, as in this case a dependency loop will result in neither being 00387 destroyed! 00388 00389 Given the above, you should be ready to do a good job! 00390 00391 murf 00392 00393 */ 00394 00395 00396 00397 /*! \brief 00398 * Typedef for an object destructor. This is called just before freeing 00399 * the memory for the object. It is passed a pointer to the user-defined 00400 * data of the object. 00401 */ 00402 typedef void (*ao2_destructor_fn)(void *); 00403 00404 /*! \brief Options available when allocating an ao2 object. */ 00405 enum ao2_alloc_opts { 00406 /*! The ao2 object has a recursive mutex lock associated with it. */ 00407 AO2_ALLOC_OPT_LOCK_MUTEX = (0 << 0), 00408 /*! The ao2 object has a non-recursive read/write lock associated with it. */ 00409 AO2_ALLOC_OPT_LOCK_RWLOCK = (1 << 0), 00410 /*! The ao2 object has no lock associated with it. */ 00411 AO2_ALLOC_OPT_LOCK_NOLOCK = (2 << 0), 00412 /*! The ao2 object locking option field mask. */ 00413 AO2_ALLOC_OPT_LOCK_MASK = (3 << 0), 00414 }; 00415 00416 /*! 00417 * \brief Allocate and initialize an object. 00418 * 00419 * \param data_size The sizeof() of the user-defined structure. 00420 * \param destructor_fn The destructor function (can be NULL) 00421 * \param options The ao2 object options (See enum ao2_alloc_opts) 00422 * \param debug_msg An ao2 object debug tracing message. 00423 * \return A pointer to user-data. 00424 * 00425 * \details 00426 * Allocates a struct astobj2 with sufficient space for the 00427 * user-defined structure. 00428 * \note 00429 * - storage is zeroed; XXX maybe we want a flag to enable/disable this. 00430 * - the refcount of the object just created is 1 00431 * - the returned pointer cannot be free()'d or realloc()'ed; 00432 * rather, we just call ao2_ref(o, -1); 00433 * 00434 * @{ 00435 */ 00436 00437 #if defined(REF_DEBUG) 00438 00439 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \ 00440 __ao2_alloc_debug((data_size), (destructor_fn), (options), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00441 #define ao2_alloc_options(data_size, destructor_fn, options) \ 00442 __ao2_alloc_debug((data_size), (destructor_fn), (options), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00443 00444 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \ 00445 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00446 #define ao2_alloc(data_size, destructor_fn) \ 00447 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00448 00449 #elif defined(__AST_DEBUG_MALLOC) 00450 00451 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \ 00452 __ao2_alloc_debug((data_size), (destructor_fn), (options), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00453 #define ao2_alloc_options(data_size, destructor_fn, options) \ 00454 __ao2_alloc_debug((data_size), (destructor_fn), (options), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00455 00456 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \ 00457 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00458 #define ao2_alloc(data_size, destructor_fn) \ 00459 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00460 00461 #else 00462 00463 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \ 00464 __ao2_alloc((data_size), (destructor_fn), (options)) 00465 #define ao2_alloc_options(data_size, destructor_fn, options) \ 00466 __ao2_alloc((data_size), (destructor_fn), (options)) 00467 00468 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \ 00469 __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX) 00470 #define ao2_alloc(data_size, destructor_fn) \ 00471 __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX) 00472 00473 #endif 00474 00475 void *__ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag, 00476 const char *file, int line, const char *func, int ref_debug); 00477 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options); 00478 00479 /*! @} */ 00480 00481 /*! \brief 00482 * Reference/unreference an object and return the old refcount. 00483 * 00484 * \param o A pointer to the object 00485 * \param delta Value to add to the reference counter. 00486 * \param tag used for debugging 00487 * \return The value of the reference counter before the operation. 00488 * 00489 * Increase/decrease the reference counter according 00490 * the value of delta. 00491 * 00492 * If the refcount goes to zero, the object is destroyed. 00493 * 00494 * \note The object must not be locked by the caller of this function, as 00495 * it is invalid to try to unlock it after releasing the reference. 00496 * 00497 * \note if we know the pointer to an object, it is because we 00498 * have a reference count to it, so the only case when the object 00499 * can go away is when we release our reference, and it is 00500 * the last one in existence. 00501 * 00502 * @{ 00503 */ 00504 00505 #ifdef REF_DEBUG 00506 00507 #define ao2_t_ref(o,delta,tag) __ao2_ref_debug((o), (delta), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00508 #define ao2_ref(o,delta) __ao2_ref_debug((o), (delta), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 00509 00510 #else 00511 00512 #define ao2_t_ref(o,delta,tag) __ao2_ref((o), (delta)) 00513 #define ao2_ref(o,delta) __ao2_ref((o), (delta)) 00514 00515 #endif 00516 00517 int __ao2_ref_debug(void *o, int delta, const char *tag, const char *file, int line, const char *func); 00518 int __ao2_ref(void *o, int delta); 00519 00520 /*! @} */ 00521 00522 /*! \brief Which lock to request. */ 00523 enum ao2_lock_req { 00524 /*! Request the mutex lock be acquired. */ 00525 AO2_LOCK_REQ_MUTEX, 00526 /*! Request the read lock be acquired. */ 00527 AO2_LOCK_REQ_RDLOCK, 00528 /*! Request the write lock be acquired. */ 00529 AO2_LOCK_REQ_WRLOCK, 00530 }; 00531 00532 /*! \brief 00533 * Lock an object. 00534 * 00535 * \param a A pointer to the object we want to lock. 00536 * \return 0 on success, other values on error. 00537 */ 00538 int __ao2_lock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var); 00539 #define ao2_lock(a) __ao2_lock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00540 #define ao2_rdlock(a) __ao2_lock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00541 #define ao2_wrlock(a) __ao2_lock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00542 00543 /*! \brief 00544 * Unlock an object. 00545 * 00546 * \param a A pointer to the object we want unlock. 00547 * \return 0 on success, other values on error. 00548 */ 00549 int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var); 00550 #define ao2_unlock(a) __ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00551 00552 /*! \brief 00553 * Try locking-- (don't block if fail) 00554 * 00555 * \param a A pointer to the object we want to lock. 00556 * \return 0 on success, other values on error. 00557 */ 00558 int __ao2_trylock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var); 00559 #define ao2_trylock(a) __ao2_trylock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00560 #define ao2_tryrdlock(a) __ao2_trylock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00561 #define ao2_trywrlock(a) __ao2_trylock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00562 00563 /*! 00564 * \brief Return the mutex lock address of an object 00565 * 00566 * \param[in] obj A pointer to the object we want. 00567 * \return the address of the mutex lock, else NULL. 00568 * 00569 * This function comes in handy mainly for debugging locking 00570 * situations, where the locking trace code reports the 00571 * lock address, this allows you to correlate against 00572 * object address, to match objects to reported locks. 00573 * 00574 * \since 1.6.1 00575 */ 00576 void *ao2_object_get_lockaddr(void *obj); 00577 00578 00579 /*! Global ao2 object holder structure. */ 00580 struct ao2_global_obj { 00581 /*! Access lock to the held ao2 object. */ 00582 ast_rwlock_t lock; 00583 /*! Global ao2 object. */ 00584 void *obj; 00585 }; 00586 00587 /*! 00588 * \brief Define a global object holder to be used to hold an ao2 object, statically initialized. 00589 * \since 11.0 00590 * 00591 * \param name This will be the name of the object holder. 00592 * 00593 * \details 00594 * This macro creates a global object holder that can be used to 00595 * hold an ao2 object accessible using the API. The structure is 00596 * allocated and initialized to be empty. 00597 * 00598 * Example usage: 00599 * \code 00600 * static AO2_GLOBAL_OBJ_STATIC(global_cfg); 00601 * \endcode 00602 * 00603 * This defines global_cfg, intended to hold an ao2 object 00604 * accessible using an API. 00605 */ 00606 #ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER 00607 #define AO2_GLOBAL_OBJ_STATIC(name) \ 00608 struct ao2_global_obj name; \ 00609 static void __attribute__((constructor)) __init_##name(void) \ 00610 { \ 00611 ast_rwlock_init(&name.lock); \ 00612 name.obj = NULL; \ 00613 } \ 00614 static void __attribute__((destructor)) __fini_##name(void) \ 00615 { \ 00616 if (name.obj) { \ 00617 ao2_ref(name.obj, -1); \ 00618 name.obj = NULL; \ 00619 } \ 00620 ast_rwlock_destroy(&name.lock); \ 00621 } \ 00622 struct __dummy_##name 00623 #else 00624 #define AO2_GLOBAL_OBJ_STATIC(name) \ 00625 struct ao2_global_obj name = { \ 00626 .lock = AST_RWLOCK_INIT_VALUE, \ 00627 } 00628 #endif 00629 00630 /*! 00631 * \brief Release the ao2 object held in the global holder. 00632 * \since 11.0 00633 * 00634 * \param holder Global ao2 object holder. 00635 * \param tag used for debugging 00636 * 00637 * \return Nothing 00638 */ 00639 #ifdef REF_DEBUG 00640 #define ao2_t_global_obj_release(holder, tag) \ 00641 __ao2_global_obj_release(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00642 #define ao2_global_obj_release(holder) \ 00643 __ao2_global_obj_release(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00644 00645 #else 00646 00647 #define ao2_t_global_obj_release(holder, tag) \ 00648 __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00649 #define ao2_global_obj_release(holder) \ 00650 __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00651 #endif 00652 00653 void __ao2_global_obj_release(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name); 00654 00655 /*! 00656 * \brief Replace an ao2 object in the global holder. 00657 * \since 11.0 00658 * 00659 * \param holder Global ao2 object holder. 00660 * \param obj Object to put into the holder. Can be NULL. 00661 * \param tag used for debugging 00662 * 00663 * \note This function automatically increases the reference 00664 * count to account for the reference that the global holder now 00665 * holds to the object. 00666 * 00667 * \retval Reference to previous global ao2 object stored. 00668 * \retval NULL if no object available. 00669 */ 00670 #ifdef REF_DEBUG 00671 #define ao2_t_global_obj_replace(holder, obj, tag) \ 00672 __ao2_global_obj_replace(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00673 #define ao2_global_obj_replace(holder, obj) \ 00674 __ao2_global_obj_replace(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00675 00676 #else 00677 00678 #define ao2_t_global_obj_replace(holder, obj, tag) \ 00679 __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00680 #define ao2_global_obj_replace(holder, obj) \ 00681 __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00682 #endif 00683 00684 void *__ao2_global_obj_replace(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name); 00685 00686 /*! 00687 * \brief Replace an ao2 object in the global holder, throwing away any old object. 00688 * \since 11.0 00689 * 00690 * \param holder Global ao2 object holder. 00691 * \param obj Object to put into the holder. Can be NULL. 00692 * \param tag used for debugging 00693 * 00694 * \note This function automatically increases the reference 00695 * count to account for the reference that the global holder now 00696 * holds to the object. It also decreases the reference count 00697 * of any object being replaced. 00698 * 00699 * \retval 0 The global object was previously empty 00700 * \retval 1 The global object was not previously empty 00701 */ 00702 #ifdef REF_DEBUG 00703 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \ 00704 __ao2_global_obj_replace_unref(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00705 #define ao2_global_obj_replace_unref(holder, obj) \ 00706 __ao2_global_obj_replace_unref(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00707 00708 #else 00709 00710 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \ 00711 __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00712 #define ao2_global_obj_replace_unref(holder, obj) \ 00713 __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00714 #endif 00715 00716 int __ao2_global_obj_replace_unref(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name); 00717 00718 /*! 00719 * \brief Get a reference to the object stored in the global holder. 00720 * \since 11.0 00721 * 00722 * \param holder Global ao2 object holder. 00723 * \param tag used for debugging 00724 * 00725 * \retval Reference to current ao2 object stored in the holder. 00726 * \retval NULL if no object available. 00727 */ 00728 #ifdef REF_DEBUG 00729 #define ao2_t_global_obj_ref(holder, tag) \ 00730 __ao2_global_obj_ref(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00731 #define ao2_global_obj_ref(holder) \ 00732 __ao2_global_obj_ref(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00733 00734 #else 00735 00736 #define ao2_t_global_obj_ref(holder, tag) \ 00737 __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00738 #define ao2_global_obj_ref(holder) \ 00739 __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder) 00740 #endif 00741 00742 void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name); 00743 00744 00745 /*! 00746 \page AstObj2_Containers AstObj2 Containers 00747 00748 Containers are data structures meant to store several objects, 00749 and perform various operations on them. 00750 Internally, objects are stored in lists, hash tables or other 00751 data structures depending on the needs. 00752 00753 \note NOTA BENE: at the moment the only container we support is the 00754 hash table and its degenerate form, the list. 00755 00756 Operations on container include: 00757 00758 - c = \b ao2_container_alloc(size, hash_fn, cmp_fn) 00759 allocate a container with desired size and default compare 00760 and hash function 00761 -The compare function returns an int, which 00762 can be 0 for not found, CMP_STOP to stop end a traversal, 00763 or CMP_MATCH if they are equal 00764 -The hash function returns an int. The hash function 00765 takes two argument, the object pointer and a flags field, 00766 00767 - \b ao2_find(c, arg, flags) 00768 returns zero or more elements matching a given criteria 00769 (specified as arg). 'c' is the container pointer. Flags 00770 can be: 00771 OBJ_UNLINK - to remove the object, once found, from the container. 00772 OBJ_NODATA - don't return the object if found (no ref count change) 00773 OBJ_MULTIPLE - don't stop at first match 00774 OBJ_POINTER - if set, 'arg' is an object pointer, and a hash table 00775 search will be done. If not, a traversal is done. 00776 OBJ_KEY - if set, 'arg', is a hashable item that is not an object. 00777 Similar to OBJ_POINTER and mutually exclusive. 00778 00779 - \b ao2_callback(c, flags, fn, arg) 00780 apply fn(obj, arg) to all objects in the container. 00781 Similar to find. fn() can tell when to stop, and 00782 do anything with the object including unlinking it. 00783 - c is the container; 00784 - flags can be 00785 OBJ_UNLINK - to remove the object, once found, from the container. 00786 OBJ_NODATA - don't return the object if found (no ref count change) 00787 OBJ_MULTIPLE - don't stop at first match 00788 OBJ_POINTER - if set, 'arg' is an object pointer, and a hash table 00789 search will be done. If not, a traversal is done through 00790 all the hash table 'buckets'.. 00791 OBJ_KEY - if set, 'arg', is a hashable item that is not an object. 00792 Similar to OBJ_POINTER and mutually exclusive. 00793 - fn is a func that returns int, and takes 3 args: 00794 (void *obj, void *arg, int flags); 00795 obj is an object 00796 arg is the same as arg passed into ao2_callback 00797 flags is the same as flags passed into ao2_callback 00798 fn returns: 00799 0: no match, keep going 00800 CMP_STOP: stop search, no match 00801 CMP_MATCH: This object is matched. 00802 00803 Note that the entire operation is run with the container 00804 locked, so nobody else can change its content while we work on it. 00805 However, we pay this with the fact that doing 00806 anything blocking in the callback keeps the container 00807 blocked. 00808 The mechanism is very flexible because the callback function fn() 00809 can do basically anything e.g. counting, deleting records, etc. 00810 possibly using arg to store the results. 00811 00812 - \b iterate on a container 00813 this is done with the following sequence 00814 00815 \code 00816 00817 struct ao2_container *c = ... // our container 00818 struct ao2_iterator i; 00819 void *o; 00820 00821 i = ao2_iterator_init(c, flags); 00822 00823 while ((o = ao2_iterator_next(&i))) { 00824 ... do something on o ... 00825 ao2_ref(o, -1); 00826 } 00827 00828 ao2_iterator_destroy(&i); 00829 \endcode 00830 00831 The difference with the callback is that the control 00832 on how to iterate is left to us. 00833 00834 - \b ao2_ref(c, -1) 00835 dropping a reference to a container destroys it, very simple! 00836 00837 Containers are ao2 objects themselves, and this is why their 00838 implementation is simple too. 00839 00840 Before declaring containers, we need to declare the types of the 00841 arguments passed to the constructor - in turn, this requires 00842 to define callback and hash functions and their arguments. 00843 00844 - \ref AstObj2 00845 - \ref astobj2.h 00846 */ 00847 00848 /*! \brief 00849 * Type of a generic callback function 00850 * \param obj pointer to the (user-defined part) of an object. 00851 * \param arg callback argument from ao2_callback() 00852 * \param flags flags from ao2_callback() 00853 * 00854 * The return values are a combination of enum _cb_results. 00855 * Callback functions are used to search or manipulate objects in a container. 00856 */ 00857 typedef int (ao2_callback_fn)(void *obj, void *arg, int flags); 00858 00859 /*! \brief 00860 * Type of a generic callback function 00861 * \param obj pointer to the (user-defined part) of an object. 00862 * \param arg callback argument from ao2_callback() 00863 * \param data arbitrary data from ao2_callback() 00864 * \param flags flags from ao2_callback() 00865 * 00866 * The return values are a combination of enum _cb_results. 00867 * Callback functions are used to search or manipulate objects in a container. 00868 */ 00869 typedef int (ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags); 00870 00871 /*! \brief A common ao2_callback is one that matches by address. */ 00872 int ao2_match_by_addr(void *obj, void *arg, int flags); 00873 00874 /*! \brief 00875 * A callback function will return a combination of CMP_MATCH and CMP_STOP. 00876 * The latter will terminate the search in a container. 00877 */ 00878 enum _cb_results { 00879 CMP_MATCH = 0x1, /*!< the object matches the request */ 00880 CMP_STOP = 0x2, /*!< stop the search now */ 00881 }; 00882 00883 /*! \brief 00884 * Flags passed to ao2_callback() and ao2_hash_fn() to modify its behaviour. 00885 */ 00886 enum search_flags { 00887 /*! 00888 * Unlink the object for which the callback function returned 00889 * CMP_MATCH. 00890 */ 00891 OBJ_UNLINK = (1 << 0), 00892 /*! 00893 * On match, don't return the object hence do not increase its 00894 * refcount. 00895 */ 00896 OBJ_NODATA = (1 << 1), 00897 /*! 00898 * Don't stop at the first match in ao2_callback() unless the 00899 * result of of the callback function has the CMP_STOP bit set. 00900 */ 00901 OBJ_MULTIPLE = (1 << 2), 00902 /*! 00903 * The given obj is an object of the same type as the one being 00904 * searched for, so use the object's hash function for optimized 00905 * searching. 00906 * 00907 * The matching function is unaffected (i.e. The cb_fn argument 00908 * to ao2_callback). 00909 */ 00910 OBJ_POINTER = (1 << 3), 00911 /*! 00912 * \brief Continue if a match is not found in the hashed out bucket 00913 * 00914 * This flag is to be used in combination with OBJ_POINTER. This tells 00915 * the ao2_callback() core to keep searching through the rest of the 00916 * buckets if a match is not found in the starting bucket defined by 00917 * the hash value on the argument. 00918 */ 00919 OBJ_CONTINUE = (1 << 4), 00920 /*! 00921 * \brief Assume that the ao2_container is already locked. 00922 * 00923 * \note For ao2_containers that have mutexes, no locking will 00924 * be done. 00925 * 00926 * \note For ao2_containers that have RWLOCKs, the lock will be 00927 * promoted to write mode as needed. The lock will be returned 00928 * to the original locked state. 00929 * 00930 * \note Only use this flag if the ao2_container is manually 00931 * locked already. 00932 */ 00933 OBJ_NOLOCK = (1 << 5), 00934 /*! 00935 * \brief The data is hashable, but is not an object. 00936 * 00937 * \details 00938 * This can be used when you want to be able to pass custom data 00939 * to the container's stored ao2_hash_fn and ao2_find 00940 * ao2_callback_fn functions that is not a full object, but 00941 * perhaps just a string. 00942 * 00943 * \note OBJ_KEY and OBJ_POINTER are mutually exclusive options. 00944 */ 00945 OBJ_KEY = (1 << 6), 00946 }; 00947 00948 /*! 00949 * Type of a generic function to generate a hash value from an object. 00950 * flags is ignored at the moment. Eventually, it will include the 00951 * value of OBJ_POINTER passed to ao2_callback(). 00952 */ 00953 typedef int (ao2_hash_fn)(const void *obj, int flags); 00954 00955 /*! \name Object Containers 00956 * Here start declarations of containers. 00957 */ 00958 /*@{ */ 00959 struct ao2_container; 00960 00961 /*! 00962 * \brief Allocate and initialize a hash container with the desired number of buckets. 00963 * 00964 * \details 00965 * We allocate space for a struct astobj_container, struct container 00966 * and the buckets[] array. 00967 * 00968 * \param options Container ao2 object options (See enum ao2_alloc_opts) 00969 * \param n_buckets Number of buckets for hash 00970 * \param hash_fn Pointer to a function computing a hash value. (NULL if everyting goes in first bucket.) 00971 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything) 00972 * \param tag used for debugging. 00973 * 00974 * \return A pointer to a struct container. 00975 * 00976 * \note Destructor is set implicitly. 00977 */ 00978 00979 #if defined(REF_DEBUG) 00980 00981 #define ao2_t_container_alloc_options(options, n_buckets, hash_fn, cmp_fn, tag) \ 00982 __ao2_container_alloc_debug((options), (n_buckets), (hash_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00983 #define ao2_container_alloc_options(options, n_buckets, hash_fn, cmp_fn) \ 00984 __ao2_container_alloc_debug((options), (n_buckets), (hash_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00985 00986 #define ao2_t_container_alloc(n_buckets, hash_fn, cmp_fn, tag) \ 00987 __ao2_container_alloc_debug(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00988 #define ao2_container_alloc(n_buckets, hash_fn, cmp_fn) \ 00989 __ao2_container_alloc_debug(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00990 00991 #elif defined(__AST_DEBUG_MALLOC) 00992 00993 #define ao2_t_container_alloc_options(options, n_buckets, hash_fn, cmp_fn, tag) \ 00994 __ao2_container_alloc_debug((options), (n_buckets), (hash_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00995 #define ao2_container_alloc_options(options, n_buckets, hash_fn, cmp_fn) \ 00996 __ao2_container_alloc_debug((options), (n_buckets), (hash_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00997 00998 #define ao2_t_container_alloc(n_buckets, hash_fn, cmp_fn, tag) \ 00999 __ao2_container_alloc_debug(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 01000 #define ao2_container_alloc(n_buckets, hash_fn, cmp_fn) \ 01001 __ao2_container_alloc_debug(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 01002 01003 #else 01004 01005 #define ao2_t_container_alloc_options(options, n_buckets, hash_fn, cmp_fn, tag) \ 01006 __ao2_container_alloc((options), (n_buckets), (hash_fn), (cmp_fn)) 01007 #define ao2_container_alloc_options(options, n_buckets, hash_fn, cmp_fn) \ 01008 __ao2_container_alloc((options), (n_buckets), (hash_fn), (cmp_fn)) 01009 01010 #define ao2_t_container_alloc(n_buckets, hash_fn, cmp_fn, tag) \ 01011 __ao2_container_alloc(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn)) 01012 #define ao2_container_alloc(n_buckets, hash_fn, cmp_fn) \ 01013 __ao2_container_alloc(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn)) 01014 01015 #endif 01016 01017 struct ao2_container *__ao2_container_alloc(unsigned int options, 01018 unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn); 01019 struct ao2_container *__ao2_container_alloc_debug(unsigned int options, 01020 unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn, 01021 const char *tag, const char *file, int line, const char *func, int ref_debug); 01022 01023 /*! \brief 01024 * Returns the number of elements in a container. 01025 */ 01026 int ao2_container_count(struct ao2_container *c); 01027 01028 /*! 01029 * \brief Copy all object references in the src container into the dest container. 01030 * \since 11.0 01031 * 01032 * \param dest Container to copy src object references into. 01033 * \param src Container to copy all object references from. 01034 * \param flags OBJ_NOLOCK if a lock is already held on both containers. 01035 * Otherwise, the src container is locked first. 01036 * 01037 * \pre The dest container must be empty. If the duplication fails, the 01038 * dest container will be returned empty. 01039 * 01040 * \note This can potentially be expensive because a malloc is 01041 * needed for every object in the src container. 01042 * 01043 * \retval 0 on success. 01044 * \retval -1 on error. 01045 */ 01046 int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags); 01047 01048 /*! 01049 * \brief Create a clone/copy of the given container. 01050 * \since 11.0 01051 * 01052 * \param orig Container to copy all object references from. 01053 * \param flags OBJ_NOLOCK if a lock is already held on the container. 01054 * 01055 * \note This can potentially be expensive because a malloc is 01056 * needed for every object in the orig container. 01057 * 01058 * \retval Clone container on success. 01059 * \retval NULL on error. 01060 */ 01061 struct ao2_container *__ao2_container_clone(struct ao2_container *orig, enum search_flags flags); 01062 struct ao2_container *__ao2_container_clone_debug(struct ao2_container *orig, enum search_flags flags, const char *tag, const char *file, int line, const char *func, int ref_debug); 01063 #if defined(REF_DEBUG) 01064 01065 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 01066 #define ao2_container_clone(orig, flags) __ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 01067 01068 #elif defined(__AST_DEBUG_MALLOC) 01069 01070 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 01071 #define ao2_container_clone(orig, flags) __ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 01072 01073 #else 01074 01075 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone(orig, flags) 01076 #define ao2_container_clone(orig, flags) __ao2_container_clone(orig, flags) 01077 01078 #endif 01079 01080 /*@} */ 01081 01082 /*! \name Object Management 01083 * Here we have functions to manage objects. 01084 * 01085 * We can use the functions below on any kind of 01086 * object defined by the user. 01087 */ 01088 /*@{ */ 01089 01090 /*! 01091 * \brief Add an object to a container. 01092 * 01093 * \param container The container to operate on. 01094 * \param obj The object to be added. 01095 * \param flags search_flags to control linking the object. (OBJ_NOLOCK) 01096 * \param tag used for debugging. 01097 * 01098 * \retval NULL on errors. 01099 * \retval !NULL on success. 01100 * 01101 * This function inserts an object in a container according its key. 01102 * 01103 * \note Remember to set the key before calling this function. 01104 * 01105 * \note This function automatically increases the reference count to account 01106 * for the reference that the container now holds to the object. 01107 */ 01108 #ifdef REF_DEBUG 01109 01110 #define ao2_t_link(container, obj, tag) __ao2_link_debug((container), (obj), 0, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01111 #define ao2_link(container, obj) __ao2_link_debug((container), (obj), 0, "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01112 01113 #define ao2_t_link_flags(container, obj, flags, tag) __ao2_link_debug((container), (obj), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01114 #define ao2_link_flags(container, obj, flags) __ao2_link_debug((container), (obj), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01115 01116 #else 01117 01118 #define ao2_t_link(container, obj, tag) __ao2_link((container), (obj), 0) 01119 #define ao2_link(container, obj) __ao2_link((container), (obj), 0) 01120 01121 #define ao2_t_link_flags(container, obj, flags, tag) __ao2_link((container), (obj), (flags)) 01122 #define ao2_link_flags(container, obj, flags) __ao2_link((container), (obj), (flags)) 01123 01124 #endif 01125 01126 void *__ao2_link_debug(struct ao2_container *c, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func); 01127 void *__ao2_link(struct ao2_container *c, void *obj_new, int flags); 01128 01129 /*! 01130 * \brief Remove an object from a container 01131 * 01132 * \param container The container to operate on. 01133 * \param obj The object to unlink. 01134 * \param flags search_flags to control unlinking the object. (OBJ_NOLOCK) 01135 * \param tag used for debugging. 01136 * 01137 * \retval NULL, always 01138 * 01139 * \note The object requested to be unlinked must be valid. However, if it turns 01140 * out that it is not in the container, this function is still safe to 01141 * be called. 01142 * 01143 * \note If the object gets unlinked from the container, the container's 01144 * reference to the object will be automatically released. (The 01145 * refcount will be decremented). 01146 */ 01147 #ifdef REF_DEBUG 01148 01149 #define ao2_t_unlink(container, obj, tag) __ao2_unlink_debug((container), (obj), 0, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01150 #define ao2_unlink(container, obj) __ao2_unlink_debug((container), (obj), 0, "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01151 01152 #define ao2_t_unlink_flags(container, obj, flags, tag) __ao2_unlink_debug((container), (obj), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01153 #define ao2_unlink_flags(container, obj, flags) __ao2_unlink_debug((container), (obj), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01154 01155 #else 01156 01157 #define ao2_t_unlink(container, obj, tag) __ao2_unlink((container), (obj), 0) 01158 #define ao2_unlink(container, obj) __ao2_unlink((container), (obj), 0) 01159 01160 #define ao2_t_unlink_flags(container, obj, flags, tag) __ao2_unlink((container), (obj), (flags)) 01161 #define ao2_unlink_flags(container, obj, flags) __ao2_unlink((container), (obj), (flags)) 01162 01163 #endif 01164 01165 void *__ao2_unlink_debug(struct ao2_container *c, void *obj, int flags, const char *tag, const char *file, int line, const char *func); 01166 void *__ao2_unlink(struct ao2_container *c, void *obj, int flags); 01167 01168 01169 /*@} */ 01170 01171 /*! \brief 01172 * ao2_callback() is a generic function that applies cb_fn() to all objects 01173 * in a container, as described below. 01174 * 01175 * \param c A pointer to the container to operate on. 01176 * \param flags A set of flags specifying the operation to perform, 01177 * partially used by the container code, but also passed to 01178 * the callback. 01179 * - If OBJ_NODATA is set, ao2_callback will return NULL. No refcounts 01180 * of any of the traversed objects will be incremented. 01181 * On the converse, if it is NOT set (the default), the ref count 01182 * of the first matching object will be incremented and returned. If 01183 * OBJ_MULTIPLE is set, the ref count of all matching objects will 01184 * be incremented in an iterator for a temporary container and returned. 01185 * - If OBJ_POINTER is set, the traversed items will be restricted 01186 * to the objects in the bucket that the object key hashes to. 01187 * \param cb_fn A function pointer, that will be called on all 01188 * objects, to see if they match. This function returns CMP_MATCH 01189 * if the object is matches the criteria; CMP_STOP if the traversal 01190 * should immediately stop, or both (via bitwise ORing), if you find a 01191 * match and want to end the traversal, and 0 if the object is not a match, 01192 * but the traversal should continue. This is the function that is applied 01193 * to each object traversed. Its arguments are: 01194 * (void *obj, void *arg, int flags), where: 01195 * obj is an object 01196 * arg is the same as arg passed into ao2_callback 01197 * flags is the same as flags passed into ao2_callback (flags are 01198 * also used by ao2_callback). 01199 * \param arg passed to the callback. 01200 * \param tag used for debugging. 01201 * 01202 * \retval NULL on failure or no matching object found. 01203 * 01204 * \retval object found if OBJ_MULTIPLE is not set in the flags 01205 * parameter. 01206 * 01207 * \retval ao2_iterator pointer if OBJ_MULTIPLE is set in the 01208 * flags parameter. The iterator must be destroyed with 01209 * ao2_iterator_destroy() when the caller no longer needs it. 01210 * 01211 * If the function returns any objects, their refcount is incremented, 01212 * and the caller is in charge of decrementing them once done. 01213 * 01214 * Typically, ao2_callback() is used for two purposes: 01215 * - to perform some action (including removal from the container) on one 01216 * or more objects; in this case, cb_fn() can modify the object itself, 01217 * and to perform deletion should set CMP_MATCH on the matching objects, 01218 * and have OBJ_UNLINK set in flags. 01219 * - to look for a specific object in a container; in this case, cb_fn() 01220 * should not modify the object, but just return a combination of 01221 * CMP_MATCH and CMP_STOP on the desired object. 01222 * Other usages are also possible, of course. 01223 * 01224 * This function searches through a container and performs operations 01225 * on objects according on flags passed. 01226 * XXX describe better 01227 * The comparison is done calling the compare function set implicitly. 01228 * The arg pointer can be a pointer to an object or to a key, 01229 * we can say this looking at flags value. 01230 * If arg points to an object we will search for the object pointed 01231 * by this value, otherwise we search for a key value. 01232 * If the key is not unique we only find the first matching value. 01233 * 01234 * The use of flags argument is the follow: 01235 * 01236 * OBJ_UNLINK unlinks the object found 01237 * OBJ_NODATA on match, do return an object 01238 * Callbacks use OBJ_NODATA as a default 01239 * functions such as find() do 01240 * OBJ_MULTIPLE return multiple matches 01241 * Default is no. 01242 * OBJ_POINTER the pointer is an object pointer 01243 * OBJ_KEY the pointer is to a hashable key 01244 * 01245 * \note When the returned object is no longer in use, ao2_ref() should 01246 * be used to free the additional reference possibly created by this function. 01247 * 01248 * @{ 01249 */ 01250 #ifdef REF_DEBUG 01251 01252 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \ 01253 __ao2_callback_debug((c), (flags), (cb_fn), (arg), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01254 #define ao2_callback(c, flags, cb_fn, arg) \ 01255 __ao2_callback_debug((c), (flags), (cb_fn), (arg), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01256 01257 #else 01258 01259 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \ 01260 __ao2_callback((c), (flags), (cb_fn), (arg)) 01261 #define ao2_callback(c, flags, cb_fn, arg) \ 01262 __ao2_callback((c), (flags), (cb_fn), (arg)) 01263 01264 #endif 01265 01266 void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags, 01267 ao2_callback_fn *cb_fn, void *arg, const char *tag, const char *file, int line, 01268 const char *func); 01269 void *__ao2_callback(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg); 01270 01271 /*! @} */ 01272 01273 /*! \brief 01274 * ao2_callback_data() is a generic function that applies cb_fn() to all objects 01275 * in a container. It is functionally identical to ao2_callback() except that 01276 * instead of taking an ao2_callback_fn *, it takes an ao2_callback_data_fn *, and 01277 * allows the caller to pass in arbitrary data. 01278 * 01279 * This call would be used instead of ao2_callback() when the caller needs to pass 01280 * OBJ_POINTER as part of the flags argument (which in turn requires passing in a 01281 * prototype ao2 object for 'arg') and also needs access to other non-global data 01282 * to complete it's comparison or task. 01283 * 01284 * See the documentation for ao2_callback() for argument descriptions. 01285 * 01286 * \see ao2_callback() 01287 */ 01288 #ifdef REF_DEBUG 01289 01290 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \ 01291 __ao2_callback_data_debug((container), (flags), (cb_fn), (arg), (data), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01292 #define ao2_callback_data(container, flags, cb_fn, arg, data) \ 01293 __ao2_callback_data_debug((container), (flags), (cb_fn), (arg), (data), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01294 01295 #else 01296 01297 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \ 01298 __ao2_callback_data((container), (flags), (cb_fn), (arg), (data)) 01299 #define ao2_callback_data(container, flags, cb_fn, arg, data) \ 01300 __ao2_callback_data((container), (flags), (cb_fn), (arg), (data)) 01301 01302 #endif 01303 01304 void *__ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags, 01305 ao2_callback_data_fn *cb_fn, void *arg, void *data, const char *tag, const char *file, 01306 int line, const char *func); 01307 void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags, 01308 ao2_callback_data_fn *cb_fn, void *arg, void *data); 01309 01310 /*! ao2_find() is a short hand for ao2_callback(c, flags, c->cmp_fn, arg) 01311 * XXX possibly change order of arguments ? 01312 */ 01313 #ifdef REF_DEBUG 01314 01315 #define ao2_t_find(container, arg, flags, tag) \ 01316 __ao2_find_debug((container), (arg), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01317 #define ao2_find(container, arg, flags) \ 01318 __ao2_find_debug((container), (arg), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01319 01320 #else 01321 01322 #define ao2_t_find(container, arg, flags, tag) \ 01323 __ao2_find((container), (arg), (flags)) 01324 #define ao2_find(container, arg, flags) \ 01325 __ao2_find((container), (arg), (flags)) 01326 01327 #endif 01328 01329 void *__ao2_find_debug(struct ao2_container *c, const void *arg, enum search_flags flags, 01330 const char *tag, const char *file, int line, const char *func); 01331 void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags); 01332 01333 /*! \brief 01334 * 01335 * 01336 * When we need to walk through a container, we use an 01337 * ao2_iterator to keep track of the current position. 01338 * 01339 * Because the navigation is typically done without holding the 01340 * lock on the container across the loop, objects can be inserted or deleted 01341 * or moved while we work. As a consequence, there is no guarantee that 01342 * we manage to touch all the elements in the container, and it is possible 01343 * that we touch the same object multiple times. 01344 * 01345 * However, within the current hash table container, the following is true: 01346 * - It is not possible to miss an object in the container while iterating 01347 * unless it gets added after the iteration begins and is added to a bucket 01348 * that is before the one the current object is in. In this case, even if 01349 * you locked the container around the entire iteration loop, you still would 01350 * not see this object, because it would still be waiting on the container 01351 * lock so that it can be added. 01352 * - It would be extremely rare to see an object twice. The only way this can 01353 * happen is if an object got unlinked from the container and added again 01354 * during the same iteration. Furthermore, when the object gets added back, 01355 * it has to be in the current or later bucket for it to be seen again. 01356 * 01357 * An iterator must be first initialized with ao2_iterator_init(), 01358 * then we can use o = ao2_iterator_next() to move from one 01359 * element to the next. Remember that the object returned by 01360 * ao2_iterator_next() has its refcount incremented, 01361 * and the reference must be explicitly released when done with it. 01362 * 01363 * In addition, ao2_iterator_init() will hold a reference to the container 01364 * being iterated, which will be freed when ao2_iterator_destroy() is called 01365 * to free up the resources used by the iterator (if any). 01366 * 01367 * Example: 01368 * 01369 * \code 01370 * 01371 * struct ao2_container *c = ... // the container we want to iterate on 01372 * struct ao2_iterator i; 01373 * struct my_obj *o; 01374 * 01375 * i = ao2_iterator_init(c, flags); 01376 * 01377 * while ((o = ao2_iterator_next(&i))) { 01378 * ... do something on o ... 01379 * ao2_ref(o, -1); 01380 * } 01381 * 01382 * ao2_iterator_destroy(&i); 01383 * 01384 * \endcode 01385 * 01386 */ 01387 01388 /*! \brief 01389 * The astobj2 iterator 01390 * 01391 * \note You are not supposed to know the internals of an iterator! 01392 * We would like the iterator to be opaque, unfortunately 01393 * its size needs to be known if we want to store it around 01394 * without too much trouble. 01395 * Anyways... 01396 * The iterator has a pointer to the container, and a flags 01397 * field specifying various things e.g. whether the container 01398 * should be locked or not while navigating on it. 01399 * The iterator "points" to the current object, which is identified 01400 * by three values: 01401 * 01402 * - a bucket number; 01403 * - the object_id, which is also the container version number 01404 * when the object was inserted. This identifies the object 01405 * uniquely, however reaching the desired object requires 01406 * scanning a list. 01407 * - a pointer, and a container version when we saved the pointer. 01408 * If the container has not changed its version number, then we 01409 * can safely follow the pointer to reach the object in constant time. 01410 * 01411 * Details are in the implementation of ao2_iterator_next() 01412 * A freshly-initialized iterator has bucket=0, version=0. 01413 */ 01414 struct ao2_iterator { 01415 /*! the container */ 01416 struct ao2_container *c; 01417 /*! operation flags */ 01418 int flags; 01419 /*! current bucket */ 01420 int bucket; 01421 /*! container version */ 01422 unsigned int c_version; 01423 /*! pointer to the current object */ 01424 void *obj; 01425 /*! container version when the object was created */ 01426 unsigned int version; 01427 }; 01428 01429 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior 01430 * of the iterator. 01431 */ 01432 enum ao2_iterator_flags { 01433 /*! 01434 * \brief Assume that the ao2_container is already locked. 01435 * 01436 * \note For ao2_containers that have mutexes, no locking will 01437 * be done. 01438 * 01439 * \note For ao2_containers that have RWLOCKs, the lock will be 01440 * promoted to write mode as needed. The lock will be returned 01441 * to the original locked state. 01442 * 01443 * \note Only use this flag if the ao2_container is manually 01444 * locked already. 01445 */ 01446 AO2_ITERATOR_DONTLOCK = (1 << 0), 01447 /*! 01448 * Indicates that the iterator was dynamically allocated by 01449 * astobj2 API and should be freed by ao2_iterator_destroy(). 01450 */ 01451 AO2_ITERATOR_MALLOCD = (1 << 1), 01452 /*! 01453 * Indicates that before the iterator returns an object from 01454 * the container being iterated, the object should be unlinked 01455 * from the container. 01456 */ 01457 AO2_ITERATOR_UNLINK = (1 << 2), 01458 }; 01459 01460 /*! 01461 * \brief Create an iterator for a container 01462 * 01463 * \param c the container 01464 * \param flags one or more flags from ao2_iterator_flags 01465 * 01466 * \retval the constructed iterator 01467 * 01468 * \note This function does \b not take a pointer to an iterator; 01469 * rather, it returns an iterator structure that should be 01470 * assigned to (overwriting) an existing iterator structure 01471 * allocated on the stack or on the heap. 01472 * 01473 * This function will take a reference on the container being iterated. 01474 * 01475 */ 01476 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags); 01477 01478 /*! 01479 * \brief Destroy a container iterator 01480 * 01481 * \param iter the iterator to destroy 01482 * 01483 * \retval none 01484 * 01485 * This function will release the container reference held by the iterator 01486 * and any other resources it may be holding. 01487 * 01488 */ 01489 #if defined(TEST_FRAMEWORK) 01490 void ao2_iterator_destroy(struct ao2_iterator *iter) __attribute__((noinline)); 01491 #else 01492 void ao2_iterator_destroy(struct ao2_iterator *iter); 01493 #endif 01494 #ifdef REF_DEBUG 01495 01496 #define ao2_t_iterator_next(iter, tag) __ao2_iterator_next_debug((iter), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01497 #define ao2_iterator_next(iter) __ao2_iterator_next_debug((iter), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01498 01499 #else 01500 01501 #define ao2_t_iterator_next(iter, tag) __ao2_iterator_next((iter)) 01502 #define ao2_iterator_next(iter) __ao2_iterator_next((iter)) 01503 01504 #endif 01505 01506 void *__ao2_iterator_next_debug(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func); 01507 void *__ao2_iterator_next(struct ao2_iterator *iter); 01508 01509 /* extra functions */ 01510 void ao2_bt(void); /* backtrace */ 01511 01512 /*! gcc __attribute__(cleanup()) functions 01513 * \note they must be able to handle NULL parameters because most of the 01514 * allocation/find functions can fail and we don't want to try to tear 01515 * down a NULL */ 01516 void __ao2_cleanup(void *obj); 01517 void __ao2_cleanup_debug(void *obj, const char *file, int line, const char *function); 01518 #ifdef REF_DEBUG 01519 #define ao2_cleanup(obj) __ao2_cleanup_debug((obj), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01520 #else 01521 #define ao2_cleanup(obj) __ao2_cleanup(obj) 01522 #endif 01523 void ao2_iterator_cleanup(struct ao2_iterator *iter); 01524 #endif /* _ASTERISK_ASTOBJ2_H */