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: 307273 $")
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
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 = ast_bt_get_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 enum ao2_callback_type {
00124 DEFAULT,
00125 WITH_DATA,
00126 };
00127
00128
00129
00130
00131
00132
00133 #define EXTERNAL_OBJ(_p) ((_p) == NULL ? NULL : (_p)->user_data)
00134
00135
00136
00137 static int internal_ao2_ref(void *user_data, const int delta);
00138 static struct ao2_container *internal_ao2_container_alloc(struct ao2_container *c, const uint n_buckets, ao2_hash_fn *hash_fn,
00139 ao2_callback_fn *cmp_fn);
00140 static struct bucket_entry *internal_ao2_link(struct ao2_container *c, void *user_data, const char *file, int line, const char *func);
00141 static void *internal_ao2_callback(struct ao2_container *c,
00142 const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type,
00143 char *tag, char *file, int line, const char *funcname);
00144 static void *internal_ao2_iterator_next(struct ao2_iterator *a, struct bucket_entry **q);
00145
00146 int __ao2_lock(void *user_data, const char *file, const char *func, int line, const char *var)
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 return __ast_pthread_mutex_lock(file, line, func, var, &p->priv_data.lock);
00158 }
00159
00160 int __ao2_unlock(void *user_data, const char *file, const char *func, int line, const char *var)
00161 {
00162 struct astobj2 *p = INTERNAL_OBJ(user_data);
00163
00164 if (p == NULL)
00165 return -1;
00166
00167 #ifdef AO2_DEBUG
00168 ast_atomic_fetchadd_int(&ao2.total_locked, -1);
00169 #endif
00170
00171 return __ast_pthread_mutex_unlock(file, line, func, var, &p->priv_data.lock);
00172 }
00173
00174 int __ao2_trylock(void *user_data, const char *file, const char *func, int line, const char *var)
00175 {
00176 struct astobj2 *p = INTERNAL_OBJ(user_data);
00177 int ret;
00178
00179 if (p == NULL)
00180 return -1;
00181 ret = __ast_pthread_mutex_trylock(file, line, func, var, &p->priv_data.lock);
00182
00183 #ifdef AO2_DEBUG
00184 if (!ret)
00185 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
00186 #endif
00187 return ret;
00188 }
00189
00190 void *ao2_object_get_lockaddr(void *obj)
00191 {
00192 struct astobj2 *p = INTERNAL_OBJ(obj);
00193
00194 if (p == NULL)
00195 return NULL;
00196
00197 return &p->priv_data.lock;
00198 }
00199
00200
00201
00202
00203
00204
00205 int __ao2_ref_debug(void *user_data, const int delta, char *tag, char *file, int line, const char *funcname)
00206 {
00207 struct astobj2 *obj = INTERNAL_OBJ(user_data);
00208
00209 if (obj == NULL)
00210 return -1;
00211
00212 if (delta != 0) {
00213 FILE *refo = fopen(REF_FILE,"a");
00214 fprintf(refo, "%p %s%d %s:%d:%s (%s) [@%d]\n", user_data, (delta<0? "":"+"), delta, file, line, funcname, tag, obj ? obj->priv_data.ref_counter : -1);
00215 fclose(refo);
00216 }
00217 if (obj->priv_data.ref_counter + delta == 0 && obj->priv_data.destructor_fn != NULL) {
00218 FILE *refo = fopen(REF_FILE,"a");
00219 fprintf(refo, "%p **call destructor** %s:%d:%s (%s)\n", user_data, file, line, funcname, tag);
00220 fclose(refo);
00221 }
00222 return internal_ao2_ref(user_data, delta);
00223 }
00224
00225 int __ao2_ref(void *user_data, const int delta)
00226 {
00227 struct astobj2 *obj = INTERNAL_OBJ(user_data);
00228
00229 if (obj == NULL)
00230 return -1;
00231
00232 return internal_ao2_ref(user_data, delta);
00233 }
00234
00235 static int internal_ao2_ref(void *user_data, const int delta)
00236 {
00237 struct astobj2 *obj = INTERNAL_OBJ(user_data);
00238 int current_value;
00239 int ret;
00240
00241 if (obj == NULL)
00242 return -1;
00243
00244
00245 if (delta == 0)
00246 return (obj->priv_data.ref_counter);
00247
00248
00249 ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
00250 current_value = ret + delta;
00251
00252 #ifdef AO2_DEBUG
00253 ast_atomic_fetchadd_int(&ao2.total_refs, delta);
00254 #endif
00255
00256
00257 if (current_value < 0)
00258 ast_log(LOG_ERROR, "refcount %d on object %p\n", current_value, user_data);
00259
00260 if (current_value <= 0) {
00261 if (obj->priv_data.destructor_fn != NULL) {
00262 obj->priv_data.destructor_fn(user_data);
00263 }
00264
00265 ast_mutex_destroy(&obj->priv_data.lock);
00266 #ifdef AO2_DEBUG
00267 ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
00268 ast_atomic_fetchadd_int(&ao2.total_objects, -1);
00269 #endif
00270
00271
00272
00273 memset(obj, '\0', sizeof(struct astobj2 *) + sizeof(void *) );
00274 ast_free(obj);
00275 }
00276
00277 return ret;
00278 }
00279
00280
00281
00282
00283
00284 static void *internal_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, const char *file, int line, const char *funcname)
00285 {
00286
00287 struct astobj2 *obj;
00288
00289 if (data_size < sizeof(void *))
00290 data_size = sizeof(void *);
00291
00292 #if defined(__AST_DEBUG_MALLOC)
00293 obj = __ast_calloc(1, sizeof(*obj) + data_size, file, line, funcname);
00294 #else
00295 obj = ast_calloc(1, sizeof(*obj) + data_size);
00296 #endif
00297
00298 if (obj == NULL)
00299 return NULL;
00300
00301 ast_mutex_init(&obj->priv_data.lock);
00302 obj->priv_data.magic = AO2_MAGIC;
00303 obj->priv_data.data_size = data_size;
00304 obj->priv_data.ref_counter = 1;
00305 obj->priv_data.destructor_fn = destructor_fn;
00306
00307 #ifdef AO2_DEBUG
00308 ast_atomic_fetchadd_int(&ao2.total_objects, 1);
00309 ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
00310 ast_atomic_fetchadd_int(&ao2.total_refs, 1);
00311 #endif
00312
00313
00314 return EXTERNAL_OBJ(obj);
00315 }
00316
00317 void *__ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, char *tag,
00318 const char *file, int line, const char *funcname, int ref_debug)
00319 {
00320
00321 void *obj;
00322 FILE *refo = ref_debug ? fopen(REF_FILE,"a") : NULL;
00323
00324 if ((obj = internal_ao2_alloc(data_size, destructor_fn, file, line, funcname)) == NULL) {
00325 fclose(refo);
00326 return NULL;
00327 }
00328
00329 if (refo) {
00330 fprintf(refo, "%p =1 %s:%d:%s (%s)\n", obj, file, line, funcname, tag);
00331 fclose(refo);
00332 }
00333
00334
00335 return obj;
00336 }
00337
00338 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
00339 {
00340 return internal_ao2_alloc(data_size, destructor_fn, __FILE__, __LINE__, __FUNCTION__);
00341 }
00342
00343
00344
00345 static void container_destruct(void *c);
00346
00347
00348 static void container_destruct_debug(void *c);
00349
00350
00351 AST_LIST_HEAD_NOLOCK(bucket, bucket_entry);
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 struct ao2_container {
00376 ao2_hash_fn *hash_fn;
00377 ao2_callback_fn *cmp_fn;
00378 int n_buckets;
00379
00380 int elements;
00381
00382 int version;
00383
00384 struct bucket buckets[0];
00385 };
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 static int hash_zero(const void *user_obj, const int flags)
00397 {
00398 return 0;
00399 }
00400
00401
00402
00403
00404 static struct ao2_container *internal_ao2_container_alloc(struct ao2_container *c, const unsigned int n_buckets, ao2_hash_fn *hash_fn,
00405 ao2_callback_fn *cmp_fn)
00406 {
00407
00408
00409
00410 if (!c)
00411 return NULL;
00412
00413 c->version = 1;
00414 c->n_buckets = hash_fn ? n_buckets : 1;
00415 c->hash_fn = hash_fn ? hash_fn : hash_zero;
00416 c->cmp_fn = cmp_fn;
00417
00418 #ifdef AO2_DEBUG
00419 ast_atomic_fetchadd_int(&ao2.total_containers, 1);
00420 #endif
00421
00422 return c;
00423 }
00424
00425 struct ao2_container *__ao2_container_alloc_debug(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
00426 ao2_callback_fn *cmp_fn, char *tag, char *file, int line,
00427 const char *funcname, int ref_debug)
00428 {
00429
00430
00431 const unsigned int num_buckets = hash_fn ? n_buckets : 1;
00432 size_t container_size = sizeof(struct ao2_container) + num_buckets * sizeof(struct bucket);
00433 struct ao2_container *c = __ao2_alloc_debug(container_size, container_destruct_debug, tag, file, line, funcname, ref_debug);
00434
00435 return internal_ao2_container_alloc(c, num_buckets, hash_fn, cmp_fn);
00436 }
00437
00438 struct ao2_container *__ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn *hash_fn,
00439 ao2_callback_fn *cmp_fn)
00440 {
00441
00442
00443
00444 const unsigned int num_buckets = hash_fn ? n_buckets : 1;
00445 size_t container_size = sizeof(struct ao2_container) + num_buckets * sizeof(struct bucket);
00446 struct ao2_container *c = __ao2_alloc(container_size, container_destruct);
00447
00448 return internal_ao2_container_alloc(c, num_buckets, hash_fn, cmp_fn);
00449 }
00450
00451
00452
00453
00454 int ao2_container_count(struct ao2_container *c)
00455 {
00456 return c->elements;
00457 }
00458
00459
00460
00461
00462
00463
00464 struct bucket_entry {
00465 AST_LIST_ENTRY(bucket_entry) entry;
00466 int version;
00467 struct astobj2 *astobj;
00468 };
00469
00470
00471
00472
00473
00474 static struct bucket_entry *internal_ao2_link(struct ao2_container *c, void *user_data, const char *file, int line, const char *func)
00475 {
00476 int i;
00477
00478 struct bucket_entry *p;
00479 struct astobj2 *obj = INTERNAL_OBJ(user_data);
00480
00481 if (obj == NULL)
00482 return NULL;
00483
00484 if (INTERNAL_OBJ(c) == NULL)
00485 return NULL;
00486
00487 p = ast_calloc(1, sizeof(*p));
00488 if (!p)
00489 return NULL;
00490
00491 i = abs(c->hash_fn(user_data, OBJ_POINTER));
00492
00493 ao2_lock(c);
00494 i %= c->n_buckets;
00495 p->astobj = obj;
00496 p->version = ast_atomic_fetchadd_int(&c->version, 1);
00497 AST_LIST_INSERT_TAIL(&c->buckets[i], p, entry);
00498 ast_atomic_fetchadd_int(&c->elements, 1);
00499
00500
00501 return p;
00502 }
00503
00504 void *__ao2_link_debug(struct ao2_container *c, void *user_data, char *tag, char *file, int line, const char *funcname)
00505 {
00506 struct bucket_entry *p = internal_ao2_link(c, user_data, file, line, funcname);
00507
00508 if (p) {
00509 __ao2_ref_debug(user_data, +1, tag, file, line, funcname);
00510 ao2_unlock(c);
00511 }
00512 return p;
00513 }
00514
00515 void *__ao2_link(struct ao2_container *c, void *user_data)
00516 {
00517 struct bucket_entry *p = internal_ao2_link(c, user_data, __FILE__, __LINE__, __PRETTY_FUNCTION__);
00518
00519 if (p) {
00520 __ao2_ref(user_data, +1);
00521 ao2_unlock(c);
00522 }
00523 return p;
00524 }
00525
00526
00527
00528
00529 int ao2_match_by_addr(void *user_data, void *arg, int flags)
00530 {
00531 return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
00532 }
00533
00534
00535
00536
00537
00538 void *__ao2_unlink_debug(struct ao2_container *c, void *user_data, char *tag,
00539 char *file, int line, const char *funcname)
00540 {
00541 if (INTERNAL_OBJ(user_data) == NULL)
00542 return NULL;
00543
00544 __ao2_callback_debug(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data, tag, file, line, funcname);
00545
00546 return NULL;
00547 }
00548
00549 void *__ao2_unlink(struct ao2_container *c, void *user_data)
00550 {
00551 if (INTERNAL_OBJ(user_data) == NULL)
00552 return NULL;
00553
00554 __ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data);
00555
00556 return NULL;
00557 }
00558
00559
00560
00561
00562 static int cb_true(void *user_data, void *arg, int flags)
00563 {
00564 return CMP_MATCH;
00565 }
00566
00567
00568
00569
00570 static int cb_true_data(void *user_data, void *arg, void *data, int flags)
00571 {
00572 return CMP_MATCH;
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583 static void *internal_ao2_callback(struct ao2_container *c,
00584 const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type,
00585 char *tag, char *file, int line, const char *funcname)
00586 {
00587 int i, start, last;
00588 void *ret = NULL;
00589 ao2_callback_fn *cb_default = NULL;
00590 ao2_callback_data_fn *cb_withdata = NULL;
00591 struct ao2_container *multi_container = NULL;
00592 struct ao2_iterator *multi_iterator = NULL;
00593
00594 if (INTERNAL_OBJ(c) == NULL)
00595 return NULL;
00596
00597
00598
00599
00600
00601
00602 if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
00603
00604
00605
00606
00607
00608
00609
00610 if (!(multi_container = __ao2_container_alloc(1, NULL, NULL))) {
00611 return NULL;
00612 }
00613 if (!(multi_iterator = ast_calloc(1, sizeof(*multi_iterator)))) {
00614 ao2_ref(multi_container, -1);
00615 return NULL;
00616 }
00617 }
00618
00619
00620 if (cb_fn == NULL) {
00621 if (type == WITH_DATA) {
00622 cb_withdata = cb_true_data;
00623 } else {
00624 cb_default = cb_true;
00625 }
00626 } else {
00627
00628
00629 if (type == WITH_DATA) {
00630 cb_withdata = cb_fn;
00631 } else {
00632 cb_default = cb_fn;
00633 }
00634 }
00635
00636
00637
00638
00639
00640
00641
00642 if ((flags & OBJ_POINTER))
00643 start = i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
00644 else
00645 start = i = -1;
00646
00647
00648 if (i < 0) {
00649 start = i = 0;
00650 last = c->n_buckets;
00651 } else if ((flags & OBJ_CONTINUE)) {
00652 last = c->n_buckets;
00653 } else {
00654 last = i + 1;
00655 }
00656
00657 ao2_lock(c);
00658
00659 for (; i < last ; i++) {
00660
00661 struct bucket_entry *cur;
00662
00663 AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
00664 int match = (CMP_MATCH | CMP_STOP);
00665
00666 if (type == WITH_DATA) {
00667 match &= cb_withdata(EXTERNAL_OBJ(cur->astobj), arg, data, flags);
00668 } else {
00669 match &= cb_default(EXTERNAL_OBJ(cur->astobj), arg, flags);
00670 }
00671
00672
00673 if (match == 0) {
00674 continue;
00675 } else if (match == CMP_STOP) {
00676 i = last;
00677 break;
00678 }
00679
00680
00681 if (!(flags & OBJ_NODATA)) {
00682
00683 ret = EXTERNAL_OBJ(cur->astobj);
00684 if (!(flags & (OBJ_UNLINK | OBJ_MULTIPLE))) {
00685 if (tag)
00686 __ao2_ref_debug(ret, 1, tag, file, line, funcname);
00687 else
00688 __ao2_ref(ret, 1);
00689 }
00690 }
00691
00692
00693
00694
00695 if (ret && (multi_container != NULL)) {
00696 if (tag) {
00697 __ao2_link_debug(multi_container, ret, tag, file, line, funcname);
00698 } else {
00699 __ao2_link(multi_container, ret);
00700 }
00701 ret = NULL;
00702 }
00703
00704 if (flags & OBJ_UNLINK) {
00705
00706 ast_atomic_fetchadd_int(&c->version, 1);
00707 AST_LIST_REMOVE_CURRENT(entry);
00708
00709 ast_atomic_fetchadd_int(&c->elements, -1);
00710
00711
00712
00713
00714
00715
00716
00717 if (flags & (OBJ_NODATA | OBJ_MULTIPLE)) {
00718 if (tag)
00719 __ao2_ref_debug(EXTERNAL_OBJ(cur->astobj), -1, tag, file, line, funcname);
00720 else
00721 __ao2_ref(EXTERNAL_OBJ(cur->astobj), -1);
00722 }
00723 ast_free(cur);
00724 }
00725
00726 if ((match & CMP_STOP) || !(flags & OBJ_MULTIPLE)) {
00727
00728
00729 i = last;
00730 break;
00731 }
00732 }
00733 AST_LIST_TRAVERSE_SAFE_END;
00734
00735 if (ret) {
00736 break;
00737 }
00738
00739 if (i == c->n_buckets - 1 && (flags & OBJ_POINTER) && (flags & OBJ_CONTINUE)) {
00740
00741 i = -1;
00742 last = start;
00743 }
00744 }
00745 ao2_unlock(c);
00746
00747
00748 if (multi_container != NULL) {
00749 *multi_iterator = ao2_iterator_init(multi_container,
00750 AO2_ITERATOR_DONTLOCK | AO2_ITERATOR_UNLINK | AO2_ITERATOR_MALLOCD);
00751 ao2_ref(multi_container, -1);
00752 return multi_iterator;
00753 } else {
00754 return ret;
00755 }
00756 }
00757
00758 void *__ao2_callback_debug(struct ao2_container *c,
00759 const enum search_flags flags,
00760 ao2_callback_fn *cb_fn, void *arg,
00761 char *tag, char *file, int line, const char *funcname)
00762 {
00763 return internal_ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, tag, file, line, funcname);
00764 }
00765
00766 void *__ao2_callback(struct ao2_container *c, const enum search_flags flags,
00767 ao2_callback_fn *cb_fn, void *arg)
00768 {
00769 return internal_ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, NULL, NULL, 0, NULL);
00770 }
00771
00772 void *__ao2_callback_data_debug(struct ao2_container *c,
00773 const enum search_flags flags,
00774 ao2_callback_data_fn *cb_fn, void *arg, void *data,
00775 char *tag, char *file, int line, const char *funcname)
00776 {
00777 return internal_ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, tag, file, line, funcname);
00778 }
00779
00780 void *__ao2_callback_data(struct ao2_container *c, const enum search_flags flags,
00781 ao2_callback_data_fn *cb_fn, void *arg, void *data)
00782 {
00783 return internal_ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, NULL, NULL, 0, NULL);
00784 }
00785
00786
00787
00788
00789 void *__ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
00790 {
00791 return __ao2_callback_debug(c, flags, c->cmp_fn, arg, tag, file, line, funcname);
00792 }
00793
00794 void *__ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
00795 {
00796 return __ao2_callback(c, flags, c->cmp_fn, arg);
00797 }
00798
00799
00800
00801
00802 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
00803 {
00804 struct ao2_iterator a = {
00805 .c = c,
00806 .flags = flags
00807 };
00808
00809 ao2_ref(c, +1);
00810
00811 return a;
00812 }
00813
00814
00815
00816
00817 void ao2_iterator_destroy(struct ao2_iterator *i)
00818 {
00819 ao2_ref(i->c, -1);
00820 if (i->flags & AO2_ITERATOR_MALLOCD) {
00821 ast_free(i);
00822 } else {
00823 i->c = NULL;
00824 }
00825 }
00826
00827
00828
00829
00830 static void *internal_ao2_iterator_next(struct ao2_iterator *a, struct bucket_entry **q)
00831 {
00832 int lim;
00833 struct bucket_entry *p = NULL;
00834 void *ret = NULL;
00835
00836 *q = NULL;
00837
00838 if (INTERNAL_OBJ(a->c) == NULL)
00839 return NULL;
00840
00841 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
00842 ao2_lock(a->c);
00843
00844
00845
00846
00847 if (a->c->version == a->c_version && (p = a->obj)) {
00848 if ((p = AST_LIST_NEXT(p, entry)))
00849 goto found;
00850
00851 a->bucket++;
00852 a->version = 0;
00853 a->obj = NULL;
00854 }
00855
00856 lim = a->c->n_buckets;
00857
00858
00859
00860
00861
00862
00863
00864 for (; a->bucket < lim; a->bucket++, a->version = 0) {
00865
00866 AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
00867 if (p->version > a->version)
00868 goto found;
00869 }
00870 }
00871
00872 found:
00873 if (p) {
00874 ret = EXTERNAL_OBJ(p->astobj);
00875 if (a->flags & AO2_ITERATOR_UNLINK) {
00876
00877 ast_atomic_fetchadd_int(&a->c->version, 1);
00878 AST_LIST_REMOVE(&a->c->buckets[a->bucket], p, entry);
00879
00880 ast_atomic_fetchadd_int(&a->c->elements, -1);
00881 a->version = 0;
00882 a->obj = NULL;
00883 a->c_version = a->c->version;
00884 ast_free(p);
00885 } else {
00886 a->version = p->version;
00887 a->obj = p;
00888 a->c_version = a->c->version;
00889
00890 *q = p;
00891 }
00892 }
00893
00894 return ret;
00895 }
00896
00897 void *__ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname)
00898 {
00899 struct bucket_entry *p;
00900 void *ret = NULL;
00901
00902 ret = internal_ao2_iterator_next(a, &p);
00903
00904 if (p) {
00905
00906 __ao2_ref_debug(ret, 1, tag, file, line, funcname);
00907 }
00908
00909 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
00910 ao2_unlock(a->c);
00911
00912 return ret;
00913 }
00914
00915 void *__ao2_iterator_next(struct ao2_iterator *a)
00916 {
00917 struct bucket_entry *p = NULL;
00918 void *ret = NULL;
00919
00920 ret = internal_ao2_iterator_next(a, &p);
00921
00922 if (p) {
00923
00924 __ao2_ref(ret, 1);
00925 }
00926
00927 if (!(a->flags & AO2_ITERATOR_DONTLOCK))
00928 ao2_unlock(a->c);
00929
00930 return ret;
00931 }
00932
00933
00934
00935
00936 static int cd_cb(void *obj, void *arg, int flag)
00937 {
00938 __ao2_ref(obj, -1);
00939 return 0;
00940 }
00941
00942 static int cd_cb_debug(void *obj, void *arg, int flag)
00943 {
00944 __ao2_ref_debug(obj, -1, "deref object via container destroy", __FILE__, __LINE__, __PRETTY_FUNCTION__);
00945 return 0;
00946 }
00947
00948 static void container_destruct(void *_c)
00949 {
00950 struct ao2_container *c = _c;
00951 int i;
00952
00953 __ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
00954
00955 for (i = 0; i < c->n_buckets; i++) {
00956 struct bucket_entry *current;
00957
00958 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
00959 ast_free(current);
00960 }
00961 }
00962
00963 #ifdef AO2_DEBUG
00964 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
00965 #endif
00966 }
00967
00968 static void container_destruct_debug(void *_c)
00969 {
00970 struct ao2_container *c = _c;
00971 int i;
00972
00973 __ao2_callback_debug(c, OBJ_UNLINK, cd_cb_debug, NULL, "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__);
00974
00975 for (i = 0; i < c->n_buckets; i++) {
00976 struct bucket_entry *current;
00977
00978 while ((current = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
00979 ast_free(current);
00980 }
00981 }
00982
00983 #ifdef AO2_DEBUG
00984 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
00985 #endif
00986 }
00987
00988 #ifdef AO2_DEBUG
00989 static int print_cb(void *obj, void *arg, int flag)
00990 {
00991 struct ast_cli_args *a = (struct ast_cli_args *) arg;
00992 char *s = (char *)obj;
00993
00994 ast_cli(a->fd, "string <%s>\n", s);
00995 return 0;
00996 }
00997
00998
00999
01000
01001 static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01002 {
01003 switch (cmd) {
01004 case CLI_INIT:
01005 e->command = "astobj2 show stats";
01006 e->usage = "Usage: astobj2 show stats\n"
01007 " Show astobj2 show stats\n";
01008 return NULL;
01009 case CLI_GENERATE:
01010 return NULL;
01011 }
01012 ast_cli(a->fd, "Objects : %d\n", ao2.total_objects);
01013 ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
01014 ast_cli(a->fd, "Memory : %d\n", ao2.total_mem);
01015 ast_cli(a->fd, "Locked : %d\n", ao2.total_locked);
01016 ast_cli(a->fd, "Refs : %d\n", ao2.total_refs);
01017 return CLI_SUCCESS;
01018 }
01019
01020
01021
01022
01023 static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01024 {
01025 struct ao2_container *c1;
01026 int i, lim;
01027 char *obj;
01028 static int prof_id = -1;
01029 struct ast_cli_args fake_args = { a->fd, 0, NULL };
01030
01031 switch (cmd) {
01032 case CLI_INIT:
01033 e->command = "astobj2 test";
01034 e->usage = "Usage: astobj2 test <num>\n"
01035 " Runs astobj2 test. Creates 'num' objects,\n"
01036 " and test iterators, callbacks and may be other stuff\n";
01037 return NULL;
01038 case CLI_GENERATE:
01039 return NULL;
01040 }
01041
01042 if (a->argc != 3) {
01043 return CLI_SHOWUSAGE;
01044 }
01045
01046 if (prof_id == -1)
01047 prof_id = ast_add_profile("ao2_alloc", 0);
01048
01049 ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
01050 lim = atoi(a->argv[2]);
01051 ast_cli(a->fd, "called astobj_test\n");
01052
01053 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
01054
01055
01056
01057
01058 c1 = ao2_t_container_alloc(100, NULL , NULL ,"test");
01059 ast_cli(a->fd, "container allocated as %p\n", c1);
01060
01061
01062
01063
01064
01065
01066 for (i = 0; i < lim; i++) {
01067 ast_mark(prof_id, 1 );
01068 obj = ao2_t_alloc(80, NULL,"test");
01069 ast_mark(prof_id, 0 );
01070 ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
01071 sprintf(obj, "-- this is obj %d --", i);
01072 ao2_link(c1, obj);
01073
01074
01075
01076
01077
01078 ao2_t_ref(obj, -1, "test");
01079 }
01080 ast_cli(a->fd, "testing callbacks\n");
01081 ao2_t_callback(c1, 0, print_cb, a, "test callback");
01082 ast_cli(a->fd, "testing iterators, remove every second object\n");
01083 {
01084 struct ao2_iterator ai;
01085 int x = 0;
01086
01087 ai = ao2_iterator_init(c1, 0);
01088 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
01089 ast_cli(a->fd, "iterator on <%s>\n", obj);
01090 if (x++ & 1)
01091 ao2_t_unlink(c1, obj,"test");
01092 ao2_t_ref(obj, -1,"test");
01093 }
01094 ao2_iterator_destroy(&ai);
01095 ast_cli(a->fd, "testing iterators again\n");
01096 ai = ao2_iterator_init(c1, 0);
01097 while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
01098 ast_cli(a->fd, "iterator on <%s>\n", obj);
01099 ao2_t_ref(obj, -1,"test");
01100 }
01101 ao2_iterator_destroy(&ai);
01102 }
01103 ast_cli(a->fd, "testing callbacks again\n");
01104 ao2_t_callback(c1, 0, print_cb, a, "test callback");
01105
01106 ast_verbose("now you should see an error message:\n");
01107 ao2_t_ref(&i, -1, "");
01108
01109 ast_cli(a->fd, "destroy container\n");
01110 ao2_t_ref(c1, -1, "");
01111 handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
01112 return CLI_SUCCESS;
01113 }
01114
01115 static struct ast_cli_entry cli_astobj2[] = {
01116 AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
01117 AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
01118 };
01119 #endif
01120
01121 int astobj2_init(void)
01122 {
01123 #ifdef AO2_DEBUG
01124 ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
01125 #endif
01126
01127 return 0;
01128 }