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; }
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); }
static dr_emit_flags_t event_analyze_bb(void *drcontext, void *tag, instrlist_t *bb, bool for_trace, bool translating, void **user_data) { instr_t *instr; trace_head_entry_t *e = NULL; if (translating) return DR_EMIT_DEFAULT; for (instr = instrlist_first_app(bb); instr != NULL; instr = instr_get_next_app(instr)) { /* Blocks containing calls are trace heads. */ if (instr_is_call(instr)) { dr_mark_trace_head(drcontext, tag); hashtable_lock(&head_table); e = hashtable_lookup(&head_table, tag); if (e == NULL) { e = create_trace_head_entry(tag); if (!hashtable_add(&head_table, tag, (void *)e)) DR_ASSERT(false); } else e->refcount++; e->is_trace_head = true; hashtable_unlock(&head_table); #ifdef VERBOSE dr_log(drcontext, DR_LOG_ALL, 3, "inline: marking bb "PFX" as call trace head\n", tag); #endif /* Doesn't matter what's in rest of the bb. */ return DR_EMIT_DEFAULT; } else if (instr_is_return(instr)) { hashtable_lock(&head_table); e = hashtable_lookup(&head_table, tag); if (e == NULL) { e = create_trace_head_entry(tag); if (!hashtable_add(&head_table, tag, (void *)e)) DR_ASSERT(false); } else e->refcount++; e->has_ret = true; hashtable_unlock(&head_table); #ifdef VERBOSE dr_log(drcontext, DR_LOG_ALL, 3, "inline: marking bb "PFX" as return trace head\n", tag); #endif } } return DR_EMIT_DEFAULT; }
void *hasher_thread(void *v) { struct args *args = (struct args *)v; fprintf(stderr,"Thread %p running\n", (void*)pthread_self()); int start_value = random() % args->num_words; int values_to_add = args->max_values; int i = 0; for (i = 0; i < values_to_add; i++) { hashtable_add(args->hash, args->words[(start_value + i) % args->num_words]); } pthread_mutex_lock(&args->done_adding_mutex); // add one to done_adding as a signal to main thread that we're done // adding to hashtable. args->done_adding += 1; // wait for main thread to signal to workers to start removing from table while (args->done_adding != -1) { pthread_cond_wait(&args->done_adding_condv, &args->done_adding_mutex); } pthread_mutex_unlock(&args->done_adding_mutex); for (i = 0; i < values_to_add; i++) { hashtable_remove(args->hash, args->words[(start_value + i) % args->num_words]); } return NULL; }
bool functor_toset(const void* item, void* data) { functor_toset_data_t* functor_data = (functor_toset_data_t*)data; hashtable_add(functor_data->set, item, NULL, NULL); return false; }
int main(void) { struct hashmap aa; struct hashmap *a= &aa; hashtable_initialize(a, 200, default_hash, 4); uint8_t key[4] = {1,2,3,4}; int i; for (i= 0; i < 10; i++) { key[0] = i; hashtable_add(a,key,(void*)5); } hashtable_iter(a,iter); printf("deleting\n"); for (i= 0; i < 5; i++) { key[0] = i; hashtable_delete(a,key); } hashtable_iter(a,iter); return 0; }
DR_EXPORT drmf_status_t drfuzz_fuzz_target(generic_func_t func_pc, uint arg_count, uint flags, uint wrap_flags, void (*pre_fuzz_cb)(void *fuzzcxt, generic_func_t target_pc, dr_mcontext_t *mc), bool (*post_fuzz_cb)(void *fuzzcxt, generic_func_t target_pc)) { fuzz_target_t *target; if (func_pc == NULL) return DRMF_ERROR_INVALID_PARAMETER; target = global_alloc(sizeof(fuzz_target_t), HEAPSTAT_MISC); memset(target, 0, sizeof(fuzz_target_t)); target->func_pc = (app_pc) func_pc; target->arg_count = arg_count; target->flags = flags; target->pre_fuzz_cb = pre_fuzz_cb; target->post_fuzz_cb = post_fuzz_cb; if (!hashtable_add(&fuzz_target_htable, func_pc, target)) { free_fuzz_target(target); return DRMF_ERROR_INVALID_PARAMETER; /* entry already exists */ } /* wrap after adding to hashtable: avoids racing on presence of hashtable entry */ if (drwrap_wrap_ex((app_pc) func_pc, pre_fuzz_handler, post_fuzz_handler, NULL, wrap_flags)) { return DRMF_SUCCESS; } else { hashtable_remove(&fuzz_target_htable, func_pc); /* ignore result: error already */ return DRMF_ERROR; } }
/** * Replace one node with another * @param ht the hashtable to do it in * @param u the node to replace it with * @return 1 if it worked */ int hashtable_replace( hashtable *ht, node *v, node *u ) { if ( hashtable_remove(ht,v,node_first_char(u)) ) return hashtable_add(ht,u); else return 0; }
struct cluster_barrier *alloc_cluster_barrier(unique_id_t key) { struct cluster_barrier *barrier; int r, i; if (!key) key = get_unique_id(&barrier_id_root); if (hashtable_find (barrier_table, key)) return ERR_PTR(-EEXIST); barrier = kmalloc (sizeof(struct cluster_barrier), GFP_KERNEL); if (!barrier) return ERR_PTR(-ENOMEM); for (i = 0; i < 2; i++) { krgnodes_clear (barrier->core[i].nodes_in_barrier); krgnodes_clear (barrier->core[i].nodes_to_wait); init_waitqueue_head(&barrier->core[i].waiting_tsk); barrier->core[i].in_barrier = 0; } spin_lock_init(&barrier->lock); barrier->id.key = key; barrier->id.toggle = 0; r = hashtable_add (barrier_table, barrier->id.key, barrier); if (r) { kfree (barrier); return ERR_PTR(r); } return barrier; }
/** Allocates, initializes and registers a new tag_info structure. * Must be called with tags_lock held. */ struct tag_info_t* tag_info_new(void* tag) { struct tag_info_t* tag_info = dr_global_alloc(sizeof(struct tag_info_t)); tag_info->id = 0; tag_info->counter = 0; memset(&tag_info->instr_info, 0, sizeof(struct instr_info_t)); hashtable_add(&tags, tag, tag_info); return tag_info; }
static char * read_module_list(char *buf, void ***tables, uint *num_mods) { char path[MAXIMUM_PATH]; uint i; uint version; PRINT(3, "Reading module table...\n"); /* versione number */ PRINT(4, "Reading version number"); if (dr_sscanf(buf, "DRCOV VERSION: %u\n", &version) != 1 && version != DRCOV_VERSION) { WARN(2, "Failed to read version number"); return NULL; } buf = move_to_next_line(buf); /* module table header */ PRINT(4, "Reading Module Table Header\n"); if (dr_sscanf(buf, "Module Table: %d\n", num_mods) != 1) { WARN(2, "Failed to read module table"); return NULL; } buf = move_to_next_line(buf); /* module lists */ PRINT(4, "Reading Module Lists\n"); *tables = calloc(*num_mods, sizeof(*tables)); for (i = 0; i < *num_mods; i++) { uint mod_id; uint64 mod_size; void *bb_table; /* assuming the string is something like: "0, 2207744, /bin/ls" */ /* XXX: i#1143: we do not use dr_sscanf since it does not support %[] */ if (sscanf(buf, " %u, %"INT64_FORMAT"u, %[^\n\r]", &mod_id, &mod_size, path) != 3) ASSERT(false, "Failed to read module table"); buf = move_to_next_line(buf); PRINT(5, "Module: %u, "PFX", %s\n", mod_id, (ptr_uint_t)mod_size, path); bb_table = hashtable_lookup(&module_htable, path); if (bb_table == NULL) { if (mod_size >= UINT_MAX) ASSERT(false, "module size is too large"); if (strstr(path, "<unknown>") != NULL || (mod_filter != NULL && strstr(path, mod_filter) == NULL)) bb_table = BB_TABLE_IGNORE; else bb_table = bb_table_create((uint)mod_size); PRINT(4, "Create bb table "PFX" for module %s\n", (ptr_uint_t)bb_table, path); num_module_htable_entries++; if (!hashtable_add(&module_htable, path, bb_table)) ASSERT(false, "Failed to add new module"); } (*tables)[i] = bb_table; } return buf; }
static devfs_node_t* devfs_create_node( devfs_node_t* parent, const char* name, int length, bool is_directory ) { devfs_node_t* node; if ( length == -1 ) { length = strlen( name ); } /* Create a new node */ node = ( devfs_node_t* )kmalloc( sizeof( devfs_node_t ) + length + 1 ); if ( node == NULL ) { goto error1; } /* Initialize the node */ node->name = ( char* )( node + 1 ); strncpy( node->name, name, length ); node->name[ length ] = 0; node->is_directory = is_directory; node->parent = parent; node->next_sibling = NULL; node->first_child = NULL; node->calls = NULL; node->atime = node->mtime = node->ctime = time( NULL ); /* Insert to the node table */ do { node->inode_number = devfs_inode_counter++; if ( devfs_inode_counter < 0 ) { devfs_inode_counter = 0; } } while ( hashtable_get( &devfs_node_table, ( const void* )&node->inode_number ) != NULL ); if ( hashtable_add( &devfs_node_table, ( hashitem_t* )node ) != 0 ) { goto error2; } if ( parent != NULL ) { node->next_sibling = parent->first_child; parent->first_child = node; } return node; error2: kfree( node ); error1: return NULL; }
static void * lookup_or_load(const char *modpath) { void *mod = hashtable_lookup(&modtable, (void*)modpath); if (mod == NULL) { mod = drsym_unix_load(modpath); if (mod != NULL) { hashtable_add(&modtable, (void*)modpath, mod); } } return mod; }
int main(){ hashtable_t * table = hashtable_new(5); int i = 0; for(;i<5;i++){ hashtable_add(table,"one"); hashtable_add(table,"two"); hashtable_add(table,"three"); hashtable_add(table,"four"); hashtable_add(table,"five"); } i=0;/* for(;i<5;i++){ hashtable_remove(table,"one"); hashtable_remove(table,"two"); hashtable_remove(table,"three"); hashtable_remove(table,"four"); hashtable_remove(table,"five"); } */ hashtable_print(table); hashtable_free(table); return 1; }
/** * Add a new MySQL user to the user table. The user name must be unique * * @param users The users table * @param user The user name * @param auth The authentication data * @return The number of users added to the table */ int mysql_users_add(USERS *users, MYSQL_USER_HOST *key, char *auth) { int add; if (key == NULL || key->user == NULL) return 0; atomic_add(&users->stats.n_adds, 1); add = hashtable_add(users->data, key, auth); atomic_add(&users->stats.n_entries, add); return add; }
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 } }
int main() { int coolint[10] = {1 , 3, 4 , 9, 19, 21, 22, 2, 29, 111}; int target = 5 ; int i; // hash table way --- for each element < target, put into hash table the element needed to sum to target. if its there, pairs ++ . // linear in time, linear in space. // an n2 time, constant space alg is to search array for any matches if ele < target. for (i=0; i< 10; i++) { if (hashtable_get(coolint[i] ) != -1 ) { printf("%d, %d", target - coolint[i] , coolint[i]) ; } else { hashtable_add(target - coolint[i] ); } } }
/** * Detects possible loops in the query cloning chain. */ int detect_loops(TEE_INSTANCE *instance, HASHTABLE* ht, SERVICE* service) { SERVICE* svc = service; int i; if (ht == NULL) { return -1; } if (hashtable_add(ht, (void*) service->name, (void*) true) == 0) { return true; } for (i = 0; i < svc->n_filters; i++) { if (strcmp(svc->filters[i]->module, "tee") == 0) { /* * Found a Tee filter, recurse down its path * if the service name isn't already in the hashtable. */ TEE_INSTANCE* ninst = (TEE_INSTANCE*) svc->filters[i]->filter; if (ninst == NULL) { /** * This tee instance hasn't been initialized yet and full * resolution of recursion cannot be done now. */ continue; } SERVICE* tgt = ninst->service; if (detect_loops((TEE_INSTANCE*) svc->filters[i]->filter, ht, tgt)) { return true; } } } return false; }
/** * Load a hashtable from disk * * @param table Hashtable to load * @param filename Filename to read hashtable from * @param keyread Pointer to function that reads a single key * @param valueread Pointer to function that reads a single value * @return Number of entries read or -1 on error */ int hashtable_load(HASHTABLE *table, char *filename, void *(*keyread)(int), void *(*valueread)(int)) { int fd, count, rval = 0; void *key, *value; char buf[40]; if ((fd = open(filename, O_RDONLY)) == -1) { return -1; } if (read(fd, buf, 7) != 7) { close(fd); return -1; } if (strncmp(buf, "HASHTABLE", 7) != 0) { close(fd); return -1; } if (read(fd, &count, sizeof(count)) != sizeof(count)) { close(fd); return -1; } while (count--) { key = keyread(fd); value = valueread(fd); if (key == NULL || value == NULL) { break; } hashtable_add(table, key, value); rval++; } close(fd); return rval; }
void retrieve_ict(struct Hashtable *cntTable, struct Hashtable *ictTable) { unsigned long i; struct Item *aItem, *aContactItem, *aIctPairItem; struct Contact *aContact, *bContact; struct Pair *aPair, *aIctPair; struct ICT *aIct; char key[64]; if(cntTable == NULL || ictTable == NULL) return; for(i=0;i<cntTable->size;i++) { aItem = cntTable->head[i]; while(aItem!=NULL) { aPair = (struct Pair*)aItem->datap; /* set up the coresponding pair in the ict pair table */ sprintf(key, "%s,%s", aPair->vName1, aPair->vName2); aIctPairItem = hashtable_find(ictTable, key); if(aIctPairItem == NULL) { aIctPair = (struct Pair*)malloc(sizeof(struct Pair)); pair_init_func(aIctPair); strncpy(aIctPair->vName1, aPair->vName1, strlen(aPair->vName1)+1); strncpy(aIctPair->vName2, aPair->vName2, strlen(aPair->vName2)+1); hashtable_add(ictTable, key, aIctPair); } else { aIctPair = (struct Pair*)aIctPairItem->datap; } aContactItem = aPair->contents.head; while(aContactItem!=NULL && aContactItem->next!=NULL) { aContact = (struct Contact*)aContactItem->datap; bContact = (struct Contact*)aContactItem->next->datap; aIct = (struct ICT*)malloc(sizeof(struct ICT)); aIct->timestamp = bContact->startAt; aIct->ict = bContact->startAt-aContact->endAt; duallist_add_to_tail(&aIctPair->contents, aIct); aContactItem = aContactItem->next; } aItem = aItem->next; } } }
void KDA_Init_conn() { NOTE_F; // Add self to world_ranks... world_ranks = hashtable_create(mpi_size); int* heap_mpi_rank = malloc(sizeof(int)); *heap_mpi_rank = mpi_size; hashtable_add(world_ranks, xheap(id), heap_mpi_rank); MPIRPC_Comm_add(MPI_COMM_WORLD); MPIRPC_Register("get_id", handle_get_id); MPIRPC_Register("get_rank", handle_get_rank); sleep(mpi_rank * 3); if (mpi_rank > 0) { int other_rank = rand_lt(mpi_rank); MPIRPC_Node node; MPIRPC_Node_make(MPI_COMM_WORLD, other_rank, &node); SHOW_I(other_rank); char* result = MPIRPC_Block(node, "get_id", NULL); KDA_ID other_id; sscanf(result, "%X", &other_id); free(result); KDA_Neighbor neighbor; KDA_Neighbor_make_id(other_id, node, &neighbor); neighbor_add(&neighbor); MPIRPC_Node dummy = {0}; KDA_Join(dummy, &neighbor); } /* int client = cmpi_mode_first_client(mpi_rank, mpi_size, kda_nodes); SHOW_I(client); int msg = -2; // MPI_Send(&msg, 1, MPI_INT, client, 0, MPI_COMM_WORLD); */ listen_loop(); }
rs_result rs_build_hash_table(rs_signature_t *sig) { rs_block_match_t m; rs_block_sig_t *b; int i; rs_signature_check(sig); sig->hashtable = hashtable_new(sig->count); if (!sig->hashtable) return RS_MEM_ERROR; for (i = 0; i < sig->count; i++) { b = rs_block_sig_ptr(sig, i); rs_block_match_init(&m, sig, b->weak_sum, &b->strong_sum, NULL, 0); if (!hashtable_find(sig->hashtable, &m)) hashtable_add(sig->hashtable, b); } hashtable_stats_init(sig->hashtable); return RS_DONE; }
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++; } }
static char* create_capability_name(const xmlChar* string, parse_context_t* context) { char tmp[8 * 1024]; memset(tmp, '\0', 8 * 1024); decode_string(tmp, string); char* name = hashtable_get(context->capabilities, tmp); if(!name) { name = malloc(sizeof(char) * (strlen(tmp) + 1)); if(!name) { error(1, errno, "error allocating capability name"); } strcpy(name, tmp); hashtable_add(context->capabilities, name, NULL, NULL); } return name; }
static int cpool_add(chunk_t *ch) { int err = 0; while (ch->cpool->nr_pages + ch->len > ch->cpool->threshold && !(err = cpool_del(ch->cpool->head))); if (err == EBUSY) return ENOBUFS; if (err) return err; if (ch->cpool->head != NULL) { ch->next = ch->cpool->head; ch->prev = ch->cpool->head->prev; ch->next->prev = ch; ch->prev->next = ch; } else ch->cpool->head = ch; for (unsigned i = 0; i < ch->len; ++i) { err = hashtable_add(ch->cpool->ht, ch->idx+i, (hval_t)ch); if (err) return err; } ch->cpool->nr_pages += ch->len; return 0; }
void handle_join(MPIRPC_Node caller, int unique, char* args, char* blob, int blob_length) { SHOW_FI(caller.rank); KDA_ID other_id; int* other_rank = malloc(sizeof(int)); sscanf(args, "%X %i", &other_id, other_rank); if (other_id != KDA_ID_CLIENT) { MPIRPC_Node node; MPIRPC_Node_make(caller.comm, caller.rank, &node); KDA_Neighbor neighbor; KDA_Neighbor_make_id(other_id, node, &neighbor); neighbor_add(&neighbor); hashtable_add(world_ranks, xheap(other_id), other_rank); } MPIRPC_Null(caller, unique); }
/** * Create a hashtable by conversion from a list of child-nodes * @param children add these nodes to the hashtable for starters * @return an initialised hashtable */ hashtable *hashtable_create( node *parent ) { hashtable *ht = calloc( 1, sizeof(hashtable) ); if ( ht != NULL ) { int nnodes = node_num_children( parent ); mem_usage += sizeof(hashtable); ht->nbuckets = nnodes*2; ht->items = calloc( ht->nbuckets, sizeof(struct bucket*) ); if ( ht->items != NULL ) { int res = 1; mem_usage += ht->nbuckets*sizeof(struct bucket*); node_iterator *iter = node_children( parent ); if ( iter != NULL ) { while ( res && node_iterator_has_next(iter) ) { node *temp = node_iterator_next( iter ); node_clear_next( temp ); res = hashtable_add( ht, temp ); } node_iterator_dispose( iter ); } else res = 0; if ( !res ) { hashtable_dispose( ht ); ht = NULL; } } else fprintf(stderr,"failed to allocate %d buckets\n",ht->nbuckets); } else fprintf(stderr,"hashtable: failed to allocate\n"); return ht; }
uint deps_add_evr(struct deps *deps, const char *name, int flags, uint epoch, const char *version, const char *release) { uint i, iter = 0, hash, size = array_get_size(&deps->names), ver, rel, nam; nam = strings_add(deps->strings, name); ver = strings_add(deps->strings, version != NULL ? version : ""); rel = strings_add(deps->strings, release != NULL ? release : ""); if (hashtable_resize(&deps->hashtable)) { for (i = 0; i < size; i++) { hash = dephash(array_get(&deps->names, i)); hashtable_add(&deps->hashtable, i, hash); } } hash = dephash(nam); while ((i = hashtable_find(&deps->hashtable, hash, &iter)) != -1) if (array_get(&deps->names, i) == nam && array_get(&deps->epochs, i) == epoch && array_get(&deps->vers, i) == ver && array_get(&deps->rels, i) == rel && array_get(&deps->flags, i) == flags) break; if (i != -1) /* already stored */ return i; i = size; array_set(&deps->names, i, nam); array_set(&deps->epochs, i, epoch); array_set(&deps->vers, i, ver); array_set(&deps->rels, i, rel); array_set(&deps->flags, i, flags); hashtable_add_dir(&deps->hashtable, i, hash, iter); return i; }
int hashtable_resize(HashTable * ht, size_t size){ HashTable * newht = hashtable_initialize(size, ht->maxCollisions, ht->growthFactor, ht->hf, ht->eq); int i; Item * next; // Re-enter all the items again into the new hashtable // with the new size. for(i=0; i<ht->size; i++){ if(ht->table[i]){ for(next=ht->table[i]; next; next=next->next){ hashtable_add(newht, next->key, next->value); } } hashtable_remove(ht, ht->table[i]->key); } free(ht->table); ht->size = newht->size; ht->table = newht->table; return(1); }
/* * Typically, called after grow(...) or shrink(...). * Relocates all hashtable items from source array to the new array * in the hashtable. */ static void rehash(struct hashtable* h, struct list** source_array, unsigned int source_size) { unsigned int i; struct list* chain; struct kv_pair* item; for (i = 0; i < source_size; ++i) { chain = source_array[i]; assert(chain != NULL); if (list_getsize(chain) > 0) { item = (struct kv_pair*)list_front(chain); while (item) { hashtable_add(h, item->key, item->keylen, item->val); list_pop_front(chain); free(item); item = (struct kv_pair*)list_front(chain); } assert(list_getsize(chain) == 0); } } }