Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:49 2009

## Asterisk developer's documentation

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Directories

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