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/linkedlists.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 and 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 reinserting 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_xxxx 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 that 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, 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 return p; 00227 } 00228 00229 static struct sip_pvt *dialog_unref_debug(struct sip_pvt *p, char *tag, const char *file, int line, const char *func) 00230 { 00231 if (p) 00232 ao2_ref_debug(p, -1, tag, file, line, func); 00233 return NULL; 00234 } 00235 #else 00236 static struct sip_pvt *dialog_ref(struct sip_pvt *p, char *tag) 00237 { 00238 if (p) 00239 ao2_ref(p, 1); 00240 else 00241 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n"); 00242 return p; 00243 } 00244 00245 static struct sip_pvt *dialog_unref(struct sip_pvt *p, char *tag) 00246 { 00247 if (p) 00248 ao2_ref(p, -1); 00249 return NULL; 00250 } 00251 #endif 00252 00253 In the above code, note that the "normal" helper funcs call ao2_ref() as 00254 normal, and the "helper" functions call ao2_ref_debug directly with the 00255 file, function, and line number info provided. You might find this 00256 well worth the effort to help track these function calls in the code. 00257 00258 To find out why objects are not destroyed (a common bug), you can 00259 edit the source file to use the ao2_t_* variants, enable REF_DEBUG 00260 in menuselect, and add a descriptive tag to each call. Recompile, 00261 and run Asterisk, exit asterisk with "core stop gracefully", which should 00262 result in every object being destroyed. 00263 00264 Then, you can "sort -k 1 {AST_LOG_DIR}/refs > x1" to get a sorted list of 00265 all the objects, or you can use "contrib/script/refcounter.py" to scan 00266 the file for you and output any problems it finds. 00267 00268 The above may seem astronomically more work than it is worth to debug 00269 reference counts, which may be true in "simple" situations, but for 00270 more complex situations, it is easily worth 100 times this effort to 00271 help find problems. 00272 00273 To debug, pair all calls so that each call that increments the 00274 refcount is paired with a corresponding call that decrements the 00275 count for the same reason. Hopefully, you will be left with one 00276 or more unpaired calls. This is where you start your search! 00277 00278 For instance, here is an example of this for a dialog object in 00279 chan_sip, that was not getting destroyed, after I moved the lines around 00280 to pair operations: 00281 00282 0x83787a0,+1,1234,chan_sip.c,5733,sip_alloc,**constructor**,(allocate a dialog(pvt) struct) 00283 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) 00284 00285 0x83787a0,+1,1234,chan_sip.c,5854,sip_alloc,1,(link pvt into dialogs table) 00286 0x83787a0,-1,1234,chan_sip.c,19150,sip_poke_peer,3,(About to change the callid -- remove the old name) 00287 0x83787a0,+1,1234,chan_sip.c,19152,sip_poke_peer,2,(Linking in under new name) 00288 0x83787a0,-1,1234,chan_sip.c,2399,dialog_unlink_all,5,(unlinking dialog via ao2_unlink) 00289 00290 0x83787a0,+1,1234,chan_sip.c,19130,sip_poke_peer,2,(copy sip alloc from p to peer->call) 00291 00292 00293 0x83787a0,+1,1234,chan_sip.c,2996,__sip_reliable_xmit,3,(__sip_reliable_xmit: setting pkt->owner) 00294 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) 00295 00296 0x83787a0,+1,1234,chan_sip.c,22356,unload_module,4,(iterate thru dialogs) 00297 0x83787a0,-1,1234,chan_sip.c,22359,unload_module,5,(toss dialog ptr from iterator_next) 00298 00299 00300 0x83787a0,+1,1234,chan_sip.c,22373,unload_module,3,(iterate thru dialogs) 00301 0x83787a0,-1,1234,chan_sip.c,22375,unload_module,2,(throw away iterator result) 00302 00303 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) 00304 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) 00305 00306 As you can see, only one unbalanced operation is in the list, a ref count increment when 00307 the peer->call was set, but no corresponding decrement was made... 00308 00309 Hopefully this helps you narrow your search and find those bugs. 00310 00311 THE ART OF REFERENCE COUNTING 00312 (by Steve Murphy) 00313 SOME TIPS for complicated code, and ref counting: 00314 00315 1. Theoretically, passing a refcounted object pointer into a function 00316 call is an act of copying the reference, and could be refcounted. 00317 But, upon examination, this sort of refcounting will explode the amount 00318 of code you have to enter, and for no tangible benefit, beyond 00319 creating more possible failure points/bugs. It will even 00320 complicate your code and make debugging harder, slow down your program 00321 doing useless increments and decrements of the ref counts. 00322 00323 2. It is better to track places where a ref counted pointer 00324 is copied into a structure or stored. Make sure to decrement the refcount 00325 of any previous pointer that might have been there, if setting 00326 this field might erase a previous pointer. ao2_find and iterate_next 00327 internally increment the ref count when they return a pointer, so 00328 you need to decrement the count before the pointer goes out of scope. 00329 00330 3. Any time you decrement a ref count, it may be possible that the 00331 object will be destroyed (freed) immediately by that call. If you 00332 are destroying a series of fields in a refcounted object, and 00333 any of the unref calls might possibly result in immediate destruction, 00334 you can first increment the count to prevent such behavior, then 00335 after the last test, decrement the pointer to allow the object 00336 to be destroyed, if the refcount would be zero. 00337 00338 Example: 00339 00340 dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done"); 00341 00342 ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink"); 00343 00344 *//* Unlink us from the owner (channel) if we have one *//* 00345 if (dialog->owner) { 00346 if (lockowner) 00347 ast_channel_lock(dialog->owner); 00348 ast_debug(1, "Detaching from channel %s\n", dialog->owner->name); 00349 dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all"); 00350 if (lockowner) 00351 ast_channel_unlock(dialog->owner); 00352 } 00353 if (dialog->registry) { 00354 if (dialog->registry->call == dialog) 00355 dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all"); 00356 dialog->registry = registry_unref(dialog->registry, "delete dialog->registry"); 00357 } 00358 ... 00359 dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time"); 00360 00361 In the above code, the ao2_t_unlink could end up destroying the dialog 00362 object; if this happens, then the subsequent usages of the dialog 00363 pointer could result in a core dump. So, we 'bump' the 00364 count upwards before beginning, and then decrementing the count when 00365 we are finished. This is analogous to 'locking' or 'protecting' operations 00366 for a short while. 00367 00368 4. One of the most insidious problems I've run into when converting 00369 code to do ref counted automatic destruction, is in the destruction 00370 routines. Where a "destroy" routine had previously been called to 00371 get rid of an object in non-refcounted code, the new regime demands 00372 that you tear that "destroy" routine into two pieces, one that will 00373 tear down the links and 'unref' them, and the other to actually free 00374 and reset fields. A destroy routine that does any reference deletion 00375 for its own object, will never be called. Another insidious problem 00376 occurs in mutually referenced structures. As an example, a dialog contains 00377 a pointer to a peer, and a peer contains a pointer to a dialog. Watch 00378 out that the destruction of one doesn't depend on the destruction of the 00379 other, as in this case a dependency loop will result in neither being 00380 destroyed! 00381 00382 Given the above, you should be ready to do a good job! 00383 00384 murf 00385 00386 */ 00387 00388 00389 00390 /*! \brief 00391 * Typedef for an object destructor. This is called just before freeing 00392 * the memory for the object. It is passed a pointer to the user-defined 00393 * data of the object. 00394 */ 00395 typedef void (*ao2_destructor_fn)(void *); 00396 00397 00398 /*! \brief 00399 * Allocate and initialize an object. 00400 * 00401 * \param data_size The sizeof() of the user-defined structure. 00402 * \param destructor_fn The destructor function (can be NULL) 00403 * \param debug_msg 00404 * \return A pointer to user-data. 00405 * 00406 * Allocates a struct astobj2 with sufficient space for the 00407 * user-defined structure. 00408 * \note 00409 * - storage is zeroed; XXX maybe we want a flag to enable/disable this. 00410 * - the refcount of the object just created is 1 00411 * - the returned pointer cannot be free()'d or realloc()'ed; 00412 * rather, we just call ao2_ref(o, -1); 00413 * 00414 * @{ 00415 */ 00416 00417 #if defined(REF_DEBUG) 00418 00419 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) __ao2_alloc_debug((data_size), (destructor_fn), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00420 #define ao2_alloc(data_size, destructor_fn) __ao2_alloc_debug((data_size), (destructor_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00421 00422 #elif defined(__AST_DEBUG_MALLOC) 00423 00424 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) __ao2_alloc_debug((data_size), (destructor_fn), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00425 #define ao2_alloc(data_size, destructor_fn) __ao2_alloc_debug((data_size), (destructor_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00426 00427 #else 00428 00429 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) __ao2_alloc((data_size), (destructor_fn)) 00430 #define ao2_alloc(data_size, destructor_fn) __ao2_alloc((data_size), (destructor_fn)) 00431 00432 #endif 00433 00434 void *__ao2_alloc_debug(const size_t data_size, ao2_destructor_fn destructor_fn, char *tag, 00435 const char *file, int line, const char *funcname, int ref_debug); 00436 void *__ao2_alloc(const size_t data_size, ao2_destructor_fn destructor_fn); 00437 00438 /*! @} */ 00439 00440 /*! \brief 00441 * Reference/unreference an object and return the old refcount. 00442 * 00443 * \param o A pointer to the object 00444 * \param delta Value to add to the reference counter. 00445 * \param tag used for debugging 00446 * \return The value of the reference counter before the operation. 00447 * 00448 * Increase/decrease the reference counter according 00449 * the value of delta. 00450 * 00451 * If the refcount goes to zero, the object is destroyed. 00452 * 00453 * \note The object must not be locked by the caller of this function, as 00454 * it is invalid to try to unlock it after releasing the reference. 00455 * 00456 * \note if we know the pointer to an object, it is because we 00457 * have a reference count to it, so the only case when the object 00458 * can go away is when we release our reference, and it is 00459 * the last one in existence. 00460 * 00461 * @{ 00462 */ 00463 00464 #ifdef REF_DEBUG 00465 00466 #define ao2_t_ref(o,delta,tag) __ao2_ref_debug((o), (delta), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00467 #define ao2_ref(o,delta) __ao2_ref_debug((o), (delta), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 00468 00469 #else 00470 00471 #define ao2_t_ref(o,delta,tag) __ao2_ref((o), (delta)) 00472 #define ao2_ref(o,delta) __ao2_ref((o), (delta)) 00473 00474 #endif 00475 00476 int __ao2_ref_debug(void *o, int delta, const char *tag, const char *file, int line, const char *funcname); 00477 int __ao2_ref(void *o, int delta); 00478 00479 /*! @} */ 00480 00481 /*! \brief 00482 * Lock an object. 00483 * 00484 * \param a A pointer to the object we want to lock. 00485 * \return 0 on success, other values on error. 00486 */ 00487 int __ao2_lock(void *a, const char *file, const char *func, int line, const char *var); 00488 #define ao2_lock(a) __ao2_lock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00489 00490 /*! \brief 00491 * Unlock an object. 00492 * 00493 * \param a A pointer to the object we want unlock. 00494 * \return 0 on success, other values on error. 00495 */ 00496 int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var); 00497 #define ao2_unlock(a) __ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00498 00499 /*! \brief 00500 * Try locking-- (don't block if fail) 00501 * 00502 * \param a A pointer to the object we want to lock. 00503 * \return 0 on success, other values on error. 00504 */ 00505 int __ao2_trylock(void *a, const char *file, const char *func, int line, const char *var); 00506 #define ao2_trylock(a) __ao2_trylock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a) 00507 00508 /*! 00509 * \brief Return the lock address of an object 00510 * 00511 * \param[in] obj A pointer to the object we want. 00512 * \return the address of the lock, else NULL. 00513 * 00514 * This function comes in handy mainly for debugging locking 00515 * situations, where the locking trace code reports the 00516 * lock address, this allows you to correlate against 00517 * object address, to match objects to reported locks. 00518 * 00519 * \since 1.6.1 00520 */ 00521 void *ao2_object_get_lockaddr(void *obj); 00522 00523 /*! 00524 \page AstObj2_Containers AstObj2 Containers 00525 00526 Containers are data structures meant to store several objects, 00527 and perform various operations on them. 00528 Internally, objects are stored in lists, hash tables or other 00529 data structures depending on the needs. 00530 00531 \note NOTA BENE: at the moment the only container we support is the 00532 hash table and its degenerate form, the list. 00533 00534 Operations on container include: 00535 00536 - c = \b ao2_container_alloc(size, hash_fn, cmp_fn) 00537 allocate a container with desired size and default compare 00538 and hash function 00539 -The compare function returns an int, which 00540 can be 0 for not found, CMP_STOP to stop end a traversal, 00541 or CMP_MATCH if they are equal 00542 -The hash function returns an int. The hash function 00543 takes two argument, the object pointer and a flags field, 00544 00545 - \b ao2_find(c, arg, flags) 00546 returns zero or more element matching a given criteria 00547 (specified as arg). 'c' is the container pointer. Flags 00548 can be: 00549 OBJ_UNLINK - to remove the object, once found, from the container. 00550 OBJ_NODATA - don't return the object if found (no ref count change) 00551 OBJ_MULTIPLE - don't stop at first match 00552 OBJ_POINTER - if set, 'arg' is an object pointer, and a hashtable 00553 search will be done. If not, a traversal is done. 00554 00555 - \b ao2_callback(c, flags, fn, arg) 00556 apply fn(obj, arg) to all objects in the container. 00557 Similar to find. fn() can tell when to stop, and 00558 do anything with the object including unlinking it. 00559 - c is the container; 00560 - flags can be 00561 OBJ_UNLINK - to remove the object, once found, from the container. 00562 OBJ_NODATA - don't return the object if found (no ref count change) 00563 OBJ_MULTIPLE - don't stop at first match 00564 OBJ_POINTER - if set, 'arg' is an object pointer, and a hashtable 00565 search will be done. If not, a traversal is done through 00566 all the hashtable 'buckets'.. 00567 - fn is a func that returns int, and takes 3 args: 00568 (void *obj, void *arg, int flags); 00569 obj is an object 00570 arg is the same as arg passed into ao2_callback 00571 flags is the same as flags passed into ao2_callback 00572 fn returns: 00573 0: no match, keep going 00574 CMP_STOP: stop search, no match 00575 CMP_MATCH: This object is matched. 00576 00577 Note that the entire operation is run with the container 00578 locked, so noone else can change its content while we work on it. 00579 However, we pay this with the fact that doing 00580 anything blocking in the callback keeps the container 00581 blocked. 00582 The mechanism is very flexible because the callback function fn() 00583 can do basically anything e.g. counting, deleting records, etc. 00584 possibly using arg to store the results. 00585 00586 - \b iterate on a container 00587 this is done with the following sequence 00588 00589 \code 00590 00591 struct ao2_container *c = ... // our container 00592 struct ao2_iterator i; 00593 void *o; 00594 00595 i = ao2_iterator_init(c, flags); 00596 00597 while ((o = ao2_iterator_next(&i))) { 00598 ... do something on o ... 00599 ao2_ref(o, -1); 00600 } 00601 00602 ao2_iterator_destroy(&i); 00603 \endcode 00604 00605 The difference with the callback is that the control 00606 on how to iterate is left to us. 00607 00608 - \b ao2_ref(c, -1) 00609 dropping a reference to a container destroys it, very simple! 00610 00611 Containers are ao2 objects themselves, and this is why their 00612 implementation is simple too. 00613 00614 Before declaring containers, we need to declare the types of the 00615 arguments passed to the constructor - in turn, this requires 00616 to define callback and hash functions and their arguments. 00617 00618 - \ref AstObj2 00619 - \ref astobj2.h 00620 */ 00621 00622 /*! \brief 00623 * Type of a generic callback function 00624 * \param obj pointer to the (user-defined part) of an object. 00625 * \param arg callback argument from ao2_callback() 00626 * \param flags flags from ao2_callback() 00627 * 00628 * The return values are a combination of enum _cb_results. 00629 * Callback functions are used to search or manipulate objects in a container. 00630 */ 00631 typedef int (ao2_callback_fn)(void *obj, void *arg, int flags); 00632 00633 /*! \brief 00634 * Type of a generic callback function 00635 * \param obj pointer to the (user-defined part) of an object. 00636 * \param arg callback argument from ao2_callback() 00637 * \param data arbitrary data from ao2_callback() 00638 * \param flags flags from ao2_callback() 00639 * 00640 * The return values are a combination of enum _cb_results. 00641 * Callback functions are used to search or manipulate objects in a container. 00642 */ 00643 typedef int (ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags); 00644 00645 /*! \brief a very common callback is one that matches by address. */ 00646 ao2_callback_fn ao2_match_by_addr; 00647 00648 /*! \brief 00649 * A callback function will return a combination of CMP_MATCH and CMP_STOP. 00650 * The latter will terminate the search in a container. 00651 */ 00652 enum _cb_results { 00653 CMP_MATCH = 0x1, /*!< the object matches the request */ 00654 CMP_STOP = 0x2, /*!< stop the search now */ 00655 }; 00656 00657 /*! \brief 00658 * Flags passed to ao2_callback() and ao2_hash_fn() to modify its behaviour. 00659 */ 00660 enum search_flags { 00661 /*! Unlink the object for which the callback function 00662 * returned CMP_MATCH. 00663 */ 00664 OBJ_UNLINK = (1 << 0), 00665 /*! On match, don't return the object hence do not increase 00666 * its refcount. 00667 */ 00668 OBJ_NODATA = (1 << 1), 00669 /*! Don't stop at the first match in ao2_callback() unless the result of 00670 * of the callback function == (CMP_STOP | CMP_MATCH). 00671 */ 00672 OBJ_MULTIPLE = (1 << 2), 00673 /*! obj is an object of the same type as the one being searched for, 00674 * so use the object's hash function for optimized searching. 00675 * The search function is unaffected (i.e. use the one passed as 00676 * argument, or match_by_addr if none specified). 00677 */ 00678 OBJ_POINTER = (1 << 3), 00679 /*! 00680 * \brief Continue if a match is not found in the hashed out bucket 00681 * 00682 * This flag is to be used in combination with OBJ_POINTER. This tells 00683 * the ao2_callback() core to keep searching through the rest of the 00684 * buckets if a match is not found in the starting bucket defined by 00685 * the hash value on the argument. 00686 */ 00687 OBJ_CONTINUE = (1 << 4), 00688 }; 00689 00690 /*! 00691 * Type of a generic function to generate a hash value from an object. 00692 * flags is ignored at the moment. Eventually, it will include the 00693 * value of OBJ_POINTER passed to ao2_callback(). 00694 */ 00695 typedef int (ao2_hash_fn)(const void *obj, const int flags); 00696 00697 /*! \name Object Containers 00698 * Here start declarations of containers. 00699 */ 00700 /*@{ */ 00701 struct ao2_container; 00702 00703 /*! \brief 00704 * Allocate and initialize a container 00705 * with the desired number of buckets. 00706 * 00707 * We allocate space for a struct astobj_container, struct container 00708 * and the buckets[] array. 00709 * 00710 * \param arg1 Number of buckets for hash 00711 * \param arg2 Pointer to a function computing a hash value. 00712 * \param arg3 Pointer to a function comparating key-value 00713 * with a string. (can be NULL) 00714 * \param arg4 00715 * 00716 * \return A pointer to a struct container. 00717 * 00718 * \note Destructor is set implicitly. 00719 */ 00720 00721 #if defined(REF_DEBUG) 00722 00723 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc_debug((arg1), (arg2), (arg3), (arg4), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00724 #define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc_debug((arg1), (arg2), (arg3), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1) 00725 00726 #elif defined(__AST_DEBUG_MALLOC) 00727 00728 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc_debug((arg1), (arg2), (arg3), (arg4), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00729 #define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc_debug((arg1), (arg2), (arg3), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0) 00730 00731 #else 00732 00733 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc((arg1), (arg2), (arg3)) 00734 #define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc((arg1), (arg2), (arg3)) 00735 00736 #endif 00737 00738 struct ao2_container *__ao2_container_alloc(const unsigned int n_buckets, 00739 ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn); 00740 struct ao2_container *__ao2_container_alloc_debug(const unsigned int n_buckets, 00741 ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn, 00742 char *tag, char *file, int line, const char *funcname, 00743 int ref_debug); 00744 00745 /*! \brief 00746 * Returns the number of elements in a container. 00747 */ 00748 int ao2_container_count(struct ao2_container *c); 00749 00750 /*@} */ 00751 00752 /*! \name Object Management 00753 * Here we have functions to manage objects. 00754 * 00755 * We can use the functions below on any kind of 00756 * object defined by the user. 00757 */ 00758 /*@{ */ 00759 00760 /*! 00761 * \brief Add an object to a container. 00762 * 00763 * \param arg1 the container to operate on. 00764 * \param arg2 the object to be added. 00765 * \param arg3 used for debuging. 00766 * 00767 * \retval NULL on errors. 00768 * \retval newobj on success. 00769 * 00770 * This function inserts an object in a container according its key. 00771 * 00772 * \note Remember to set the key before calling this function. 00773 * 00774 * \note This function automatically increases the reference count to account 00775 * for the reference that the container now holds to the object. 00776 */ 00777 #ifdef REF_DEBUG 00778 00779 #define ao2_t_link(arg1, arg2, arg3) __ao2_link_debug((arg1), (arg2), (arg3), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00780 #define ao2_link(arg1, arg2) __ao2_link_debug((arg1), (arg2), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 00781 00782 #else 00783 00784 #define ao2_t_link(arg1, arg2, arg3) __ao2_link((arg1), (arg2)) 00785 #define ao2_link(arg1, arg2) __ao2_link((arg1), (arg2)) 00786 00787 #endif 00788 00789 void *__ao2_link_debug(struct ao2_container *c, void *new_obj, char *tag, char *file, int line, const char *funcname); 00790 void *__ao2_link(struct ao2_container *c, void *newobj); 00791 00792 /*! 00793 * \brief Remove an object from a container 00794 * 00795 * \param arg1 the container 00796 * \param arg2 the object to unlink 00797 * \param arg3 tag for debugging 00798 * 00799 * \retval NULL, always 00800 * 00801 * \note The object requested to be unlinked must be valid. However, if it turns 00802 * out that it is not in the container, this function is still safe to 00803 * be called. 00804 * 00805 * \note If the object gets unlinked from the container, the container's 00806 * reference to the object will be automatically released. (The 00807 * refcount will be decremented). 00808 */ 00809 #ifdef REF_DEBUG 00810 00811 #define ao2_t_unlink(arg1, arg2, arg3) __ao2_unlink_debug((arg1), (arg2), (arg3), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00812 #define ao2_unlink(arg1, arg2) __ao2_unlink_debug((arg1), (arg2), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 00813 00814 #else 00815 00816 #define ao2_t_unlink(arg1, arg2, arg3) __ao2_unlink((arg1), (arg2)) 00817 #define ao2_unlink(arg1, arg2) __ao2_unlink((arg1), (arg2)) 00818 00819 #endif 00820 00821 void *__ao2_unlink_debug(struct ao2_container *c, void *obj, char *tag, char *file, int line, const char *funcname); 00822 void *__ao2_unlink(struct ao2_container *c, void *obj); 00823 00824 00825 /*@} */ 00826 00827 /*! \brief 00828 * ao2_callback() is a generic function that applies cb_fn() to all objects 00829 * in a container, as described below. 00830 * 00831 * \param c A pointer to the container to operate on. 00832 * \param flags A set of flags specifying the operation to perform, 00833 partially used by the container code, but also passed to 00834 the callback. 00835 - If OBJ_NODATA is set, ao2_callback will return NULL. No refcounts 00836 of any of the traversed objects will be incremented. 00837 On the converse, if it is NOT set (the default), The ref count 00838 of each object for which CMP_MATCH was set will be incremented, 00839 and you will have no way of knowing which those are, until 00840 the multiple-object-return functionality is implemented. 00841 - If OBJ_POINTER is set, the traversed items will be restricted 00842 to the objects in the bucket that the object key hashes to. 00843 * \param cb_fn A function pointer, that will be called on all 00844 objects, to see if they match. This function returns CMP_MATCH 00845 if the object is matches the criteria; CMP_STOP if the traversal 00846 should immediately stop, or both (via bitwise ORing), if you find a 00847 match and want to end the traversal, and 0 if the object is not a match, 00848 but the traversal should continue. This is the function that is applied 00849 to each object traversed. Its arguments are: 00850 (void *obj, void *arg, int flags), where: 00851 obj is an object 00852 arg is the same as arg passed into ao2_callback 00853 flags is the same as flags passed into ao2_callback (flags are 00854 also used by ao2_callback). 00855 * \param arg passed to the callback. 00856 * \param tag used for debuging. 00857 * \return when OBJ_MULTIPLE is not included in the flags parameter, 00858 * the return value will be either the object found or NULL if no 00859 * no matching object was found. if OBJ_MULTIPLE is included, 00860 * the return value will be a pointer to an ao2_iterator object, 00861 * which must be destroyed with ao2_iterator_destroy() when the 00862 * caller no longer needs it. 00863 * 00864 * If the function returns any objects, their refcount is incremented, 00865 * and the caller is in charge of decrementing them once done. 00866 * 00867 * Typically, ao2_callback() is used for two purposes: 00868 * - to perform some action (including removal from the container) on one 00869 * or more objects; in this case, cb_fn() can modify the object itself, 00870 * and to perform deletion should set CMP_MATCH on the matching objects, 00871 * and have OBJ_UNLINK set in flags. 00872 * - to look for a specific object in a container; in this case, cb_fn() 00873 * should not modify the object, but just return a combination of 00874 * CMP_MATCH and CMP_STOP on the desired object. 00875 * Other usages are also possible, of course. 00876 00877 * This function searches through a container and performs operations 00878 * on objects according on flags passed. 00879 * XXX describe better 00880 * The comparison is done calling the compare function set implicitly. 00881 * The p pointer can be a pointer to an object or to a key, 00882 * we can say this looking at flags value. 00883 * If p points to an object we will search for the object pointed 00884 * by this value, otherwise we serch for a key value. 00885 * If the key is not unique we only find the first matching valued. 00886 * 00887 * The use of flags argument is the follow: 00888 * 00889 * OBJ_UNLINK unlinks the object found 00890 * OBJ_NODATA on match, do return an object 00891 * Callbacks use OBJ_NODATA as a default 00892 * functions such as find() do 00893 * OBJ_MULTIPLE return multiple matches 00894 * Default is no. 00895 * OBJ_POINTER the pointer is an object pointer 00896 * 00897 * \note When the returned object is no longer in use, ao2_ref() should 00898 * be used to free the additional reference possibly created by this function. 00899 * 00900 * @{ 00901 */ 00902 #ifdef REF_DEBUG 00903 00904 #define ao2_t_callback(c,flags,cb_fn,arg,tag) __ao2_callback_debug((c), (flags), (cb_fn), (arg), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00905 #define ao2_callback(c,flags,cb_fn,arg) __ao2_callback_debug((c), (flags), (cb_fn), (arg), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 00906 00907 #else 00908 00909 #define ao2_t_callback(c,flags,cb_fn,arg,tag) __ao2_callback((c), (flags), (cb_fn), (arg)) 00910 #define ao2_callback(c,flags,cb_fn,arg) __ao2_callback((c), (flags), (cb_fn), (arg)) 00911 00912 #endif 00913 00914 void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, 00915 void *arg, char *tag, char *file, int line, const char *funcname); 00916 void *__ao2_callback(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg); 00917 00918 /*! @} */ 00919 00920 /*! \brief 00921 * ao2_callback_data() is a generic function that applies cb_fn() to all objects 00922 * in a container. It is functionally identical to ao2_callback() except that 00923 * instead of taking an ao2_callback_fn *, it takes an ao2_callback_data_fn *, and 00924 * allows the caller to pass in arbitrary data. 00925 * 00926 * This call would be used instead of ao2_callback() when the caller needs to pass 00927 * OBJ_POINTER as part of the flags argument (which in turn requires passing in a 00928 * prototype ao2 object for 'arg') and also needs access to other non-global data 00929 * to complete it's comparison or task. 00930 * 00931 * See the documentation for ao2_callback() for argument descriptions. 00932 * 00933 * \see ao2_callback() 00934 */ 00935 #ifdef REF_DEBUG 00936 00937 #define ao2_t_callback_data(arg1,arg2,arg3,arg4,arg5,arg6) __ao2_callback_data_debug((arg1), (arg2), (arg3), (arg4), (arg5), (arg6), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00938 #define ao2_callback_data(arg1,arg2,arg3,arg4,arg5) __ao2_callback_data_debug((arg1), (arg2), (arg3), (arg4), (arg5), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 00939 00940 #else 00941 00942 #define ao2_t_callback_data(arg1,arg2,arg3,arg4,arg5,arg6) __ao2_callback_data((arg1), (arg2), (arg3), (arg4), (arg5)) 00943 #define ao2_callback_data(arg1,arg2,arg3,arg4,arg5) __ao2_callback_data((arg1), (arg2), (arg3), (arg4), (arg5)) 00944 00945 #endif 00946 00947 void *__ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags, 00948 ao2_callback_data_fn *cb_fn, void *arg, void *data, char *tag, 00949 char *file, int line, const char *funcname); 00950 void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags, 00951 ao2_callback_data_fn *cb_fn, void *arg, void *data); 00952 00953 /*! ao2_find() is a short hand for ao2_callback(c, flags, c->cmp_fn, arg) 00954 * XXX possibly change order of arguments ? 00955 */ 00956 #ifdef REF_DEBUG 00957 00958 #define ao2_t_find(arg1,arg2,arg3,arg4) __ao2_find_debug((arg1), (arg2), (arg3), (arg4), __FILE__, __LINE__, __PRETTY_FUNCTION__) 00959 #define ao2_find(arg1,arg2,arg3) __ao2_find_debug((arg1), (arg2), (arg3), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 00960 00961 #else 00962 00963 #define ao2_t_find(arg1,arg2,arg3,arg4) __ao2_find((arg1), (arg2), (arg3)) 00964 #define ao2_find(arg1,arg2,arg3) __ao2_find((arg1), (arg2), (arg3)) 00965 00966 #endif 00967 00968 void *__ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag, 00969 char *file, int line, const char *funcname); 00970 void *__ao2_find(struct ao2_container *c, void *arg, enum search_flags flags); 00971 00972 /*! \brief 00973 * 00974 * 00975 * When we need to walk through a container, we use an 00976 * ao2_iterator to keep track of the current position. 00977 * 00978 * Because the navigation is typically done without holding the 00979 * lock on the container across the loop, objects can be inserted or deleted 00980 * or moved while we work. As a consequence, there is no guarantee that 00981 * we manage to touch all the elements in the container, and it is possible 00982 * that we touch the same object multiple times. 00983 * 00984 * However, within the current hash table container, the following is true: 00985 * - It is not possible to miss an object in the container while iterating 00986 * unless it gets added after the iteration begins and is added to a bucket 00987 * that is before the one the current object is in. In this case, even if 00988 * you locked the container around the entire iteration loop, you still would 00989 * not see this object, because it would still be waiting on the container 00990 * lock so that it can be added. 00991 * - It would be extremely rare to see an object twice. The only way this can 00992 * happen is if an object got unlinked from the container and added again 00993 * during the same iteration. Furthermore, when the object gets added back, 00994 * it has to be in the current or later bucket for it to be seen again. 00995 * 00996 * An iterator must be first initialized with ao2_iterator_init(), 00997 * then we can use o = ao2_iterator_next() to move from one 00998 * element to the next. Remember that the object returned by 00999 * ao2_iterator_next() has its refcount incremented, 01000 * and the reference must be explicitly released when done with it. 01001 * 01002 * In addition, ao2_iterator_init() will hold a reference to the container 01003 * being iterated, which will be freed when ao2_iterator_destroy() is called 01004 * to free up the resources used by the iterator (if any). 01005 * 01006 * Example: 01007 * 01008 * \code 01009 * 01010 * struct ao2_container *c = ... // the container we want to iterate on 01011 * struct ao2_iterator i; 01012 * struct my_obj *o; 01013 * 01014 * i = ao2_iterator_init(c, flags); 01015 * 01016 * while ((o = ao2_iterator_next(&i))) { 01017 * ... do something on o ... 01018 * ao2_ref(o, -1); 01019 * } 01020 * 01021 * ao2_iterator_destroy(&i); 01022 * 01023 * \endcode 01024 * 01025 */ 01026 01027 /*! \brief 01028 * The astobj2 iterator 01029 * 01030 * \note You are not supposed to know the internals of an iterator! 01031 * We would like the iterator to be opaque, unfortunately 01032 * its size needs to be known if we want to store it around 01033 * without too much trouble. 01034 * Anyways... 01035 * The iterator has a pointer to the container, and a flags 01036 * field specifying various things e.g. whether the container 01037 * should be locked or not while navigating on it. 01038 * The iterator "points" to the current object, which is identified 01039 * by three values: 01040 * 01041 * - a bucket number; 01042 * - the object_id, which is also the container version number 01043 * when the object was inserted. This identifies the object 01044 * uniquely, however reaching the desired object requires 01045 * scanning a list. 01046 * - a pointer, and a container version when we saved the pointer. 01047 * If the container has not changed its version number, then we 01048 * can safely follow the pointer to reach the object in constant time. 01049 * 01050 * Details are in the implementation of ao2_iterator_next() 01051 * A freshly-initialized iterator has bucket=0, version=0. 01052 */ 01053 struct ao2_iterator { 01054 /*! the container */ 01055 struct ao2_container *c; 01056 /*! operation flags */ 01057 int flags; 01058 /*! current bucket */ 01059 int bucket; 01060 /*! container version */ 01061 unsigned int c_version; 01062 /*! pointer to the current object */ 01063 void *obj; 01064 /*! container version when the object was created */ 01065 unsigned int version; 01066 }; 01067 01068 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior 01069 * of the iterator. 01070 */ 01071 enum ao2_iterator_flags { 01072 /*! Prevents ao2_iterator_next() from locking the container 01073 * while retrieving the next object from it. 01074 */ 01075 AO2_ITERATOR_DONTLOCK = (1 << 0), 01076 /*! Indicates that the iterator was dynamically allocated by 01077 * astobj2 API and should be freed by ao2_iterator_destroy(). 01078 */ 01079 AO2_ITERATOR_MALLOCD = (1 << 1), 01080 /*! Indicates that before the iterator returns an object from 01081 * the container being iterated, the object should be unlinked 01082 * from the container. 01083 */ 01084 AO2_ITERATOR_UNLINK = (1 << 2), 01085 }; 01086 01087 /*! 01088 * \brief Create an iterator for a container 01089 * 01090 * \param c the container 01091 * \param flags one or more flags from ao2_iterator_flags 01092 * 01093 * \retval the constructed iterator 01094 * 01095 * \note This function does \b not take a pointer to an iterator; 01096 * rather, it returns an iterator structure that should be 01097 * assigned to (overwriting) an existing iterator structure 01098 * allocated on the stack or on the heap. 01099 * 01100 * This function will take a reference on the container being iterated. 01101 * 01102 */ 01103 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags); 01104 01105 /*! 01106 * \brief Destroy a container iterator 01107 * 01108 * \param i the iterator to destroy 01109 * 01110 * \retval none 01111 * 01112 * This function will release the container reference held by the iterator 01113 * and any other resources it may be holding. 01114 * 01115 */ 01116 void ao2_iterator_destroy(struct ao2_iterator *i); 01117 01118 #ifdef REF_DEBUG 01119 01120 #define ao2_t_iterator_next(arg1, arg2) __ao2_iterator_next_debug((arg1), (arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__) 01121 #define ao2_iterator_next(arg1) __ao2_iterator_next_debug((arg1), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) 01122 01123 #else 01124 01125 #define ao2_t_iterator_next(arg1, arg2) __ao2_iterator_next((arg1)) 01126 #define ao2_iterator_next(arg1) __ao2_iterator_next((arg1)) 01127 01128 #endif 01129 01130 void *__ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname); 01131 void *__ao2_iterator_next(struct ao2_iterator *a); 01132 01133 /* extra functions */ 01134 void ao2_bt(void); /* backtrace */ 01135 01136 #endif /* _ASTERISK_ASTOBJ2_H */