Newer
Older
static int print_cb(void *obj, void *arg, int flag)
struct ast_cli_args *a = (struct ast_cli_args *) arg;
ast_cli(a->fd, "string <%s>\n", s);
return 0;
}
/*
* Print stats
*/
static char *handle_astobj2_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
switch (cmd) {
case CLI_INIT:
e->command = "astobj2 show stats";
e->usage = "Usage: astobj2 show stats\n"
" Show astobj2 show stats\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
ast_cli(a->fd, "Objects : %d\n", ao2.total_objects);
ast_cli(a->fd, "Containers : %d\n", ao2.total_containers);
ast_cli(a->fd, "Memory : %d\n", ao2.total_mem);
ast_cli(a->fd, "Locked : %d\n", ao2.total_locked);
ast_cli(a->fd, "Refs : %d\n", ao2.total_refs);
return CLI_SUCCESS;
}
/*
* This is testing code for astobj
*/
static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
int i, lim;
char *obj;
static int prof_id = -1;
struct ast_cli_args fake_args = { a->fd, 0, NULL };
switch (cmd) {
case CLI_INIT:
e->command = "astobj2 test";
e->usage = "Usage: astobj2 test <num>\n"
" Runs astobj2 test. Creates 'num' objects,\n"
" and test iterators, callbacks and may be other stuff\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 3) {
return CLI_SHOWUSAGE;
}
if (prof_id == -1)
prof_id = ast_add_profile("ao2_alloc", 0);
ast_cli(a->fd, "argc %d argv %s %s %s\n", a->argc, a->argv[0], a->argv[1], a->argv[2]);
lim = atoi(a->argv[2]);
ast_cli(a->fd, "called astobj_test\n");
handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
/*
* allocate a container with no default callback, and no hash function.
* No hash means everything goes in the same bucket.
*/
c1 = ao2_t_container_alloc(100, NULL /* no callback */, NULL /* no hash */,"test");
ast_cli(a->fd, "container allocated as %p\n", c1);
/*
* fill the container with objects.
* ao2_alloc() gives us a reference which we pass to the
* container when we do the insert.
*/
for (i = 0; i < lim; i++) {
ast_mark(prof_id, 1 /* start */);
obj = ao2_t_alloc(80, NULL,"test");
ast_mark(prof_id, 0 /* stop */);
ast_cli(a->fd, "object %d allocated as %p\n", i, obj);
sprintf(obj, "-- this is obj %d --", i);
ao2_link(c1, obj);
/* At this point, the refcount on obj is 2 due to the allocation
* and linking. We can go ahead and reduce the refcount by 1
* right here so that when the container is unreffed later, the
* objects will be freed
*/
ao2_t_ref(obj, -1, "test");
ast_cli(a->fd, "testing callbacks\n");
ao2_t_callback(c1, 0, print_cb, a, "test callback");
ast_cli(a->fd, "testing iterators, remove every second object\n");
int x = 0;
ai = ao2_iterator_init(c1, 0);
while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
ast_cli(a->fd, "iterator on <%s>\n", obj);
ao2_t_unlink(c1, obj,"test");
ao2_t_ref(obj, -1,"test");
ast_cli(a->fd, "testing iterators again\n");
ai = ao2_iterator_init(c1, 0);
while ( (obj = ao2_t_iterator_next(&ai,"test")) ) {
ast_cli(a->fd, "iterator on <%s>\n", obj);
ao2_t_ref(obj, -1,"test");
ast_cli(a->fd, "testing callbacks again\n");
ao2_t_callback(c1, 0, print_cb, a, "test callback");
ast_verbose("now you should see an error message:\n");
ao2_t_ref(&i, -1, ""); /* i is not a valid object so we print an error here */
ast_cli(a->fd, "destroy container\n");
ao2_t_ref(c1, -1, ""); /* destroy container */
handle_astobj2_stats(e, CLI_HANDLER, &fake_args);
return CLI_SUCCESS;
}
static struct ast_cli_entry cli_astobj2[] = {
Jason Parker
committed
AST_CLI_DEFINE(handle_astobj2_stats, "Print astobj2 statistics"),
AST_CLI_DEFINE(handle_astobj2_test, "Test astobj2"),
int astobj2_init(void)
{
ast_cli_register_multiple(cli_astobj2, ARRAY_LEN(cli_astobj2));