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