示例#1
0
文件: pvapi.c 项目: btriller/kamailio
/*!
 * \brief Free allocated memory of transformation list
 * \param t transformation list
 */
void tr_free(trans_t *t)
{
	trans_t *t0;

	while(t)
	{
		t0 = t;
		t = t->next;
		tr_destroy(t0);
		pkg_free(t0);
	}
}
示例#2
0
文件: trie.c 项目: IFGHou/Unicornscan
int main(int argc, char *argv[]) {
		
	struct trie *t = NULL;
	char keybuf[1024], databuf[1024];
	size_t i;

	tr_compar = charcmp;

	t = malloc(sizeof(struct trie));
	if (!t)
		err(1, "malloc");

	memset(t, 0, sizeof(struct trie));

	stats_init();
	while (fgets(keybuf, 1023, stdin)) {

		keybuf[strlen(keybuf)-1] = '\0'; /* FIXME \r\n */
		
		snprintf(databuf, 1023, "%s is the data as well", keybuf);
	
		stats_depth_0();
		int ret = tr_insert(t, keybuf, strdup(databuf));
		if (ret != 0)
			err(1, "tr_insert");
	}

	stats_dump();

	for (i = 1; i < (size_t)argc; i++) {
		tr_search(t, argv[i]);
	}

	printf("<?xml version='1.0' standalone='no'?>\n"
                "<svg:svg width='100%%' height='100%%' version='1.1' xmlns:svg='http://www.w3.org/2000/svg'>\n");

	t->tr_key='*';
	tr_print(t, 0.0, canvas_width, 512.0, 32.0);
	printf("</svg:svg>\n");

	tr_destroy(t);
	stats_fini();

	return (0);
}
示例#3
0
文件: trie.c 项目: IFGHou/Unicornscan
void tr_destroy(struct trie *t) {

	size_t i;

	for (i = 0; i < t->tr_nmemb; i++) {
		tr_destroy(t->tr_members[i]);
		t->tr_members[i] = TR_POISON1;
	}
	
	if (t->tr_nmemb) {
		free(t->tr_members);
		t->tr_members = TR_POISON2;
	}

	if (t->tr_data) {
		free(t->tr_data);
		t->tr_data = TR_POISON3;
	}

	free(t);
}
示例#4
0
int main(int argc, char *argv[])
{
  TALLOC_CTX *main_ctx=NULL;

  TR_INSTANCE *tr = NULL;
  struct cmdline_args opts;
  struct event_base *ev_base;
  struct tr_socket_event tids_ev;
  struct event *cfgwatch_ev;

  configure_signals();

  /* we're going to be multithreaded, so disable null context tracking */
  talloc_set_abort_fn(tr_abort);
  talloc_disable_null_tracking();
#if TALLOC_DEBUG_ENABLE
  talloc_set_log_fn(tr_talloc_log);
#endif /* TALLOC_DEBUG_ENABLE */
  main_ctx=talloc_new(NULL);

  /* Use standalone logging */
  tr_log_open();

  /***** parse command-line arguments *****/
  /* set defaults */
  opts.config_dir=".";

  /* parse the command line*/
  argp_parse(&argp, argc, argv, 0, 0, &opts);

  /* process options */
  remove_trailing_slash(opts.config_dir);

  /***** create a Trust Router instance *****/
  if (NULL == (tr = tr_create(main_ctx))) {
    tr_crit("Unable to create Trust Router instance, exiting.");
    return 1;
  }

  /***** initialize the trust path query server instance *****/
  if (NULL == (tr->tids = tids_create (tr))) {
    tr_crit("Error initializing Trust Path Query Server instance.");
    return 1;
  }

  /***** initialize the trust router protocol server instance *****/
  if (NULL == (tr->trps = trps_new(tr))) {
    tr_crit("Error initializing Trust Router Protocol Server instance.");
    return 1;
  }

  /***** process configuration *****/
  tr->cfgwatch=tr_cfgwatch_create(tr);
  if (tr->cfgwatch == NULL) {
    tr_crit("Unable to create configuration watcher object, exiting.");
    return 1;
  }
  tr->cfgwatch->config_dir=opts.config_dir;
  tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
  tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
  tr->cfgwatch->update_cookie=(void *)tr;
  if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
    tr_crit("Error reading configuration, exiting.");
    return 1;
  }

  /***** Set up the event loop *****/
  ev_base=tr_event_loop_init(); /* Set up the event loop */
  if (ev_base==NULL) {
    tr_crit("Error initializing event loop.");
    return 1;
  }

  /* already set config_dir, fstat_list and n_files earlier */
  if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
    tr_crit("Error initializing configuration file watcher.");
    return 1;
  }

  /*tr_status_event_init();*/ /* install status reporting events */

  /* install TID server events */
  if (0 != tr_tids_event_init(ev_base,
                              tr->tids,
                              tr->cfg_mgr,
                              tr->trps,
                             &tids_ev)) {
    tr_crit("Error initializing Trust Path Query Server instance.");
    return 1;
  }

  /* install TRP handler events */
  if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
    tr_crit("Error initializing Trust Path Query Server instance.");
    return 1;
  }

  tr_event_loop_run(ev_base); /* does not return until we are done */

  tr_destroy(tr); /* thanks to talloc, should destroy everything */

  talloc_free(main_ctx);
  return 0;
}
示例#5
0
static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
{
  tr_data_t *data;
  int status;
  int i;

  data = (tr_data_t *) malloc (sizeof (*data));
  if (data == NULL)
  {
    ERROR ("tr_create: malloc failed.");
    return (-ENOMEM);
  }
  memset (data, 0, sizeof (*data));

  data->host = NULL;
  data->plugin = NULL;
  data->plugin_instance = NULL;
  /* data->type = NULL; */
  data->type_instance = NULL;

  status = 0;
  for (i = 0; i < ci->children_num; i++)
  {
    oconfig_item_t *child = ci->children + i;

    if ((strcasecmp ("Host", child->key) == 0)
        || (strcasecmp ("Hostname", child->key) == 0))
      status = tr_config_add_action (&data->host, child,
          /* may be empty = */ 0);
    else if (strcasecmp ("Plugin", child->key) == 0)
      status = tr_config_add_action (&data->plugin, child,
          /* may be empty = */ 0);
    else if (strcasecmp ("PluginInstance", child->key) == 0)
      status = tr_config_add_action (&data->plugin_instance, child,
          /* may be empty = */ 1);
#if 0
    else if (strcasecmp ("Type", child->key) == 0)
      status = tr_config_add_action (&data->type, child,
          /* may be empty = */ 0);
#endif
    else if (strcasecmp ("TypeInstance", child->key) == 0)
      status = tr_config_add_action (&data->type_instance, child,
          /* may be empty = */ 1);
    else
    {
      ERROR ("Target `replace': The `%s' configuration option is not understood "
          "and will be ignored.", child->key);
      status = 0;
    }

    if (status != 0)
      break;
  }

  /* Additional sanity-checking */
  while (status == 0)
  {
    if ((data->host == NULL)
        && (data->plugin == NULL)
        && (data->plugin_instance == NULL)
        /* && (data->type == NULL) */
        && (data->type_instance == NULL))
    {
      ERROR ("Target `replace': You need to set at lease one of `Host', "
          "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
      status = -1;
    }

    break;
  }

  if (status != 0)
  {
    tr_destroy ((void *) &data);
    return (status);
  }

  *user_data = data;
  return (0);
} /* }}} int tr_create */
示例#6
0
文件: pvapi.c 项目: btriller/kamailio
char* tr_lookup(str *in, trans_t **tr)
{
	char *p;
	char *p0;
	str tclass;
	tr_export_t *te = NULL;
	trans_t *t = NULL;
	trans_t *t0 = NULL;
	str s;

	if(in==NULL || in->s==NULL || tr==NULL)
		return NULL;

	p = in->s;
	do {
		while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
		if(*p != TR_LBRACKET)
			break;
		p++;

		if((t = tr_new())==NULL) return NULL;

		if(t0==NULL) *tr = t;
		else t0->next = t;
		t0 = t;

		/* find transformation class */
		p = tr_get_class(in, p, &tclass);
		if(p==NULL) goto error;

		/* locate transformation */
		te = tr_lookup_class(&tclass);
		if(te==NULL)
		{
			LM_ERR("unknown transformation: [%.*s] in [%.*s]\n",
				tclass.len, tclass.s, in->len, in->s);
			goto error;
		}

		s.s = p; s.len = in->s + in->len - p;
		p0 = te->tparse(&s, t);
		if(p0==NULL)
			goto error;
		p = p0;

		if(*p != TR_RBRACKET)
		{
			LM_ERR("invalid transformation: %.*s | %c !!\n", in->len,
					in->s, *p);
			goto error;
		}

		p++;
		if(!is_in_str(p, in))
			break;
	} while(1);

	return p;
error:
	LM_ERR("error parsing [%.*s]\n", in->len, in->s);
	t = *tr;
	while(t)
	{
		t0 = t;
		t = t->next;
		tr_destroy(t0);
		pkg_free(t0);
	}
	return NULL;
}
示例#7
0
文件: main.c 项目: glebmak/Structs
int main()
{
    //-------------------------------------------------------
    // init, destroy tests 
    // test, where memory allocation is not successful
    
    root_t *root = tr_init();
    tr_destroy(root);
    tr_destroy(root);
    tr_destroy(NULL);
    
    //-------------------------------------------------------
    
    //-------------------------------------------------------
    // insert & delete & dump tests
    // test, where memory allocation is not successful
    
    root = tr_init();
    
    tr_insert(root, 0);
    tr_insert(root, 10);
    tr_insert(root, -10);
    tr_insert(root, 5);
    tr_insert(root, 15);
    tr_insert(root, 3);

    tr_delete(root, 13);
    tr_delete(root, 15);
    tr_delete(root, 0);

    tr_insert(root, 5);
    tr_insert(root, 6);
    tr_insert(root, 7);
    tr_insert(root, 8);
    tr_insert(root, 9);

    tr_fdump(root, stdout);

    tr_delete(root, 2);
    tr_delete(root, 7);
    tr_delete(root, 5);
    tr_delete(root, 6);
    tr_delete(root, 8);
    tr_delete(root, 9);

    tr_destroy(root);
    tr_insert(root, 0);
    tr_delete(root, 0);
    tr_insert(NULL, 0);
    tr_delete(NULL, 0);
    //-------------------------------------------------------

    //-------------------------------------------------------
    // height & elem & ok tests

    root = tr_init();

    tr_dump(root);

    tr_insert(root, 0);
    tr_elem(root, -1);
    tr_elem(root, 1);
    tr_elem(root, 0);


    tr_dump(NULL);
    tr_dump(root);
    tr_nok(root);
    tr_nok(NULL);

    tr_destroy(root);
    tr_elem(root, 0);
    tr_elem(NULL, 0);
     //-------------------------------------------------------
    

    //-------------------------------------------------------
    //iterator test 
    root = tr_init();
    tr_insert(root, 0);
    tr_insert(root, 10);
    tr_insert(root, -10);
    tr_insert(root, 5);
    tr_insert(root, 15);
    tr_insert(root, 3);


    int status;
    void *node = tr_itfirst(root, NULL);
    node = tr_itfirst(NULL, &status);
    node = tr_itfirst (NULL, NULL);
    node = tr_itfirst(root, &status);

    while (!tr_itislast(root, node, &status)) 
        node = tr_itnext (node);

    while (node != tr_itfirst(root, NULL)) 
        node = tr_itprev (node);


    node = tr_itleft(node);
    node = tr_itright(node);
    printf("%d\n", tr_itgetheight(node));
    printf("%d\n", tr_itgetkey(NULL, &status));

    printf("%d\n", tr_itgetkey(node, &status));

    tr_itleft(NULL);
    tr_itright(NULL);

    tr_itislast(NULL, node, NULL);
    tr_itnext (NULL);
    tr_itprev (NULL);



    tr_destroy(root);

    //-------------------------------------------------------
    return 0;
}