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: 123271 $")
00023
00024 #include "asterisk/astobj2.h"
00025 #include "asterisk/utils.h"
00026 #include "asterisk/cli.h"
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 struct __priv_data {
00039 ast_mutex_t lock;
00040 int ref_counter;
00041 ao2_destructor_fn destructor_fn;
00042
00043 size_t data_size;
00044
00045
00046 uint32_t magic;
00047 };
00048
00049 #define AO2_MAGIC 0xa570b123
00050
00051
00052
00053
00054
00055 struct astobj2 {
00056 struct __priv_data priv_data;
00057 void *user_data[0];
00058 };
00059
00060 #ifdef AST_DEVMODE
00061 #define AO2_DEBUG 1
00062 #endif
00063
00064 #ifdef AO2_DEBUG
00065 struct ao2_stats {
00066 volatile int total_objects;
00067 volatile int total_mem;
00068 volatile int total_containers;
00069 volatile int total_refs;
00070 volatile int total_locked;
00071 };
00072
00073 static struct ao2_stats ao2;
00074 #endif
00075
00076 #ifndef HAVE_BKTR
00077 void ao2_bt(void) {}
00078 #else
00079 #include <execinfo.h>
00080
00081 void ao2_bt(void)
00082 {
00083 int c, i;
00084 #define N1 20
00085 void *addresses[N1];
00086 char **strings;
00087
00088 c = backtrace(addresses, N1);
00089 strings = backtrace_symbols(addresses,c);
00090 ast_verbose("backtrace returned: %d\n", c);
00091 for(i = 0; i < c; i++) {
00092 ast_verbose("%d: %p %s\n", i, addresses[i], strings[i]);
00093 }
00094 free(strings);
00095 }
00096 #endif
00097
00098
00099
00100
00101
00102
00103 static inline struct astobj2 *INTERNAL_OBJ(void *user_data)
00104 {
00105 struct astobj2 *p;
00106
00107 if (!user_data) {
00108 ast_log(LOG_ERROR, "user_data is NULL\n");
00109 return NULL;
00110 }
00111
00112 p = (struct astobj2 *) ((char *) user_data - sizeof(*p));
00113 if (AO2_MAGIC != (p->priv_data.magic) ) {
00114 ast_log(LOG_ERROR, "bad magic number 0x%x for %p\n", p->priv_data.magic, p);
00115 p = NULL;
00116 }
00117
00118 return p;
00119 }
00120
00121
00122
00123
00124
00125
00126 #define EXTERNAL_OBJ(_p) ((_p) == NULL ? NULL : (_p)->user_data)
00127
00128 int ao2_lock(void *user_data)
00129 {
00130 struct astobj2 *p = INTERNAL_OBJ(user_data);
00131
00132 if (p == NULL)
00133 return -1;
00134
00135 #ifdef AO2_DEBUG
00136 ast_atomic_fetchadd_int(&ao2.total_locked, 1);
00137 #endif
00138
00139 return ast_mutex_lock(&p->priv_data.lock);
00140 }
00141
00142 int ao2_unlock(void *user_data)
00143 {
00144 struct astobj2 *p = INTERNAL_OBJ(user_data);
00145
00146 if (p == NULL)
00147 return -1;
00148
00149 #ifdef AO2_DEBUG
00150 ast_atomic_fetchadd_int(&ao2.total_locked, -1);
00151 #endif
00152
00153 return ast_mutex_unlock(&p->priv_data.lock);
00154 }
00155
00156
00157
00158
00159 int ao2_ref(void *user_data, const int delta)
00160 {
00161 int current_value;
00162 int ret;
00163 struct astobj2 *obj = INTERNAL_OBJ(user_data);
00164
00165 if (obj == NULL)
00166 return -1;
00167
00168
00169 if (delta == 0)
00170 return (obj->priv_data.ref_counter);
00171
00172
00173 ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
00174 current_value = ret + delta;
00175
00176 #ifdef AO2_DEBUG
00177 ast_atomic_fetchadd_int(&ao2.total_refs, delta);
00178 #endif
00179
00180
00181 if (current_value < 0)
00182 ast_log(LOG_ERROR, "refcount %d on object %p\n", current_value, user_data);
00183
00184 if (current_value <= 0) {
00185 if (obj->priv_data.destructor_fn != NULL)
00186 obj->priv_data.destructor_fn(user_data);
00187
00188 ast_mutex_destroy(&obj->priv_data.lock);
00189 #ifdef AO2_DEBUG
00190 ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
00191 ast_atomic_fetchadd_int(&ao2.total_objects, -1);
00192 #endif
00193
00194
00195
00196 bzero(obj, sizeof(struct astobj2 *) + sizeof(void *) );
00197 free(obj);
00198 }
00199
00200 return ret;
00201 }
00202
00203
00204
00205
00206
00207 void *ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn)
00208 {
00209
00210 struct astobj2 *obj;
00211
00212 if (data_size < sizeof(void *))
00213 data_size = sizeof(void *);
00214
00215 obj = ast_calloc(1, sizeof(*obj) + data_size);
00216
00217 if (obj == NULL)
00218 return NULL;
00219
00220 ast_mutex_init(&obj->priv_data.lock);
00221 obj->priv_data.magic = AO2_MAGIC;
00222 obj->priv_data.data_size = data_size;
00223 obj->priv_data.ref_counter = 1;
00224 obj->priv_data.destructor_fn = destructor_fn;
00225
00226 #ifdef AO2_DEBUG
00227 ast_atomic_fetchadd_int(&ao2.total_objects, 1);
00228 ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
00229 ast_atomic_fetchadd_int(&ao2.total_refs, 1);
00230 #endif
00231
00232
00233 return EXTERNAL_OBJ(obj);
00234 }
00235
00236
00237 static void container_destruct(void *c);
00238
00239
00240 AST_LIST_HEAD_NOLOCK(bucket, bucket_list);
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 struct ao2_container {
00265 ao2_hash_fn hash_fn;
00266 ao2_callback_fn cmp_fn;
00267 int n_buckets;
00268
00269 int elements;
00270
00271 int version;
00272
00273 struct bucket buckets[0];
00274 };
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 static int hash_zero(const void *user_obj, const int flags)
00286 {
00287 return 0;
00288 }
00289
00290
00291
00292
00293 struct ao2_container *
00294 ao2_container_alloc(const uint n_buckets, ao2_hash_fn hash_fn,
00295 ao2_callback_fn cmp_fn)
00296 {
00297
00298
00299 size_t container_size = sizeof(struct ao2_container) + n_buckets * sizeof(struct bucket);
00300
00301 struct ao2_container *c = ao2_alloc(container_size, container_destruct);
00302
00303 if (!c)
00304 return NULL;
00305
00306 c->version = 1;
00307 c->n_buckets = n_buckets;
00308 c->hash_fn = hash_fn ? hash_fn : hash_zero;
00309 c->cmp_fn = cmp_fn;
00310
00311 #ifdef AO2_DEBUG
00312 ast_atomic_fetchadd_int(&ao2.total_containers, 1);
00313 #endif
00314
00315 return c;
00316 }
00317
00318
00319
00320
00321 int ao2_container_count(struct ao2_container *c)
00322 {
00323 return c->elements;
00324 }
00325
00326
00327
00328
00329
00330
00331 struct bucket_list {
00332 AST_LIST_ENTRY(bucket_list) entry;
00333 int version;
00334 struct astobj2 *astobj;
00335 };
00336
00337
00338
00339
00340 void *__ao2_link(struct ao2_container *c, void *user_data, int iax2_hack)
00341 {
00342 int i;
00343
00344 struct bucket_list *p;
00345 struct astobj2 *obj = INTERNAL_OBJ(user_data);
00346
00347 if (!obj)
00348 return NULL;
00349
00350 if (INTERNAL_OBJ(c) == NULL)
00351 return NULL;
00352
00353 p = ast_calloc(1, sizeof(*p));
00354 if (!p)
00355 return NULL;
00356
00357 i = c->hash_fn(user_data, OBJ_POINTER);
00358
00359 ao2_lock(c);
00360 i %= c->n_buckets;
00361 p->astobj = obj;
00362 p->version = ast_atomic_fetchadd_int(&c->version, 1);
00363 if (iax2_hack)
00364 AST_LIST_INSERT_HEAD(&c->buckets[i], p, entry);
00365 else
00366 AST_LIST_INSERT_TAIL(&c->buckets[i], p, entry);
00367 ast_atomic_fetchadd_int(&c->elements, 1);
00368 ao2_ref(user_data, +1);
00369 ao2_unlock(c);
00370
00371 return p;
00372 }
00373
00374
00375
00376
00377 int ao2_match_by_addr(void *user_data, void *arg, int flags)
00378 {
00379 return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0;
00380 }
00381
00382
00383
00384
00385
00386 void *ao2_unlink(struct ao2_container *c, void *user_data)
00387 {
00388 if (INTERNAL_OBJ(user_data) == NULL)
00389 return NULL;
00390
00391 ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data);
00392
00393 return NULL;
00394 }
00395
00396
00397
00398
00399 static int cb_true(void *user_data, void *arg, int flags)
00400 {
00401 return CMP_MATCH;
00402 }
00403
00404
00405
00406
00407
00408
00409 void *ao2_callback(struct ao2_container *c,
00410 const enum search_flags flags,
00411 ao2_callback_fn cb_fn, void *arg)
00412 {
00413 int i, last;
00414 void *ret = NULL;
00415
00416 if (INTERNAL_OBJ(c) == NULL)
00417 return NULL;
00418
00419 if ((flags & (OBJ_MULTIPLE | OBJ_NODATA)) == OBJ_MULTIPLE) {
00420 ast_log(LOG_WARNING, "multiple data return not implemented yet (flags %x)\n", flags);
00421 return NULL;
00422 }
00423
00424
00425 #if 0
00426
00427
00428
00429
00430 if (flags & OBJ_POINTER)
00431 cb_fn = match_by_addr;
00432 else
00433 #endif
00434 if (cb_fn == NULL)
00435 cb_fn = cb_true;
00436
00437
00438
00439
00440
00441
00442 if ((flags & OBJ_POINTER))
00443 i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
00444 else
00445 i = -1;
00446
00447
00448 if (i < 0) {
00449 i = 0;
00450 last = c->n_buckets;
00451 } else {
00452 last = i + 1;
00453 }
00454
00455 ao2_lock(c);
00456
00457 for (; i < last ; i++) {
00458
00459 struct bucket_list *cur;
00460
00461 AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) {
00462 int match = cb_fn(EXTERNAL_OBJ(cur->astobj), arg, flags) & (CMP_MATCH | CMP_STOP);
00463
00464
00465 if (match == 0) {
00466 continue;
00467 } else if (match == CMP_STOP) {
00468 i = last;
00469 break;
00470 }
00471
00472 if (!(flags & OBJ_NODATA)) {
00473
00474 ret = EXTERNAL_OBJ(cur->astobj);
00475 ao2_ref(ret, 1);
00476 }
00477
00478 if (flags & OBJ_UNLINK) {
00479 struct bucket_list *x = cur;
00480
00481
00482 ast_atomic_fetchadd_int(&c->version, 1);
00483 AST_LIST_REMOVE_CURRENT(&c->buckets[i], entry);
00484
00485 ast_atomic_fetchadd_int(&c->elements, -1);
00486 ao2_ref(EXTERNAL_OBJ(x->astobj), -1);
00487 free(x);
00488 }
00489
00490 if ((match & CMP_STOP) || (flags & OBJ_MULTIPLE) == 0) {
00491
00492 i = last;
00493 break;
00494 }
00495 if (!(flags & OBJ_NODATA)) {
00496 #if 0
00497
00498
00499
00500
00501 #endif
00502 }
00503 }
00504 AST_LIST_TRAVERSE_SAFE_END
00505 }
00506 ao2_unlock(c);
00507 return ret;
00508 }
00509
00510
00511
00512
00513 void *ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
00514 {
00515 return ao2_callback(c, flags, c->cmp_fn, arg);
00516 }
00517
00518
00519
00520
00521 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
00522 {
00523 struct ao2_iterator a = {
00524 .c = c,
00525 .flags = flags
00526 };
00527
00528 return a;
00529 }
00530
00531
00532
00533
00534 void * ao2_iterator_next(struct ao2_iterator *a)
00535 {
00536 int lim;
00537 struct bucket_list *p = NULL;
00538 void *ret = NULL;
00539
00540 if (INTERNAL_OBJ(a->c) == NULL)
00541 return NULL;
00542
00543 if (!(a->flags & F_AO2I_DONTLOCK))
00544 ao2_lock(a->c);
00545
00546
00547
00548
00549 if (a->c->version == a->c_version && (p = a->obj) ) {
00550 if ( (p = AST_LIST_NEXT(p, entry)) )
00551 goto found;
00552
00553 a->bucket++;
00554 a->version = 0;
00555 a->obj = NULL;
00556 }
00557
00558 lim = a->c->n_buckets;
00559
00560
00561
00562
00563
00564
00565
00566 for (; a->bucket < lim; a->bucket++, a->version = 0) {
00567
00568 AST_LIST_TRAVERSE(&a->c->buckets[a->bucket], p, entry) {
00569 if (p->version > a->version)
00570 goto found;
00571 }
00572 }
00573
00574 found:
00575 if (p) {
00576 a->version = p->version;
00577 a->obj = p;
00578 a->c_version = a->c->version;
00579 ret = EXTERNAL_OBJ(p->astobj);
00580
00581 ao2_ref(ret, 1);
00582 }
00583
00584 if (!(a->flags & F_AO2I_DONTLOCK))
00585 ao2_unlock(a->c);
00586
00587 return ret;
00588 }
00589
00590
00591
00592
00593 static int cd_cb(void *obj, void *arg, int flag)
00594 {
00595 ao2_ref(obj, -1);
00596 return 0;
00597 }
00598
00599 static void container_destruct(void *_c)
00600 {
00601 struct ao2_container *c = _c;
00602 int i;
00603
00604 ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
00605
00606 for (i = 0; i < c->n_buckets; i++) {
00607 struct bucket_list *cur;
00608
00609 while ((cur = AST_LIST_REMOVE_HEAD(&c->buckets[i], entry))) {
00610 ast_free(cur);
00611 }
00612 }
00613
00614 #ifdef AO2_DEBUG
00615 ast_atomic_fetchadd_int(&ao2.total_containers, -1);
00616 #endif
00617 }
00618
00619 #ifdef AO2_DEBUG
00620 static int print_cb(void *obj, void *arg, int flag)
00621 {
00622 int *fd = arg;
00623 char *s = (char *)obj;
00624
00625 ast_cli(*fd, "string <%s>\n", s);
00626 return 0;
00627 }
00628
00629
00630
00631
00632 static int handle_astobj2_stats(int fd, int argc, char *argv[])
00633 {
00634 ast_cli(fd, "Objects : %d\n", ao2.total_objects);
00635 ast_cli(fd, "Containers : %d\n", ao2.total_containers);
00636 ast_cli(fd, "Memory : %d\n", ao2.total_mem);
00637 ast_cli(fd, "Locked : %d\n", ao2.total_locked);
00638 ast_cli(fd, "Refs : %d\n", ao2.total_refs);
00639 return 0;
00640 }
00641
00642
00643
00644
00645 static int handle_astobj2_test(int fd, int argc, char *argv[])
00646 {
00647 struct ao2_container *c1;
00648 int i, lim;
00649 char *obj;
00650 static int prof_id = -1;
00651
00652 if (prof_id == -1)
00653 prof_id = ast_add_profile("ao2_alloc", 0);
00654
00655 ast_cli(fd, "argc %d argv %s %s %s\n", argc, argv[0], argv[1], argv[2]);
00656 lim = atoi(argv[2]);
00657 ast_cli(fd, "called astobj_test\n");
00658
00659 handle_astobj2_stats(fd, 0, NULL);
00660
00661
00662
00663
00664 c1 = ao2_container_alloc(100, NULL , NULL );
00665 ast_cli(fd, "container allocated as %p\n", c1);
00666
00667
00668
00669
00670
00671
00672 for (i = 0; i < lim; i++) {
00673 ast_mark(prof_id, 1 );
00674 obj = ao2_alloc(80, NULL);
00675 ast_mark(prof_id, 0 );
00676 ast_cli(fd, "object %d allocated as %p\n", i, obj);
00677 sprintf(obj, "-- this is obj %d --", i);
00678 ao2_link(c1, obj);
00679 }
00680 ast_cli(fd, "testing callbacks\n");
00681 ao2_callback(c1, 0, print_cb, &fd);
00682
00683 ast_cli(fd, "testing iterators, remove every second object\n");
00684 {
00685 struct ao2_iterator ai;
00686 int x = 0;
00687
00688 ai = ao2_iterator_init(c1, 0);
00689 while ( (obj = ao2_iterator_next(&ai)) ) {
00690 ast_cli(fd, "iterator on <%s>\n", obj);
00691 if (x++ & 1)
00692 ao2_unlink(c1, obj);
00693 ao2_ref(obj, -1);
00694 }
00695 ast_cli(fd, "testing iterators again\n");
00696 ai = ao2_iterator_init(c1, 0);
00697 while ( (obj = ao2_iterator_next(&ai)) ) {
00698 ast_cli(fd, "iterator on <%s>\n", obj);
00699 ao2_ref(obj, -1);
00700 }
00701 }
00702 ast_cli(fd, "testing callbacks again\n");
00703 ao2_callback(c1, 0, print_cb, &fd);
00704
00705 ast_verbose("now you should see an error message:\n");
00706 ao2_ref(&i, -1);
00707
00708 ast_cli(fd, "destroy container\n");
00709 ao2_ref(c1, -1);
00710 handle_astobj2_stats(fd, 0, NULL);
00711 return 0;
00712 }
00713
00714 static struct ast_cli_entry cli_astobj2[] = {
00715 { { "astobj2", "stats", NULL },
00716 handle_astobj2_stats, "Print astobj2 statistics", },
00717 { { "astobj2", "test", NULL } , handle_astobj2_test, "Test astobj2", },
00718 };
00719 #endif
00720
00721 int astobj2_init(void)
00722 {
00723 #ifdef AO2_DEBUG
00724 ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));
00725 #endif
00726
00727 return 0;
00728 }