Thu Oct 1 13:08:40 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: 216015 $")
00023 
00024 #include "asterisk/astobj2.h"
00025 #include "asterisk/utils.h"
00026 #include "asterisk/cli.h"
00027 
00028 /*!
00029  * astobj2 objects are always prepended this data structure,
00030  * which contains a lock, a reference counter,
00031  * the flags and a pointer to a destructor.
00032  * The refcount is used to decide when it is time to
00033  * invoke the destructor.
00034  * The magic number is used for consistency check.
00035  * XXX the lock is not always needed, and its initialization may be
00036  * expensive. Consider making it external.
00037  */
00038 struct __priv_data {
00039    ast_mutex_t lock;
00040    int ref_counter;
00041    ao2_destructor_fn destructor_fn;
00042    /*! for stats */
00043    size_t data_size;
00044    /*! magic number.  This is used to verify that a pointer passed in is a
00045     *  valid astobj2 */
00046    uint32_t magic;
00047 };
00048 
00049 #define  AO2_MAGIC   0xa570b123
00050 
00051 /*!
00052  * What an astobj2 object looks like: fixed-size private data
00053  * followed by variable-size user data.
00054  */
00055 struct astobj2 {
00056    struct __priv_data priv_data;
00057    void *user_data[0];
00058 };
00059 
00060 #ifdef AST_DEVMODE
00061 #define AO2_DEBUG 1
00062 #endif
00063 
00064 #ifdef AO2_DEBUG
00065 struct ao2_stats {
00066    volatile int total_objects;
00067    volatile int total_mem;
00068    volatile int total_containers;
00069    volatile int total_refs;
00070    volatile int total_locked;
00071 };
00072 
00073 static struct ao2_stats ao2;
00074 #endif
00075 
00076 #ifndef HAVE_BKTR /* backtrace support */
00077 void ao2_bt(void) {}
00078 #else
00079 #include <execinfo.h>    /* for backtrace */
00080 
00081 void ao2_bt(void)
00082 {
00083     int c, i;
00084 #define N1  20
00085     void *addresses[N1];
00086     char **strings;
00087 
00088     c = backtrace(addresses, N1);
00089     strings = backtrace_symbols(addresses,c);
00090     ast_verbose("backtrace returned: %d\n", c);
00091     for(i = 0; i < c; i++) {
00092         ast_verbose("%d: %p %s\n", i, addresses[i], strings[i]);
00093     }
00094     free(strings);
00095 }
00096 #endif
00097 
00098 /*!
00099  * \brief convert from a pointer _p to a user-defined object
00100  *
00101  * \return the pointer to the astobj2 structure
00102  */
00103 static inline struct astobj2 *INTERNAL_OBJ(void *user_data)
00104 {
00105    struct astobj2 *p;
00106 
00107    if (!user_data) {
00108       ast_log(LOG_ERROR, "user_data is NULL\n");
00109       return NULL;
00110    }
00111 
00112    p = (struct astobj2 *) ((char *) user_data - sizeof(*p));
00113    if (AO2_MAGIC != (p->priv_data.magic) ) {
00114       ast_log(LOG_ERROR, "bad magic number 0x%x for %p\n", p->priv_data.magic, p);
00115       p = NULL;
00116    }
00117 
00118    return p;
00119 }
00120 
00121 /*!
00122  * \brief convert from a pointer _p to an astobj2 object
00123  *
00124  * \return the pointer to the user-defined portion.
00125  */
00126 #define EXTERNAL_OBJ(_p)   ((_p) == NULL ? NULL : (_p)->user_data)
00127 
00128 #ifndef DEBUG_THREADS
00129 int ao2_lock(void *user_data)
00130 #else
00131 int __ao2_lock(void *user_data, const char *file, const char *func, int line, const char *var)
00132 #endif
00133 {
00134    struct astobj2 *p = INTERNAL_OBJ(user_data);
00135 
00136    if (p == NULL)
00137       return -1;
00138 
00139 #ifdef AO2_DEBUG
00140    ast_atomic_fetchadd_int(&ao2.total_locked, 1);
00141 #endif
00142 
00143 #ifndef DEBUG_THREADS
00144    return ast_mutex_lock(&p->priv_data.lock);
00145 #else
00146    return __ast_pthread_mutex_lock(file, line, func, var, &p->priv_data.lock);
00147 #endif
00148 }
00149 
00150 #ifndef DEBUG_THREADS
00151 int ao2_trylock(void *user_data)
00152 #else
00153 int __ao2_trylock(void *user_data, const char *file, const char *func, int line, const char *var)
00154 #endif
00155 {
00156    struct astobj2 *p = INTERNAL_OBJ(user_data);
00157    int res;
00158 
00159    if (p == NULL)
00160       return -1;
00161 
00162 #ifndef DEBUG_THREADS
00163    res = ast_mutex_trylock(&p->priv_data.lock);
00164 #else
00165    res = __ast_pthread_mutex_trylock(file, line, func, var, &p->priv_data.lock);
00166 #endif
00167 
00168 #ifdef AO2_DEBUG
00169    if (!res) {
00170       ast_atomic_fetchadd_int(&ao2.total_locked, 1);
00171    }
00172 #endif
00173 
00174    return res;
00175 }
00176 
00177 #ifndef DEBUG_THREADS
00178 int ao2_unlock(void *user_data)
00179 #else
00180 int __ao2_unlock(void *user_data, const char *file, const char *func, int line, const char *var)
00181 #endif
00182 {
00183    struct astobj2 *p = INTERNAL_OBJ(user_data);
00184 
00185    if (p == NULL)
00186       return -1;
00187 
00188 #ifdef AO2_DEBUG
00189    ast_atomic_fetchadd_int(&ao2.total_locked, -1);
00190 #endif
00191 
00192 #ifndef DEBUG_THREADS
00193    return ast_mutex_unlock(&p->priv_data.lock);
00194 #else
00195    return __ast_pthread_mutex_unlock(file, line, func, var, &p->priv_data.lock);
00196 #endif
00197 }
00198 
00199 /*
00200  * The argument is a pointer to the user portion.
00201  */
00202 int ao2_ref(void *user_data, const int delta)
00203 {
00204    int current_value;
00205    int ret;
00206    struct astobj2 *obj = INTERNAL_OBJ(user_data);
00207 
00208    if (obj == NULL)
00209       return -1;
00210 
00211    /* if delta is 0, just return the refcount */
00212    if (delta == 0)
00213       return (obj->priv_data.ref_counter);
00214 
00215    /* we modify with an atomic operation the reference counter */
00216    ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
00217    current_value = ret + delta;
00218 
00219 #ifdef AO2_DEBUG  
00220    ast_atomic_fetchadd_int(&ao2.total_refs, delta);
00221 #endif
00222 
00223    /* this case must never happen */
00224    if (current_value < 0)
00225       ast_log(LOG_ERROR, "refcount %d on object %p\n", current_value, user_data);
00226 
00227    if (current_value <= 0) { /* last reference, destroy the object */
00228       if (obj->priv_data.destructor_fn != NULL) 
00229          obj->priv_data.destructor_fn(user_data);
00230 
00231       ast_mutex_destroy(&obj->priv_data.lock);
00232 #ifdef AO2_DEBUG
00233       ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
00234       ast_atomic_fetchadd_int(&ao2.total_objects, -1);
00235 #endif
00236       /* for safety, zero-out the astobj2 header and also the
00237        * first word of the user-data, which we make sure is always
00238        * allocated. */
00239       bzero(obj, sizeof(struct astobj2 *) + sizeof(void *) );
00240       free(obj);
00241    }
00242 
00243    return ret;
00244 }
00245 
00246 /*
00247  * We always alloc at least the size of a void *,
00248  * for debugging purposes.
00249  */
00250 void *ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
00251 {
00252    /* allocation */
00253    struct astobj2 *obj;
00254 
00255    if (data_size < sizeof(void *))
00256       data_size = sizeof(void *);
00257 
00258    obj = ast_calloc(1, sizeof(*obj) + data_size);
00259 
00260    if (obj == NULL)
00261       return NULL;
00262 
00263    ast_mutex_init(&obj->priv_data.lock);
00264    obj->priv_data.magic = AO2_MAGIC;
00265    obj->priv_data.data_size = data_size;
00266    obj->priv_data.ref_counter = 1;
00267    obj->priv_data.destructor_fn = destructor_fn;   /* can be NULL */
00268 
00269 #ifdef AO2_DEBUG
00270    ast_atomic_fetchadd_int(&ao2.total_objects, 1);
00271    ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
00272    ast_atomic_fetchadd_int(&ao2.total_refs, 1);
00273 #endif
00274 
00275    /* return a pointer to the user data */
00276    return EXTERNAL_OBJ(obj);
00277 }
00278 
00279 /* internal callback to destroy a container. */
00280 static void container_destruct(void *c);
00281 
00282 /* each bucket in the container is a tailq. */
00283 AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
00284 
00285 /*!
00286  * A container; stores the hash and callback functions, information on
00287  * the size, the hash bucket heads, and a version number, starting at 0
00288  * (for a newly created, empty container)
00289  * and incremented every time an object is inserted or deleted.
00290  * The assumption is that an object is never moved in a container,
00291  * but removed and readded with the new number.
00292  * The version number is especially useful when implementing iterators.
00293  * In fact, we can associate a unique, monotonically increasing number to
00294  * each object, which means that, within an iterator, we can store the
00295  * version number of the current object, and easily look for the next one,
00296  * which is the next one in the list with a higher number.
00297  * Since all objects have a version >0, we can use 0 as a marker for
00298  * 'we need the first object in the bucket'.
00299  *
00300  * \todo Linking and unlink objects is typically expensive, as it
00301  * involves a malloc() of a small object which is very inefficient.
00302  * To optimize this, we allocate larger arrays of bucket_list's
00303  * when we run out of them, and then manage our own freelist.
00304  * This will be more efficient as we can do the freelist management while
00305  * we hold the lock (that we need anyways).
00306  */
00307 struct ao2_container {
00308    ao2_hash_fn hash_fn;
00309    ao2_callback_fn cmp_fn;
00310    int n_buckets;
00311    /*! Number of elements in the container */
00312    int elements;
00313    /*! described above */
00314    int version;
00315    /*! variable size */
00316    struct bucket buckets[0];
00317 };
00318  
00319 /*!
00320  * \brief always zero hash function
00321  *
00322  * it is convenient to have a hash function that always returns 0.
00323  * This is basically used when we want to have a container that is
00324  * a simple linked list.
00325  *
00326  * \returns 0
00327  */
00328 static int hash_zero(const void *user_obj, const int flags)
00329 {
00330    return 0;
00331 }
00332 
00333 /*
00334  * A container is just an object, after all!
00335  */
00336 struct ao2_container *
00337 ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn hash_fn,
00338       ao2_callback_fn cmp_fn)
00339 {
00340    /* XXX maybe consistency check on arguments ? */
00341    /* compute the container size */
00342    size_t container_size = sizeof(struct ao2_container) + n_buckets * sizeof(struct bucket);
00343 
00344    struct ao2_container *c = ao2_alloc(container_size, container_destruct);
00345 
00346    if (!c)
00347       return NULL;
00348    
00349    c->version = 1;   /* 0 is a reserved value here */
00350    c->n_buckets = n_buckets;
00351    c->hash_fn = hash_fn ? hash_fn : hash_zero;
00352    c->cmp_fn = cmp_fn;
00353 
00354 #ifdef AO2_DEBUG
00355    ast_atomic_fetchadd_int(&ao2.total_containers, 1);
00356 #endif
00357 
00358    return c;
00359 }
00360 
00361 /*!
00362  * return the number of elements in the container
00363  */
00364 int ao2_container_count(struct ao2_container *c)
00365 {
00366    return c->elements;
00367 }
00368 
00369 /*!
00370  * A structure to create a linked list of entries,
00371  * used within a bucket.
00372  * XXX \todo this should be private to the container code
00373  */
00374 struct bucket_list {
00375    AST_LIST_ENTRY(bucket_list) entry;
00376    int version;
00377    struct astobj2 *astobj;    /* pointer to internal data */
00378 }; 
00379 
00380 /*
00381  * link an object to a container
00382  */
00383 void *__ao2_link(struct ao2_container *c, void *user_data, int iax2_hack)
00384 {
00385    int i;
00386    /* create a new list entry */
00387    struct bucket_list *p;
00388    struct astobj2 *obj = INTERNAL_OBJ(user_data);
00389    
00390    if (!obj)
00391       return NULL;
00392 
00393    if (INTERNAL_OBJ(c) == NULL)
00394       return NULL;
00395 
00396    p = ast_calloc(1, sizeof(*p));
00397    if (!p)
00398       return NULL;
00399 
00400    i = c->hash_fn(user_data, OBJ_POINTER);
00401 
00402    ao2_lock(c);
00403    i %= c->n_buckets;
00404    p->astobj = obj;
00405    p->version = ast_atomic_fetchadd_int(&c->version, 1);
00406    if (iax2_hack)
00407       AST_LIST_INSERT_HEAD(&c->buckets[i], p, entry);
00408    else
00409       AST_LIST_INSERT_TAIL(&c->buckets[i], p, entry);
00410    ast_atomic_fetchadd_int(&c->elements, 1);
00411    ao2_ref(user_data, +1);
00412    ao2_unlock(c);
00413    
00414    return p;
00415 }
00416 
00417 /*!
00418  * \brief another convenience function is a callback that matches on address
00419  */
00420 int ao2_match_by_addr(void *user_data, void *arg, int flags)
00421 {
00422    return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
00423 }
00424 
00425 /*
00426  * Unlink an object from the container
00427  * and destroy the associated * ao2_bucket_list structure.
00428  */
00429 void *ao2_unlink(struct ao2_container *c, void *user_data)
00430 {
00431    if (INTERNAL_OBJ(user_data) == NULL)   /* safety check on the argument */
00432       return NULL;
00433 
00434    ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data);
00435 
00436    return NULL;
00437 }
00438 
00439 /*! 
00440  * \brief special callback that matches all 
00441  */ 
00442 static int cb_true(void *user_data, void *arg, int flags)
00443 {
00444    return CMP_MATCH;
00445 }
00446 
00447 /*!
00448  * Browse the container using different stategies accoding the flags.
00449  * \return Is a pointer to an object or to a list of object if OBJ_MULTIPLE is 
00450  * specified.
00451  */
00452 void *ao2_callback(struct ao2_container *c,
00453    const enum search_flags flags,
00454    ao2_callback_fn cb_fn, void *arg)
00455 {
00456    int i, start, last;  /* search boundaries */
00457    void *ret = NULL;
00458 
00459    if (INTERNAL_OBJ(c) == NULL)  /* safety check on the argument */
00460       return NULL;
00461 
00462    if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
00463       ast_log(LOG_WARNING, "multiple data return not implemented yet (flags %x)\n", flags);
00464       return NULL;
00465    }
00466 
00467    /* override the match function if necessary */
00468 #if 0
00469    /* Removing this slightly changes the meaning of OBJ_POINTER, but makes it
00470     * do what I want it to.  I'd like to hint to ao2_callback that the arg is
00471     * of the same object type, so it can be passed to the hash function.
00472     * However, I don't want to imply that this is the object being searched for. */
00473    if (flags & OBJ_POINTER)
00474       cb_fn = match_by_addr;
00475    else
00476 #endif
00477    if (cb_fn == NULL)   /* if NULL, match everything */
00478       cb_fn = cb_true;
00479    /*
00480     * XXX this can be optimized.
00481     * If we have a hash function and lookup by pointer,
00482     * run the hash function. Otherwise, scan the whole container
00483     * (this only for the time being. We need to optimize this.)
00484     */
00485    if ((flags & OBJ_POINTER)) /* we know hash can handle this case */
00486       start = i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
00487    else        /* don't know, let's scan all buckets */
00488       i = -1;     /* XXX this must be fixed later. */
00489 
00490    /* determine the search boundaries: i..last-1 */
00491    if (i < 0) {
00492       start = i = 0;
00493       last = c->n_buckets;
00494    } else if ((flags & OBJ_CONTINUE)) {
00495       last = c->n_buckets;
00496    } else {
00497       last = i + 1;
00498    }
00499 
00500    ao2_lock(c);   /* avoid modifications to the content */
00501 
00502    for (; i < last ; i++) {
00503       /* scan the list with prev-cur pointers */
00504       struct bucket_list *cur;
00505 
00506       AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
00507          int match = cb_fn(EXTERNAL_OBJ(cur->astobj), arg, flags) & (CMP_MATCH | CMP_STOP);
00508 
00509          /* we found the object, performing operations according flags */
00510          if (match == 0) { /* no match, no stop, continue */
00511             continue;
00512          } else if (match == CMP_STOP) {  /* no match but stop, we are done */
00513             i = last;
00514             break;
00515          }
00516          /* we have a match (CMP_MATCH) here */
00517          if (!(flags & OBJ_NODATA)) {  /* if must return the object, record the value */
00518             /* it is important to handle this case before the unlink */
00519             ret = EXTERNAL_OBJ(cur->astobj);
00520             ao2_ref(ret, 1);
00521          }
00522 
00523          if (flags & OBJ_UNLINK) {  /* must unlink */
00524             struct bucket_list *x = cur;
00525 
00526             /* we are going to modify the container, so update version */
00527             ast_atomic_fetchadd_int(&c->version, 1);
00528             AST_LIST_REMOVE_CURRENT(&c->buckets[i], entry);
00529             /* update number of elements and version */
00530             ast_atomic_fetchadd_int(&c->elements, -1);
00531             ao2_ref(EXTERNAL_OBJ(x->astobj), -1);
00532             free(x); /* free the link record */
00533          }
00534 
00535          if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
00536             /* We found the only match we need */
00537             i = last;   /* force exit from outer loop */
00538             break;
00539          }
00540          if (!(flags & OBJ_NODATA)) {
00541 #if 0 /* XXX to be completed */
00542             /*
00543              * This is the multiple-return case. We need to link
00544              * the object in a list. The refcount is already increased.
00545              */
00546 #endif
00547          }
00548       }
00549       AST_LIST_TRAVERSE_SAFE_END
00550 
00551       if (ret) {
00552          /* This assumes OBJ_MULTIPLE with !OBJ_NODATA is still not implemented */
00553          break;
00554       }
00555 
00556       if (i == c->n_buckets - 1 && (flags & OBJ_POINTER) && (flags & OBJ_CONTINUE)) {
00557          /* Move to the beginning to ensure we check every bucket */
00558          i = -1;
00559          last = start;
00560       }
00561    }
00562    ao2_unlock(c);
00563    return ret;
00564 }
00565 
00566 /*!
00567  * the find function just invokes the default callback with some reasonable flags.
00568  */
00569 void *ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
00570 {
00571    return ao2_callback(c, flags, c->cmp_fn, arg);
00572 }
00573 
00574 /*!
00575  * initialize an iterator so we start from the first object
00576  */
00577 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
00578 {
00579    struct ao2_iterator a = {
00580       .c = c,
00581       .flags = flags
00582    };
00583    
00584    return a;
00585 }
00586 
00587 /*
00588  * move to the next element in the container.
00589  */
00590 void * ao2_iterator_next(struct ao2_iterator *a)
00591 {
00592    int lim;
00593    struct bucket_list *p = NULL;
00594    void *ret = NULL;
00595 
00596    if (INTERNAL_OBJ(a->c) == NULL)
00597       return NULL;
00598 
00599    if (!(a->flags & F_AO2I_DONTLOCK))
00600       ao2_lock(a->c);
00601 
00602    /* optimization. If the container is unchanged and
00603     * we have a pointer, try follow it
00604     */
00605    if (a->c->version == a->c_version && (p = a->obj) ) {
00606       if ( (p = AST_LIST_NEXT(p, entry)) )
00607          goto found;
00608       /* nope, start from the next bucket */
00609       a->bucket++;
00610       a->version = 0;
00611       a->obj = NULL;
00612    }
00613 
00614    lim = a->c->n_buckets;
00615 
00616    /* Browse the buckets array, moving to the next
00617     * buckets if we don't find the entry in the current one.
00618     * Stop when we find an element with version number greater
00619     * than the current one (we reset the version to 0 when we
00620     * switch buckets).
00621     */
00622    for (; a->bucket < lim; a->bucket++, a->version = 0) {
00623       /* scan the current bucket */
00624       AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
00625          if (p->version > a->version)
00626             goto found;
00627       }
00628    }
00629 
00630 found:
00631    if (p) {
00632       a->version = p->version;
00633       a->obj = p;
00634       a->c_version = a->c->version;
00635       ret = EXTERNAL_OBJ(p->astobj);
00636       /* inc refcount of returned object */
00637       ao2_ref(ret, 1);
00638    }
00639 
00640    if (!(a->flags & F_AO2I_DONTLOCK))
00641       ao2_unlock(a->c);
00642 
00643    return ret;
00644 }
00645 
00646 /* callback for destroying container.
00647  * we can make it simple as we know what it does
00648  */
00649 static int cd_cb(void *obj, void *arg, int flag)
00650 {
00651    ao2_ref(obj, -1);
00652    return 0;
00653 }
00654    
00655 static void container_destruct(void *_c)
00656 {
00657    struct ao2_container *c = _c;
00658    int i;
00659 
00660    ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
00661 
00662    for (i = 0; i < c->n_buckets; i++) {
00663       struct bucket_list *cur;
00664 
00665       while ((cur = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
00666          ast_free(cur);
00667       }
00668    }
00669 
00670 #ifdef AO2_DEBUG
00671    ast_atomic_fetchadd_int(&ao2.total_containers, -1);
00672 #endif
00673 }
00674 
00675 #ifdef AO2_DEBUG
00676 static int print_cb(void *obj, void *arg, int flag)
00677 {
00678    int *fd = arg;
00679    char *s = (char *)obj;
00680 
00681    ast_cli(*fd, "string <%s>\n", s);
00682    return 0;
00683 }
00684 
00685 /*
00686  * Print stats
00687  */
00688 static int handle_astobj2_stats(int fd, int argc, char *argv[])
00689 {
00690    ast_cli(fd, "Objects    : %d\n", ao2.total_objects);
00691    ast_cli(fd, "Containers : %d\n", ao2.total_containers);
00692    ast_cli(fd, "Memory     : %d\n", ao2.total_mem);
00693    ast_cli(fd, "Locked     : %d\n", ao2.total_locked);
00694    ast_cli(fd, "Refs       : %d\n", ao2.total_refs);
00695    return 0;
00696 }
00697 
00698 /*
00699  * This is testing code for astobj
00700  */
00701 static int handle_astobj2_test(int fd, int argc, char *argv[])
00702 {
00703    struct ao2_container *c1;
00704    int i, lim;
00705    char *obj;
00706    static int prof_id = -1;
00707 
00708    if (prof_id == -1)
00709       prof_id = ast_add_profile("ao2_alloc", 0);
00710 
00711    ast_cli(fd, "argc %d argv %s %s %s\n", argc, argv[0], argv[1], argv[2]);
00712    lim = atoi(argv[2]);
00713    ast_cli(fd, "called astobj_test\n");
00714 
00715    handle_astobj2_stats(fd, 0, NULL);
00716    /*
00717     * allocate a container with no default callback, and no hash function.
00718     * No hash means everything goes in the same bucket.
00719     */
00720    c1 = ao2_container_alloc(100, NULL /* no callback */, NULL /* no hash */);
00721    ast_cli(fd, "container allocated as %p\n", c1);
00722 
00723    /*
00724     * fill the container with objects.
00725     * ao2_alloc() gives us a reference which we pass to the
00726     * container when we do the insert.
00727     */
00728    for (i = 0; i < lim; i++) {
00729       ast_mark(prof_id, 1 /* start */);
00730       obj = ao2_alloc(80, NULL);
00731       ast_mark(prof_id, 0 /* stop */);
00732       ast_cli(fd, "object %d allocated as %p\n", i, obj);
00733       sprintf(obj, "-- this is obj %d --", i);
00734       ao2_link(c1, obj);
00735    }
00736    ast_cli(fd, "testing callbacks\n");
00737    ao2_callback(c1, 0, print_cb, &fd);
00738 
00739    ast_cli(fd, "testing iterators, remove every second object\n");
00740    {
00741       struct ao2_iterator ai;
00742       int x = 0;
00743 
00744       ai = ao2_iterator_init(c1, 0);
00745       while ( (obj = ao2_iterator_next(&ai)) ) {
00746          ast_cli(fd, "iterator on <%s>\n", obj);
00747          if (x++ & 1)
00748             ao2_unlink(c1, obj);
00749          ao2_ref(obj, -1);
00750       }
00751       ast_cli(fd, "testing iterators again\n");
00752       ai = ao2_iterator_init(c1, 0);
00753       while ( (obj = ao2_iterator_next(&ai)) ) {
00754          ast_cli(fd, "iterator on <%s>\n", obj);
00755          ao2_ref(obj, -1);
00756       }
00757    }
00758    ast_cli(fd, "testing callbacks again\n");
00759    ao2_callback(c1, 0, print_cb, &fd);
00760 
00761    ast_verbose("now you should see an error message:\n");
00762    ao2_ref(&i, -1);  /* i is not a valid object so we print an error here */
00763 
00764    ast_cli(fd, "destroy container\n");
00765    ao2_ref(c1, -1);  /* destroy container */
00766    handle_astobj2_stats(fd, 0, NULL);
00767    return 0;
00768 }
00769 
00770 static struct ast_cli_entry cli_astobj2[] = {
00771    { { "astobj2", "stats", NULL },
00772    handle_astobj2_stats, "Print astobj2 statistics", },
00773    { { "astobj2", "test", NULL } , handle_astobj2_test, "Test astobj2", },
00774 };
00775 #endif /* AO2_DEBUG */
00776 
00777 int astobj2_init(void)
00778 {
00779 #ifdef AO2_DEBUG
00780    ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
00781 #endif
00782 
00783    return 0;
00784 }

Generated on Thu Oct 1 13:08:40 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7