00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00032
00033
00034
00035
00036
00037
00038
00039
00040 struct __priv_data {
00041 ast_mutex_t lock;
00042 int ref_counter;
00043 ao2_destructor_fn destructor_fn;
00044
00045 size_t data_size;
00046
00047
00048 uint32_t magic;
00049 };
00050
00051 #define AO2_MAGIC 0xa570b123
00052
00053
00054
00055
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
00079 void ao2_bt(void) {}
00080 #else
00081 #include <execinfo.h>
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
00102
00103
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
00125
00126
00127
00128 #define EXTERNAL_OBJ(_p) ((_p) == NULL ? NULL : (_p)->user_data)
00129
00130
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
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) {
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
00262 if (delta == 0)
00263 return (obj->priv_data.ref_counter);
00264
00265
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
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) {
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
00288
00289
00290 memset(obj, '\0', sizeof(struct astobj2 *) + sizeof(void *) );
00291 free(obj);
00292 }
00293
00294 return ret;
00295 }
00296
00297
00298
00299
00300
00301 static void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
00302 {
00303
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;
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
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
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
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
00357 static void container_destruct(void *c);
00358
00359
00360 static void container_destruct_debug(void *c);
00361
00362
00363 AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 struct ao2_container {
00388 ao2_hash_fn *hash_fn;
00389 ao2_callback_fn *cmp_fn;
00390 int n_buckets;
00391
00392 int elements;
00393
00394 int version;
00395
00396 struct bucket buckets[0];
00397 };
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 static int hash_zero(const void *user_obj, const int flags)
00409 {
00410 return 0;
00411 }
00412
00413
00414
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
00420
00421
00422 if (!c)
00423 return NULL;
00424
00425 c->version = 1;
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
00441
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
00453
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
00463
00464 int ao2_container_count(struct ao2_container *c)
00465 {
00466 return c->elements;
00467 }
00468
00469
00470
00471
00472
00473
00474 struct bucket_list {
00475 AST_LIST_ENTRY(bucket_list) entry;
00476 int version;
00477 struct astobj2 *astobj;
00478 };
00479
00480
00481
00482
00483
00484 static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data)
00485 {
00486 int i;
00487
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
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
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
00546
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)
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)
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
00571
00572 static int cb_true(void *user_data, void *arg, int flags)
00573 {
00574 return CMP_MATCH;
00575 }
00576
00577
00578
00579
00580
00581
00582
00583
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;
00590 void *ret = NULL;
00591
00592 if (INTERNAL_OBJ(c) == NULL)
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
00601 if (cb_fn == NULL)
00602 cb_fn = cb_true;
00603
00604
00605
00606
00607
00608
00609 if ((flags & OBJ_POINTER))
00610 i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
00611 else
00612 i = -1;
00613
00614
00615 if (i < 0) {
00616 i = 0;
00617 last = c->n_buckets;
00618 } else {
00619 last = i + 1;
00620 }
00621
00622 ao2_lock(c);
00623
00624 for (; i < last ; i++) {
00625
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
00632 if (match == 0) {
00633 continue;
00634 } else if (match == CMP_STOP) {
00635 i = last;
00636 break;
00637 }
00638
00639 if (!(flags & OBJ_NODATA)) {
00640
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) {
00649 struct bucket_list *x = cur;
00650
00651
00652 ast_atomic_fetchadd_int(&c->version, 1);
00653 AST_LIST_REMOVE_CURRENT(entry);
00654
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);
00661 }
00662
00663 if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
00664
00665 i = last;
00666 break;
00667 }
00668 if (!(flags & OBJ_NODATA)) {
00669 #if 0
00670
00671
00672
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
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
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
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
00741
00742
00743 if (a->c->version == a->c_version && (p = a->obj) ) {
00744 if ( (p = AST_LIST_NEXT(p, entry)) )
00745 goto found;
00746
00747 a->bucket++;
00748 a->version = 0;
00749 a->obj = NULL;
00750 }
00751
00752 lim = a->c->n_buckets;
00753
00754
00755
00756
00757
00758
00759
00760 for (; a->bucket < lim; a->bucket++, a->version = 0) {
00761
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
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
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
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
00818
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
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
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
00940
00941
00942 c1 = ao2_t_container_alloc(100, NULL , NULL ,"test");
00943 ast_cli(a->fd, "container allocated as %p\n", c1);
00944
00945
00946
00947
00948
00949
00950 for (i = 0; i < lim; i++) {
00951 ast_mark(prof_id, 1 );
00952 obj = ao2_t_alloc(80, NULL,"test");
00953 ast_mark(prof_id, 0 );
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
00958
00959
00960
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, "");
00990
00991 ast_cli(a->fd, "destroy container\n");
00992 ao2_t_ref(c1, -1, "");
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
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 }