Wed Aug 18 22:33:44 2010

Asterisk developer's documentation


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 
00022 /*! \file
00023  * \ref AstObj2
00024  *
00025  * \page AstObj2 Object Model implementing objects and containers.
00026 
00027 This module implements an abstraction for objects (with locks and
00028 reference counts), and containers for these user-defined objects,
00029 also supporting locking, reference counting and callbacks.
00030 
00031 The internal implementation of objects and containers is opaque to the user,
00032 so we can use different data structures as needs arise.
00033 
00034 \section AstObj2_UsageObjects USAGE - OBJECTS
00035 
00036 An ao2 object is a block of memory that the user code can access,
00037 and for which the system keeps track (with a bit of help from the
00038 programmer) of the number of references around.  When an object has
00039 no more references (refcount == 0), it is destroyed, by first
00040 invoking whatever 'destructor' function the programmer specifies
00041 (it can be NULL if none is necessary), and then freeing the memory.
00042 This way objects can be shared without worrying who is in charge
00043 of freeing them.
00044 As an additional feature, ao2 objects are associated to individual
00045 locks.
00046 
00047 Creating an object requires the size of the object and
00048 and a pointer to the destructor function:
00049 
00050     struct foo *o;
00051 
00052     o = ao2_alloc(sizeof(struct foo), my_destructor_fn);
00053 
00054 The value returned points to the user-visible portion of the objects
00055 (user-data), but is also used as an identifier for all object-related
00056 operations such as refcount and lock manipulations.
00057 
00058 On return from ao2_alloc():
00059 
00060  - the object has a refcount = 1;
00061  - the memory for the object is allocated dynamically and zeroed;
00062  - we cannot realloc() the object itself;
00063  - we cannot call free(o) to dispose of the object. Rather, we
00064    tell the system that we do not need the reference anymore:
00065 
00066     ao2_ref(o, -1)
00067 
00068   causing the destructor to be called (and then memory freed) when
00069   the refcount goes to 0.
00070 
00071 - ao2_ref(o, +1) can be used to modify the refcount on the
00072   object in case we want to pass it around.
00073 
00074 - ao2_lock(obj), ao2_unlock(obj), ao2_trylock(obj) can be used
00075   to manipulate the lock associated with the object.
00076 
00077 
00078 \section AstObj2_UsageContainers USAGE - CONTAINERS
00079 
00080 An ao2 container is an abstract data structure where we can store
00081 ao2 objects, search them (hopefully in an efficient way), and iterate
00082 or apply a callback function to them. A container is just an ao2 object
00083 itself.
00084 
00085 A container must first be allocated, specifying the initial
00086 parameters. At the moment, this is done as follows:
00087 
00088     <b>Sample Usage:</b>
00089     \code
00090 
00091     struct ao2_container *c;
00092 
00093     c = ao2_container_alloc(MAX_BUCKETS, my_hash_fn, my_cmp_fn);
00094     \endcode
00095 
00096 where
00097 
00098 - MAX_BUCKETS is the number of buckets in the hash table,
00099 - my_hash_fn() is the (user-supplied) function that returns a
00100   hash key for the object (further reduced modulo MAX_BUCKETS
00101   by the container's code);
00102 - my_cmp_fn() is the default comparison function used when doing
00103   searches on the container,
00104 
00105 A container knows little or nothing about the objects it stores,
00106 other than the fact that they have been created by ao2_alloc().
00107 All knowledge of the (user-defined) internals of the objects
00108 is left to the (user-supplied) functions passed as arguments
00109 to ao2_container_alloc().
00110 
00111 If we want to insert an object in a container, we should
00112 initialize its fields -- especially, those used by my_hash_fn() --
00113 to compute the bucket to use.
00114 Once done, we can link an object to a container with
00115 
00116     ao2_link(c, o);
00117 
00118 The function returns NULL in case of errors (and the object
00119 is not inserted in the container). Other values mean success
00120 (we are not supposed to use the value as a pointer to anything).
00121 Linking an object to a container increases its refcount by 1
00122 automatically.
00123 
00124 \note While an object o is in a container, we expect that
00125 my_hash_fn(o) will always return the same value. The function
00126 does not lock the object to be computed, so modifications of
00127 those fields that affect the computation of the hash should
00128 be done by extracting the object from the container, and
00129 reinserting it after the change (this is not terribly expensive).
00130 
00131 \note A container with a single buckets is effectively a linked
00132 list. However there is no ordering among elements.
00133 
00134 - \ref AstObj2_Containers
00135 - \ref astobj2.h All documentation for functions and data structures
00136 
00137  */
00138 
00139 /*
00140 \note DEBUGGING REF COUNTS BIBLE:
00141 An interface to help debug refcounting is provided
00142 in this package. It is dependent on the REF_DEBUG macro being
00143 defined in a source file, before the #include of astobj2.h,
00144 and in using variants of the normal ao2_xxxx functions
00145 that are named ao2_t_xxxx instead, with an extra argument, a string,
00146 that will be printed out into /tmp/refs when the refcount for an
00147 object is changed.
00148 
00149   these ao2_t_xxxx variants are provided:
00150 
00151 ao2_t_alloc(arg1, arg2, arg3)
00152 ao2_t_ref(arg1,arg2,arg3)
00153 ao2_t_container_alloc(arg1,arg2,arg3,arg4)
00154 ao2_t_link(arg1, arg2, arg3)
00155 ao2_t_unlink(arg1, arg2, arg3)
00156 ao2_t_callback(arg1,arg2,arg3,arg4,arg5)
00157 ao2_t_find(arg1,arg2,arg3,arg4)
00158 ao2_t_iterator_next(arg1, arg2)
00159 
00160 If you study each argument list, you will see that these functions all have
00161 one extra argument that their ao2_xxx counterpart. The last argument in
00162 each case is supposed to be a string pointer, a "tag", that should contain
00163 enough of an explanation, that you can pair operations that increment the
00164 ref count, with operations that are meant to decrement the refcount.
00165 
00166 Each of these calls will generate at least one line of output in /tmp/refs.
00167 These lines look like this:
00168 ...
00169 0x8756f00 =1   chan_sip.c:22240:load_module (allocate users)
00170 0x86e3408 =1   chan_sip.c:22241:load_module (allocate peers)
00171 0x86dd380 =1   chan_sip.c:22242:load_module (allocate peers_by_ip)
00172 0x822d020 =1   chan_sip.c:22243:load_module (allocate dialogs)
00173 0x8930fd8 =1   chan_sip.c:20025:build_peer (allocate a peer struct)
00174 0x8930fd8 +1   chan_sip.c:21467:reload_config (link peer into peer table) [@1]
00175 0x8930fd8 -1   chan_sip.c:2370:unref_peer (unref_peer: from reload_config) [@2]
00176 0x89318b0 =1   chan_sip.c:20025:build_peer (allocate a peer struct)
00177 0x89318b0 +1   chan_sip.c:21467:reload_config (link peer into peer table) [@1]
00178 0x89318b0 -1   chan_sip.c:2370:unref_peer (unref_peer: from reload_config) [@2]
00179 0x8930218 =1   chan_sip.c:20025:build_peer (allocate a peer struct)
00180 0x8930218 +1   chan_sip.c:21539:reload_config (link peer into peers table) [@1]
00181 0x868c040 -1   chan_sip.c:2424:dialog_unlink_all (unset the relatedpeer->call field in tandem with relatedpeer field itself) [@2]
00182 0x868c040 -1   chan_sip.c:2443:dialog_unlink_all (Let's unbump the count in the unlink so the poor pvt can disappear if it is time) [@1]
00183 0x868c040 **call destructor** chan_sip.c:2443:dialog_unlink_all (Let's unbump the count in the unlink so the poor pvt can disappear if it is time)
00184 0x8cc07e8 -1   chan_sip.c:2370:unref_peer (unsetting a dialog relatedpeer field in sip_destroy) [@3]
00185 0x8cc07e8 +1   chan_sip.c:3876:find_peer (ao2_find in peers table) [@2]
00186 0x8cc07e8 -1   chan_sip.c:2370:unref_peer (unref_peer, from sip_devicestate, release ref from find_peer) [@3]
00187 ...
00188 
00189 The first column is the object address.
00190 The second column reflects how the operation affected the ref count
00191     for that object. Creation sets the ref count to 1 (=1).
00192     increment or decrement and amount are specified (-1/+1).
00193 The remainder of the line specifies where in the file the call was made,
00194     and the function name, and the tag supplied in the function call.
00195 
00196 The **call destructor** is specified when the the destroy routine is
00197 run for an object. It does not affect the ref count, but is important
00198 in debugging, because it is possible to have the astobj2 system run it
00199 multiple times on the same object, commonly fatal to asterisk.
00200 
00201 Sometimes you have some helper functions to do object ref/unref
00202 operations. Using these normally hides the place where these
00203 functions were called. To get the location where these functions
00204 were called to appear in /tmp/refs, you can do this sort of thing:
00205 
00206 #ifdef REF_DEBUG
00207 #define dialog_ref(arg1,arg2) dialog_ref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00208 #define dialog_unref(arg1,arg2) dialog_unref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00209 static struct sip_pvt *dialog_ref_debug(struct sip_pvt *p, char *tag, char *file, int line, const char *func)
00210 {
00211    if (p)
00212       ao2_ref_debug(p, 1, tag, file, line, func);
00213    else
00214       ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
00215    return p;
00216 }
00217 
00218 static struct sip_pvt *dialog_unref_debug(struct sip_pvt *p, char *tag, char *file, int line, const char *func)
00219 {
00220    if (p)
00221       ao2_ref_debug(p, -1, tag, file, line, func);
00222    return NULL;
00223 }
00224 #else
00225 static struct sip_pvt *dialog_ref(struct sip_pvt *p, char *tag)
00226 {
00227    if (p)
00228       ao2_ref(p, 1);
00229    else
00230       ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
00231    return p;
00232 }
00233 
00234 static struct sip_pvt *dialog_unref(struct sip_pvt *p, char *tag)
00235 {
00236    if (p)
00237       ao2_ref(p, -1);
00238    return NULL;
00239 }
00240 #endif
00241 
00242 In the above code, note that the "normal" helper funcs call ao2_ref() as
00243 normal, and the "helper" functions call ao2_ref_debug directly with the
00244 file, function, and line number info provided. You might find this
00245 well worth the effort to help track these function calls in the code.
00246 
00247 To find out why objects are not destroyed (a common bug), you can
00248 edit the source file to use the ao2_t_* variants, add the #define REF_DEBUG 1
00249 before the #include "asterisk/astobj2.h" line, and add a descriptive
00250 tag to each call. Recompile, and run Asterisk, exit asterisk with
00251 "stop gracefully", which should result in every object being destroyed.
00252 Then, you can "sort -k 1 /tmp/refs > x1" to get a sorted list of
00253 all the objects, or you can use "util/refcounter" to scan the file
00254 for you and output any problems it finds.
00255 
00256 The above may seem astronomically more work than it is worth to debug
00257 reference counts, which may be true in "simple" situations, but for
00258 more complex situations, it is easily worth 100 times this effort to
00259 help find problems.
00260 
00261 To debug, pair all calls so that each call that increments the
00262 refcount is paired with a corresponding call that decrements the
00263 count for the same reason. Hopefully, you will be left with one
00264 or more unpaired calls. This is where you start your search!
00265 
00266 For instance, here is an example of this for a dialog object in
00267 chan_sip, that was not getting destroyed, after I moved the lines around
00268 to pair operations:
00269 
00270    0x83787a0 =1   chan_sip.c:5733:sip_alloc (allocate a dialog(pvt) struct)
00271    0x83787a0 -1   chan_sip.c:19173:sip_poke_peer (unref dialog at end of sip_poke_peer, obtained from sip_alloc, just before it goes out of scope) [@4]
00272 
00273    0x83787a0 +1   chan_sip.c:5854:sip_alloc (link pvt into dialogs table) [@1]
00274    0x83787a0 -1   chan_sip.c:19150:sip_poke_peer (About to change the callid -- remove the old name) [@3]
00275    0x83787a0 +1   chan_sip.c:19152:sip_poke_peer (Linking in under new name) [@2]
00276    0x83787a0 -1   chan_sip.c:2399:dialog_unlink_all (unlinking dialog via ao2_unlink) [@5]
00277 
00278    0x83787a0 +1   chan_sip.c:19130:sip_poke_peer (copy sip alloc from p to peer->call) [@2]
00279 
00280 
00281    0x83787a0 +1   chan_sip.c:2996:__sip_reliable_xmit (__sip_reliable_xmit: setting pkt->owner) [@3]
00282    0x83787a0 -1   chan_sip.c:2425:dialog_unlink_all (remove all current packets in this dialog, and the pointer to the dialog too as part of __sip_destroy) [@4]
00283 
00284    0x83787a0 +1   chan_sip.c:22356:unload_module (iterate thru dialogs) [@4]
00285    0x83787a0 -1   chan_sip.c:22359:unload_module (toss dialog ptr from iterator_next) [@5]
00286 
00287 
00288    0x83787a0 +1   chan_sip.c:22373:unload_module (iterate thru dialogs) [@3]
00289    0x83787a0 -1   chan_sip.c:22375:unload_module (throw away iterator result) [@2]
00290 
00291    0x83787a0 +1   chan_sip.c:2397:dialog_unlink_all (Let's bump the count in the unlink so it doesn't accidentally become dead before we are done) [@4]
00292    0x83787a0 -1   chan_sip.c:2436:dialog_unlink_all (Let's unbump the count in the unlink so the poor pvt can disappear if it is time) [@3]
00293 
00294 As you can see, only one unbalanced operation is in the list, a ref count increment when
00295 the peer->call was set, but no corresponding decrement was made...
00296 
00297 Hopefully this helps you narrow your search and find those bugs.
00298 
00299 THE ART OF REFERENCE COUNTING
00300 (by Steve Murphy)
00301 SOME TIPS for complicated code, and ref counting:
00302 
00303 1. Theoretically, passing a refcounted object pointer into a function
00304 call is an act of copying the reference, and could be refcounted.
00305 But, upon examination, this sort of refcounting will explode the amount
00306 of code you have to enter, and for no tangible benefit, beyond
00307 creating more possible failure points/bugs. It will even
00308 complicate your code and make debugging harder, slow down your program
00309 doing useless increments and decrements of the ref counts.
00310 
00311 2. It is better to track places where a ref counted pointer
00312 is copied into a structure or stored. Make sure to decrement the refcount
00313 of any previous pointer that might have been there, if setting
00314 this field might erase a previous pointer. ao2_find and iterate_next
00315 internally increment the ref count when they return a pointer, so
00316 you need to decrement the count before the pointer goes out of scope.
00317 
00318 3. Any time you decrement a ref count, it may be possible that the
00319 object will be destroyed (freed) immediately by that call. If you
00320 are destroying a series of fields in a refcounted object, and
00321 any of the unref calls might possibly result in immediate destruction,
00322 you can first increment the count to prevent such behavior, then
00323 after the last test, decrement the pointer to allow the object
00324 to be destroyed, if the refcount would be zero.
00325 
00326 Example:
00327 
00328    dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
00329 
00330    ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink");
00331 
00332    *//* Unlink us from the owner (channel) if we have one *//*
00333    if (dialog->owner) {
00334       if (lockowner)
00335          ast_channel_lock(dialog->owner);
00336       ast_debug(1, "Detaching from channel %s\n", dialog->owner->name);
00337       dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all");
00338       if (lockowner)
00339          ast_channel_unlock(dialog->owner);
00340    }
00341    if (dialog->registry) {
00342       if (dialog->registry->call == dialog)
00343          dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all");
00344       dialog->registry = registry_unref(dialog->registry, "delete dialog->registry");
00345    }
00346     ...
00347    dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
00348 
00349 In the above code, the ao2_t_unlink could end up destroying the dialog
00350 object; if this happens, then the subsequent usages of the dialog
00351 pointer could result in a core dump. So, we 'bump' the
00352 count upwards before beginning, and then decrementing the count when
00353 we are finished. This is analogous to 'locking' or 'protecting' operations
00354 for a short while.
00355 
00356 4. One of the most insidious problems I've run into when converting
00357 code to do ref counted automatic destruction, is in the destruction
00358 routines. Where a "destroy" routine had previously been called to
00359 get rid of an object in non-refcounted code, the new regime demands
00360 that you tear that "destroy" routine into two pieces, one that will
00361 tear down the links and 'unref' them, and the other to actually free
00362 and reset fields. A destroy routine that does any reference deletion
00363 for its own object, will never be called. Another insidious problem
00364 occurs in mutually referenced structures. As an example, a dialog contains
00365 a pointer to a peer, and a peer contains a pointer to a dialog. Watch
00366 out that the destruction of one doesn't depend on the destruction of the
00367 other, as in this case a dependency loop will result in neither being
00368 destroyed!
00369 
00370 Given the above, you should be ready to do a good job!
00371 
00372 murf
00373 
00374 */
00375 
00376 
00377 
00378 /*! \brief
00379  * Typedef for an object destructor. This is called just before freeing
00380  * the memory for the object. It is passed a pointer to the user-defined
00381  * data of the object.
00382  */
00383 typedef void (*ao2_destructor_fn)(void *);
00384 
00385 
00386 /*! \brief
00387  * Allocate and initialize an object.
00388  *
00389  * \param data_size The sizeof() of the user-defined structure.
00390  * \param destructor_fn The destructor function (can be NULL)
00391  * \return A pointer to user-data.
00392  *
00393  * Allocates a struct astobj2 with sufficient space for the
00394  * user-defined structure.
00395  * \note
00396  * - storage is zeroed; XXX maybe we want a flag to enable/disable this.
00397  * - the refcount of the object just created is 1
00398  * - the returned pointer cannot be free()'d or realloc()'ed;
00399  *   rather, we just call ao2_ref(o, -1);
00400  */
00401 
00402 #if defined(REF_DEBUG)
00403 
00404 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) _ao2_alloc_debug((data_size), (destructor_fn), (debug_msg),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
00405 #define ao2_alloc(data_size, destructor_fn)              _ao2_alloc_debug((data_size), (destructor_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
00406 
00407 #elif defined(__AST_DEBUG_MALLOC)
00408 
00409 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) _ao2_alloc_debug((data_size), (destructor_fn), (debug_msg),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
00410 #define ao2_alloc(data_size, destructor_fn)              _ao2_alloc_debug((data_size), (destructor_fn), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
00411 
00412 #else
00413 
00414 #define ao2_t_alloc(arg1,arg2,arg3) _ao2_alloc((arg1), (arg2))
00415 #define ao2_alloc(arg1,arg2)        _ao2_alloc((arg1), (arg2))
00416 
00417 #endif
00418 
00419 void *_ao2_alloc_debug(const size_t data_size, ao2_destructor_fn destructor_fn, char *tag,
00420          const char *file, int line, const char *funcname, int ref_debug);
00421 void *_ao2_alloc(const size_t data_size, ao2_destructor_fn destructor_fn);
00422 
00423 /*! @} */
00424 
00425 
00426 /*! \brief
00427  * Reference/unreference an object and return the old refcount.
00428  *
00429  * \param o A pointer to the object
00430  * \param delta Value to add to the reference counter.
00431  * \return The value of the reference counter before the operation.
00432  *
00433  * Increase/decrease the reference counter according
00434  * the value of delta.
00435  *
00436  * If the refcount goes to zero, the object is destroyed.
00437  *
00438  * \note The object must not be locked by the caller of this function, as
00439  *       it is invalid to try to unlock it after releasing the reference.
00440  *
00441  * \note if we know the pointer to an object, it is because we
00442  * have a reference count to it, so the only case when the object
00443  * can go away is when we release our reference, and it is
00444  * the last one in existence.
00445  */
00446 
00447 #ifdef REF_DEBUG
00448 
00449 #define ao2_t_ref(arg1,arg2,arg3) _ao2_ref_debug((arg1), (arg2), (arg3),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00450 #define ao2_ref(arg1,arg2)        _ao2_ref_debug((arg1), (arg2), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00451 
00452 #else
00453 
00454 #define ao2_t_ref(arg1,arg2,arg3) _ao2_ref((arg1), (arg2))
00455 #define ao2_ref(arg1,arg2)        _ao2_ref((arg1), (arg2))
00456 
00457 #endif
00458 
00459 int _ao2_ref_debug(void *o, int delta, char *tag, char *file, int line, const char *funcname);
00460 int _ao2_ref(void *o, int delta);
00461 
00462 /*! @} */
00463 
00464 /*! \brief
00465  * Lock an object.
00466  *
00467  * \param a A pointer to the object we want to lock.
00468  * \return 0 on success, other values on error.
00469  */
00470 int ao2_lock(void *a);
00471 int _ao2_lock(void *a, const char *file, const char *func, int line, const char *var);
00472 #ifdef DEBUG_THREADS
00473 #define ao2_lock(a) _ao2_lock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00474 #endif
00475 
00476 /*! \brief
00477  * Unlock an object.
00478  *
00479  * \param a A pointer to the object we want unlock.
00480  * \return 0 on success, other values on error.
00481  */
00482 int ao2_unlock(void *a);
00483 int _ao2_unlock(void *a, const char *file, const char *func, int line, const char *var);
00484 #ifdef DEBUG_THREADS
00485 #define ao2_unlock(a) _ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00486 #endif
00487 
00488 /*! \brief
00489  * Try locking-- (don't block if fail)
00490  *
00491  * \param a A pointer to the object we want to lock.
00492  * \return 0 on success, other values on error.
00493  */
00494 int ao2_trylock(void *a);
00495 int _ao2_trylock(void *a, const char *file, const char *func, int line, const char *var);
00496 #ifdef DEBUG_THREADS
00497 #define ao2_trylock(a) _ao2_trylock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00498 #endif
00499 
00500 /*!
00501  * \brief Return the lock address of an object
00502  *
00503  * \param[in] obj A pointer to the object we want.
00504  * \return the address of the lock, else NULL.
00505  *
00506  * This function comes in handy mainly for debugging locking
00507  * situations, where the locking trace code reports the
00508  * lock address, this allows you to correlate against
00509  * object address, to match objects to reported locks.
00510  *
00511  * \since 1.6.1
00512  */
00513 void *ao2_object_get_lockaddr(void *obj);
00514 
00515 /*!
00516  \page AstObj2_Containers AstObj2 Containers
00517 
00518 Containers are data structures meant to store several objects,
00519 and perform various operations on them.
00520 Internally, objects are stored in lists, hash tables or other
00521 data structures depending on the needs.
00522 
00523 \note NOTA BENE: at the moment the only container we support is the
00524    hash table and its degenerate form, the list.
00525 
00526 Operations on container include:
00527 
00528   -  c = \b ao2_container_alloc(size, cmp_fn, hash_fn)
00529    allocate a container with desired size and default compare
00530    and hash function
00531          -The compare function returns an int, which
00532          can be 0 for not found, CMP_STOP to stop end a traversal,
00533          or CMP_MATCH if they are equal
00534          -The hash function returns an int. The hash function
00535          takes two argument, the object pointer and a flags field,
00536 
00537   -  \b ao2_find(c, arg, flags)
00538    returns zero or more element matching a given criteria
00539    (specified as arg). 'c' is the container pointer. Flags
00540     can be:
00541    OBJ_UNLINK - to remove the object, once found, from the container.
00542    OBJ_NODATA - don't return the object if found (no ref count change)
00543    OBJ_MULTIPLE - don't stop at first match (not fully implemented)
00544    OBJ_POINTER - if set, 'arg' is an object pointer, and a hashtable
00545                   search will be done. If not, a traversal is done.
00546 
00547   -  \b ao2_callback(c, flags, fn, arg)
00548    apply fn(obj, arg) to all objects in the container.
00549    Similar to find. fn() can tell when to stop, and
00550    do anything with the object including unlinking it.
00551      - c is the container;
00552       - flags can be
00553         OBJ_UNLINK   - to remove the object, once found, from the container.
00554         OBJ_NODATA   - don't return the object if found (no ref count change)
00555         OBJ_MULTIPLE - don't stop at first match (not fully implemented)
00556         OBJ_POINTER  - if set, 'arg' is an object pointer, and a hashtable
00557                         search will be done. If not, a traversal is done through
00558                         all the hashtable 'buckets'..
00559       - fn is a func that returns int, and takes 3 args:
00560         (void *obj, void *arg, int flags);
00561           obj is an object
00562           arg is the same as arg passed into ao2_callback
00563           flags is the same as flags passed into ao2_callback
00564          fn returns:
00565            0: no match, keep going
00566            CMP_STOP: stop search, no match
00567            CMP_MATCH: This object is matched.
00568 
00569    Note that the entire operation is run with the container
00570    locked, so noone else can change its content while we work on it.
00571    However, we pay this with the fact that doing
00572    anything blocking in the callback keeps the container
00573    blocked.
00574    The mechanism is very flexible because the callback function fn()
00575    can do basically anything e.g. counting, deleting records, etc.
00576    possibly using arg to store the results.
00577 
00578   -  \b iterate on a container
00579    this is done with the following sequence
00580 
00581 \code
00582 
00583        struct ao2_container *c = ... // our container
00584        struct ao2_iterator i;
00585        void *o;
00586 
00587        i = ao2_iterator_init(c, flags);
00588 
00589        while ( (o = ao2_iterator_next(&i)) ) {
00590       ... do something on o ...
00591       ao2_ref(o, -1);
00592        }
00593 
00594        ao2_iterator_destroy(&i);
00595 \endcode
00596 
00597    The difference with the callback is that the control
00598    on how to iterate is left to us.
00599 
00600     - \b ao2_ref(c, -1)
00601    dropping a reference to a container destroys it, very simple!
00602 
00603 Containers are ao2 objects themselves, and this is why their
00604 implementation is simple too.
00605 
00606 Before declaring containers, we need to declare the types of the
00607 arguments passed to the constructor - in turn, this requires
00608 to define callback and hash functions and their arguments.
00609 
00610 - \ref AstObj2
00611 - \ref astobj2.h
00612  */
00613 
00614 /*! \brief
00615  * Type of a generic callback function
00616  * \param obj  pointer to the (user-defined part) of an object.
00617  * \param arg callback argument from ao2_callback()
00618  * \param flags flags from ao2_callback()
00619  *
00620  * The return values are a combination of enum _cb_results.
00621  * Callback functions are used to search or manipulate objects in a container,
00622  */
00623 typedef int (ao2_callback_fn)(void *obj, void *arg, int flags);
00624 
00625 /*! \brief a very common callback is one that matches by address. */
00626 ao2_callback_fn ao2_match_by_addr;
00627 
00628 /*! \brief
00629  * A callback function will return a combination of CMP_MATCH and CMP_STOP.
00630  * The latter will terminate the search in a container.
00631  */
00632 enum _cb_results {
00633    CMP_MATCH   = 0x1,   /*!< the object matches the request */
00634    CMP_STOP = 0x2,   /*!< stop the search now */
00635 };
00636 
00637 /*! \brief
00638  * Flags passed to ao2_callback() and ao2_hash_fn() to modify its behaviour.
00639  */
00640 enum search_flags {
00641    /*! Unlink the object for which the callback function
00642     *  returned CMP_MATCH . This is the only way to extract
00643     *  objects from a container. */
00644    OBJ_UNLINK   = (1 << 0),
00645    /*! On match, don't return the object hence do not increase
00646     *  its refcount. */
00647    OBJ_NODATA   = (1 << 1),
00648    /*! Don't stop at the first match in ao2_callback()
00649     *  \note This is not fully implemented.   Using OBJ_MULTIME with OBJ_NODATA
00650     *  is perfectly fine.  The part that is not implemented is the case where
00651     *  multiple objects should be returned by ao2_callback().
00652     */
00653    OBJ_MULTIPLE = (1 << 2),
00654    /*! obj is an object of the same type as the one being searched for,
00655     *  so use the object's hash function for optimized searching.
00656     *  The search function is unaffected (i.e. use the one passed as
00657     *  argument, or match_by_addr if none specified). */
00658    OBJ_POINTER  = (1 << 3),
00659    /*! 
00660     * \brief Continue if a match is not found in the hashed out bucket
00661     *
00662     * This flag is to be used in combination with OBJ_POINTER.  This tells
00663     * the ao2_callback() core to keep searching through the rest of the
00664     * buckets if a match is not found in the starting bucket defined by
00665     * the hash value on the argument.
00666     */
00667    OBJ_CONTINUE     = (1 << 4),
00668 };
00669 
00670 /*!
00671  * Type of a generic function to generate a hash value from an object.
00672  * flags is ignored at the moment. Eventually, it will include the
00673  * value of OBJ_POINTER passed to ao2_callback().
00674  */
00675 typedef int (ao2_hash_fn)(const void *obj, const int flags);
00676 
00677 /*! \name Object Containers
00678  * Here start declarations of containers.
00679  */
00680 /*@{ */
00681 struct ao2_container;
00682 
00683 /*! \brief
00684  * Allocate and initialize a container
00685  * with the desired number of buckets.
00686  *
00687  * We allocate space for a struct astobj_container, struct container
00688  * and the buckets[] array.
00689  *
00690  * \param n_buckets Number of buckets for hash
00691  * \param hash_fn Pointer to a function computing a hash value.
00692  * \param cmp_fn Pointer to a function comparating key-value
00693  *          with a string. (can be NULL)
00694  * \return A pointer to a struct container.
00695  *
00696  * destructor is set implicitly.
00697  */
00698 
00699 #if defined(REF_DEBUG)
00700 
00701 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) _ao2_container_alloc_debug((arg1), (arg2), (arg3), (arg4),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
00702 #define ao2_container_alloc(arg1,arg2,arg3)        _ao2_container_alloc_debug((arg1), (arg2), (arg3), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
00703 
00704 #elif defined(__AST_DEBUG_MALLOC)
00705 
00706 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) _ao2_container_alloc_debug((arg1), (arg2), (arg3), (arg4),  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
00707 #define ao2_container_alloc(arg1,arg2,arg3)        _ao2_container_alloc_debug((arg1), (arg2), (arg3), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
00708 
00709 #else
00710 
00711 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) _ao2_container_alloc((arg1), (arg2), (arg3))
00712 #define ao2_container_alloc(arg1,arg2,arg3)        _ao2_container_alloc((arg1), (arg2), (arg3))
00713 
00714 #endif
00715 
00716 struct ao2_container *_ao2_container_alloc(const unsigned int n_buckets,
00717                   ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn);
00718 struct ao2_container *_ao2_container_alloc_debug(const unsigned int n_buckets,
00719                    ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn,
00720                    char *tag, char *file, int line, const char *funcname,
00721                    int ref_debug);
00722 
00723 /*! \brief
00724  * Returns the number of elements in a container.
00725  */
00726 int ao2_container_count(struct ao2_container *c);
00727 
00728 /*@} */
00729 
00730 /*! \name Object Management
00731  * Here we have functions to manage objects.
00732  *
00733  * We can use the functions below on any kind of
00734  * object defined by the user.
00735  */
00736 /*@{ */
00737 
00738 /*!
00739  * \brief Add an object to a container.
00740  *
00741  * \param c the container to operate on.
00742  * \param newobj the object to be added.
00743  *
00744  * \retval NULL on errors
00745  * \retval newobj on success.
00746  *
00747  * This function inserts an object in a container according its key.
00748  *
00749  * \note Remember to set the key before calling this function.
00750  *
00751  * \note This function automatically increases the reference count to account
00752  *       for the reference that the container now holds to the object.
00753  */
00754 #ifdef REF_DEBUG
00755 
00756 #define ao2_t_link(arg1, arg2, arg3) _ao2_link_debug((arg1), (arg2), (arg3),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00757 #define ao2_link(arg1, arg2)         _ao2_link_debug((arg1), (arg2), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00758 
00759 #else
00760 
00761 #define ao2_t_link(arg1, arg2, arg3) _ao2_link((arg1), (arg2))
00762 #define ao2_link(arg1, arg2)         _ao2_link((arg1), (arg2))
00763 
00764 #endif
00765 
00766 void *_ao2_link_debug(struct ao2_container *c, void *new_obj, char *tag, char *file, int line, const char *funcname);
00767 void *_ao2_link(struct ao2_container *c, void *newobj);
00768 
00769 /*!
00770  * \brief Remove an object from a container
00771  *
00772  * \param c the container
00773  * \param obj the object to unlink
00774  *
00775  * \retval NULL, always
00776  *
00777  * \note The object requested to be unlinked must be valid.  However, if it turns
00778  *       out that it is not in the container, this function is still safe to
00779  *       be called.
00780  *
00781  * \note If the object gets unlinked from the container, the container's
00782  *       reference to the object will be automatically released. (The
00783  *       refcount will be decremented).
00784  */
00785 #ifdef REF_DEBUG
00786 
00787 #define ao2_t_unlink(arg1, arg2, arg3) _ao2_unlink_debug((arg1), (arg2), (arg3),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00788 #define ao2_unlink(arg1, arg2)         _ao2_unlink_debug((arg1), (arg2), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00789 
00790 #else
00791 
00792 #define ao2_t_unlink(arg1, arg2, arg3) _ao2_unlink((arg1), (arg2))
00793 #define ao2_unlink(arg1, arg2)         _ao2_unlink((arg1), (arg2))
00794 
00795 #endif
00796 
00797 void *_ao2_unlink_debug(struct ao2_container *c, void *obj, char *tag, char *file, int line, const char *funcname);
00798 void *_ao2_unlink(struct ao2_container *c, void *obj);
00799 
00800 
00801 /*! \brief Used as return value if the flag OBJ_MULTIPLE is set */
00802 struct ao2_list {
00803    struct ao2_list *next;
00804    void *obj;  /* pointer to the user portion of the object */
00805 };
00806 
00807 /*@} */
00808 
00809 /*! \brief
00810  * ao2_callback() is a generic function that applies cb_fn() to all objects
00811  * in a container, as described below.
00812  *
00813  * \param c A pointer to the container to operate on.
00814  * \param flags A set of flags specifying the operation to perform,
00815    partially used by the container code, but also passed to
00816    the callback.
00817      - If OBJ_NODATA is set, ao2_callback will return NULL. No refcounts
00818        of any of the traversed objects will be incremented.
00819        On the converse, if it is NOT set (the default), The ref count
00820        of each object for which CMP_MATCH was set will be incremented,
00821        and you will have no way of knowing which those are, until
00822        the multiple-object-return functionality is implemented.
00823      - If OBJ_POINTER is set, the traversed items will be restricted
00824        to the objects in the bucket that the object key hashes to.
00825  * \param cb_fn A function pointer, that will be called on all
00826     objects, to see if they match. This function returns CMP_MATCH
00827     if the object is matches the criteria; CMP_STOP if the traversal
00828     should immediately stop, or both (via bitwise ORing), if you find a
00829     match and want to end the traversal, and 0 if the object is not a match,
00830     but the traversal should continue. This is the function that is applied
00831     to each object traversed. It's arguments are:
00832         (void *obj, void *arg, int flags), where:
00833           obj is an object
00834           arg is the same as arg passed into ao2_callback
00835           flags is the same as flags passed into ao2_callback (flags are
00836            also used by ao2_callback).
00837  * \param arg passed to the callback.
00838  * \return  A pointer to the object found/marked,
00839  *       a pointer to a list of objects matching comparison function,
00840  *       NULL if not found.
00841  *
00842  * If the function returns any objects, their refcount is incremented,
00843  * and the caller is in charge of decrementing them once done.
00844  * Also, in case of multiple values returned, the list used
00845  * to store the objects must be freed by the caller.
00846  *
00847  * Typically, ao2_callback() is used for two purposes:
00848  * - to perform some action (including removal from the container) on one
00849  *   or more objects; in this case, cb_fn() can modify the object itself,
00850  *   and to perform deletion should set CMP_MATCH on the matching objects,
00851  *   and have OBJ_UNLINK set in flags.
00852  * - to look for a specific object in a container; in this case, cb_fn()
00853  *   should not modify the object, but just return a combination of
00854  *   CMP_MATCH and CMP_STOP on the desired object.
00855  * Other usages are also possible, of course.
00856 
00857  * This function searches through a container and performs operations
00858  * on objects according on flags passed.
00859  * XXX describe better
00860  * The comparison is done calling the compare function set implicitly.
00861  * The p pointer can be a pointer to an object or to a key,
00862  * we can say this looking at flags value.
00863  * If p points to an object we will search for the object pointed
00864  * by this value, otherwise we serch for a key value.
00865  * If the key is not uniq we only find the first matching valued.
00866  * If we use the OBJ_MARK flags, we mark all the objects matching
00867  * the condition.
00868  *
00869  * The use of flags argument is the follow:
00870  *
00871  * OBJ_UNLINK     unlinks the object found
00872  * OBJ_NODATA     on match, do return an object
00873  *          Callbacks use OBJ_NODATA as a default
00874  *          functions such as find() do
00875  * OBJ_MULTIPLE      return multiple matches
00876  *          Default for _find() is no.
00877  *          to a key (not yet supported)
00878  * OBJ_POINTER       the pointer is an object pointer
00879  *
00880  * In case we return a list, the callee must take care to destroy
00881  * that list when no longer used.
00882  *
00883  * \note When the returned object is no longer in use, ao2_ref() should
00884  * be used to free the additional reference possibly created by this function.
00885  */
00886 #ifdef REF_DEBUG
00887 
00888 #define ao2_t_callback(arg1,arg2,arg3,arg4,arg5) _ao2_callback_debug((arg1), (arg2), (arg3), (arg4), (arg5),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00889 #define ao2_callback(arg1,arg2,arg3,arg4)        _ao2_callback_debug((arg1), (arg2), (arg3), (arg4), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00890 
00891 #else
00892 
00893 #define ao2_t_callback(arg1,arg2,arg3,arg4,arg5) _ao2_callback((arg1), (arg2), (arg3), (arg4))
00894 #define ao2_callback(arg1,arg2,arg3,arg4)        _ao2_callback((arg1), (arg2), (arg3), (arg4))
00895 
00896 #endif
00897 
00898 void *_ao2_callback_debug(struct ao2_container *c, enum search_flags flags,
00899                     ao2_callback_fn *cb_fn, void *arg, char *tag,
00900                     char *file, int line, const char *funcname);
00901 void *_ao2_callback(struct ao2_container *c,
00902                enum search_flags flags,
00903                ao2_callback_fn *cb_fn, void *arg);
00904 
00905 /*! ao2_find() is a short hand for ao2_callback(c, flags, c->cmp_fn, arg)
00906  * XXX possibly change order of arguments ?
00907  */
00908 #ifdef REF_DEBUG
00909 
00910 #define ao2_t_find(arg1,arg2,arg3,arg4) _ao2_find_debug((arg1), (arg2), (arg3), (arg4),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00911 #define ao2_find(arg1,arg2,arg3)        _ao2_find_debug((arg1), (arg2), (arg3), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
00912 
00913 #else
00914 
00915 #define ao2_t_find(arg1,arg2,arg3,arg4) _ao2_find((arg1), (arg2), (arg3))
00916 #define ao2_find(arg1,arg2,arg3)        _ao2_find((arg1), (arg2), (arg3))
00917 
00918 #endif
00919 
00920 void *_ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname);
00921 void *_ao2_find(struct ao2_container *c, void *arg, enum search_flags flags);
00922 
00923 /*! \brief
00924  *
00925  *
00926  * When we need to walk through a container, we use an
00927  * ao2_iterator to keep track of the current position.
00928  *
00929  * Because the navigation is typically done without holding the
00930  * lock on the container across the loop, objects can be inserted or deleted
00931  * or moved while we work. As a consequence, there is no guarantee that
00932  * we manage to touch all the elements in the container, and it is possible
00933  * that we touch the same object multiple times.
00934  *
00935  * However, within the current hash table container, the following is true:
00936  *  - It is not possible to miss an object in the container while iterating
00937  *    unless it gets added after the iteration begins and is added to a bucket
00938  *    that is before the one the current object is in.  In this case, even if
00939  *    you locked the container around the entire iteration loop, you still would
00940  *    not see this object, because it would still be waiting on the container
00941  *    lock so that it can be added.
00942  *  - It would be extremely rare to see an object twice.  The only way this can
00943  *    happen is if an object got unlinked from the container and added again
00944  *    during the same iteration.  Furthermore, when the object gets added back,
00945  *    it has to be in the current or later bucket for it to be seen again.
00946  *
00947  * An iterator must be first initialized with ao2_iterator_init(),
00948  * then we can use o = ao2_iterator_next() to move from one
00949  * element to the next. Remember that the object returned by
00950  * ao2_iterator_next() has its refcount incremented,
00951  * and the reference must be explicitly released when done with it.
00952  *
00953  * In addition, ao2_iterator_init() will hold a reference to the container
00954  * being iterated, which will be freed when ao2_iterator_destroy() is called
00955  * to free up the resources used by the iterator (if any).
00956  *
00957  * Example:
00958  *
00959  *  \code
00960  *
00961  *  struct ao2_container *c = ... // the container we want to iterate on
00962  *  struct ao2_iterator i;
00963  *  struct my_obj *o;
00964  *
00965  *  i = ao2_iterator_init(c, flags);
00966  *
00967  *  while ( (o = ao2_iterator_next(&i)) ) {
00968  *     ... do something on o ...
00969  *     ao2_ref(o, -1);
00970  *  }
00971  *
00972  *  ao2_iterator_destroy(&i);
00973  *
00974  *  \endcode
00975  *
00976  */
00977 
00978 /*! \brief
00979  * The astobj2 iterator
00980  *
00981  * \note You are not supposed to know the internals of an iterator!
00982  * We would like the iterator to be opaque, unfortunately
00983  * its size needs to be known if we want to store it around
00984  * without too much trouble.
00985  * Anyways...
00986  * The iterator has a pointer to the container, and a flags
00987  * field specifying various things e.g. whether the container
00988  * should be locked or not while navigating on it.
00989  * The iterator "points" to the current object, which is identified
00990  * by three values:
00991  *
00992  * - a bucket number;
00993  * - the object_id, which is also the container version number
00994  *   when the object was inserted. This identifies the object
00995  *   uniquely, however reaching the desired object requires
00996  *   scanning a list.
00997  * - a pointer, and a container version when we saved the pointer.
00998  *   If the container has not changed its version number, then we
00999  *   can safely follow the pointer to reach the object in constant time.
01000  *
01001  * Details are in the implementation of ao2_iterator_next()
01002  * A freshly-initialized iterator has bucket=0, version=0.
01003  */
01004 struct ao2_iterator {
01005    /*! the container */
01006    struct ao2_container *c;
01007    /*! operation flags */
01008    int flags;
01009    /*! current bucket */
01010    int bucket;
01011    /*! container version */
01012    unsigned int c_version;
01013    /*! pointer to the current object */
01014    void *obj;
01015    /*! container version when the object was created */
01016    unsigned int version;
01017 };
01018 
01019 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior
01020  * of the iterator.
01021  */
01022 enum ao2_iterator_flags {
01023    /*! Prevents ao2_iterator_next() from locking the container
01024     * while retrieving the next object from it.
01025     */
01026    AO2_ITERATOR_DONTLOCK = (1 << 0),
01027 };
01028 
01029 /*!
01030  * \brief Create an iterator for a container
01031  *
01032  * \param c the container
01033  * \param flags one or more flags from ao2_iterator_flags
01034  *
01035  * \retval the constructed iterator
01036  *
01037  * \note This function does \b not take a pointer to an iterator;
01038  *       rather, it returns an iterator structure that should be
01039  *       assigned to (overwriting) an existing iterator structure
01040  *       allocated on the stack or on the heap.
01041  *
01042  * This function will take a reference on the container being iterated.
01043  *
01044  */
01045 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags);
01046 
01047 /*!
01048  * \brief Destroy a container iterator
01049  *
01050  * \param i the iterator to destroy
01051  *
01052  * \retval none
01053  *
01054  * This function will release the container reference held by the iterator
01055  * and any other resources it may be holding.
01056  *
01057  */
01058 void ao2_iterator_destroy(struct ao2_iterator *i);
01059 
01060 #ifdef REF_DEBUG
01061 
01062 #define ao2_t_iterator_next(arg1, arg2) _ao2_iterator_next_debug((arg1), (arg2),  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01063 #define ao2_iterator_next(arg1)         _ao2_iterator_next_debug((arg1), "",  __FILE__, __LINE__, __PRETTY_FUNCTION__)
01064 
01065 #else
01066 
01067 #define ao2_t_iterator_next(arg1, arg2) _ao2_iterator_next((arg1))
01068 #define ao2_iterator_next(arg1)         _ao2_iterator_next((arg1))
01069 
01070 #endif
01071 
01072 void *_ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname);
01073 void *_ao2_iterator_next(struct ao2_iterator *a);
01074 
01075 /* extra functions */
01076 void ao2_bt(void);   /* backtrace */
01077 
01078 #endif /* _ASTERISK_ASTOBJ2_H */

Generated on Wed Aug 18 22:33:44 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7