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: 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
00033
00034
00035
00036
00037
00038
00039
00040
00041 struct __priv_data {
00042 ast_mutex_t lock;
00043 int ref_counter;
00044 ao2_destructor_fn destructor_fn;
00045
00046 size_t data_size;
00047
00048
00049 uint32_t magic;
00050 };
00051
00052 #define AO2_MAGIC 0xa570b123
00053
00054
00055
00056
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
00080 void ao2_bt(void) {}
00081 #else
00082 #include <execinfo.h>
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
00103
00104
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
00126
00127
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
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
00172 if (delta == 0)
00173 return (obj->priv_data.ref_counter);
00174
00175
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
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) {
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
00197
00198
00199 bzero(obj, sizeof(struct astobj2 *) + sizeof(void *) );
00200 free(obj);
00201 }
00202
00203 return ret;
00204 }
00205
00206
00207
00208
00209
00210 void *ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
00211 {
00212
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;
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
00236 return EXTERNAL_OBJ(obj);
00237 }
00238
00239
00240 static void container_destruct(void *c);
00241
00242
00243 AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 struct ao2_container {
00268 ao2_hash_fn hash_fn;
00269 ao2_callback_fn cmp_fn;
00270 int n_buckets;
00271
00272 int elements;
00273
00274 int version;
00275
00276 struct bucket buckets[0];
00277 };
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 static int hash_zero(const void *user_obj, const int flags)
00289 {
00290 return 0;
00291 }
00292
00293
00294
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
00301
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;
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
00323
00324 int ao2_container_count(struct ao2_container *c)
00325 {
00326 return c->elements;
00327 }
00328
00329
00330
00331
00332
00333
00334 struct bucket_list {
00335 AST_LIST_ENTRY(bucket_list) entry;
00336 int version;
00337 struct astobj2 *astobj;
00338 };
00339
00340
00341
00342
00343 void *__ao2_link(struct ao2_container *c, void *user_data, int iax2_hack)
00344 {
00345 int i;
00346
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
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
00387
00388
00389 void *ao2_unlink(struct ao2_container *c, void *user_data)
00390 {
00391 if (INTERNAL_OBJ(user_data) == NULL)
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
00401
00402 static int cb_true(void *user_data, void *arg, int flags)
00403 {
00404 return CMP_MATCH;
00405 }
00406
00407
00408
00409
00410
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;
00417 void *ret = NULL;
00418
00419 if (INTERNAL_OBJ(c) == NULL)
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
00428 #if 0
00429
00430
00431
00432
00433 if (flags & OBJ_POINTER)
00434 cb_fn = match_by_addr;
00435 else
00436 #endif
00437 if (cb_fn == NULL)
00438 cb_fn = cb_true;
00439
00440
00441
00442
00443
00444
00445 if ((flags & OBJ_POINTER))
00446 start = i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
00447 else
00448 i = -1;
00449
00450
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);
00461
00462 for (; i < last ; i++) {
00463
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
00470 if (match == 0) {
00471 continue;
00472 } else if (match == CMP_STOP) {
00473 i = last;
00474 break;
00475 }
00476
00477 if (!(flags & OBJ_NODATA)) {
00478
00479 ret = EXTERNAL_OBJ(cur->astobj);
00480 ao2_ref(ret, 1);
00481 }
00482
00483 if (flags & OBJ_UNLINK) {
00484 struct bucket_list *x = cur;
00485
00486
00487 ast_atomic_fetchadd_int(&c->version, 1);
00488 AST_LIST_REMOVE_CURRENT(&c->buckets[i], entry);
00489
00490 ast_atomic_fetchadd_int(&c->elements, -1);
00491 ao2_ref(EXTERNAL_OBJ(x->astobj), -1);
00492 free(x);
00493 }
00494
00495 if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
00496
00497 i = last;
00498 break;
00499 }
00500 if (!(flags & OBJ_NODATA)) {
00501 #if 0
00502
00503
00504
00505
00506 #endif
00507 }
00508 }
00509 AST_LIST_TRAVERSE_SAFE_END
00510
00511 if (ret) {
00512
00513 break;
00514 }
00515
00516 if (i == c->n_buckets - 1 && (flags & OBJ_POINTER) && (flags & OBJ_CONTINUE)) {
00517
00518 i = -1;
00519 last = start;
00520 }
00521 }
00522 ao2_unlock(c);
00523 return ret;
00524 }
00525
00526
00527
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
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
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
00563
00564
00565 if (a->c->version == a->c_version && (p = a->obj) ) {
00566 if ( (p = AST_LIST_NEXT(p, entry)) )
00567 goto found;
00568
00569 a->bucket++;
00570 a->version = 0;
00571 a->obj = NULL;
00572 }
00573
00574 lim = a->c->n_buckets;
00575
00576
00577
00578
00579
00580
00581
00582 for (; a->bucket < lim; a->bucket++, a->version = 0) {
00583
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
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
00607
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
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
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
00669
00670
00671 c1 = ao2_container_alloc(100, NULL , NULL );
00672 ast_cli(fd, "container allocated as %p\n", c1);
00673
00674
00675
00676
00677
00678
00679 for (i = 0; i < lim; i++) {
00680 ast_mark(prof_id, 1 );
00681 obj = ao2_alloc(80, NULL);
00682 ast_mark(prof_id, 0 );
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);
00714
00715 ast_cli(fd, "destroy container\n");
00716 ao2_ref(c1, -1);
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
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 }