Thu Jul 9 13:40:23 2009

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

Generated on Thu Jul 9 13:40:23 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7