Exemplo n.º 1
0
void hash_test()
{
    stu_t stu[3] = {
        {"aaa","12345"},
        {"bbb","dadad"},
        {"ccc","xxxxx"},
    };

    hashtable_t* hash = hashtable_init(5,hash_fun);
    int i = 0;
    for ( i = 0 ; i < 3 ; ++i)
    {
        hashtable_add(hash,stu[i].name,strlen(stu[i].name),stu[i].school,strlen(stu[i].name));
    }

    hashtable_del(hash,"aaa",strlen("aaa"));
    hashtable_add(hash,stu[0].name,strlen(stu[0].name),stu[0].school,strlen(stu[0].name));


    char *str = hashtable_search(hash,"aaa",3);
    if  ( NULL == str )
        printf("not fount\n");
    else
        printf("name:\"aaa\",school:%s\n",str);


    hashtable_destroy(&hash);
}
Exemplo n.º 2
0
int main (int argc, char **argv) {

  int srv_fd, cfd;


  hashtable_init(&db, 16);
  log_fd = log_init("CaStore.log");
  log_replay(log_fd, &db);

  struct sockaddr_in servaddr;
  srv_fd = socket(AF_INET, SOCK_STREAM, 0);
  int yep = 1;
  setsockopt(srv_fd, SOL_SOCKET, SO_REUSEADDR, &yep, sizeof(int));

  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htons(INADDR_ANY);
  servaddr.sin_port = htons(11124);

  bind(srv_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
  listen(srv_fd, 10);
    while (1) {
        cfd = accept(srv_fd, (struct sockaddr*) NULL, NULL);
        setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, &yep, sizeof(int));
        printf("Accettato il client %d\n", cfd);

        struct client *cl =(struct client *)malloc(sizeof(struct client));
        pthread_t tred_id;
        cl->fd = cfd;
        pthread_create(&tred_id, NULL, client_thread, cl);
    }
  close(srv_fd);
  return(0);
}
Exemplo n.º 3
0
drmf_status_t
drsyscall_os_init(void *drcontext)
{
    uint i;
    hashtable_init_ex(&systable, SYSTABLE_HASH_BITS, HASH_INTPTR, false/*!strdup*/,
                      false/*!synch*/, NULL, sysnum_hash, sysnum_cmp);
    /* We initialize & leave secondary_systable empty to be in sync with our
     * Windows & Linux solutions. Xref i#1438 i#1549.
     */
    hashtable_init_ex(&secondary_systable, SECONDARY_SYSTABLE_HASH_BITS,
                      HASH_INTPTR, false/*!strdup*/, false/*!synch*/, NULL,
                      sysnum_hash, sysnum_cmp);

    hashtable_init(&name2num_table, NAME2NUM_TABLE_HASH_BITS, HASH_STRING,
                   false/*!strdup*/);

    dr_recurlock_lock(systable_lock);
    for (i = 0; i < count_syscall_info_bsd; i++) {
        IF_DEBUG(bool ok =)
            hashtable_add(&systable, (void *) &syscall_info_bsd[i].num,
                          (void *) &syscall_info_bsd[i]);
        ASSERT(ok, "no dups");

        IF_DEBUG(ok =)
            hashtable_add(&name2num_table, (void *) syscall_info_bsd[i].name,
                          (void *) &syscall_info_bsd[i].num);
        ASSERT(ok || strcmp(syscall_info_bsd[i].name, "ni_syscall") == 0, "no dups");
    }
    dr_recurlock_unlock(systable_lock);
    return DRMF_SUCCESS;
}
Exemplo n.º 4
0
void hash() {
    printf(">> HASH\n");
    hashtable_t hash;

    test_hash_t a = { "Hello" };
    test_hash_t b = { "Intrusive" };
    test_hash_t c = { "World" };

    hashtable_init(&hash, 10, &hash_func);

    hashtable_insert(&hash, &a.node, "a", 2);
    hashtable_insert(&hash, &b.node, "b", 2);
    hashtable_insert(&hash, &c.node, "c", 2);

    hashnode_t *ga = hashtable_search(&hash, "a", 2);
    hashnode_t *gb = hashtable_search(&hash, "b", 2);
    hashnode_t *gc = hashtable_search(&hash, "c", 2);

    test_hash_t *aa = hashtable_ref(ga, test_hash_t, node);
    test_hash_t *bb = hashtable_ref(gb, test_hash_t, node);
    test_hash_t *cc = hashtable_ref(gc, test_hash_t, node);

    printf("%s\n", aa->message);
    printf("%s\n", bb->message);
    printf("%s\n", cc->message);
}
Exemplo n.º 5
0
int mysql_pool_init() {
    if (0 > hashtable_init(&ht_idle_connections, 128))
        return -1;
    if (0 > vmbuf_init(&misc, 4096))
        return -1;
    return 0;
}
Exemplo n.º 6
0
symtable_t *create_symbol_table(const char* name)
{
  symtable_t *t = (symtable_t*)calloc(1, sizeof(symtable_t));
  strncpy(t->name, name, 32 -1 );
  t->curid = 1;
  hashtable_init(&t->ht, HASH_INIT_SIZE, NULL);
  return t;
}
Exemplo n.º 7
0
Arquivo: node.c Projeto: pierce-m/ring
void initialize_AST () {
    program_start = malloc (sizeof (program_state_t));
    program_start->st_list = NULL;
    env_t *e = malloc (sizeof (env_t));
    e->parent = NULL;
    e->bindings = hashtable_init (10);
    program_start->env = e;
}
Exemplo n.º 8
0
void deps_init(struct deps *deps, struct strings *strings) {
	array_init(&deps->names, 0);
	array_init(&deps->epochs, 0);
	array_init(&deps->vers, 0);
	array_init(&deps->rels, 0);
	array_init(&deps->flags, 0);
	hashtable_init(&deps->hashtable);
	deps->strings = strings;
}
Exemplo n.º 9
0
t_tcp_conn_map* tcp_conn_map_init()
{
	t_tcp_conn_map* tcp_conn_map;
	
	tcp_conn_map = kmalloc(sizeof(t_tcp_conn_map));
	tcp_conn_map->conn_map = hashtable_init(TCP_CONN_MAP_SIZE);
	tcp_conn_map->duplicate_conn_list = new_dllist();
	tcp_conn_map->is_key_unique = 0;
 }
Exemplo n.º 10
0
void verr_init() {
  if (initialized) return;
  initialized = true;

  hashtable_init(providers, hasher_fnv64, memcmp, sizeof(int), sizeof(ErrorProvider*));
  verr_register(VERR_PGENERAL, &general_provider);
  verr_register(VERR_PIO, &io_provider);
  verr_thread_init();
}
Exemplo n.º 11
0
/*!
 *****************************************************************************
 *
 ****************************************************************************/
void dircache_init()
{
        struct hash_table_ops ops = {
                .alloc = __alloc,
                .free = __free,
        };

        ht = hashtable_init(DIRCACHE_SZ, &ops);
        pthread_mutex_init(&dir_access_mutex, NULL);
}
Exemplo n.º 12
0
/**
 * @brief Init the hashtable for NFSv4 owner cache
 *
 * @retval 0 if successful.
 * @retval -1 if we failed.
 */
int Init_nfs4_owner(void)
{
	ht_nfs4_owner = hashtable_init(&nfs4_owner_param);

	if (ht_nfs4_owner == NULL) {
		LogCrit(COMPONENT_STATE, "Cannot init NFS Open Owner cache");
		return -1;
	}

	return 0;
}				/* nfs4_Init_nfs4_owner */
Exemplo n.º 13
0
int Init_9p_hash(void)
{
	ht_9p_owner = hashtable_init(&_9p_owner_hash_param);

	if (ht_9p_owner == NULL) {
		LogCrit(COMPONENT_STATE, "Cannot init 9P Owner cache");
		return -1;
	}

	return 0;
}
Exemplo n.º 14
0
int nfs41_Init_session_id(void)
{
    ht_session_id = hashtable_init(&session_id_param);

    if (ht_session_id == NULL) {
        LogCrit(COMPONENT_SESSIONS,
                "NFS SESSION_ID: Cannot init Session Id cache");
        return -1;
    }

    return 0;
}
Exemplo n.º 15
0
} END_TEST

START_TEST(wraparoundHashTest) {
  unsigned numEntries = NUM_OPS/ NUM_THREADS * 4 + 3;
  unsigned power = 1;
  while ( (1ull << power ) < numEntries ) {
    ++power;
  }
#ifdef DBUG_TEST
  ht = hashtable_init(HT_ENTRIES);
#else
  ht = hashtable_init(numEntries);
#endif
  pageid_t *data = malloc(sizeof(pageid_t) * THREAD_ENTRIES);

  for(int i = 1; i <= THREAD_ENTRIES; i++) {
    data[i-1] = -1 * (((i << power) - 6 + stasis_util_random64(13)) / 13);
  }
  worker(data);
  hashtable_deinit(ht);
} END_TEST
Exemplo n.º 16
0
vchan_t* vchans_get(int id)
{
	vchan_t *ret;
	blob_t id_blob = {&id, sizeof(int)};
	hashtable_init(&vchans, sizeof(vchan_t));
	ret = hashtable_get(&vchans, &id_blob, HT_CREATE);
	if(ret->id == 0) {
		memset(ret->cur_ctl_vals, 0xFF, sizeof(ret->cur_ctl_vals));
		ret->id = id;
	}
	return (vchan_t*)ret;
}
Exemplo n.º 17
0
} END_TEST

START_TEST(concurrentHashTest) {
#ifdef DBUG_TEST
  ht = hashtable_init(HT_ENTRIES);
#else
  ht = hashtable_init((pageid_t)((double)NUM_ENTRIES * 1.1));
#endif
  pthread_t workers[NUM_THREADS];
  for(int i = 0 ; i < NUM_THREADS; i++) {
    pageid_t *data = malloc(sizeof(pageid_t) * THREAD_ENTRIES);

    for(int j = 1; j <= THREAD_ENTRIES; j++) {
      data[j-1] = -1 * (i + (j * NUM_THREADS));
    }
    pthread_create(&workers[i], 0, worker, data);
  }
  for(int i = 0 ; i < NUM_THREADS; i++) {
    pthread_join(workers[i],0);
  }
  hashtable_deinit(ht);
} END_TEST
Exemplo n.º 18
0
int main(void)
{
    
    item i1;
    i1.key = "key";
    i1.nkey = strlen(i1.key)+1;
    i1.value = "value";
    i1.nvalue = strlen(i1.value)+1;
    i1.h_next = 0;
    hashtable_init();
    hashtable_insert(i1,0);
    return 0;
}
Exemplo n.º 19
0
int main(int argc, char** argv) {
    char* key = "key 2";
    HASHTABLE* hashtable = hashtable_init();
    hashtable->put(hashtable, "key 0", "value 0", hashtable->string_create, hashtable->string_destroy);
    hashtable->put(hashtable, "key 1", "value 1", hashtable->string_create, hashtable->string_destroy);
    hashtable->put(hashtable, "key 2", "value 2", hashtable->string_create, hashtable->string_destroy);
    fprintf(stdout, "\nThe value with key %s is: %s", key, (char*)hashtable->get(hashtable, key));
    fprintf(stdout, "\nDoes the hashtable contain the key: %s?: %d", key, hashtable->contains(hashtable, key));
    fprintf(stdout, "\nRemoving %s...", key);
    hashtable->remove(hashtable, key);
    fprintf(stdout, "\nDoes the hashtable contain the key: %s?: %d", key, hashtable->contains(hashtable, key));
    hashtable_quit(hashtable);
    return 0;
}
Exemplo n.º 20
0
hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
                              free_fn free_key, free_fn free_value)
{
    hashtable_t *hashtable = malloc(sizeof(hashtable_t));
    if(!hashtable)
        return NULL;

    if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value))
    {
        free(hashtable);
        return NULL;
    }

    return hashtable;
}
Exemplo n.º 21
0
/**
 * @brief Init the hashtable for stateids
 *
 * @retval 0 if successful.
 * @retval -1 on failure.
 */
int nfs4_Init_state_id(void)
{
	/* Init  all_one */
	memset(all_zero, 0, OTHERSIZE);
	memset(all_ones, 0xFF, OTHERSIZE);

	ht_state_id = hashtable_init(&state_id_param);

	if (ht_state_id == NULL) {
		LogCrit(COMPONENT_STATE, "Cannot init State Id cache");
		return -1;
	}

	return 0;
}
Exemplo n.º 22
0
HashTable *
hashtable_new(size_t size, HashFunc hash_func, EqualFunc compare_keys, FreeFunc free_key, FreeFunc free_value)
{
	HashTable *table;

	if(!(table = (HashTable *)malloc(sizeof(HashTable))))
	{
		fprintf(stderr, "Couldn't allocate memory.\n");
		abort();
	}

	hashtable_init(table, size, hash_func, compare_keys, free_key, free_value);

	return table;
}
Exemplo n.º 23
0
void
replace_init(void)
{
    if (options.replace_libc) {
        app_pc addr;
        int i;
        char *s;

        /* replace_module_load will be called for each module to populate the hashtable */
        ASSERT(PAGE_START(get_function_entry((app_pc)replace_memset)) ==
               PAGE_START(get_function_entry((app_pc)replace_memmove)),
               "replace_ routines taking up more than one page");
        ASSERT(sizeof(int) >= sizeof(wchar_t),
               "wchar_t replacement functions assume wchar_t is not larger than int");
        replace_routine_start = (app_pc)
            PAGE_START(get_function_entry((app_pc)replace_memset));
        
        /* PR 485412: we support passing in addresses of libc routines to
         * be replaced if statically included in the executable and if
         * we have no symbols available
         */
        s = options.libc_addrs;
        i = 0;
        while (s != NULL) {
            if (sscanf(s, PIFX, (ptr_uint_t *)&addr) == 1 ||
                /* we save option space by having no 0x prefix but assuming hex */
                sscanf(s, PIFMT, (ptr_uint_t *)&addr) == 1) {
                LOG(2, "replacing %s @"PFX" in executable from options\n",
                    replace_routine_name[i], addr);
                if (!drwrap_replace((app_pc)addr, (app_pc)replace_routine_addr[i], false))
                    ASSERT(false, "failed to replace");
            }
            s = strchr(s, ',');
            if (s != NULL)
                s++;
            i++;
        }

#ifdef USE_DRSYMS
        hashtable_init(&replace_name_table, REPLACE_NAME_TABLE_HASH_BITS, HASH_STRING,
                       false/*!strdup*/);
        for (i=0; i<REPLACE_NUM; i++) {
            hashtable_add(&replace_name_table, (void *) replace_routine_name[i],
                          (void *)(ptr_int_t)(i+1)/*since 0 is "not found"*/);
        }
#endif
    }
}
Exemplo n.º 24
0
int main(int argc, char** argv) {

  hashtable_init(argc, argv);

  unsigned i;

  if (grt_id == 0) {
    printf("TABLE_SIZE=%u\n", TABLE_SIZE);
    printf("NUM_WRITES=%u\n", NUM_WRITES);
    printf("grt_num_procs=%u\n", grt_num_procs);
    printf("WRITES_PER_PROC=%u\n", WRITES_PER_PROC);
  }

  double sum = 0;
  double min_time_per_op = -1;
  double min_of_max = -1;
  times = 
    (grt_word_t*) grt_all_alloc(0, grt_num_procs * sizeof(grt_word_t));

  for (i = 0; i < 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 / NUM_RUNS);
    printf("min time per op=%f us\n", min_time_per_op);
    printf("min of max=%f us\n", min_of_max);
  }

  hashtable_exit(0);
}
Exemplo n.º 25
0
int main(void)
{
    char line[MAXLINE];
    node *hashtable;

    hashtable = (node*)malloc(HASHSIZE*sizeof(node));
    hashtable_init(hashtable);

    while (fgets(line, MAXLINE, stdin)!=NULL)
    {
        htable_insert(hashtable, line);
    }

    htable_display(hashtable);
    return 0;
}
Exemplo n.º 26
0
json_t *json_object(void)
{
    json_object_t *object = malloc(sizeof(json_object_t));
    if(!object)
        return NULL;
    json_init(&object->json, JSON_OBJECT);

    if(hashtable_init(&object->hashtable, hash_string, string_equal,
                      free, value_decref))
    {
        free(object);
        return NULL;
    }

    object->visited = 0;

    return &object->json;
}
Exemplo n.º 27
0
json_t *json_object(void)
{
    json_object_t *object = jsonp_malloc(sizeof(json_object_t));
    if(!object)
        return NULL;
    json_init(&object->json, JSON_OBJECT);

    if(hashtable_init(&object->hashtable))
    {
        jsonp_free(object);
        return NULL;
    }

    object->serial = 0;
    object->visited = 0;

    return &object->json;
}
Exemplo n.º 28
0
static int
add_algorithm(state *s,
              hashname_t pos,
              char *name,
              uint16_t len,
              int ( *func_init)(void *),
              int ( *func_update)(void *, unsigned char *, uint64_t ),
              int ( *func_finalize)(void *, unsigned char *),
              int inuse)
{
    if (NULL == s)
        return TRUE;

    s->hashes[pos] = (algorithm_t *)malloc(sizeof(algorithm_t));
    if (NULL == s->hashes[pos])
        return TRUE;

    s->hashes[pos]->name = strdup(name);
    if (NULL == s->hashes[pos]->name)
        return TRUE;

    s->hashes[pos]->known = (hashtable_t *)malloc(sizeof(hashtable_t));
    if (NULL == s->hashes[pos]->known)
        return TRUE;

    hashtable_init(s->hashes[pos]->known);

    s->hashes[pos]->hash_sum = (unsigned char *)malloc(len * 2);
    if (NULL == s->hashes[pos]->hash_sum)
        return TRUE;

    s->hashes[pos]->hash_context = malloc(ALGORITHM_CONTEXT_SIZE);
    if (NULL == s->hashes[pos]->hash_context)
        return TRUE;

    s->hashes[pos]->f_init      = func_init;
    s->hashes[pos]->f_update    = func_update;
    s->hashes[pos]->f_finalize  = func_finalize;
    s->hashes[pos]->byte_length = len;
    s->hashes[pos]->inuse       = inuse;

    return FALSE;
}
Exemplo n.º 29
0
DR_EXPORT
void dr_init(client_id_t id)
{
    uint i = 0;
    uint const_arrays_num;
    drsys_options_t ops = { sizeof(ops), 0, };

    dr_set_client_name("Dr. STrace", "http://drmemory.org/issues");

#ifdef WINDOWS
    dr_enable_console_printing();
#endif

    options_init(id);
    drsym_init(0);
    drmgr_init();
    drx_init();

    if (drsys_init(id, &ops) != DRMF_SUCCESS)
        ASSERT(false, "drsys failed to init");
    dr_register_exit_event(exit_event);

    dr_register_filter_syscall_event(event_filter_syscall);
    drmgr_register_pre_syscall_event(event_pre_syscall);
    drmgr_register_post_syscall_event(event_post_syscall);
    if (drsys_filter_all_syscalls() != DRMF_SUCCESS)
        ASSERT(false, "drsys_filter_all_syscalls should never fail");
    open_log_file();

    const_arrays_num = get_const_arrays_num();
    hashtable_init(&nconsts_table, HASHTABLE_BITSIZE, HASH_STRING, false);
    while (i < const_arrays_num) {
        const_values_t *named_consts = const_struct_array[i];
        bool res = hashtable_add(&nconsts_table,
                                 (void *) named_consts[0].const_name,
                                 (void *) named_consts);
        if (!res)
            ASSERT(false, "drstrace failed to add to hashtable");
        i++;
    }

}
Exemplo n.º 30
0
int mime_types_init(void) {
    if (hashtable_is_initialized(&ht_mime_types))
        return 1;
    LOGGER_INFO("initializing mime types");
    int fd = open(MIME_TYPES, O_RDONLY);
    struct stat st;
    if (0 > fstat(fd, &st))
        return LOGGER_PERROR("mime_types fstat"), close(fd), -1;
    void *mem = mmap(NULL, st.st_size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
    if (MAP_FAILED == mem)
        return LOGGER_PERROR("mime_types mmap"), close(fd), -1;

    hashtable_init(&ht_mime_types, 4096);
    char *content = (char *)mem;
    char *content_end = content + st.st_size;

    while (content < content_end)
    {
        // find end of line
        char *line = content, *p = line;
        for (; p < content_end && *p != '\n'; ++p);
        *p = 0; // mmap'd one extra byte to avoid checks
        content = p + 1;

        p = strchrnul(line, '#');
        *p = 0; // truncate line after comment

        p = line;
        char *saveptr;
        char *mime = strtok_r(p, MIME_DELIMS, &saveptr);
        if (!mime)
            continue;
        char *ext;
        while (NULL != (ext = strtok_r(NULL, MIME_DELIMS, &saveptr))) {
            for (p = ext; *p; *p = tolower(*p), ++p);
            hashtable_insert(&ht_mime_types, ext, strlen(ext), mime, strlen(mime) + 1);
        }
    }
    munmap(mem, st.st_size + 1);
    close(fd);
    return 0;
}