Fri Jan 29 14:25:10 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  *
00024  * \brief Object Model implementing objects and containers.
00025 
00026 These functions implement an abstraction for objects (with
00027 locks and reference counts) and containers for these user-defined objects,
00028 supporting locking, reference counting and callbacks.
00029 
00030 The internal implementation of the container is opaque to the user,
00031 so we can use different data structures as needs arise.
00032 
00033 At the moment, however, the only internal data structure is a hash
00034 table. When other structures will be implemented, the initialization
00035 function may change.
00036 
00037 USAGE - OBJECTS
00038 
00039 An object is a block of memory that must be allocated with the
00040 function ao2_alloc(), and for which the system keeps track (with
00041 abit of help from the programmer) of the number of references around.
00042 When an object has no more references, it is destroyed, by first
00043 invoking whatever 'destructor' function the programmer specifies
00044 (it can be NULL), and then freeing the memory.
00045 This way objects can be shared without worrying who is in charge
00046 of freeing them.
00047 
00048 Basically, creating an object requires the size of the object and
00049 and a pointer to the destructor function:
00050  
00051     struct foo *o;
00052  
00053     o = ao2_alloc(sizeof(struct foo), my_destructor_fn);
00054 
00055 The object returned has a refcount = 1.
00056 Note that the memory for the object is allocated and zeroed.
00057 - We cannot realloc() the object itself.
00058 - We cannot call free(o) to dispose of the object; rather we
00059   tell the system that we do not need the reference anymore:
00060 
00061     ao2_ref(o, -1)
00062 
00063   causing the destructor to be called (and then memory freed) when
00064   the refcount goes to 0. This is also available as ao2_unref(o),
00065   and returns NULL as a convenience, so you can do things like
00066    o = ao2_unref(o);
00067   and clean the original pointer to prevent errors.
00068 
00069 - ao2_ref(o, +1) can be used to modify the refcount on the
00070   object in case we want to pass it around.
00071    
00072 
00073 - other calls on the object are ao2_lock(obj), ao2_unlock(),
00074   ao2_trylock(), to manipulate the lock.
00075 
00076 
00077 USAGE - CONTAINERS
00078 
00079 A containers is an abstract data structure where we can store
00080 objects, search them (hopefully in an efficient way), and iterate
00081 or apply a callback function to them. A container is just an object
00082 itself.
00083 
00084 A container must first be allocated, specifying the initial
00085 parameters. At the moment, this is done as follows:
00086 
00087     <b>Sample Usage:</b>
00088     \code
00089 
00090     struct ao2_container *c;
00091 
00092     c = ao2_container_alloc(MAX_BUCKETS, my_hash_fn, my_cmp_fn);
00093 
00094 where
00095 - MAX_BUCKETS is the number of buckets in the hash table,
00096 - my_hash_fn() is the (user-supplied) function that returns a
00097   hash key for the object (further reduced moduly MAX_BUCKETS
00098   by the container's code);
00099 - my_cmp_fn() is the default comparison function used when doing
00100   searches on the container,
00101 
00102 A container knows little or nothing about the object itself,
00103 other than the fact that it has been created by ao2_alloc()
00104 All knowledge of the (user-defined) internals of the object
00105 is left to the (user-supplied) functions passed as arguments
00106 to ao2_container_alloc().
00107 
00108 If we want to insert the object in the container, we should
00109 initialize its fields -- especially, those used by my_hash_fn() --
00110 to compute the bucket to use.
00111 Once done, we can link an object to a container with
00112 
00113     ao2_link(c, o);
00114 
00115 The function returns NULL in case of errors (and the object
00116 is not inserted in the container). Other values mean success
00117 (we are not supposed to use the value as a pointer to anything).
00118 
00119 \note While an object o is in a container, we expect that
00120 my_hash_fn(o) will always return the same value. The function
00121 does not lock the object to be computed, so modifications of
00122 those fields that affect the computation of the hash should
00123 be done by extractiong the object from the container, and
00124 reinserting it after the change (this is not terribly expensive).
00125 
00126 \note A container with a single buckets is effectively a linked
00127 list. However there is no ordering among elements.
00128 
00129 Objects implement a reference counter keeping the count
00130 of the number of references that reference an object.
00131 
00132 When this number becomes zero the destructor will be
00133 called and the object will be free'd.
00134  */
00135 
00136 /*!
00137  * Invoked just before freeing the memory for the object.
00138  * It is passed a pointer to user data.
00139  */
00140 typedef void (*ao2_destructor_fn)(void *);
00141 
00142 void ao2_bt(void);   /* backtrace */
00143 /*!
00144  * Allocate and initialize an object.
00145  * 
00146  * \param data_size The sizeof() of user-defined structure.
00147  * \param destructor_fn The function destructor (can be NULL)
00148  * \return A pointer to user data. 
00149  *
00150  * Allocates a struct astobj2 with sufficient space for the
00151  * user-defined structure.
00152  * \notes:
00153  * - storage is zeroed; XXX maybe we want a flag to enable/disable this.
00154  * - the refcount of the object just created is 1
00155  * - the returned pointer cannot be free()'d or realloc()'ed;
00156  *   rather, we just call ao2_ref(o, -1);
00157  */
00158 void *ao2_alloc(const size_t data_size, ao2_destructor_fn destructor_fn);
00159 
00160 /*!
00161  * Reference/unreference an object and return the old refcount.
00162  *
00163  * \param o A pointer to the object
00164  * \param delta Value to add to the reference counter.
00165  * \return The value of the reference counter before the operation.
00166  *
00167  * Increase/decrease the reference counter according
00168  * the value of delta.
00169  *
00170  * If the refcount goes to zero, the object is destroyed.
00171  *
00172  * \note The object must not be locked by the caller of this function, as
00173  *       it is invalid to try to unlock it after releasing the reference.
00174  *
00175  * \note if we know the pointer to an object, it is because we
00176  * have a reference count to it, so the only case when the object
00177  * can go away is when we release our reference, and it is
00178  * the last one in existence.
00179  */
00180 int ao2_ref(void *o, int delta);
00181 
00182 /*!
00183  * Lock an object.
00184  * 
00185  * \param a A pointer to the object we want lock.
00186  * \return 0 on success, other values on error.
00187  */
00188 #ifndef DEBUG_THREADS
00189 int ao2_lock(void *a);
00190 #else
00191 #define ao2_lock(a) __ao2_lock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00192 int __ao2_lock(void *a, const char *file, const char *func, int line, const char *var);
00193 #endif
00194 
00195 #ifndef DEBUG_THREADS
00196 int ao2_trylock(void *a);
00197 #else
00198 #define ao2_trylock(a) __ao2_trylock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00199 int __ao2_trylock(void *a, const char *file, const char *func, int line, const char *var);
00200 #endif
00201 
00202 /*!
00203  * Unlock an object.
00204  * 
00205  * \param a A pointer to the object we want unlock.
00206  * \return 0 on success, other values on error.
00207  */
00208 #ifndef DEBUG_THREADS
00209 int ao2_unlock(void *a);
00210 #else
00211 #define ao2_unlock(a) __ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
00212 int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var);
00213 #endif
00214 
00215 /*!
00216  *
00217  * Containers
00218 
00219 containers are data structures meant to store several objects,
00220 and perform various operations on them.
00221 Internally, objects are stored in lists, hash tables or other
00222 data structures depending on the needs.
00223 
00224 NOTA BENE: at the moment the only container we support is the
00225 hash table and its degenerate form, the list.
00226 
00227 Operations on container include:
00228 
00229     c = ao2_container_alloc(size, cmp_fn, hash_fn)
00230    allocate a container with desired size and default compare
00231    and hash function
00232 
00233     ao2_find(c, arg, flags)
00234    returns zero or more element matching a given criteria
00235    (specified as arg). Flags indicate how many results we
00236    want (only one or all matching entries), and whether we
00237    should unlink the object from the container.
00238 
00239     ao2_callback(c, flags, fn, arg)
00240    apply fn(obj, arg) to all objects in the container.
00241    Similar to find. fn() can tell when to stop, and
00242    do anything with the object including unlinking it.
00243    Note that the entire operation is run with the container
00244    locked, so noone else can change its content while we work on it.
00245    However, we pay this with the fact that doing
00246    anything blocking in the callback keeps the container
00247    blocked.
00248    The mechanism is very flexible because the callback function fn()
00249    can do basically anything e.g. counting, deleting records, etc.
00250    possibly using arg to store the results.
00251    
00252     iterate on a container
00253    this is done with the following sequence
00254 
00255        struct ao2_container *c = ... // our container
00256        struct ao2_iterator i;
00257        void *o;
00258 
00259        i = ao2_iterator_init(c, flags);
00260      
00261        while ( (o = ao2_iterator_next(&i)) ) {
00262       ... do something on o ...
00263       ao2_ref(o, -1);
00264        }
00265 
00266        ao2_iterator_destroy(&i);
00267 
00268    The difference with the callback is that the control
00269    on how to iterate is left to us.
00270 
00271     ao2_ref(c, -1)
00272    dropping a reference to a container destroys it, very simple!
00273  
00274 Containers are astobj2 object themselves, and this is why their
00275 implementation is simple too.
00276 
00277  */
00278 
00279 /*!
00280  * We can perform different operation on an object. We do this
00281  * according the following flags.
00282  */
00283 enum search_flags {
00284    /*! unlink the object found */
00285    OBJ_UNLINK   = (1 << 0),
00286    /*! on match, don't return the object or increase its reference count. */
00287    OBJ_NODATA   = (1 << 1),
00288    /*! don't stop at the first match 
00289     *  \note This is not fully implemented. */
00290    OBJ_MULTIPLE = (1 << 2),
00291    /*! obj is an object of the same type as the one being searched for.
00292     *  This implies that it can be passed to the object's hash function
00293     *  for optimized searching. */
00294    OBJ_POINTER  = (1 << 3),
00295    /*! 
00296     * \brief Continue if a match is not found in the hashed out bucket
00297     *
00298     * This flag is to be used in combination with OBJ_POINTER.  This tells
00299     * the ao2_callback() core to keep searching through the rest of the
00300     * buckets if a match is not found in the starting bucket defined by
00301     * the hash value on the argument.
00302     */
00303    OBJ_CONTINUE = (1 << 4),
00304 
00305 };
00306 
00307 /*!
00308  * Type of a generic function to generate a hash value from an object.
00309  *
00310  */
00311 typedef int (*ao2_hash_fn)(const void *obj, const int flags);
00312 
00313 /*!
00314  * valid callback results:
00315  * We return a combination of
00316  * CMP_MATCH when the object matches the request,
00317  * and CMP_STOP when we should not continue the search further.
00318  */
00319 enum _cb_results {
00320    CMP_MATCH   = 0x1,
00321    CMP_STOP = 0x2,
00322 };
00323 
00324 /*!
00325  * generic function to compare objects.
00326  * This, as other callbacks, should return a combination of
00327  * _cb_results as described above.
00328  *
00329  * \param o object from container
00330  * \param arg  search parameters (directly from ao2_find)
00331  * \param flags   passed directly from ao2_find
00332  * XXX explain.
00333  */
00334 
00335 /*!
00336  * Type of a generic callback function
00337  * \param obj  pointer to the (user-defined part) of an object.
00338  * \param arg callback argument from ao2_callback()
00339  * \param flags flags from ao2_callback()
00340  * The return values are the same as a compare function.
00341  * In fact, they are the same thing.
00342  */
00343 typedef int (*ao2_callback_fn)(void *obj, void *arg, int flags);
00344 
00345 /*!
00346  * Here start declarations of containers.
00347  */
00348 struct ao2_container;
00349 
00350 /*!
00351  * Allocate and initialize a container 
00352  * with the desired number of buckets.
00353  * 
00354  * We allocate space for a struct astobj_container, struct container
00355  * and the buckets[] array.
00356  *
00357  * \param my_hash_fn Pointer to a function computing a hash value.
00358  * \param my_cmp_fn Pointer to a function comparating key-value 
00359  *          with a string. (can be NULL)
00360  * \return A pointer to a struct container.
00361  *
00362  * destructor is set implicitly.
00363  */
00364 struct ao2_container *ao2_container_alloc(const unsigned int n_buckets,
00365       ao2_hash_fn hash_fn, ao2_callback_fn cmp_fn);
00366 
00367 /*!
00368  * Returns the number of elements in a container.
00369  */
00370 int ao2_container_count(struct ao2_container *c);
00371 
00372 /*
00373  * Here we have functions to manage objects.
00374  *
00375  * We can use the functions below on any kind of 
00376  * object defined by the user.
00377  */
00378 
00379 /*!
00380  * \brief Add an object to a container.
00381  *
00382  * \param c the container to operate on.
00383  * \param newobj the object to be added.
00384  *
00385  * \return NULL on errors, other values on success.
00386  *
00387  * This function inserts an object in a container according its key.
00388  *
00389  * \note Remember to set the key before calling this function.
00390  *
00391  * \note This function automatically increases the reference count to
00392  *       account for the reference to the object that the container now holds.
00393  *
00394  * For Asterisk 1.4 only, there is a dirty hack here to ensure that chan_iax2
00395  * can have objects linked in to the container at the head instead of tail
00396  * when it is just a linked list.  This is to maintain some existing behavior
00397  * where the order must be maintained as it was before this conversion so that
00398  * matching behavior doesn't change.
00399  */
00400 #define ao2_link(c, o) __ao2_link(c, o, 0)
00401 void *__ao2_link(struct ao2_container *c, void *newobj, int iax2_hack);
00402 
00403 /*!
00404  * \brief Remove an object from the container
00405  *
00406  * \arg c the container
00407  * \arg obj the object to unlink
00408  *
00409  * \retval NULL, always
00410  *
00411  * \note The object requested to be unlinked must be valid.  However, if it turns
00412  *       out that it is not in the container, this function is still safe to
00413  *       be called.
00414  *
00415  * \note If the object gets unlinked from the container, the container's
00416  *       reference to the object will be automatically released.
00417  */
00418 void *ao2_unlink(struct ao2_container *c, void *obj);
00419 
00420 /*! \struct Used as return value if the flag OBJ_MULTIPLE is set */
00421 struct ao2_list {
00422    struct ao2_list *next;
00423    void *obj;  /* pointer to the user portion of the object */
00424 };
00425 
00426 /*!
00427  * ao2_callback() and astob2_find() are the same thing with only one difference:
00428  * the latter uses as a callback the function passed as my_cmp_f() at
00429  * the time of the creation of the container.
00430  * 
00431  * \param c A pointer to the container to operate on.
00432  * \param arg passed to the callback.
00433  * \param flags A set of flags specifying the operation to perform,
00434    partially used by the container code, but also passed to
00435    the callback.
00436  * \return  A pointer to the object found/marked, 
00437  *       a pointer to a list of objects matching comparison function,
00438  *       NULL if not found.
00439  * If the function returns any objects, their refcount is incremented,
00440  * and the caller is in charge of decrementing them once done.
00441  * Also, in case of multiple values returned, the list used
00442  * to store the objects must be freed by the caller.
00443  *
00444  * This function searches through a container and performs operations
00445  * on objects according on flags passed.
00446  * XXX describe better
00447  * The comparison is done calling the compare function set implicitly. 
00448  * The p pointer can be a pointer to an object or to a key, 
00449  * we can say this looking at flags value.
00450  * If p points to an object we will search for the object pointed
00451  * by this value, otherwise we serch for a key value.
00452  * If the key is not uniq we only find the first matching valued.
00453  * If we use the OBJ_MARK flags, we mark all the objects matching 
00454  * the condition.
00455  *
00456  * The use of flags argument is the follow:
00457  *
00458  * OBJ_UNLINK     unlinks the object found
00459  * OBJ_NODATA     on match, do return an object
00460  *          Callbacks use OBJ_NODATA as a default
00461  *          functions such as find() do
00462  * OBJ_MULTIPLE      return multiple matches
00463  *          Default for _find() is no.
00464  *          to a key (not yet supported)
00465  * OBJ_POINTER       the pointer is an object pointer
00466  *
00467  * In case we return a list, the callee must take care to destroy 
00468  * that list when no longer used.
00469  *
00470  * \note When the returned object is no longer in use, ao2_ref() should
00471  * be used to free the additional reference possibly created by this function.
00472  */
00473 /* XXX order of arguments to find */
00474 void *ao2_find(struct ao2_container *c, void *arg, enum search_flags flags);
00475 void *ao2_callback(struct ao2_container *c,
00476    enum search_flags flags,
00477    ao2_callback_fn cb_fn, void *arg);
00478 
00479 int ao2_match_by_addr(void *user_data, void *arg, int flags);
00480 /*!
00481  *
00482  *
00483  * When we need to walk through a container, we use
00484  * ao2_iterator to keep track of the current position.
00485  * 
00486  * Because the navigation is typically done without holding the
00487  * lock on the container across the loop,
00488  * objects can be inserted or deleted or moved
00489  * while we work. As a consequence, there is no guarantee that
00490  * the we manage to touch all the elements on the list, or it
00491  * is possible that we touch the same object multiple times.
00492  * However, within the current hash table container, the following is true:
00493  *  - It is not possible to miss an object in the container while iterating
00494  *    unless it gets added after the iteration begins and is added to a bucket
00495  *    that is before the one the current object is in.  In this case, even if
00496  *    you locked the container around the entire iteration loop, you still would
00497  *    not see this object, because it would still be waiting on the container
00498  *    lock so that it can be added.
00499  *  - It would be extremely rare to see an object twice.  The only way this can
00500  *    happen is if an object got unlinked from the container and added again 
00501  *    during the same iteration.  Furthermore, when the object gets added back,
00502  *    it has to be in the current or later bucket for it to be seen again.
00503  *
00504  * An iterator must be first initialized with ao2_iterator_init(),
00505  * then we can use o = ao2_iterator_next() to move from one
00506  * element to the next. Remember that the object returned by
00507  * ao2_iterator_next() has its refcount incremented,
00508  * and the reference must be explicitly released when done with it.
00509  *
00510  * In addition, ao2_iterator_init() will hold a reference to the container
00511  * being iterated, which will be freed when ao2_iterator_destroy() is called
00512  * to free up the resources used by the iterator (if any).
00513  *
00514  * Example:
00515  *
00516  *  \code
00517  *
00518  *  struct ao2_container *c = ... // the container we want to iterate on
00519  *  struct ao2_iterator i;
00520  *  struct my_obj *o;
00521  *
00522  *  i = ao2_iterator_init(c, flags);
00523  *
00524  *  while ( (o = ao2_iterator_next(&i)) ) {
00525  *     ... do something on o ...
00526  *     ao2_ref(o, -1);
00527  *  }
00528  *
00529  *  ao2_iterator_destroy(&i);
00530  *
00531  *  \endcode
00532  *
00533  */
00534 
00535 /*!
00536  * You are not supposed to know the internals of an iterator!
00537  * We would like the iterator to be opaque, unfortunately
00538  * its size needs to be known if we want to store it around
00539  * without too much trouble.
00540  * Anyways...
00541  * The iterator has a pointer to the container, and a flags
00542  * field specifying various things e.g. whether the container
00543  * should be locked or not while navigating on it.
00544  * The iterator "points" to the current object, which is identified
00545  * by three values:
00546  * - a bucket number;
00547  * - the object_id, which is also the container version number
00548  *   when the object was inserted. This identifies the object
00549  *   uniquely, however reaching the desired object requires
00550  *   scanning a list.
00551  * - a pointer, and a container version when we saved the pointer.
00552  *   If the container has not changed its version number, then we
00553  *   can safely follow the pointer to reach the object in constant time.
00554  * Details are in the implementation of ao2_iterator_next()
00555  * A freshly-initialized iterator has bucket=0, version=0.
00556  */
00557 
00558 struct ao2_iterator {
00559    /*! the container */
00560    struct ao2_container *c;
00561    /*! operation flags */
00562    int flags;
00563    /*! current bucket */
00564    int bucket;
00565    /*! container version */
00566    unsigned int c_version;
00567    /*! pointer to the current object */
00568    void *obj;
00569    /*! container version when the object was created */
00570    unsigned int version;
00571 };
00572 
00573 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior
00574  * of the iterator.
00575  */
00576 enum ao2_iterator_flags {
00577    /*! Prevents ao2_iterator_next() from locking the container
00578     * while retrieving the next object from it.
00579     */
00580    AO2_ITERATOR_DONTLOCK = (1 << 0),
00581 };
00582 
00583 /*!
00584  * \brief Create an iterator for a container
00585  *
00586  * \param c the container
00587  * \param flags one or more flags from ao2_iterator_flags
00588  *
00589  * \retval the constructed iterator
00590  *
00591  * \note This function does \b not take a pointer to an iterator;
00592  *       rather, it returns an iterator structure that should be
00593  *       assigned to (overwriting) an existing iterator structure
00594  *       allocated on the stack or on the heap.
00595  *
00596  * This function will take a reference on the container being iterated.
00597  *
00598  */
00599 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags);
00600 
00601 /*!
00602  * \brief Destroy a container iterator
00603  *
00604  * \param i the iterator to destroy
00605  *
00606  * \retval none
00607  *
00608  * This function will release the container reference held by the iterator
00609  * and any other resources it may be holding.
00610  *
00611  */
00612 void ao2_iterator_destroy(struct ao2_iterator *i);
00613 
00614 void *ao2_iterator_next(struct ao2_iterator *a);
00615 
00616 #endif /* _ASTERISK_ASTOBJ2_H */

Generated on Fri Jan 29 14:25:10 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7