astobj2.h

Go to the documentation of this file.
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 /*!
00398  * \brief Typedef for an object destructor.
00399  *
00400  * \param vdoomed Object to destroy.
00401  *
00402  * \details
00403  * This is called just before freeing the memory for the object.
00404  * It is passed a pointer to the user-defined data of the
00405  * object.
00406  *
00407  * \return Nothing
00408  */
00409 typedef void (*ao2_destructor_fn)(void *vdoomed);
00410 
00411 /*! \brief Options available when allocating an ao2 object. */
00412 enum ao2_alloc_opts {
00413    /*! The ao2 object has a recursive mutex lock associated with it. */
00414    AO2_ALLOC_OPT_LOCK_MUTEX = (0 << 0),
00415    /*! The ao2 object has a non-recursive read/write lock associated with it. */
00416    AO2_ALLOC_OPT_LOCK_RWLOCK = (1 << 0),
00417    /*! The ao2 object has no lock associated with it. */
00418    AO2_ALLOC_OPT_LOCK_NOLOCK = (2 << 0),
00419    /*! The ao2 object locking option field mask. */
00420    AO2_ALLOC_OPT_LOCK_MASK = (3 << 0),
00421 };
00422 
00423 /*!
00424  * \brief Allocate and initialize an object.
00425  *
00426  * \param data_size The sizeof() of the user-defined structure.
00427  * \param destructor_fn The destructor function (can be NULL)
00428  * \param options The ao2 object options (See enum ao2_alloc_opts)
00429  * \param debug_msg An ao2 object debug tracing message.
00430  * \return A pointer to user-data.
00431  *
00432  * \details
00433  * Allocates a struct astobj2 with sufficient space for the
00434  * user-defined structure.
00435  * \note
00436  * - storage is zeroed; XXX maybe we want a flag to enable/disable this.
00437  * - the refcount of the object just created is 1
00438  * - the returned pointer cannot be free()'d or realloc()'ed;
00439  *   rather, we just call ao2_ref(o, -1);
00440  *
00441  * @{
00442  */
00443 
00444 #if defined(REF_DEBUG)
00445 
00446 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
00447    __ao2_alloc_debug((data_size), (destructor_fn), (options), (debug_msg),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
00448 #define ao2_alloc_options(data_size, destructor_fn, options) \
00449    __ao2_alloc_debug((data_size), (destructor_fn), (options), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
00450 
00451 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
00452    __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
00453 #define ao2_alloc(data_size, destructor_fn) \
00454    __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
00455 
00456 #elif defined(__AST_DEBUG_MALLOC)
00457 
00458 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
00459    __ao2_alloc_debug((data_size), (destructor_fn), (options), (debug_msg),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
00460 #define ao2_alloc_options(data_size, destructor_fn, options) \
00461    __ao2_alloc_debug((data_size), (destructor_fn), (options), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
00462 
00463 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
00464    __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
00465 #define ao2_alloc(data_size, destructor_fn) \
00466    __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
00467 
00468 #else
00469 
00470 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
00471    __ao2_alloc((data_size), (destructor_fn), (options))
00472 #define ao2_alloc_options(data_size, destructor_fn, options) \
00473    __ao2_alloc((data_size), (destructor_fn), (options))
00474 
00475 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
00476    __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX)
00477 #define ao2_alloc(data_size, destructor_fn) \
00478    __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX)
00479 
00480 #endif
00481 
00482 void *__ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag,
00483    const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
00484 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options) attribute_warn_unused_result;
00485 
00486 /*! @} */
00487 
00488 /*! \brief
00489  * Reference/unreference an object and return the old refcount.
00490  *
00491  * \param o A pointer to the object
00492  * \param delta Value to add to the reference counter.
00493  * \param tag used for debugging
00494  * \return The value of the reference counter before the operation.
00495  *
00496  * Increase/decrease the reference counter according
00497  * the value of delta.
00498  *
00499  * If the refcount goes to zero, the object is destroyed.
00500  *
00501  * \note The object must not be locked by the caller of this function, as
00502  *       it is invalid to try to unlock it after releasing the reference.
00503  *
00504  * \note if we know the pointer to an object, it is because we
00505  * have a reference count to it, so the only case when the object
00506  * can go away is when we release our reference, and it is
00507  * the last one in existence.
00508  *
00509  * @{
00510  */
00511 
00512 #ifdef REF_DEBUG
00513 
00514 #define ao2_t_ref(o,delta,tag) __ao2_ref_debug((o), (delta), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00515 #define ao2_ref(o,delta)       __ao2_ref_debug((o), (delta), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00516 
00517 #else
00518 
00519 #define ao2_t_ref(o,delta,tag) __ao2_ref((o), (delta))
00520 #define ao2_ref(o,delta)       __ao2_ref((o), (delta))
00521 
00522 #endif
00523 
00524 /*!
00525  * \brief Retrieve the ao2 options used to create the object.
00526  * \param obj pointer to the (user-defined part) of an object.
00527  * \return options from enum ao2_alloc_opts.
00528  */
00529 unsigned int ao2_options_get(void *obj);
00530 
00531 /*!
00532  * \since 12
00533  * \brief Bump refcount on an AO2 object by one, returning the object.
00534  *
00535  * This is useful for inlining a ref bump, and you don't care about the ref
00536  * count. Also \c NULL safe, for even more convenience.
00537  *
00538  * \param obj AO2 object to bump the refcount on.
00539  * \retval The given \a obj pointer.
00540  */
00541 #define ao2_t_bump(obj, tag)                 \
00542    ({                   \
00543       typeof(obj) __obj_ ## __LINE__ = (obj);      \
00544       if (__obj_ ## __LINE__) {        \
00545          ao2_t_ref(__obj_ ## __LINE__, +1, (tag)); \
00546       }                 \
00547       __obj_ ## __LINE__;           \
00548    })
00549 #define ao2_bump(obj) \
00550    ao2_t_bump((obj), "")
00551 
00552 int __ao2_ref_debug(void *o, int delta, const char *tag, const char *file, int line, const char *func);
00553 int __ao2_ref(void *o, int delta);
00554 
00555 /*!
00556  * \since 12.4.0
00557  * \brief Replace one object reference with another cleaning up the original.
00558  *
00559  * \param dst Pointer to the object that will be cleaned up.
00560  * \param src Pointer to the object replacing it.
00561  */
00562 #define ao2_t_replace(dst, src, tag) \
00563    {\
00564       typeof(dst) *__dst_ ## __LINE__ = &dst; \
00565       typeof(src) __src_ ## __LINE__ = src; \
00566       if (__src_ ## __LINE__ != *__dst_ ## __LINE__) { \
00567          if (__src_ ## __LINE__) {\
00568             ao2_t_ref(__src_ ## __LINE__, +1, (tag)); \
00569          } \
00570          if (*__dst_ ## __LINE__) {\
00571             ao2_t_ref(*__dst_ ## __LINE__, -1, (tag)); \
00572          } \
00573          *__dst_ ## __LINE__ = __src_ ## __LINE__; \
00574       } \
00575    }
00576 #define ao2_replace(dst, src) \
00577    ao2_t_replace((dst), (src), "")
00578 
00579 /*! @} */
00580 
00581 /*! \brief Which lock to request. */
00582 enum ao2_lock_req {
00583    /*! Request the mutex lock be acquired. */
00584    AO2_LOCK_REQ_MUTEX,
00585    /*! Request the read lock be acquired. */
00586    AO2_LOCK_REQ_RDLOCK,
00587    /*! Request the write lock be acquired. */
00588    AO2_LOCK_REQ_WRLOCK,
00589 };
00590 
00591 /*! \brief
00592  * Lock an object.
00593  *
00594  * \param a A pointer to the object we want to lock.
00595  * \param lock_how, file, func, line, var
00596  * \return 0 on success, other values on error.
00597  */
00598 int __ao2_lock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var);
00599 #define ao2_lock(a) __ao2_lock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00600 #define ao2_rdlock(a) __ao2_lock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00601 #define ao2_wrlock(a) __ao2_lock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00602 
00603 /*! \brief
00604  * Unlock an object.
00605  *
00606  * \param a A pointer to the object we want unlock.
00607  * \param file, func, line, var
00608  * \return 0 on success, other values on error.
00609  */
00610 int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var);
00611 #define ao2_unlock(a) __ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00612 
00613 /*! \brief
00614  * Try locking-- (don't block if fail)
00615  *
00616  * \param a A pointer to the object we want to lock.
00617  * \param lock_how, file, func, line, var
00618  * \return 0 on success, other values on error.
00619  */
00620 int __ao2_trylock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var);
00621 #define ao2_trylock(a) __ao2_trylock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00622 #define ao2_tryrdlock(a) __ao2_trylock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00623 #define ao2_trywrlock(a) __ao2_trylock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00624 
00625 /*!
00626  * \brief Return the mutex lock address of an object
00627  *
00628  * \param[in] obj A pointer to the object we want.
00629  * \return the address of the mutex lock, else NULL.
00630  *
00631  * This function comes in handy mainly for debugging locking
00632  * situations, where the locking trace code reports the
00633  * lock address, this allows you to correlate against
00634  * object address, to match objects to reported locks.
00635  *
00636  * \since 1.6.1
00637  */
00638 void *ao2_object_get_lockaddr(void *obj);
00639 
00640 
00641 /*! Global ao2 object holder structure. */
00642 struct ao2_global_obj {
00643    /*! Access lock to the held ao2 object. */
00644    ast_rwlock_t lock;
00645    /*! Global ao2 object. */
00646    void *obj;
00647 };
00648 
00649 /*!
00650  * \brief Define a global object holder to be used to hold an ao2 object, statically initialized.
00651  * \since 11.0
00652  *
00653  * \param name This will be the name of the object holder.
00654  *
00655  * \details
00656  * This macro creates a global object holder that can be used to
00657  * hold an ao2 object accessible using the API.  The structure is
00658  * allocated and initialized to be empty.
00659  *
00660  * Example usage:
00661  * \code
00662  * static AO2_GLOBAL_OBJ_STATIC(global_cfg);
00663  * \endcode
00664  *
00665  * This defines global_cfg, intended to hold an ao2 object
00666  * accessible using an API.
00667  */
00668 #ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER
00669 #define AO2_GLOBAL_OBJ_STATIC(name)                            \
00670    struct ao2_global_obj name;                                 \
00671    static void  __attribute__((constructor)) __init_##name(void)     \
00672    {                                                  \
00673       ast_rwlock_init(&name.lock);                          \
00674       name.obj = NULL;                                   \
00675    }                                                  \
00676    static void  __attribute__((destructor)) __fini_##name(void)      \
00677    {                                                  \
00678       if (name.obj) {                                       \
00679          ao2_ref(name.obj, -1);                             \
00680          name.obj = NULL;                                \
00681       }                                               \
00682       ast_rwlock_destroy(&name.lock);                          \
00683    }                                                  \
00684    struct __dummy_##name
00685 #else
00686 #define AO2_GLOBAL_OBJ_STATIC(name)                            \
00687    struct ao2_global_obj name = {                              \
00688       .lock = AST_RWLOCK_INIT_VALUE,                           \
00689    }
00690 #endif
00691 
00692 /*!
00693  * \brief Release the ao2 object held in the global holder.
00694  * \since 11.0
00695  *
00696  * \param holder Global ao2 object holder.
00697  * \param tag used for debugging
00698  *
00699  * \return Nothing
00700  */
00701 #ifdef REF_DEBUG
00702 #define ao2_t_global_obj_release(holder, tag)   \
00703    __ao2_global_obj_release(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00704 #define ao2_global_obj_release(holder) \
00705    __ao2_global_obj_release(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00706 
00707 #else
00708 
00709 #define ao2_t_global_obj_release(holder, tag)   \
00710    __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00711 #define ao2_global_obj_release(holder) \
00712    __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00713 #endif
00714 
00715 void __ao2_global_obj_release(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name);
00716 
00717 /*!
00718  * \brief Replace an ao2 object in the global holder.
00719  * \since 11.0
00720  *
00721  * \param holder Global ao2 object holder.
00722  * \param obj Object to put into the holder.  Can be NULL.
00723  * \param tag used for debugging
00724  *
00725  * \note This function automatically increases the reference
00726  * count to account for the reference that the global holder now
00727  * holds to the object.
00728  *
00729  * \retval Reference to previous global ao2 object stored.
00730  * \retval NULL if no object available.
00731  */
00732 #ifdef REF_DEBUG
00733 #define ao2_t_global_obj_replace(holder, obj, tag) \
00734    __ao2_global_obj_replace(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00735 #define ao2_global_obj_replace(holder, obj)  \
00736    __ao2_global_obj_replace(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00737 
00738 #else
00739 
00740 #define ao2_t_global_obj_replace(holder, obj, tag) \
00741    __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00742 #define ao2_global_obj_replace(holder, obj)  \
00743    __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00744 #endif
00745 
00746 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) attribute_warn_unused_result;
00747 
00748 /*!
00749  * \brief Replace an ao2 object in the global holder, throwing away any old object.
00750  * \since 11.0
00751  *
00752  * \param holder Global ao2 object holder.
00753  * \param obj Object to put into the holder.  Can be NULL.
00754  * \param tag used for debugging
00755  *
00756  * \note This function automatically increases the reference
00757  * count to account for the reference that the global holder now
00758  * holds to the object.  It also decreases the reference count
00759  * of any object being replaced.
00760  *
00761  * \retval 0 The global object was previously empty
00762  * \retval 1 The global object was not previously empty
00763  */
00764 #ifdef REF_DEBUG
00765 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \
00766    __ao2_global_obj_replace_unref(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00767 #define ao2_global_obj_replace_unref(holder, obj)  \
00768    __ao2_global_obj_replace_unref(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00769 
00770 #else
00771 
00772 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \
00773    __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00774 #define ao2_global_obj_replace_unref(holder, obj)  \
00775    __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00776 #endif
00777 
00778 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);
00779 
00780 /*!
00781  * \brief Get a reference to the object stored in the global holder.
00782  * \since 11.0
00783  *
00784  * \param holder Global ao2 object holder.
00785  * \param tag used for debugging
00786  *
00787  * \retval Reference to current ao2 object stored in the holder.
00788  * \retval NULL if no object available.
00789  */
00790 #ifdef REF_DEBUG
00791 #define ao2_t_global_obj_ref(holder, tag) \
00792    __ao2_global_obj_ref(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00793 #define ao2_global_obj_ref(holder)  \
00794    __ao2_global_obj_ref(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00795 
00796 #else
00797 
00798 #define ao2_t_global_obj_ref(holder, tag) \
00799    __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00800 #define ao2_global_obj_ref(holder)  \
00801    __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
00802 #endif
00803 
00804 void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name) attribute_warn_unused_result;
00805 
00806 
00807 /*!
00808  \page AstObj2_Containers AstObj2 Containers
00809 
00810 Containers are data structures meant to store several objects,
00811 and perform various operations on them.
00812 Internally, objects are stored in lists, hash tables or other
00813 data structures depending on the needs.
00814 
00815 \note NOTA BENE: at the moment the only container we support is the
00816     hash table and its degenerate form, the list.
00817 
00818 Operations on container include:
00819 
00820   -  c = \b ao2_container_alloc(size, hash_fn, cmp_fn)
00821     allocate a container with desired size and default compare
00822     and hash function
00823          -The compare function returns an int, which
00824          can be 0 for not found, CMP_STOP to stop end a traversal,
00825          or CMP_MATCH if they are equal
00826          -The hash function returns an int. The hash function
00827          takes two argument, the object pointer and a flags field,
00828 
00829   -  \b ao2_find(c, arg, flags)
00830     returns zero or more elements matching a given criteria
00831     (specified as arg). 'c' is the container pointer. Flags
00832     can be:
00833     OBJ_UNLINK - to remove the object, once found, from the container.
00834     OBJ_NODATA - don't return the object if found (no ref count change)
00835     OBJ_MULTIPLE - don't stop at first match
00836     OBJ_SEARCH_OBJECT - if set, 'arg' is an object pointer, and a hash table
00837                   search will be done. If not, a traversal is done.
00838     OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
00839               Similar to OBJ_SEARCH_OBJECT and mutually exclusive.
00840     OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
00841               Similar to OBJ_SEARCH_KEY and mutually exclusive.
00842 
00843   -  \b ao2_callback(c, flags, fn, arg)
00844     apply fn(obj, arg) to all objects in the container.
00845     Similar to find. fn() can tell when to stop, and
00846     do anything with the object including unlinking it.
00847       - c is the container;
00848       - flags can be
00849          OBJ_UNLINK   - to remove the object, once found, from the container.
00850          OBJ_NODATA   - don't return the object if found (no ref count change)
00851          OBJ_MULTIPLE - don't stop at first match
00852          OBJ_SEARCH_OBJECT - if set, 'arg' is an object pointer, and a hash table
00853                         search will be done. If not, a traversal is done through
00854                         all the hash table 'buckets'..
00855          OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
00856                         Similar to OBJ_SEARCH_OBJECT and mutually exclusive.
00857          OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
00858                         Similar to OBJ_SEARCH_KEY and mutually exclusive.
00859       - fn is a func that returns int, and takes 3 args:
00860         (void *obj, void *arg, int flags);
00861           obj is an object
00862           arg is the same as arg passed into ao2_callback
00863           flags is the same as flags passed into ao2_callback
00864          fn returns:
00865            0: no match, keep going
00866            CMP_STOP: stop search, no match
00867            CMP_MATCH: This object is matched.
00868 
00869     Note that the entire operation is run with the container
00870     locked, so nobody else can change its content while we work on it.
00871     However, we pay this with the fact that doing
00872     anything blocking in the callback keeps the container
00873     blocked.
00874     The mechanism is very flexible because the callback function fn()
00875     can do basically anything e.g. counting, deleting records, etc.
00876     possibly using arg to store the results.
00877 
00878   -  \b iterate on a container
00879     this is done with the following sequence
00880 
00881 \code
00882 
00883         struct ao2_container *c = ... // our container
00884         struct ao2_iterator i;
00885         void *o;
00886 
00887         i = ao2_iterator_init(c, flags);
00888 
00889         while ((o = ao2_iterator_next(&i))) {
00890             ... do something on o ...
00891             ao2_ref(o, -1);
00892         }
00893 
00894         ao2_iterator_destroy(&i);
00895 \endcode
00896 
00897     The difference with the callback is that the control
00898     on how to iterate is left to us.
00899 
00900     - \b ao2_ref(c, -1)
00901     dropping a reference to a container destroys it, very simple!
00902 
00903 Containers are ao2 objects themselves, and this is why their
00904 implementation is simple too.
00905 
00906 Before declaring containers, we need to declare the types of the
00907 arguments passed to the constructor - in turn, this requires
00908 to define callback and hash functions and their arguments.
00909 
00910 - \ref AstObj2
00911 - \ref astobj2.h
00912  */
00913 
00914 /*! \brief
00915  * A callback function will return a combination of CMP_MATCH and CMP_STOP.
00916  * The latter will terminate the search in a container.
00917  */
00918 enum _cb_results {
00919    CMP_MATCH   = 0x1,   /*!< the object matches the request */
00920    CMP_STOP = 0x2,   /*!< stop the search now */
00921 };
00922 
00923 /*!
00924  * \brief Flags passed to ao2_callback_fn(), ao2_hash_fn(), and ao2_sort_fn() to modify behaviour.
00925  */
00926 enum search_flags {
00927    /*!
00928     * Unlink the object for which the callback function returned
00929     * CMP_MATCH.
00930     */
00931    OBJ_UNLINK = (1 << 0),
00932    /*!
00933     * On match, don't return the object hence do not increase its
00934     * refcount.
00935     */
00936    OBJ_NODATA = (1 << 1),
00937    /*!
00938     * Don't stop at the first match in ao2_callback() unless the
00939     * result of of the callback function has the CMP_STOP bit set.
00940     */
00941    OBJ_MULTIPLE = (1 << 2),
00942    /*!
00943     * \brief Assume that the ao2_container is already locked.
00944     *
00945     * \note For ao2_containers that have mutexes, no locking will
00946     * be done.
00947     *
00948     * \note For ao2_containers that have RWLOCKs, the lock will be
00949     * promoted to write mode as needed.  The lock will be returned
00950     * to the original locked state.
00951     *
00952     * \note Only use this flag if the ao2_container is manually
00953     * locked already.
00954     */
00955    OBJ_NOLOCK = (1 << 4),
00956 
00957    /*!
00958     * \brief Search option field mask.
00959     *
00960     * \todo Eventually OBJ_SEARCH_MASK will shrink to a two bit
00961     * field when the codebase is made to use the search field
00962     * values as a field instead of independent bits.
00963     */
00964    OBJ_SEARCH_MASK = (0x07 << 5),
00965    /*! \brief The arg parameter has no meaning to the astobj2 code. */
00966    OBJ_SEARCH_NONE = (0 << 5),
00967    /*!
00968     * \brief The arg parameter is an object of the same type.
00969     *
00970     * \details
00971     * The arg parameter is an object of the same type as the one
00972     * being searched for, so use the object's ao2_hash_fn and/or
00973     * ao2_sort_fn functions for optimized searching.
00974     *
00975     * \note The supplied ao2_callback_fn is called after the
00976     * container nodes have been filtered by the ao2_hash_fn and/or
00977     * ao2_sort_fn functions.
00978     */
00979    OBJ_SEARCH_OBJECT = (1 << 5),
00980    /*!
00981     * \brief The arg parameter is a search key, but is not an object.
00982     *
00983     * \details
00984     * This can be used when you want to be able to pass custom data
00985     * to the container's stored ao2_hash_fn, ao2_sort_fn, and
00986     * ao2_find ao2_callback_fn functions that is not a full object,
00987     * but perhaps just a string.
00988     *
00989     * \note The supplied ao2_callback_fn is called after the
00990     * container nodes have been filtered by the ao2_hash_fn and/or
00991     * ao2_sort_fn functions.
00992     */
00993    OBJ_SEARCH_KEY = (2 << 5),
00994    /*!
00995     * \brief The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
00996     *
00997     * \details
00998     * The partial key can be used by the ao2_sort_fn to guide the
00999     * search to find a contiguous subset of a sorted container.
01000     * For example, a sorted container holds: "A", "B", "Bert",
01001     * "Beth", "Earnie".  Doing a partial key search with "B" will
01002     * find the sorted subset of all held objects starting with "B".
01003     *
01004     * \note The supplied ao2_callback_fn is called after the
01005     * container nodes have been filtered by the ao2_sort_fn
01006     * function.
01007     */
01008    OBJ_SEARCH_PARTIAL_KEY = (4 << 5),
01009 
01010    /*! \brief Traverse order option field mask. */
01011    OBJ_ORDER_MASK = (0x03 << 8),
01012    /*! \brief Traverse in ascending order (First to last container object) */
01013    OBJ_ORDER_ASCENDING = (0 << 8),
01014    /*! \brief Traverse in descending order (Last to first container object) */
01015    OBJ_ORDER_DESCENDING = (1 << 8),
01016    /*!
01017     * \brief Traverse in pre-order (Node then children, for tree container)
01018     *
01019     * \note For non-tree containers, it is up to the container type
01020     * to make the best interpretation of the order.  For list and
01021     * hash containers, this also means ascending order because a
01022     * binary tree can degenerate into a list.
01023     */
01024    OBJ_ORDER_PRE = (2 << 8),
01025    /*!
01026     * \brief Traverse in post-order (Children then node, for tree container)
01027     *
01028     * \note For non-tree containers, it is up to the container type
01029     * to make the best interpretation of the order.  For list and
01030     * hash containers, this also means descending order because a
01031     * binary tree can degenerate into a list.
01032     */
01033    OBJ_ORDER_POST = (3 << 8),
01034 };
01035 
01036 /*
01037  * Deprecated backward compatible flag names.
01038  *
01039  * Note: OBJ_POINTER, OBJ_KEY, and OBJ_PARTIAL_KEY are mutually
01040  * exclusive.
01041  */
01042 #define OBJ_POINTER     OBJ_SEARCH_OBJECT    /*!< Deprecated name */
01043 #define OBJ_KEY         OBJ_SEARCH_KEY       /*!< Deprecated name */
01044 #define OBJ_PARTIAL_KEY OBJ_SEARCH_PARTIAL_KEY  /*!< Deprecated name */
01045 
01046 /*!
01047  * \brief Options available when allocating an ao2 container object.
01048  *
01049  * \note Each option is open to some interpretation by the
01050  * container type as long as it makes sense with the option
01051  * name.
01052  */
01053 enum ao2_container_opts {
01054    /*!
01055     * \brief Insert objects at the beginning of the container.
01056     * (Otherwise it is the opposite; insert at the end.)
01057     *
01058     * \note If an ao2_sort_fn is provided, the object is inserted
01059     * before any objects with duplicate keys.
01060     *
01061     * \note Hash containers insert the object in the computed hash
01062     * bucket in the indicated manner.
01063     */
01064    AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN = (1 << 0),
01065 
01066    /*!
01067     * \brief The ao2 container objects with duplicate keys option field mask.
01068     */
01069    AO2_CONTAINER_ALLOC_OPT_DUPS_MASK = (3 << 1),
01070    /*!
01071     * \brief Allow objects with duplicate keys in container.
01072     */
01073    AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW = (0 << 1),
01074    /*!
01075     * \brief Reject objects with duplicate keys in container.
01076     *
01077     * \note The container must be sorted.  i.e. have an
01078     * ao2_sort_fn.
01079     */
01080    AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT = (1 << 1),
01081    /*!
01082     * \brief Reject duplicate objects in container.
01083     *
01084     * \details Don't link the same object into the container twice.
01085     * However, you can link a different object with the same key.
01086     *
01087     * \note The container must be sorted.  i.e. have an
01088     * ao2_sort_fn.
01089     *
01090     * \note It is assumed that the objects are located where the
01091     * search key says they should be located.
01092     */
01093    AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT = (2 << 1),
01094    /*!
01095     * \brief Replace objects with duplicate keys in container.
01096     *
01097     * \details The existing duplicate object is removed and the new
01098     * object takes the old object's place.
01099     *
01100     * \note The container must be sorted.  i.e. have an
01101     * ao2_sort_fn.
01102     */
01103    AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE = (3 << 1),
01104 };
01105 
01106 /*!
01107  * \brief Type of a generic callback function
01108  * \param obj  pointer to the (user-defined part) of an object.
01109  * \param arg callback argument from ao2_callback()
01110  * \param flags flags from ao2_callback()
01111  *   OBJ_SEARCH_OBJECT - if set, 'arg', is an object.
01112  *   OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
01113  *   OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
01114  *
01115  * The return values are a combination of enum _cb_results.
01116  * Callback functions are used to search or manipulate objects in a container.
01117  */
01118 typedef int (ao2_callback_fn)(void *obj, void *arg, int flags);
01119 
01120 /*! \brief A common ao2_callback is one that matches by address. */
01121 int ao2_match_by_addr(void *obj, void *arg, int flags);
01122 
01123 /*!
01124  * \brief Type of a generic callback function
01125  * \param obj pointer to the (user-defined part) of an object.
01126  * \param arg callback argument from ao2_callback()
01127  * \param data arbitrary data from ao2_callback()
01128  * \param flags flags from ao2_callback()
01129  *   OBJ_SEARCH_OBJECT - if set, 'arg', is an object.
01130  *   OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
01131  *   OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
01132  *
01133  * The return values are a combination of enum _cb_results.
01134  * Callback functions are used to search or manipulate objects in a container.
01135  */
01136 typedef int (ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags);
01137 
01138 /*!
01139  * Type of a generic function to generate a hash value from an object.
01140  *
01141  * \param obj pointer to the (user-defined part) of an object.
01142  * \param flags flags from ao2_callback()
01143  *   OBJ_SEARCH_OBJECT - if set, 'obj', is an object.
01144  *   OBJ_SEARCH_KEY - if set, 'obj', is a search key item that is not an object.
01145  *
01146  * \note This function must be idempotent.
01147  *
01148  * \return Computed hash value.
01149  */
01150 typedef int (ao2_hash_fn)(const void *obj, int flags);
01151 
01152 /*!
01153  * \brief Type of generic container sort function.
01154  *
01155  * \param obj_left pointer to the (user-defined part) of an object.
01156  * \param obj_right pointer to the (user-defined part) of an object.
01157  * \param flags flags from ao2_callback()
01158  *   OBJ_SEARCH_OBJECT - if set, 'obj_right', is an object.
01159  *   OBJ_SEARCH_KEY - if set, 'obj_right', is a search key item that is not an object.
01160  *   OBJ_SEARCH_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
01161  *
01162  * \note This function must be idempotent.
01163  *
01164  * \retval <0 if obj_left < obj_right
01165  * \retval =0 if obj_left == obj_right
01166  * \retval >0 if obj_left > obj_right
01167  */
01168 typedef int (ao2_sort_fn)(const void *obj_left, const void *obj_right, int flags);
01169 
01170 /*! \name Object Containers
01171  * Here start declarations of containers.
01172  */
01173 /*@{ */
01174 struct ao2_container;
01175 
01176 /*!
01177  * \brief Allocate and initialize a hash container with the desired number of buckets.
01178  *
01179  * \details
01180  * We allocate space for a struct astobj_container, struct container
01181  * and the buckets[] array.
01182  *
01183  * \param options Container ao2 object options (See enum ao2_alloc_opts)
01184  * \param n_buckets Number of buckets for hash
01185  * \param hash_fn Pointer to a function computing a hash value. (NULL if everyting goes in first bucket.)
01186  * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
01187  * \param tag used for debugging.
01188  *
01189  * \return A pointer to a struct container.
01190  *
01191  * \note Destructor is set implicitly.
01192  * \note This is legacy container creation that is mapped to the new method.
01193  */
01194 
01195 #define ao2_t_container_alloc_options(options, n_buckets, hash_fn, cmp_fn, tag) \
01196    ao2_t_container_alloc_hash((options), 0, (n_buckets), (hash_fn), NULL, (cmp_fn), (tag))
01197 #define ao2_container_alloc_options(options, n_buckets, hash_fn, cmp_fn) \
01198    ao2_container_alloc_hash((options), 0, (n_buckets), (hash_fn), NULL, (cmp_fn))
01199 
01200 #define ao2_t_container_alloc(n_buckets, hash_fn, cmp_fn, tag) \
01201    ao2_t_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn), (tag))
01202 #define ao2_container_alloc(n_buckets, hash_fn, cmp_fn) \
01203    ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn))
01204 
01205 /*!
01206  * \brief Allocate and initialize a hash container with the desired number of buckets.
01207  *
01208  * \details
01209  * We allocate space for a struct astobj_container, struct container
01210  * and the buckets[] array.
01211  *
01212  * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
01213  * \param container_options Container behaviour options (See enum ao2_container_opts)
01214  * \param n_buckets Number of buckets for hash
01215  * \param hash_fn Pointer to a function computing a hash value. (NULL if everyting goes in first bucket.)
01216  * \param sort_fn Pointer to a sort function. (NULL to not sort the buckets.)
01217  * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
01218  * \param tag used for debugging.
01219  *
01220  * \return A pointer to a struct container.
01221  *
01222  * \note Destructor is set implicitly.
01223  */
01224 
01225 #if defined(REF_DEBUG)
01226 
01227 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
01228    __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
01229 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
01230    __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
01231 
01232 #elif defined(__AST_DEBUG_MALLOC)
01233 
01234 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
01235    __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
01236 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
01237    __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
01238 
01239 #else
01240 
01241 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
01242    __ao2_container_alloc_hash((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn))
01243 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
01244    __ao2_container_alloc_hash((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn))
01245 
01246 #endif
01247 
01248 struct ao2_container *__ao2_container_alloc_hash(unsigned int ao2_options,
01249    unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
01250    ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
01251 struct ao2_container *__ao2_container_alloc_hash_debug(unsigned int ao2_options,
01252    unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
01253    ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
01254    const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
01255 
01256 /*!
01257  * \brief Allocate and initialize a list container.
01258  *
01259  * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
01260  * \param container_options Container behaviour options (See enum ao2_container_opts)
01261  * \param sort_fn Pointer to a sort function. (NULL if list not sorted.)
01262  * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
01263  * \param tag used for debugging.
01264  *
01265  * \return A pointer to a struct container.
01266  *
01267  * \note Destructor is set implicitly.
01268  * \note Implemented as a degenerate hash table.
01269  */
01270 
01271 #if defined(REF_DEBUG)
01272 
01273 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
01274    __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
01275 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
01276    __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
01277 
01278 #elif defined(__AST_DEBUG_MALLOC)
01279 
01280 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
01281    __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
01282 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
01283    __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
01284 
01285 #else
01286 
01287 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
01288    __ao2_container_alloc_list((ao2_options), (container_options), (sort_fn), (cmp_fn))
01289 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
01290    __ao2_container_alloc_list((ao2_options), (container_options), (sort_fn), (cmp_fn))
01291 
01292 #endif
01293 
01294 struct ao2_container *__ao2_container_alloc_list(unsigned int ao2_options,
01295    unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
01296 struct ao2_container *__ao2_container_alloc_list_debug(unsigned int ao2_options,
01297    unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
01298    const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
01299 
01300 /*!
01301  * \brief Allocate and initialize a red-black tree container.
01302  *
01303  * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
01304  * \param container_options Container behaviour options (See enum ao2_container_opts)
01305  * \param sort_fn Pointer to a sort function.
01306  * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
01307  * \param tag used for debugging.
01308  *
01309  * \return A pointer to a struct container.
01310  *
01311  * \note Destructor is set implicitly.
01312  */
01313 
01314 #if defined(REF_DEBUG)
01315 
01316 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
01317    __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
01318 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
01319    __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
01320 
01321 #elif defined(__AST_DEBUG_MALLOC)
01322 
01323 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
01324    __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
01325 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
01326    __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
01327 
01328 #else
01329 
01330 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
01331    __ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn))
01332 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
01333    __ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn))
01334 
01335 #endif
01336 
01337 struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options,
01338    ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
01339 struct ao2_container *__ao2_container_alloc_rbtree_debug(unsigned int ao2_options, unsigned int container_options,
01340    ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
01341    const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
01342 
01343 /*! \brief
01344  * Returns the number of elements in a container.
01345  */
01346 int ao2_container_count(struct ao2_container *c);
01347 
01348 /*!
01349  * \brief Copy all object references in the src container into the dest container.
01350  * \since 11.0
01351  *
01352  * \param dest Container to copy src object references into.
01353  * \param src Container to copy all object references from.
01354  * \param flags OBJ_NOLOCK if a lock is already held on both containers.
01355  *    Otherwise, the src container is locked first.
01356  *
01357  * \pre The dest container must be empty.  If the duplication fails, the
01358  * dest container will be returned empty.
01359  *
01360  * \note This can potentially be expensive because a malloc is
01361  * needed for every object in the src container.
01362  *
01363  * \retval 0 on success.
01364  * \retval -1 on error.
01365  */
01366 int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags);
01367 
01368 /*!
01369  * \brief Create a clone/copy of the given container.
01370  * \since 11.0
01371  *
01372  * \param orig Container to copy all object references from.
01373  * \param flags OBJ_NOLOCK if a lock is already held on the container.
01374  *
01375  * \note This can potentially be expensive because a malloc is
01376  * needed for every object in the orig container.
01377  *
01378  * \retval Clone container on success.
01379  * \retval NULL on error.
01380  */
01381 struct ao2_container *__ao2_container_clone(struct ao2_container *orig, enum search_flags flags) attribute_warn_unused_result;
01382 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) attribute_warn_unused_result;
01383 #if defined(REF_DEBUG)
01384 
01385 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
01386 #define ao2_container_clone(orig, flags)     __ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
01387 
01388 #elif defined(__AST_DEBUG_MALLOC)
01389 
01390 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
01391 #define ao2_container_clone(orig, flags)     __ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
01392 
01393 #else
01394 
01395 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone(orig, flags)
01396 #define ao2_container_clone(orig, flags)     __ao2_container_clone(orig, flags)
01397 
01398 #endif
01399 
01400 /*!
01401  * \brief Print output.
01402  * \since 12.0.0
01403  *
01404  * \param where User data pointer needed to determine where to put output.
01405  * \param fmt printf type format string.
01406  *
01407  * \return Nothing
01408  */
01409 typedef void (ao2_prnt_fn)(void *where, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
01410 
01411 /*!
01412  * \brief Print object key.
01413  * \since 12.0.0
01414  *
01415  * \param v_obj A pointer to the object we want the key printed.
01416  * \param where User data needed by prnt to determine where to put output.
01417  * \param prnt Print output callback function to use.
01418  *
01419  * \return Nothing
01420  */
01421 typedef void (ao2_prnt_obj_fn)(void *v_obj, void *where, ao2_prnt_fn *prnt);
01422 
01423 /*!
01424  * \brief Display contents of the specified container.
01425  * \since 12.0.0
01426  *
01427  * \param self Container to dump.
01428  * \param flags OBJ_NOLOCK if a lock is already held on the container.
01429  * \param name Container name.  (NULL if anonymous)
01430  * \param where User data needed by prnt to determine where to put output.
01431  * \param prnt Print output callback function to use.
01432  * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
01433  *
01434  * \return Nothing
01435  */
01436 void ao2_container_dump(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj);
01437 
01438 /*!
01439  * \brief Display statistics of the specified container.
01440  * \since 12.0.0
01441  *
01442  * \param self Container to display statistics.
01443  * \param flags OBJ_NOLOCK if a lock is already held on the container.
01444  * \param name Container name.  (NULL if anonymous)
01445  * \param where User data needed by prnt to determine where to put output.
01446  * \param prnt Print output callback function to use.
01447  *
01448  * \return Nothing
01449  */
01450 void ao2_container_stats(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt);
01451 
01452 /*!
01453  * \brief Perform an integrity check on the specified container.
01454  * \since 12.0.0
01455  *
01456  * \param self Container to check integrity.
01457  * \param flags OBJ_NOLOCK if a lock is already held on the container.
01458  *
01459  * \retval 0 on success.
01460  * \retval -1 on error.
01461  */
01462 int ao2_container_check(struct ao2_container *self, enum search_flags flags);
01463 
01464 /*!
01465  * \brief Register a container for CLI stats and integrity check.
01466  * \since 12.0.0
01467  *
01468  * \param name Name to register the container under.
01469  * \param self Container to register.
01470  * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
01471  *
01472  * \retval 0 on success.
01473  * \retval -1 on error.
01474  */
01475 int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj);
01476 
01477 /*!
01478  * \brief Unregister a container for CLI stats and integrity check.
01479  * \since 12.0.0
01480  *
01481  * \param name Name the container is registered under.
01482  *
01483  * \return Nothing
01484  */
01485 void ao2_container_unregister(const char *name);
01486 
01487 /*@} */
01488 
01489 /*! \name Object Management
01490  * Here we have functions to manage objects.
01491  *
01492  * We can use the functions below on any kind of
01493  * object defined by the user.
01494  */
01495 /*@{ */
01496 
01497 /*!
01498  * \brief Add an object to a container.
01499  *
01500  * \param container The container to operate on.
01501  * \param obj The object to be added.
01502  * \param tag used for debugging.
01503  *
01504  * \retval 0 on errors.
01505  * \retval 1 on success.
01506  *
01507  * This function inserts an object in a container according its key.
01508  *
01509  * \note Remember to set the key before calling this function.
01510  *
01511  * \note This function automatically increases the reference count to account
01512  *       for the reference that the container now holds to the object.
01513  */
01514 #ifdef REF_DEBUG
01515 
01516 #define ao2_t_link(container, obj, tag)               __ao2_link_debug((container), (obj), 0, (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01517 #define ao2_link(container, obj)                __ao2_link_debug((container), (obj), 0, "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01518 
01519 /*!
01520  * \brief Add an object to a container.
01521  *
01522  * \param container The container to operate on.
01523  * \param obj The object to be added.
01524  * \param flags search_flags to control linking the object.  (OBJ_NOLOCK)
01525  * \param tag used for debugging.
01526  *
01527  * \retval 0 on errors.
01528  * \retval 1 on success.
01529  *
01530  * This function inserts an object in a container according its key.
01531  *
01532  * \note Remember to set the key before calling this function.
01533  *
01534  * \note This function automatically increases the reference count to account
01535  *       for the reference that the container now holds to the object.
01536  */
01537 #define ao2_t_link_flags(container, obj, flags, tag)  __ao2_link_debug((container), (obj), (flags), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01538 #define ao2_link_flags(container, obj, flags)         __ao2_link_debug((container), (obj), (flags), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01539 
01540 #else
01541 
01542 #define ao2_t_link(container, obj, tag)               __ao2_link((container), (obj), 0)
01543 #define ao2_link(container, obj)                __ao2_link((container), (obj), 0)
01544 
01545 #define ao2_t_link_flags(container, obj, flags, tag)  __ao2_link((container), (obj), (flags))
01546 #define ao2_link_flags(container, obj, flags)         __ao2_link((container), (obj), (flags))
01547 
01548 #endif
01549 
01550 int __ao2_link_debug(struct ao2_container *c, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func);
01551 int __ao2_link(struct ao2_container *c, void *obj_new, int flags);
01552 
01553 /*!
01554  * \brief Remove an object from a container
01555  *
01556  * \param container The container to operate on.
01557  * \param obj The object to unlink.
01558  * \param tag used for debugging.
01559  *
01560  * \retval NULL, always
01561  *
01562  * \note The object requested to be unlinked must be valid.  However, if it turns
01563  *       out that it is not in the container, this function is still safe to
01564  *       be called.
01565  *
01566  * \note If the object gets unlinked from the container, the container's
01567  *       reference to the object will be automatically released. (The
01568  *       refcount will be decremented).
01569  */
01570 #ifdef REF_DEBUG
01571 
01572 #define ao2_t_unlink(container, obj, tag)          __ao2_unlink_debug((container), (obj), 0, (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01573 #define ao2_unlink(container, obj)                 __ao2_unlink_debug((container), (obj), 0, "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01574 
01575 /*!
01576  * \brief Remove an object from a container
01577  *
01578  * \param container The container to operate on.
01579  * \param obj The object to unlink.
01580  * \param flags search_flags to control unlinking the object.  (OBJ_NOLOCK)
01581  * \param tag used for debugging.
01582  *
01583  * \retval NULL, always
01584  *
01585  * \note The object requested to be unlinked must be valid.  However, if it turns
01586  *       out that it is not in the container, this function is still safe to
01587  *       be called.
01588  *
01589  * \note If the object gets unlinked from the container, the container's
01590  *       reference to the object will be automatically released. (The
01591  *       refcount will be decremented).
01592  */
01593 
01594 #define ao2_t_unlink_flags(container, obj, flags, tag)   __ao2_unlink_debug((container), (obj), (flags), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01595 #define ao2_unlink_flags(container, obj, flags)       __ao2_unlink_debug((container), (obj), (flags), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01596 
01597 #else
01598 
01599 #define ao2_t_unlink(container, obj, tag)          __ao2_unlink((container), (obj), 0)
01600 #define ao2_unlink(container, obj)                 __ao2_unlink((container), (obj), 0)
01601 
01602 #define ao2_t_unlink_flags(container, obj, flags, tag)   __ao2_unlink((container), (obj), (flags))
01603 #define ao2_unlink_flags(container, obj, flags)       __ao2_unlink((container), (obj), (flags))
01604 
01605 #endif
01606 
01607 void *__ao2_unlink_debug(struct ao2_container *c, void *obj, int flags, const char *tag, const char *file, int line, const char *func);
01608 void *__ao2_unlink(struct ao2_container *c, void *obj, int flags);
01609 
01610 
01611 /*@} */
01612 
01613 /*! \brief
01614  * ao2_callback() is a generic function that applies cb_fn() to all objects
01615  * in a container, as described below.
01616  *
01617  * \param c A pointer to the container to operate on.
01618  * \param flags A set of flags specifying the operation to perform,
01619  *  partially used by the container code, but also passed to
01620  *  the callback.
01621  *   - If OBJ_NODATA is set, ao2_callback will return NULL. No refcounts
01622  *     of any of the traversed objects will be incremented.
01623  *     On the converse, if it is NOT set (the default), the ref count
01624  *     of the first matching object will be incremented and returned.
01625  *   - If OBJ_MULTIPLE is set, the ref count of all matching objects will
01626  *     be incremented in an iterator for a temporary container and returned.
01627  *   - If OBJ_SEARCH_OBJECT is set, the traversed items will be restricted
01628  *     to the objects in the bucket that the object key hashes to.
01629  *   - If OBJ_SEARCH_KEY is set, the traversed items will be restricted
01630  *     to the objects in the bucket that the object key hashes to.
01631  * \param cb_fn A function pointer, that will be called on all
01632  *  objects, to see if they match. This function returns CMP_MATCH
01633  *  if the object is matches the criteria; CMP_STOP if the traversal
01634  *  should immediately stop, or both (via bitwise ORing), if you find a
01635  *  match and want to end the traversal, and 0 if the object is not a match,
01636  *  but the traversal should continue. This is the function that is applied
01637  *  to each object traversed. Its arguments are:
01638  *      (void *obj, void *arg, int flags), where:
01639  *        obj is an object
01640  *        arg is the same as arg passed into ao2_callback
01641  *        flags is the same as flags passed into ao2_callback (flags are
01642  *         also used by ao2_callback).
01643  * \param arg passed to the callback.
01644  * \param tag used for debugging.
01645  *
01646  * \retval NULL on failure or no matching object found.
01647  *
01648  * \retval object found if OBJ_MULTIPLE is not set in the flags
01649  * parameter.
01650  *
01651  * \retval ao2_iterator pointer if OBJ_MULTIPLE is set in the
01652  * flags parameter.  The iterator must be destroyed with
01653  * ao2_iterator_destroy() when the caller no longer needs it.
01654  *
01655  * If the function returns any objects, their refcount is incremented,
01656  * and the caller is in charge of decrementing them once done.
01657  *
01658  * Typically, ao2_callback() is used for two purposes:
01659  * - to perform some action (including removal from the container) on one
01660  *   or more objects; in this case, cb_fn() can modify the object itself,
01661  *   and to perform deletion should set CMP_MATCH on the matching objects,
01662  *   and have OBJ_UNLINK set in flags.
01663  * - to look for a specific object in a container; in this case, cb_fn()
01664  *   should not modify the object, but just return a combination of
01665  *   CMP_MATCH and CMP_STOP on the desired object.
01666  * Other usages are also possible, of course.
01667  *
01668  * This function searches through a container and performs operations
01669  * on objects according on flags passed.
01670  * XXX describe better
01671  * The comparison is done calling the compare function set implicitly.
01672  * The arg pointer can be a pointer to an object or to a key,
01673  * we can say this looking at flags value.
01674  * If arg points to an object we will search for the object pointed
01675  * by this value, otherwise we search for a key value.
01676  * If the key is not unique we only find the first matching value.
01677  *
01678  * The use of flags argument is the follow:
01679  *
01680  *      OBJ_UNLINK              unlinks the object found
01681  *      OBJ_NODATA              on match, do return an object
01682  *                              Callbacks use OBJ_NODATA as a default
01683  *                              functions such as find() do
01684  *      OBJ_MULTIPLE            return multiple matches
01685  *                              Default is no.
01686  *      OBJ_SEARCH_OBJECT       the pointer is to an object
01687  *      OBJ_SEARCH_KEY          the pointer is to a search key
01688  *      OBJ_SEARCH_PARTIAL_KEY  the pointer is to a partial search key
01689  *
01690  * \note When the returned object is no longer in use, ao2_ref() should
01691  * be used to free the additional reference possibly created by this function.
01692  *
01693  * @{
01694  */
01695 #ifdef REF_DEBUG
01696 
01697 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \
01698    __ao2_callback_debug((c), (flags), (cb_fn), (arg), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
01699 #define ao2_callback(c, flags, cb_fn, arg) \
01700    __ao2_callback_debug((c), (flags), (cb_fn), (arg), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
01701 
01702 #else
01703 
01704 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \
01705    __ao2_callback((c), (flags), (cb_fn), (arg))
01706 #define ao2_callback(c, flags, cb_fn, arg) \
01707    __ao2_callback((c), (flags), (cb_fn), (arg))
01708 
01709 #endif
01710 
01711 void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags,
01712    ao2_callback_fn *cb_fn, void *arg, const char *tag, const char *file, int line,
01713    const char *func);
01714 void *__ao2_callback(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg);
01715 
01716 /*! @} */
01717 
01718 /*! \brief
01719  * ao2_callback_data() is a generic function that applies cb_fn() to all objects
01720  * in a container.  It is functionally identical to ao2_callback() except that
01721  * instead of taking an ao2_callback_fn *, it takes an ao2_callback_data_fn *, and
01722  * allows the caller to pass in arbitrary data.
01723  *
01724  * This call would be used instead of ao2_callback() when the caller needs to pass
01725  * OBJ_SEARCH_OBJECT, OBJ_SEARCH_KEY, or OBJ_SEARCH_PARTIAL_KEY as part of the flags
01726  * argument (which in turn requires passing in a known pointer type for 'arg') and
01727  * also needs access to other non-global data to complete it's comparison or task.
01728  *
01729  * See the documentation for ao2_callback() for argument descriptions.
01730  *
01731  * \see ao2_callback()
01732  */
01733 #ifdef REF_DEBUG
01734 
01735 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \
01736    __ao2_callback_data_debug((container), (flags), (cb_fn), (arg), (data), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
01737 #define ao2_callback_data(container, flags, cb_fn, arg, data) \
01738    __ao2_callback_data_debug((container), (flags), (cb_fn), (arg), (data), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
01739 
01740 #else
01741 
01742 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \
01743    __ao2_callback_data((container), (flags), (cb_fn), (arg), (data))
01744 #define ao2_callback_data(container, flags, cb_fn, arg, data) \
01745    __ao2_callback_data((container), (flags), (cb_fn), (arg), (data))
01746 
01747 #endif
01748 
01749 void *__ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags,
01750    ao2_callback_data_fn *cb_fn, void *arg, void *data, const char *tag, const char *file,
01751    int line, const char *func);
01752 void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags,
01753    ao2_callback_data_fn *cb_fn, void *arg, void *data);
01754 
01755 /*! ao2_find() is a short hand for ao2_callback(c, flags, c->cmp_fn, arg)
01756  * XXX possibly change order of arguments ?
01757  */
01758 #ifdef REF_DEBUG
01759 
01760 #define ao2_t_find(container, arg, flags, tag) \
01761    __ao2_find_debug((container), (arg), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
01762 #define ao2_find(container, arg, flags) \
01763    __ao2_find_debug((container), (arg), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
01764 
01765 #else
01766 
01767 #define ao2_t_find(container, arg, flags, tag) \
01768    __ao2_find((container), (arg), (flags))
01769 #define ao2_find(container, arg, flags) \
01770    __ao2_find((container), (arg), (flags))
01771 
01772 #endif
01773 
01774 void *__ao2_find_debug(struct ao2_container *c, const void *arg, enum search_flags flags,
01775    const char *tag, const char *file, int line, const char *func);
01776 void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags);
01777 
01778 /*! \brief
01779  *
01780  *
01781  * When we need to walk through a container, we use an
01782  * ao2_iterator to keep track of the current position.
01783  *
01784  * Because the navigation is typically done without holding the
01785  * lock on the container across the loop, objects can be
01786  * inserted or deleted or moved while we work.  As a
01787  * consequence, there is no guarantee that we manage to touch
01788  * all the elements in the container, and it is possible that we
01789  * touch the same object multiple times.
01790  *
01791  * An iterator must be first initialized with
01792  * ao2_iterator_init(), then we can use o = ao2_iterator_next()
01793  * to move from one element to the next.  Remember that the
01794  * object returned by ao2_iterator_next() has its refcount
01795  * incremented, and the reference must be explicitly released
01796  * when done with it.
01797  *
01798  * In addition, ao2_iterator_init() will hold a reference to the
01799  * container being iterated and the last container node found.
01800  * Thes objects will be unreffed when ao2_iterator_destroy() is
01801  * called to free up the resources used by the iterator (if
01802  * any).
01803  *
01804  * Example:
01805  *
01806  *  \code
01807  *
01808  *  struct ao2_container *c = ... // the container we want to iterate on
01809  *  struct ao2_iterator i;
01810  *  struct my_obj *o;
01811  *
01812  *  i = ao2_iterator_init(c, flags);
01813  *
01814  *  while ((o = ao2_iterator_next(&i))) {
01815  *     ... do something on o ...
01816  *     ao2_ref(o, -1);
01817  *  }
01818  *
01819  *  ao2_iterator_restart(&i);
01820  *  while ((o = ao2_iterator_next(&i))) {
01821  *     ... do something on o ...
01822  *     ao2_ref(o, -1);
01823  *  }
01824  *
01825  *  ao2_iterator_destroy(&i);
01826  *
01827  *  \endcode
01828  *
01829  */
01830 
01831 /*!
01832  * \brief The astobj2 iterator
01833  *
01834  * \note You are not supposed to know the internals of an iterator!
01835  * We would like the iterator to be opaque, unfortunately
01836  * its size needs to be known if we want to store it around
01837  * without too much trouble.
01838  * Anyways...
01839  * The iterator has a pointer to the container, and a flags
01840  * field specifying various things e.g. whether the container
01841  * should be locked or not while navigating on it.
01842  * The iterator "points" to the current container node.
01843  *
01844  * Details are in the implementation of ao2_iterator_next()
01845  */
01846 struct ao2_iterator {
01847    /*! The container (Has a reference) */
01848    struct ao2_container *c;
01849    /*! Last container node (Has a reference) */
01850    void *last_node;
01851    /*! Nonzero if the iteration has completed. */
01852    int complete;
01853    /*! operation flags (enum ao2_iterator_flags) */
01854    int flags;
01855 };
01856 
01857 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior
01858  * of the iterator.
01859  */
01860 enum ao2_iterator_flags {
01861    /*!
01862     * \brief Assume that the ao2_container is already locked.
01863     *
01864     * \note For ao2_containers that have mutexes, no locking will
01865     * be done.
01866     *
01867     * \note For ao2_containers that have RWLOCKs, the lock will be
01868     * promoted to write mode as needed.  The lock will be returned
01869     * to the original locked state.
01870     *
01871     * \note Only use this flag if the ao2_container is manually
01872     * locked already.  You should hold the lock until after
01873     * ao2_iterator_destroy().  If you must release the lock then
01874     * you must at least hold the lock whenever you call an
01875     * ao2_iterator_xxx function with this iterator.
01876     */
01877    AO2_ITERATOR_DONTLOCK = (1 << 0),
01878    /*!
01879     * Indicates that the iterator was dynamically allocated by
01880     * astobj2 API and should be freed by ao2_iterator_destroy().
01881     */
01882    AO2_ITERATOR_MALLOCD = (1 << 1),
01883    /*!
01884     * Indicates that before the iterator returns an object from
01885     * the container being iterated, the object should be unlinked
01886     * from the container.
01887     */
01888    AO2_ITERATOR_UNLINK = (1 << 2),
01889    /*!
01890     * Iterate in descending order (Last to first container object)
01891     * (Otherwise ascending order)
01892     *
01893     * \note Other traversal orders such as pre-order and post-order
01894     * do not make sense because they require the container
01895     * structure to be static during the traversal.  Iterators just
01896     * about guarantee that is not going to happen because the
01897     * container is allowed to change by other threads during the
01898     * iteration.
01899     */
01900    AO2_ITERATOR_DESCENDING = (1 << 3),
01901 };
01902 
01903 /*!
01904  * \brief Create an iterator for a container
01905  *
01906  * \param c the container
01907  * \param flags one or more flags from ao2_iterator_flags.
01908  *
01909  * \retval the constructed iterator
01910  *
01911  * \note This function does \b not take a pointer to an iterator;
01912  *       rather, it returns an iterator structure that should be
01913  *       assigned to (overwriting) an existing iterator structure
01914  *       allocated on the stack or on the heap.
01915  *
01916  * This function will take a reference on the container being iterated.
01917  */
01918 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result;
01919 
01920 /*!
01921  * \brief Destroy a container iterator
01922  *
01923  * \param iter the iterator to destroy
01924  *
01925  * \retval none
01926  *
01927  * This function will release the container reference held by the iterator
01928  * and any other resources it may be holding.
01929  */
01930 #if defined(TEST_FRAMEWORK)
01931 void ao2_iterator_destroy(struct ao2_iterator *iter) __attribute__((noinline));
01932 #else
01933 void ao2_iterator_destroy(struct ao2_iterator *iter);
01934 #endif   /* defined(TEST_FRAMEWORK) */
01935 
01936 #ifdef REF_DEBUG
01937 
01938 #define ao2_t_iterator_next(iter, tag) __ao2_iterator_next_debug((iter), (tag),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01939 #define ao2_iterator_next(iter)        __ao2_iterator_next_debug((iter), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01940 
01941 #else
01942 
01943 #define ao2_t_iterator_next(iter, tag) __ao2_iterator_next((iter))
01944 #define ao2_iterator_next(iter)        __ao2_iterator_next((iter))
01945 
01946 #endif
01947 
01948 void *__ao2_iterator_next_debug(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
01949 void *__ao2_iterator_next(struct ao2_iterator *iter) attribute_warn_unused_result;
01950 
01951 /*!
01952  * \brief Restart an iteration.
01953  *
01954  * \param iter the iterator to restart
01955  *
01956  * \note A restart is not going to have any effect if the
01957  * iterator was created with the AO2_ITERATOR_UNLINK flag.  Any
01958  * previous objects returned were removed from the container.
01959  *
01960  * \retval none
01961  */
01962 void ao2_iterator_restart(struct ao2_iterator *iter);
01963 
01964 /*! gcc __attribute__(cleanup()) functions
01965  * \note they must be able to handle NULL parameters because most of the
01966  * allocation/find functions can fail and we don't want to try to tear
01967  * down a NULL */
01968 void __ao2_cleanup(void *obj);
01969 void __ao2_cleanup_debug(void *obj, const char *tag, const char *file, int line, const char *function);
01970 #ifdef REF_DEBUG
01971 #define ao2_cleanup(obj) __ao2_cleanup_debug((obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
01972 #define ao2_t_cleanup(obj, tag) __ao2_cleanup_debug((obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
01973 #else
01974 #define ao2_cleanup(obj) __ao2_cleanup(obj)
01975 #define ao2_t_cleanup(obj, tag) __ao2_cleanup((obj))
01976 #endif
01977 void ao2_iterator_cleanup(struct ao2_iterator *iter);
01978 
01979 /*!
01980  * \brief Get a count of the iterated container objects.
01981  *
01982  * \param iter the iterator to query
01983  *
01984  * \retval The number of objects in the iterated container
01985  */
01986 int ao2_iterator_count(struct ao2_iterator *iter);
01987 
01988 #endif /* _ASTERISK_ASTOBJ2_H */

Generated on Thu Apr 16 06:27:14 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6