Exemplo n.º 1
0
/* hash_map_t */
void _type_init_hash_map(const void* cpv_input, void* pv_output)
{
    bool_t b_result = false;
    assert(cpv_input != NULL && pv_output != NULL);

    b_result = _create_hash_map_auxiliary((hash_map_t*)cpv_input, (char*)pv_output);
    assert(b_result);
    hash_map_init((hash_map_t*)cpv_input);
}
Exemplo n.º 2
0
static inline int __container_hash_map_init(container_t *ct,uint32_t data_size)
{
	hash_map_t *hmap =  ct->priv.hmap;

	dbg_str(DBG_CONTAINER_DETAIL,"__container_hash_map_init");

	return  hash_map_init(hmap,//hash_map_t *hmap,
			hmap->key_size,//uint32_t key_size,
			data_size,//uint32_t data_size,
			hmap->bucket_size,//uint32_t bucket_size,
			hmap->hash_func,//hash_func_fpt hash_func,
			hmap->key_cmp_func);//key_cmp_fpt key_cmp_func);
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: eerimoq/simba
int test_add_get_remove(struct harness_t *harness)
{
    struct hash_map_t map;
    struct hash_map_bucket_t buckets[8];
    struct hash_map_entry_t entries[4];

    hash_map_init(&map,
                  buckets,
                  membersof(buckets),
                  entries,
                  membersof(entries),
                  hash);

    /* Add three entries. */
    BTASSERT(hash_map_add(&map, 37, (void *)34) == 0);
    BTASSERT(hash_map_add(&map, 38, (void *)35) == 0);
    BTASSERT(hash_map_add(&map, 39, (void *)36) == 0);
    BTASSERT(hash_map_add(&map, 39, (void *)36) == 0);

    /* Get them. */
    BTASSERT(hash_map_get(&map, 38) == (void *)35);
    BTASSERT(hash_map_get(&map, 39) == (void *)36);
    BTASSERT(hash_map_get(&map, 37) == (void *)34);

    /* Remove first two. */
    BTASSERT(hash_map_remove(&map, 37) == 0);
    BTASSERT(hash_map_remove(&map, 38) == 0);
    BTASSERT(hash_map_remove(&map, 38) == -1);

    /* Get removed entries. */
    BTASSERT(hash_map_get(&map, 37) == NULL);
    BTASSERT(hash_map_get(&map, 38) == NULL);

    /* Get, remove and get last entry. */
    BTASSERT(hash_map_get(&map, 39) == (void *)36);
    BTASSERT(hash_map_remove(&map, 39) == 0);
    BTASSERT(hash_map_remove(&map, 39) == -1);
    BTASSERT(hash_map_get(&map, 39) == NULL);

    /* Add one entry over limit. */
    BTASSERT(hash_map_add(&map, 37, (void *)4) == 0);
    BTASSERT(hash_map_add(&map, 39, (void *)5) == 0);
    BTASSERT(hash_map_add(&map, 41, (void *)6) == 0);
    BTASSERT(hash_map_add(&map, 43, (void *)7) == 0);
    BTASSERT(hash_map_add(&map, 45, (void *)8) == -ENOMEM);

    return (0);
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: eerimoq/simba
static void init(void)
{
    long number;
    struct fat16_dir_t dir;
    struct fat16_dir_entry_t entry;
    struct song_t *song_p;
    uint32_t seconds;
    fat16_read_t read;
    fat16_write_t write;
    void * arg_p;

    sys_start();
    uart_module_init();

    uart_init(&uart, &uart_device[0], 38400, qinbuf, sizeof(qinbuf));
    uart_start(&uart);

    sys_set_stdout(&uart.chout);

    std_printf(sys_get_info());

    fs_command_init(&cmd_list, FSTR("/list"), cmd_list_cb, NULL);
    fs_command_register(&cmd_list);
    fs_command_init(&cmd_play, FSTR("/play"), cmd_play_cb, NULL);
    fs_command_register(&cmd_play);
    fs_command_init(&cmd_pause, FSTR("/pause"), cmd_pause_cb, NULL);
    fs_command_register(&cmd_pause);
    fs_command_init(&cmd_next, FSTR("/next"), cmd_next_cb, NULL);
    fs_command_register(&cmd_next);
    fs_command_init(&cmd_prev, FSTR("/prev"), cmd_prev_cb, NULL);
    fs_command_register(&cmd_prev);
    fs_command_init(&cmd_stop, FSTR("/stop"), cmd_stop_cb, NULL);
    fs_command_register(&cmd_stop);
    fs_command_init(&cmd_repeat, FSTR("/repeat"), cmd_repeat_cb, NULL);
    fs_command_register(&cmd_repeat);
    fs_command_init(&cmd_set_bits_per_sample,
                    FSTR("/set_bits_per_sample"),
                    cmd_set_bits_per_sample_cb,
                    NULL);
    fs_command_register(&cmd_set_bits_per_sample);

    if (storage_init(&read, &write, &arg_p) != 0) {
        std_printf(FSTR("storage init failed\r\n"));
        return;
    }

    std_printf(FSTR("initializing fat16\r\n"));
    fat16_init(&fs, read, write, arg_p, 0);

    std_printf(FSTR("fat16 initialized\r\n"));

    if (fat16_mount(&fs) != 0) {
        std_printf(FSTR("failed to mount fat16\r\n"));
        return;
    }

    std_printf(FSTR("fat16 mounted\r\n"));

    event_init(&event);

    exti_module_init();

    /* Initialize the buttons. */
    exti_init(&buttons[0],
              &exti_d18_dev,
              EXTI_TRIGGER_FALLING_EDGE,
              on_button_play,
              NULL);
    exti_start(&buttons[0]);
    exti_init(&buttons[1],
              &exti_d19_dev,
              EXTI_TRIGGER_FALLING_EDGE,
              on_button_prev,
              NULL);
    exti_start(&buttons[1]);
    exti_init(&buttons[2],
              &exti_d20_dev,
              EXTI_TRIGGER_FALLING_EDGE,
              on_button_next,
              NULL);
    exti_start(&buttons[2]);
    exti_init(&buttons[3],
              &exti_d21_dev,
              EXTI_TRIGGER_FALLING_EDGE,
              on_button_stop,
              NULL);
    exti_start(&buttons[3]);

    dac_init(&dac,
             &dac_0_dev,
             &pin_dac0_dev,
             &pin_dac1_dev,
             2 * SAMPLES_PER_SOCOND);

    hash_map_init(&song_map,
                  buckets,
                  membersof(buckets),
                  entries,
                  membersof(entries),
                  hash_number);

    sem_init(&sem, 0, 1);

    music_player_init(&music_player,
                      &fs,
                      &dac,
                      get_current_song_path,
                      get_next_song_path,
                      NULL);

    /* Initialize the song number. */
    current_song = FIRST_SONG_NUMBER;
    number = FIRST_SONG_NUMBER;

    /* Add songs to the hash map. */
    fat16_dir_open(&fs, &dir, ".", O_READ);

    while (fat16_dir_read(&dir, &entry) == 1) {
        if (number - FIRST_SONG_NUMBER == membersof(songs)) {
            std_printf("Maximum number of songs already added. "
                       "Skipping the rest of the songs.\r\n");
            break;
        }

        /* Skip folders. */
        if (entry.is_dir == 1) {
            continue;
        }

        song_p = &songs[number - FIRST_SONG_NUMBER];

        /* Initialize the song entry. */
        song_p->number = number;
        strcpy(song_p->name, entry.name);
        seconds = (entry.size / 4 / SAMPLES_PER_SOCOND);
        song_p->minutes = (seconds / 60);
        song_p->seconds = (seconds % 60);

        std_printf("Adding song %s to playlist.\r\n",
                   entry.name);

        hash_map_add(&song_map, number, song_p);
        number++;
    }

    fat16_dir_close(&dir);

    last_song_number = (number - 1);

    music_player_start(&music_player);
}
Exemplo n.º 5
0
int main(){
	
	printf("ok, it is done\n");
	lru_cache_t * lru;
	hash_map_t * hash_map;
	int res = hash_map_init(&hash_map, &simple_hash_function, &key_cmp_cbf, &key_free_cbf, & key_clone_cbf);
	printf("hash init result: %d\n", res);
	int rest = lru_cache_init(&lru, &simple_hash_function, &key_cmp_cbf, &key_free_cbf, &key_clone_cbf);
	printf("lru init result: %d\n", rest);
	lru_dump(lru, &value_to_string, &key_to_string);
	int i = 1;
	test_key_t key;
	test_value_t value;
	test_value_t * vp;
	int ret = 0;
	while(i){
		printf("0: quit\n1: insert\n2: lookup\n3: delete\nyour choice:");
		scanf("%d", &i);
		switch(i){
		case 1:
			printf("key:");
			scanf("%19s", key.key);
			printf("got key: %s\nvalue:", key.key);
			scanf("%19s", value.value);
			if(value.value[0] == '0') i=0;
			ret = lru_cache_insert(lru, (const void *)(&key), (const void *)(&value), &value_clone_cbf, &value_free_cbf); 
			if(!ret){
				printf("insert (%s, %s) failed", key.key, value.value);
			}
			break;
		case 3:
			ret = lru_delete_auto(lru);
			if(!ret){
				printf("delete failed\n");
			}
			break;
		case 2:
			printf("key:");
			scanf("%19s", key.key);
			ret = lru_cache_get(lru, (const void *) &key, (void **)(&vp));
			if(!ret || !vp){
				printf("key does not exist\n");
			}
			else{
				printf("key=%s, value=%s\n", key_to_string((const void *)&key), value_to_string((const void *)vp));
			}
			break;
		default:
			i = 0;
			break;
		}
		lru_dump(lru, value_to_string, key_to_string);
	}
	printf("quit cache; test hash map");
	i = 1;
	while(i){
		printf("0: quit\n1: insert\n2: lookup\n3: delete\nyour choice:");
		scanf("%d", &i);
		switch(i){
		case 1:
			printf("key:");
			scanf("%19s", key.key);
			printf("got key: %s\nvalue:", key.key);
			scanf("%19s", value.value);
			if(value.value[0] == '0') i=0;
			ret = hash_map_insert(hash_map, (const void *)(&key), (const void *)(&value), &value_clone_cbf, &value_free_cbf); 
			if(!ret){
				printf("insert (%s, %s) failed", key.key, value.value);
			}
			break;
		case 3:
			printf("key:");
			scanf("%19s", key.key);
			ret = hash_map_delete_entry(hash_map, (const void *) &key);
			if(!ret){
				printf("delete failed\n");
			}
			break;
		case 2:
			printf("key:");
			scanf("%19s", key.key);
			ret = hash_map_lookup(hash_map, (const void *) &key, (void **)(&vp));
			if(!ret || !vp){
				printf("key does not exist\n");
			}
			else{
				printf("key=%s, value=%s\n", key_to_string((const void *)&key), value_to_string((const void *)vp));
			}
			break;
		default:
			i = 0;
			break;
		}
		hash_map_dump(hash_map, &key_to_string, &value_to_string);
	}
	hash_map_fini(hash_map);
	printf("quit hash map\n");
	
	return 0;
}
Exemplo n.º 6
0
int main(int argc, char** argv) {

  unsigned i;
  struct timeval tv;
  double sum = 0;
  double min_time_per_op = -1;
  double min_of_max = -1;

  grt_random_init();

  int got_params = get_params();
  hash_map_init(argc, argv, params[SHARE_POWER]);
  if (!got_params) {
    if (grt_id == 0) {
      fprintf(stderr, "*** USING DEFAULT PARAMETERS ***\n");
    }
    grt_barrier();
  }

  keys = malloc(MY_NUM_OPS * sizeof(grt_word_t));
  values = malloc(MY_NUM_OPS * sizeof(grt_word_t));
  int nth = 4*grt_distrib_start(params[NUM_OPS])+1;
  grt_random_nth(grt_distrib_start(nth));
  for (i = 0; i < MY_NUM_OPS; ++i) {
    keys[i] = RAND_MAX * grt_random_next();
    values[i] = RAND_MAX * grt_random_next();
  }

  grt_barrier();

  if (grt_id == 0) {
    printf("User-specified parameters:\n");
    for (i = 0; i < NUM_PARAMS; ++i) {
      const char *name = param_names[i];
      unsigned param = params[i];
      printf("  %s=", name);
      if (i == ON_PTHREAD)
	printf((param == GRT_TRUE) ? "yes" : "no");
      else
	printf("%u", param);
      printf("\n");
    }
    printf("  grt_num_procs=%u\n", grt_num_procs);
  }

  grt_barrier();

  times = 
    (grt_word_t*) grt_all_alloc(0, grt_num_procs * sizeof(grt_word_t));

  for (i = 0; i < params[NUM_RUNS]; ++i) {
    if (grt_id == 0) {
      printf("\nRUN NUMBER %u\n\n", i);
      fflush(stdout);
    }
    BARRIER();
    run();
    if (grt_id == 0) {
      sum += time_per_op;
      if (min_time_per_op == -1 ||
	  time_per_op < min_time_per_op)
	min_time_per_op = time_per_op;
      if (min_of_max == -1 ||
	  max_time < min_of_max)
	min_of_max = max_time;
    }
  }
  
  BARRIER();

  if (grt_id == 0) {
    printf("\nSUMMARY RESULTS\n\n");
    printf("average time per operation=%f us\n", sum / params[NUM_RUNS]);
    printf("min time per op=%f us\n", min_time_per_op);
    printf("min of max=%f us\n", min_of_max);
  }

  hash_map_exit(0);
}
Exemplo n.º 7
0
int main (int argc, char *argv[])
{
  hash_map_t *map = NULL;
  struct thread_info *tinfo = NULL;
  pthread_attr_t attr;
  pthread_barrier_t barrier = {0,};
  int ret = -1;
  int tcount = 100, idx = 0;

  map = hash_map_init ();
  if (map == NULL) {
    printf ("failed to initialize hash map\n");
  }

  hh_random_init ();

  ret = pthread_attr_init(&attr);
  if (ret != 0)
    printf ("ERROR: failed to initialize pthread attr\n");

  pthread_barrier_init (&barrier, NULL, tcount + 1);

  tinfo = calloc (tcount, sizeof(struct thread_info));
  if (tinfo == NULL)
    printf ("ERROR: calloc failed\n");

  for (idx = 0; idx < tcount; idx++) {
    tinfo[idx].action = (hh_random() % 3) + 1;

    if (tinfo[idx].action == ACTION_INSERTION)
      tinfo[idx].size = 100 + idx;
    else
      tinfo[idx].size = 23;

    tinfo[idx].map = map;

    tinfo[idx].barrier = &barrier;
  }

  for (idx = 0; idx < tcount; idx++)
    pthread_create (&tinfo[idx].thread_id, &attr, &thread_start, &tinfo[idx]);

  /* tinfo[0].action = ACTION_INSERTION; */
  /* tinfo[0].size = 100; */

  /* tinfo[1].action = ACTION_REMOVAL; */
  /* tinfo[1].size = 20; */

  /* tinfo[2].action = ACTION_RETRIEVAL; */
  /* tinfo[2].size = 20; */

  /* tinfo[0].map = tinfo[1].map = tinfo[2].map = map; */

  /* tinfo[0].barrier = tinfo[1].barrier = tinfo[2].barrier = &barrier; */

  /* //  insertions (map, 100); */
  /* pthread_create (&tinfo[0].thread_id, &attr, &thread_start, &tinfo[0]); */

  /* //  removals (map, 20); */
  /* pthread_create (&tinfo[1].thread_id, &attr, &thread_start, &tinfo[1]); */

  /* //  retrievals (map, 10); */
  /* pthread_create (&tinfo[2].thread_id, &attr, &thread_start, &tinfo[2]); */

  pthread_barrier_wait (&barrier);

  for (idx = 0; idx < tcount; idx++)
    pthread_join (tinfo[idx].thread_id, NULL);


  /* pthread_join (tinfo[0].thread_id, NULL); */
  /* pthread_join (tinfo[1].thread_id, NULL); */
  /* pthread_join (tinfo[2].thread_id, NULL); */

  hash_map_dump (map);

  return 0;
}
Exemplo n.º 8
0
void setUp() {
	map = safe_malloc(sizeof(hash_map));
	hash_map_init(map, 1000, (hash_map_comparator) strcmp, NULL);
}