Fri Jun 19 12:09:31 2009

Asterisk developer's documentation


astobj2.c

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 /*
00018  * Function implementing astobj2 objects.
00019  */
00020 #include "asterisk.h"
00021 
00022 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 175123 $")
00023 
00024 #include "asterisk/_private.h"
00025 #include "asterisk/astobj2.h"
00026 #include "asterisk/utils.h"
00027 #include "asterisk/cli.h"
00028 #define REF_FILE "/tmp/refs"
00029 
00030 /*!
00031  * astobj2 objects are always preceded by this data structure,
00032  * which contains a lock, a reference counter,
00033  * the flags and a pointer to a destructor.
00034  * The refcount is used to decide when it is time to
00035  * invoke the destructor.
00036  * The magic number is used for consistency check.
00037  * XXX the lock is not always needed, and its initialization may be
00038  * expensive. Consider making it external.
00039  */
00040 struct __priv_data {
00041    ast_mutex_t lock;
00042    int ref_counter;
00043    ao2_destructor_fn destructor_fn;
00044    /*! for stats */
00045    size_t data_size;
00046    /*! magic number.  This is used to verify that a pointer passed in is a
00047     *  valid astobj2 */
00048    uint32_t magic;
00049 };
00050 
00051 #define  AO2_MAGIC   0xa570b123
00052 
00053 /*!
00054  * What an astobj2 object looks like: fixed-size private data
00055  * followed by variable-size user data.
00056  */
00057 struct astobj2 {
00058    struct __priv_data priv_data;
00059    void *user_data[0];
00060 };
00061 
00062 #ifdef AST_DEVMODE
00063 #define AO2_DEBUG 1
00064 #endif
00065 
00066 #ifdef AO2_DEBUG
00067 struct ao2_stats {
00068    volatile int total_objects;
00069    volatile int total_mem;
00070    volatile int total_containers;
00071    volatile int total_refs;
00072    volatile int total_locked;
00073 };
00074 
00075 static struct ao2_stats ao2;
00076 #endif
00077 
00078 #ifndef HAVE_BKTR /* backtrace support */
00079 void ao2_bt(void) {}
00080 #else
00081 #include <execinfo.h>    /* for backtrace */
00082 
00083 void ao2_bt(void)
00084 {
00085    int c, i;
00086 #define N1  20
00087    void *addresses[N1];
00088    char **strings;
00089 
00090    c = backtrace(addresses, N1);
00091    strings = backtrace_symbols(addresses,c);
00092    ast_verbose("backtrace returned: %d\n", c);
00093    for(i = 0; i < c; i++) {
00094       ast_verbose("%d: %p %s\n", i, addresses[i], strings[i]);
00095    }
00096    free(strings);
00097 }
00098 #endif
00099 
00100 /*!
00101  * \brief convert from a pointer _p to a user-defined object
00102  *
00103  * \return the pointer to the astobj2 structure
00104  */
00105 static inline struct astobj2 *INTERNAL_OBJ(void *user_data)
00106 {
00107    struct astobj2 *p;
00108 
00109    if (!user_data) {
00110       ast_log(LOG_ERROR, "user_data is NULL\n");
00111       return NULL;
00112    }
00113 
00114    p = (struct astobj2 *) ((char *) user_data - sizeof(*p));
00115    if (AO2_MAGIC != (p->priv_data.magic) ) {
00116       ast_log(LOG_ERROR, "bad magic number 0x%x for %p\n", p->priv_data.magic, p);
00117       p = NULL;
00118    }
00119 
00120    return p;
00121 }
00122 
00123 /*!
00124  * \brief convert from a pointer _p to an astobj2 object
00125  *
00126  * \return the pointer to the user-defined portion.
00127  */
00128 #define EXTERNAL_OBJ(_p)   ((_p) == NULL ? NULL : (_p)->user_data)
00129 
00130 /* the underlying functions common to debug and non-debug versions */
00131 
00132 static int __ao2_ref(void *user_data, const int delta);
00133 static void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn);
00134 static struct ao2_container *__ao2_container_alloc(struct ao2_container *c, const uint n_buckets, ao2_hash_fn *hash_fn,
00135                                  ao2_callback_fn *cmp_fn);
00136 static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data);
00137 static void *__ao2_callback(struct ao2_container *c,
00138    const enum search_flags flags, ao2_callback_fn *cb_fn, void *arg,
00139                 char *tag, char *file, int line, const char *funcname);
00140 static void * __ao2_iterator_next(struct ao2_iterator *a, struct bucket_list **q);
00141 
00142 #ifndef DEBUG_THREADS
00143 int ao2_lock(void *user_data)
00144 #else
00145 int _ao2_lock(void *user_data, const char *file, const char *func, int line, const char *var)
00146 #endif
00147 {
00148    struct astobj2 *p = INTERNAL_OBJ(user_data);
00149 
00150    if (p == NULL)
00151       return -1;
00152 
00153 #ifdef AO2_DEBUG
00154    ast_atomic_fetchadd_int(&ao2.total_locked, 1);
00155 #endif
00156 
00157 #ifndef DEBUG_THREADS
00158    return ast_mutex_lock(&p->priv_data.lock);
00159 #else
00160    return __ast_pthread_mutex_lock(file, line, func, var, &p->priv_data.lock);
00161 #endif
00162 }
00163 
00164 #ifndef DEBUG_THREADS
00165 int ao2_unlock(void *user_data)
00166 #else
00167 int _ao2_unlock(void *user_data, const char *file, const char *func, int line, const char *var)
00168 #endif
00169 {
00170    struct astobj2 *p = INTERNAL_OBJ(user_data);
00171 
00172    if (p == NULL)
00173       return -1;
00174 
00175 #ifdef AO2_DEBUG
00176    ast_atomic_fetchadd_int(&ao2.total_locked, -1);
00177 #endif
00178 
00179 #ifndef DEBUG_THREADS
00180    return ast_mutex_unlock(&p->priv_data.lock);
00181 #else
00182    return __ast_pthread_mutex_unlock(file, line, func, var, &p->priv_data.lock);
00183 #endif
00184 }
00185 
00186 #ifndef DEBUG_THREADS
00187 int ao2_trylock(void *user_data)
00188 #else
00189 int _ao2_trylock(void *user_data, const char *file, const char *func, int line, const char *var)
00190 #endif
00191 {
00192    struct astobj2 *p = INTERNAL_OBJ(user_data);
00193    int ret;
00194    
00195    if (p == NULL)
00196       return -1;
00197 #ifndef DEBUG_THREADS
00198    ret = ast_mutex_trylock(&p->priv_data.lock);
00199 #else
00200    ret = __ast_pthread_mutex_trylock(file, line, func, var, &p->priv_data.lock);
00201 #endif
00202 
00203 #ifdef AO2_DEBUG
00204    if (!ret)
00205       ast_atomic_fetchadd_int(&ao2.total_locked, 1);
00206 #endif
00207    return ret;
00208 }
00209 
00210 void *ao2_object_get_lockaddr(void *obj)
00211 {
00212    struct astobj2 *p = INTERNAL_OBJ(obj);
00213    
00214    if (p == NULL)
00215       return NULL;
00216 
00217    return &p->priv_data.lock;
00218 }
00219 
00220 /*
00221  * The argument is a pointer to the user portion.
00222  */
00223 
00224 
00225 int _ao2_ref_debug(void *user_data, const int delta, char *tag, char *file, int line, const char *funcname)
00226 {
00227    struct astobj2 *obj = INTERNAL_OBJ(user_data);
00228    
00229    if (obj == NULL)
00230       return -1;
00231 
00232    if (delta != 0) {
00233       FILE *refo = fopen(REF_FILE,"a");
00234       fprintf(refo, "%p %s%d   %s:%d:%s (%s) [@%d]\n", user_data, (delta<0? "":"+"), delta, file, line, funcname, tag, obj->priv_data.ref_counter);
00235       fclose(refo);
00236    }
00237    if (obj->priv_data.ref_counter + delta == 0 && obj->priv_data.destructor_fn != NULL) { /* this isn't protected with lock; just for o/p */
00238          FILE *refo = fopen(REF_FILE,"a");    
00239          fprintf(refo, "%p **call destructor** %s:%d:%s (%s)\n", user_data, file, line, funcname, tag);   
00240          fclose(refo);
00241    }
00242    return __ao2_ref(user_data, delta);
00243 }
00244 
00245 int _ao2_ref(void *user_data, const int delta)
00246 {
00247    struct astobj2 *obj = INTERNAL_OBJ(user_data);
00248 
00249    if (obj == NULL)
00250       return -1;
00251 
00252    return __ao2_ref(user_data, delta);
00253 }
00254 
00255 static int __ao2_ref(void *user_data, const int delta)
00256 {
00257    struct astobj2 *obj = INTERNAL_OBJ(user_data);
00258    int current_value;
00259    int ret;
00260 
00261    /* if delta is 0, just return the refcount */
00262    if (delta == 0)
00263       return (obj->priv_data.ref_counter);
00264 
00265    /* we modify with an atomic operation the reference counter */
00266    ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
00267    current_value = ret + delta;
00268 
00269 #ifdef AO2_DEBUG  
00270    ast_atomic_fetchadd_int(&ao2.total_refs, delta);
00271 #endif
00272 
00273    /* this case must never happen */
00274    if (current_value < 0)
00275       ast_log(LOG_ERROR, "refcount %d on object %p\n", current_value, user_data);
00276 
00277    if (current_value <= 0) { /* last reference, destroy the object */
00278       if (obj->priv_data.destructor_fn != NULL) {
00279          obj->priv_data.destructor_fn(user_data);
00280       }
00281 
00282       ast_mutex_destroy(&obj->priv_data.lock);
00283 #ifdef AO2_DEBUG
00284       ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
00285       ast_atomic_fetchadd_int(&ao2.total_objects, -1);
00286 #endif
00287       /* for safety, zero-out the astobj2 header and also the
00288        * first word of the user-data, which we make sure is always
00289        * allocated. */
00290       memset(obj, '\0', sizeof(struct astobj2 *) + sizeof(void *) );
00291       free(obj);
00292    }
00293 
00294    return ret;
00295 }
00296 
00297 /*
00298  * We always alloc at least the size of a void *,
00299  * for debugging purposes.
00300  */
00301 static void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
00302 {
00303    /* allocation */
00304    struct astobj2 *obj;
00305 
00306    if (data_size < sizeof(void *))
00307       data_size = sizeof(void *);
00308 
00309    obj = ast_calloc(1, sizeof(*obj) + data_size);
00310 
00311    if (obj == NULL)
00312       return NULL;
00313 
00314    ast_mutex_init(&obj->priv_data.lock);
00315    obj->priv_data.magic = AO2_MAGIC;
00316    obj->priv_data.data_size = data_size;
00317    obj->priv_data.ref_counter = 1;
00318    obj->priv_data.destructor_fn = destructor_fn;   /* can be NULL */
00319 
00320 #ifdef AO2_DEBUG
00321    ast_atomic_fetchadd_int(&ao2.total_objects, 1);
00322    ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
00323    ast_atomic_fetchadd_int(&ao2.total_refs, 1);
00324 #endif
00325 
00326    /* return a pointer to the user data */
00327    return EXTERNAL_OBJ(obj);
00328 }
00329 
00330 void *_ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, char *tag, char *file, int line, const char *funcname)
00331 {
00332    /* allocation */
00333    void *obj;
00334    FILE *refo = fopen(REF_FILE,"a");
00335 
00336    obj = __ao2_alloc(data_size, destructor_fn);
00337 
00338    if (obj == NULL)
00339       return NULL;
00340    
00341    if (refo) {
00342       fprintf(refo, "%p =1   %s:%d:%s (%s)\n", obj, file, line, funcname, tag);
00343       fclose(refo);
00344    }
00345 
00346    /* return a pointer to the user data */
00347    return obj;
00348 }
00349 
00350 void *_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
00351 {
00352    return __ao2_alloc(data_size, destructor_fn);
00353 }
00354 
00355 
00356 /* internal callback to destroy a container. */
00357 static void container_destruct(void *c);
00358 
00359 /* internal callback to destroy a container. */
00360 static void container_destruct_debug(void *c);
00361 
00362 /* each bucket in the container is a tailq. */
00363 AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
00364 
00365 /*!
00366  * A container; stores the hash and callback functions, information on
00367  * the size, the hash bucket heads, and a version number, starting at 0
00368  * (for a newly created, empty container)
00369  * and incremented every time an object is inserted or deleted.
00370  * The assumption is that an object is never moved in a container,
00371  * but removed and readded with the new number.
00372  * The version number is especially useful when implementing iterators.
00373  * In fact, we can associate a unique, monotonically increasing number to
00374  * each object, which means that, within an iterator, we can store the
00375  * version number of the current object, and easily look for the next one,
00376  * which is the next one in the list with a higher number.
00377  * Since all objects have a version >0, we can use 0 as a marker for
00378  * 'we need the first object in the bucket'.
00379  *
00380  * \todo Linking and unlink objects is typically expensive, as it
00381  * involves a malloc() of a small object which is very inefficient.
00382  * To optimize this, we allocate larger arrays of bucket_list's
00383  * when we run out of them, and then manage our own freelist.
00384  * This will be more efficient as we can do the freelist management while
00385  * we hold the lock (that we need anyways).
00386  */
00387 struct ao2_container {
00388    ao2_hash_fn *hash_fn;
00389    ao2_callback_fn *cmp_fn;
00390    int n_buckets;
00391    /*! Number of elements in the container */
00392    int elements;
00393    /*! described above */
00394    int version;
00395    /*! variable size */
00396    struct bucket buckets[0];
00397 };
00398  
00399 /*!
00400  * \brief always zero hash function
00401  *
00402  * it is convenient to have a hash function that always returns 0.
00403  * This is basically used when we want to have a container that is
00404  * a simple linked list.
00405  *
00406  * \returns 0
00407  */
00408 static int hash_zero(const void *user_obj, const int flags)
00409 {
00410    return 0;
00411 }
00412 
00413 /*
00414  * A container is just an object, after all!
00415  */
00416 static struct ao2_container *__ao2_container_alloc(struct ao2_container *c, const unsigned int n_buckets, ao2_hash_fn *hash_fn,
00417                                  ao2_callback_fn *cmp_fn)
00418 {
00419    /* XXX maybe consistency check on arguments ? */
00420    /* compute the container size */
00421 
00422    if (!c)
00423       return NULL;
00424    
00425    c->version = 1;   /* 0 is a reserved value here */
00426    c->n_buckets = n_buckets;
00427    c->hash_fn = hash_fn ? hash_fn : hash_zero;
00428    c->cmp_fn = cmp_fn;
00429 
00430 #ifdef AO2_DEBUG
00431    ast_atomic_fetchadd_int(&ao2.total_containers, 1);
00432 #endif
00433 
00434    return c;
00435 }
00436 
00437 struct ao2_container *_ao2_container_alloc_debug(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
00438       ao2_callback_fn *cmp_fn, char *tag, char *file, int line, const char *funcname)
00439 {
00440    /* XXX maybe consistency check on arguments ? */
00441    /* compute the container size */
00442    size_t container_size = sizeof(struct ao2_container) + n_buckets * sizeof(struct bucket);
00443    struct ao2_container *c = _ao2_alloc_debug(container_size, container_destruct_debug, tag, file, line, funcname);
00444 
00445    return __ao2_container_alloc(c, n_buckets, hash_fn, cmp_fn);
00446 }
00447 
00448 struct ao2_container *
00449 _ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
00450       ao2_callback_fn *cmp_fn)
00451 {
00452    /* XXX maybe consistency check on arguments ? */
00453    /* compute the container size */
00454 
00455    size_t container_size = sizeof(struct ao2_container) + n_buckets * sizeof(struct bucket);
00456    struct ao2_container *c = _ao2_alloc(container_size, container_destruct);
00457 
00458    return __ao2_container_alloc(c, n_buckets, hash_fn, cmp_fn);
00459 }
00460 
00461 /*!
00462  * return the number of elements in the container
00463  */
00464 int ao2_container_count(struct ao2_container *c)
00465 {
00466    return c->elements;
00467 }
00468 
00469 /*!
00470  * A structure to create a linked list of entries,
00471  * used within a bucket.
00472  * XXX \todo this should be private to the container code
00473  */
00474 struct bucket_list {
00475    AST_LIST_ENTRY(bucket_list) entry;
00476    int version;
00477    struct astobj2 *astobj;    /* pointer to internal data */
00478 }; 
00479 
00480 /*
00481  * link an object to a container
00482  */
00483 
00484 static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data)
00485 {
00486    int i;
00487    /* create a new list entry */
00488    struct bucket_list *p;
00489    struct astobj2 *obj = INTERNAL_OBJ(user_data);
00490    
00491    if (!obj)
00492       return NULL;
00493 
00494    if (INTERNAL_OBJ(c) == NULL)
00495       return NULL;
00496 
00497    p = ast_calloc(1, sizeof(*p));
00498    if (!p)
00499       return NULL;
00500 
00501    i = c->hash_fn(user_data, OBJ_POINTER);
00502 
00503    ao2_lock(c);
00504    i %= c->n_buckets;
00505    p->astobj = obj;
00506    p->version = ast_atomic_fetchadd_int(&c->version, 1);
00507    AST_LIST_INSERT_TAIL(&c->buckets[i], p, entry);
00508    ast_atomic_fetchadd_int(&c->elements, 1);
00509 
00510    /* the last two operations (ao2_ref, ao2_unlock) must be done by the calling func */
00511    return p;
00512 }
00513 
00514 void *_ao2_link_debug(struct ao2_container *c, void *user_data, char *tag, char *file, int line, const char *funcname)
00515 {
00516    struct bucket_list *p = __ao2_link(c, user_data);
00517    
00518    if (p) {
00519       _ao2_ref_debug(user_data, +1, tag, file, line, funcname);
00520       ao2_unlock(c);
00521    }
00522    return p;
00523 }
00524 
00525 void *_ao2_link(struct ao2_container *c, void *user_data)
00526 {
00527    struct bucket_list *p = __ao2_link(c, user_data);
00528    
00529    if (p) {
00530       _ao2_ref(user_data, +1);
00531       ao2_unlock(c);
00532    }
00533    return p;
00534 }
00535 
00536 /*!
00537  * \brief another convenience function is a callback that matches on address
00538  */
00539 int ao2_match_by_addr(void *user_data, void *arg, int flags)
00540 {
00541    return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
00542 }
00543 
00544 /*
00545  * Unlink an object from the container
00546  * and destroy the associated * ao2_bucket_list structure.
00547  */
00548 void *_ao2_unlink_debug(struct ao2_container *c, void *user_data, char *tag,
00549                   char *file, int line, const char *funcname)
00550 {
00551    if (INTERNAL_OBJ(user_data) == NULL)   /* safety check on the argument */
00552       return NULL;
00553 
00554    _ao2_callback_debug(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data, tag, file, line, funcname);
00555 
00556    return NULL;
00557 }
00558 
00559 void *_ao2_unlink(struct ao2_container *c, void *user_data)
00560 {
00561    if (INTERNAL_OBJ(user_data) == NULL)   /* safety check on the argument */
00562       return NULL;
00563 
00564    _ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data);
00565 
00566    return NULL;
00567 }
00568 
00569 /*! 
00570  * \brief special callback that matches all 
00571  */ 
00572 static int cb_true(void *user_data, void *arg, int flags)
00573 {
00574    return CMP_MATCH;
00575 }
00576 
00577 /*!
00578  * Browse the container using different stategies accoding the flags.
00579  * \return Is a pointer to an object or to a list of object if OBJ_MULTIPLE is 
00580  * specified.
00581  * Luckily, for debug purposes, the added args (tag, file, line, funcname)
00582  * aren't an excessive load to the system, as the callback should not be
00583  * called as often as, say, the ao2_ref func is called.
00584  */
00585 static void *__ao2_callback(struct ao2_container *c,
00586    const enum search_flags flags, ao2_callback_fn *cb_fn, void *arg,
00587    char *tag, char *file, int line, const char *funcname)
00588 {
00589    int i, last;   /* search boundaries */
00590    void *ret = NULL;
00591 
00592    if (INTERNAL_OBJ(c) == NULL)  /* safety check on the argument */
00593       return NULL;
00594 
00595    if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
00596       ast_log(LOG_WARNING, "multiple data return not implemented yet (flags %x)\n", flags);
00597       return NULL;
00598    }
00599 
00600    /* override the match function if necessary */
00601    if (cb_fn == NULL)   /* if NULL, match everything */
00602       cb_fn = cb_true;
00603    /*
00604     * XXX this can be optimized.
00605     * If we have a hash function and lookup by pointer,
00606     * run the hash function. Otherwise, scan the whole container
00607     * (this only for the time being. We need to optimize this.)
00608     */
00609    if ((flags & OBJ_POINTER)) /* we know hash can handle this case */
00610       i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
00611    else        /* don't know, let's scan all buckets */
00612       i = -1;     /* XXX this must be fixed later. */
00613 
00614    /* determine the search boundaries: i..last-1 */
00615    if (i < 0) {
00616       i = 0;
00617       last = c->n_buckets;
00618    } else {
00619       last = i + 1;
00620    }
00621 
00622    ao2_lock(c);   /* avoid modifications to the content */
00623 
00624    for (; i < last ; i++) {
00625       /* scan the list with prev-cur pointers */
00626       struct bucket_list *cur;
00627 
00628       AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
00629          int match = cb_fn(EXTERNAL_OBJ(cur->astobj), arg, flags) & (CMP_MATCH | CMP_STOP);
00630 
00631          /* we found the object, performing operations according flags */
00632          if (match == 0) { /* no match, no stop, continue */
00633             continue;
00634          } else if (match == CMP_STOP) {  /* no match but stop, we are done */
00635             i = last;
00636             break;
00637          }
00638          /* we have a match (CMP_MATCH) here */
00639          if (!(flags & OBJ_NODATA)) {  /* if must return the object, record the value */
00640             /* it is important to handle this case before the unlink */
00641             ret = EXTERNAL_OBJ(cur->astobj);
00642             if (tag)
00643                _ao2_ref_debug(ret, 1, tag, file, line, funcname);
00644             else
00645                _ao2_ref(ret, 1);
00646          }
00647 
00648          if (flags & OBJ_UNLINK) {  /* must unlink */
00649             struct bucket_list *x = cur;
00650 
00651             /* we are going to modify the container, so update version */
00652             ast_atomic_fetchadd_int(&c->version, 1);
00653             AST_LIST_REMOVE_CURRENT(entry);
00654             /* update number of elements and version */
00655             ast_atomic_fetchadd_int(&c->elements, -1);
00656             if (tag)
00657                _ao2_ref_debug(EXTERNAL_OBJ(x->astobj), -1, tag, file, line, funcname);
00658             else
00659                _ao2_ref(EXTERNAL_OBJ(x->astobj), -1);
00660             free(x); /* free the link record */
00661          }
00662 
00663          if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
00664             /* We found the only match we need */
00665             i = last;   /* force exit from outer loop */
00666             break;
00667          }
00668          if (!(flags & OBJ_NODATA)) {
00669 #if 0 /* XXX to be completed */
00670             /*
00671              * This is the multiple-return case. We need to link
00672              * the object in a list. The refcount is already increased.
00673              */
00674 #endif
00675          }
00676       }
00677       AST_LIST_TRAVERSE_SAFE_END;
00678    }
00679    ao2_unlock(c);
00680    return ret;
00681 }
00682 
00683 void *_ao2_callback_debug(struct ao2_container *c,
00684                    const enum search_flags flags,
00685                    ao2_callback_fn *cb_fn, void *arg, 
00686                    char *tag, char *file, int line, const char *funcname)
00687 {
00688    return __ao2_callback(c,flags, cb_fn, arg, tag, file, line, funcname);
00689 }
00690 
00691 void *_ao2_callback(struct ao2_container *c,const enum search_flags flags,
00692                     ao2_callback_fn *cb_fn, void *arg)
00693 {
00694    return __ao2_callback(c,flags, cb_fn, arg, NULL, NULL, 0, NULL);
00695 }
00696 
00697 /*!
00698  * the find function just invokes the default callback with some reasonable flags.
00699  */
00700 void *_ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
00701 {
00702    return _ao2_callback_debug(c, flags, c->cmp_fn, arg, tag, file, line, funcname);
00703 }
00704 
00705 void *_ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
00706 {
00707    return _ao2_callback(c, flags, c->cmp_fn, arg);
00708 }
00709 
00710 /*!
00711  * initialize an iterator so we start from the first object
00712  */
00713 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
00714 {
00715    struct ao2_iterator a = {
00716       .c = c,
00717       .flags = flags
00718    };
00719    
00720    return a;
00721 }
00722 
00723 /*
00724  * move to the next element in the container.
00725  */
00726 static void * __ao2_iterator_next(struct ao2_iterator *a, struct bucket_list **q)
00727 {
00728    int lim;
00729    struct bucket_list *p = NULL;
00730    void *ret = NULL;
00731 
00732    *q = NULL;
00733    
00734    if (INTERNAL_OBJ(a->c) == NULL)
00735       return NULL;
00736 
00737    if (!(a->flags & F_AO2I_DONTLOCK))
00738       ao2_lock(a->c);
00739 
00740    /* optimization. If the container is unchanged and
00741     * we have a pointer, try follow it
00742     */
00743    if (a->c->version == a->c_version && (p = a->obj) ) {
00744       if ( (p = AST_LIST_NEXT(p, entry)) )
00745          goto found;
00746       /* nope, start from the next bucket */
00747       a->bucket++;
00748       a->version = 0;
00749       a->obj = NULL;
00750    }
00751 
00752    lim = a->c->n_buckets;
00753 
00754    /* Browse the buckets array, moving to the next
00755     * buckets if we don't find the entry in the current one.
00756     * Stop when we find an element with version number greater
00757     * than the current one (we reset the version to 0 when we
00758     * switch buckets).
00759     */
00760    for (; a->bucket < lim; a->bucket++, a->version = 0) {
00761       /* scan the current bucket */
00762       AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
00763          if (p->version > a->version)
00764             goto found;
00765       }
00766    }
00767 
00768 found:
00769    if (p) {
00770       a->version = p->version;
00771       a->obj = p;
00772       a->c_version = a->c->version;
00773       ret = EXTERNAL_OBJ(p->astobj);
00774       /* inc refcount of returned object */
00775       *q = p;
00776    }
00777 
00778    return ret;
00779 }
00780 
00781 void * _ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname)
00782 {
00783    struct bucket_list *p;
00784    void *ret = NULL;
00785 
00786    ret = __ao2_iterator_next(a, &p);
00787    
00788    if (p) {
00789       /* inc refcount of returned object */
00790       _ao2_ref_debug(ret, 1, tag, file, line, funcname);
00791    }
00792 
00793    if (!(a->flags & F_AO2I_DONTLOCK))
00794       ao2_unlock(a->c);
00795 
00796    return ret;
00797 }
00798 
00799 void * _ao2_iterator_next(struct ao2_iterator *a)
00800 {
00801    struct bucket_list *p = NULL;
00802    void *ret = NULL;
00803 
00804    ret = __ao2_iterator_next(a, &p);
00805    
00806    if (p) {
00807       /* inc refcount of returned object */
00808       _ao2_ref(ret, 1);
00809    }
00810 
00811    if (!(a->flags & F_AO2I_DONTLOCK))
00812       ao2_unlock(a->c);
00813 
00814    return ret;
00815 }
00816 
00817 /* callback for destroying container.
00818  * we can make it simple as we know what it does
00819  */
00820 static int cd_cb(void *obj, void *arg, int flag)
00821 {
00822    _ao2_ref(obj, -1);
00823    return 0;
00824 }
00825    
00826 static int cd_cb_debug(void *obj, void *arg, int flag)
00827 {
00828    _ao2_ref_debug(obj, -1, "deref object via container destroy",  __FILE__, __LINE__, __PRETTY_FUNCTION__);
00829    return 0;
00830 }
00831    
00832 static void container_destruct(void *_c)
00833 {
00834    struct ao2_container *c = _c;
00835    int i;
00836 
00837    _ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
00838 
00839    for (i = 0; i < c->n_buckets; i++) {
00840       struct bucket_list *current;
00841 
00842       while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
00843          ast_free(current);
00844       }
00845    }
00846 
00847 #ifdef AO2_DEBUG
00848    ast_atomic_fetchadd_int(&ao2.total_containers, -1);
00849 #endif
00850 }
00851 
00852 static void container_destruct_debug(void *_c)
00853 {
00854    struct ao2_container *c = _c;
00855    int i;
00856 
00857    _ao2_callback_debug(c, OBJ_UNLINK, cd_cb_debug, NULL, "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__);
00858 
00859    for (i = 0; i < c->n_buckets; i++) {
00860       struct bucket_list *current;
00861 
00862       while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
00863          ast_free(current);
00864       }
00865    }
00866 
00867 #ifdef AO2_DEBUG
00868    ast_atomic_fetchadd_int(&ao2.total_containers, -1);
00869 #endif
00870 }
00871 
00872 #ifdef AO2_DEBUG
00873 static int print_cb(void *obj, void *arg, int flag)
00874 {
00875    int *fd = arg;
00876    char *s = (char *)obj;
00877 
00878    ast_cli(*fd, "string <%s>\n", s);
00879    return 0;
00880 }
00881 
00882 /*
00883  * Print stats
00884  */
00885 static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00886 {
00887    switch (cmd) {
00888    case CLI_INIT:
00889       e->command = "astobj2 stats";
00890       e->usage = "Usage: astobj2 stats\n"
00891             "       Show astobj2 stats\n";
00892       return NULL;
00893    case CLI_GENERATE:
00894       return NULL;
00895    }
00896    ast_cli(a->fd, "Objects    : %d\n", ao2.total_objects);
00897    ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
00898    ast_cli(a->fd, "Memory     : %d\n", ao2.total_mem);
00899    ast_cli(a->fd, "Locked     : %d\n", ao2.total_locked);
00900    ast_cli(a->fd, "Refs       : %d\n", ao2.total_refs);
00901    return CLI_SUCCESS;
00902 }
00903 
00904 /*
00905  * This is testing code for astobj
00906  */
00907 static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00908 {
00909    struct ao2_container *c1;
00910    int i, lim;
00911    char *obj;
00912    static int prof_id = -1;
00913    struct ast_cli_args fake_args = { a->fd, 0, NULL };
00914 
00915    switch (cmd) {
00916    case CLI_INIT:
00917       e->command = "astobj2 test";
00918       e->usage = "Usage: astobj2 test <num>\n"
00919             "       Runs astobj2 test. Creates 'num' objects,\n"
00920             "       and test iterators, callbacks and may be other stuff\n";
00921       return NULL;
00922    case CLI_GENERATE:
00923       return NULL;
00924    }
00925 
00926    if (a->argc != 3) {
00927       return CLI_SHOWUSAGE;
00928    }
00929 
00930    if (prof_id == -1)
00931       prof_id = ast_add_profile("ao2_alloc", 0);
00932 
00933    ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
00934    lim = atoi(a->argv[2]);
00935    ast_cli(a->fd, "called astobj_test\n");
00936 
00937    handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
00938    /*
00939     * allocate a container with no default callback, and no hash function.
00940     * No hash means everything goes in the same bucket.
00941     */
00942    c1 = ao2_t_container_alloc(100, NULL /* no callback */, NULL /* no hash */,"test");
00943    ast_cli(a->fd, "container allocated as %p\n", c1);
00944 
00945    /*
00946     * fill the container with objects.
00947     * ao2_alloc() gives us a reference which we pass to the
00948     * container when we do the insert.
00949     */
00950    for (i = 0; i < lim; i++) {
00951       ast_mark(prof_id, 1 /* start */);
00952       obj = ao2_t_alloc(80, NULL,"test");
00953       ast_mark(prof_id, 0 /* stop */);
00954       ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
00955       sprintf(obj, "-- this is obj %d --", i);
00956       ao2_link(c1, obj);
00957       /* At this point, the refcount on obj is 2 due to the allocation
00958        * and linking. We can go ahead and reduce the refcount by 1
00959        * right here so that when the container is unreffed later, the
00960        * objects will be freed
00961        */
00962       ao2_t_ref(obj, -1, "test");
00963    }
00964    ast_cli(a->fd, "testing callbacks\n");
00965    ao2_t_callback(c1, 0, print_cb, &a->fd,"test callback");
00966    ast_cli(a->fd, "testing iterators, remove every second object\n");
00967    {
00968       struct ao2_iterator ai;
00969       int x = 0;
00970 
00971       ai = ao2_iterator_init(c1, 0);
00972       while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
00973          ast_cli(a->fd, "iterator on <%s>\n", obj);
00974          if (x++ & 1)
00975             ao2_t_unlink(c1, obj,"test");
00976          ao2_t_ref(obj, -1,"test");
00977       }
00978       ast_cli(a->fd, "testing iterators again\n");
00979       ai = ao2_iterator_init(c1, 0);
00980       while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
00981          ast_cli(a->fd, "iterator on <%s>\n", obj);
00982          ao2_t_ref(obj, -1,"test");
00983       }
00984    }
00985    ast_cli(a->fd, "testing callbacks again\n");
00986    ao2_t_callback(c1, 0, print_cb, &a->fd,"test callback");
00987 
00988    ast_verbose("now you should see an error message:\n");
00989    ao2_t_ref(&i, -1, "");  /* i is not a valid object so we print an error here */
00990 
00991    ast_cli(a->fd, "destroy container\n");
00992    ao2_t_ref(c1, -1, "");  /* destroy container */
00993    handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
00994    return CLI_SUCCESS;
00995 }
00996 
00997 static struct ast_cli_entry cli_astobj2[] = {
00998    AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
00999    AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
01000 };
01001 #endif /* AO2_DEBUG */
01002 
01003 int astobj2_init(void)
01004 {
01005 #ifdef AO2_DEBUG
01006    ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
01007 #endif
01008 
01009    return 0;
01010 }

Generated on Fri Jun 19 12:09:31 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7