static uint32_t wb_push_block(struct cache_info * info, bdesc_t * block, uint32_t number) { uint32_t index = info->blocks[0].free_index; assert(index && index <= info->size && !info->blocks[index].block); assert(!hash_map_find_val(info->block_map, (void *) number)); if(hash_map_insert(info->block_map, (void *) number, (void *) index) < 0) return INVALID_BLOCK; info->blocks[index].block = block; block->cache_number = number; FSTITCH_DEBUG_SEND(FDB_MODULE_INFO, FDB_INFO_BDESC_NUMBER, block, number, 1); /* not a free slot anymore */ info->blocks[0].free_index = info->blocks[index].next_index; info->blocks[index].prev = &info->blocks[0]; info->blocks[index].next = info->blocks[0].mru; /* this will set blocks[0].lru if this is the first block */ info->blocks[0].mru->prev = &info->blocks[index]; info->blocks[0].mru = &info->blocks[index]; bdesc_retain(block); return index; }
static open_ufsfile_t * get_ufsfile(hash_map_t * filemap, inode_t ino, int * exists) { open_ufsfile_t * existing_file; ufs_fdesc_t * new_file; int r; if (!filemap) return NULL; existing_file = hash_map_find_val(filemap, (void *) ino); if (existing_file) { existing_file->count++; *exists = 1; return existing_file; } *exists = 0; new_file = malloc(sizeof(ufs_fdesc_t)); if (!new_file) return NULL; new_file->common = &new_file->base; new_file->base.parent = INODE_NONE; // If file struct is not in memory existing_file = open_ufsfile_create(new_file); if (!existing_file) { free(new_file); return NULL; } r = hash_map_insert(filemap, (void *) ino, existing_file); assert(r == 0); return existing_file; }
dict* dict_insert(dict* d, char *key, void *val) { if (d) { #ifdef TSTC dict_entry *ent = dict_entry_new(val, key); if (d->dict) { d->dict = tstc_insert(d->dict, key, ent); } else { d->dict = tstc_insert(NULL, key, ent); } #elif defined (HASHMAP) d->dict = hash_map_insert(d->dict, key, strlen(key), val); #else if (d->dict) { tst_insert(d->dict, key, val); } else { d->dict = tst_insert(NULL, key, val); } #endif d->n += 1; } return d; }
boolean task_processor_add_task_queue( task_processor* processor, task_queue* queue ) { boolean ret = FALSE; event_group_element_type group_element; ASSERT( NULL != queue ); synchronize_lock_mutex( processor->mutex ); do { group_element = synchronize_event_group_add_element( processor->event_group ); if ( NULL == group_element ) { ret = FALSE; break; } ret = hash_map_insert( &processor->task_queues, &group_element, &queue ); if ( !ret ) { synchronize_event_group_remove_element( processor->event_group, group_element ); break; } synchronize_lock_mutex( queue->mutex ); ret = list_push_back( &queue->event_list, &group_element ); synchronize_unlock_mutex( queue->mutex ); if ( !ret ) { hash_map_remove( &processor->task_queues, &group_element ); synchronize_event_group_remove_element( processor->event_group, group_element ); break; } } while (FALSE); synchronize_unlock_mutex( processor->mutex ); return ret; }
hash_node_t * hash_map_makelabel(hash_map_t * hash_map) { static int label_num = 1000000; char label_name[128]; sprintf(label_name, "_label_n%d", label_num++); hash_map_insert(hash_map, SYMBOL_LABEL, label_name); return hash_map_search(hash_map, label_name); }
hash_node_t * hash_map_maketemp(hash_map_t * hash_map) { static int temp_num = 1000; char temp_name[128]; sprintf(temp_name, "_temp_n%d", temp_num++); hash_map_insert(hash_map, SYMBOL_VARIABLE, temp_name); return hash_map_search(hash_map, temp_name); }
void hash_map_set(struct hash_map *map, u32 key, u32 value) { const size_t bucket_num = hash_bucket_num(map, key); struct hash_node *node = NULL; for (node = map->buckets[bucket_num]; node != NULL; node = node->next) { if (node->key == key) { node->value = value; return; } } hash_map_insert(map, key, value); }
static int init_fuse_entry(mount_t * mount, inode_t parent, inode_t cfs_ino, fuse_ino_t fuse_ino, struct fuse_entry_param * e) { int r; r = hash_map_insert(mount->parents, (void *) cfs_ino, (void *) parent); if (r < 0) return r; memset(e, 0, sizeof(*e)); e->ino = fuse_ino; e->attr_timeout = STDTIMEOUT; e->entry_timeout = STDTIMEOUT; r = fill_stat(mount, cfs_ino, e->ino, &e->attr); assert(r >= 0); return 0; }
/* ===========Реализация интерфейсных функций.========================= */ int yaup_global_container_create(void *elem) { init_container_map(); int *free_id = (int *)malloc(sizeof(int)); struct id_list *prev; if (_container.free_ids == 0) { *free_id = ++_container.max_id; } else { prev = _container.free_ids; *free_id = prev->id; _container.free_ids = _container.free_ids->next; free(prev); } hash_map_insert(_container.map_id, free_id, elem); return *free_id; }
int fuse_serve_mount_set_root(CFS_t * root_cfs) { int r; Dprintf("%s(%s)\n", __FUNCTION__, modman_name_cfs(root_cfs)); if (!root) return -1; if (root_service_started) return -EBUSY; if ((r = CALL(root_cfs, get_root, &root->root_ino)) < 0) return r; if ((r = hash_map_insert(root->parents, (void *) root->root_ino, (void *) root->root_ino)) < 0) return r; root->cfs = root_cfs; printf("Mounted \"/\" from %s\n", modman_name_cfs(root_cfs)); return 0; }
/* This also gets called for clone()! Check task->pid and task->tgid. */ static void fork_handler(struct task_struct * child) { patchgroup_scope_t * parent_scope; /* Why is this assertion not always true? */ /* assert(current == child->real_parent); */ spin_lock(&scope_lock); parent_scope = hash_map_find_val(scope_map, child->real_parent); if(parent_scope && patchgroup_scope_size(parent_scope) > 0) { patchgroup_scope_t * child_scope; /* We are executing in the context of the parent, which is the * only process that could alter its scope. Thus it is OK to * release the scope lock, call fstitchd_enter(), and then reacquire * the scope lock. */ spin_unlock(&scope_lock); fstitchd_enter(); spin_lock(&scope_lock); child_scope = patchgroup_scope_copy(parent_scope); if(child_scope) { int r = hash_map_insert(scope_map, child, child_scope); if(r < 0) { patchgroup_scope_destroy(child_scope); goto fail; } } else fail: fprintf(stderr, "error creating child scope for PID %d!\n", child->pid); fstitchd_leave(0); } spin_unlock(&scope_lock); }
patchgroup_scope_t * process_patchgroup_scope(const struct task_struct * task) { patchgroup_scope_t * scope; if(task == fstitchd_task) return NULL; spin_lock(&scope_lock); scope = hash_map_find_val(scope_map, task); if(!scope && (scope = patchgroup_scope_create())) { if(hash_map_insert(scope_map, (void *) task, scope) < 0) { patchgroup_scope_destroy(scope); scope = NULL; } } spin_unlock(&scope_lock); return scope; }
int fuse_serve_mount_add(CFS_t * cfs, const char * path) { mount_t * m; queue_entry_t * qe; int r; Dprintf("%s(%s, \"%s\")\n", __FUNCTION__, modman_name_cfs(cfs), path); if (shutdown_has_started()) return -EBUSY; // We might be able to allow this; but at least for now, reject if (!(m = calloc(1, sizeof(*m)))) return -ENOMEM; if (!(qe = calloc(1, sizeof(*qe)))) { r = -ENOMEM; goto error_m; } qe->mount = m; qe->action = QEMOUNT; m->mounted = 0; if (!(m->fstitch_path = strdup(path))) { r = -ENOMEM; goto error_qe; } if (!(m->parents = hash_map_create())) { r = -ENOMEM; goto error_path; } m->cfs = cfs; if ((r = CALL(cfs, get_root, &m->root_ino)) < 0) goto error_parents; if ((r = hash_map_insert(m->parents, (void *) m->root_ino, (void *) m->root_ino)) < 0) goto error_parents; if ((r = fuse_args_copy(&root->args, &m->args)) < 0) goto error_parents; m->mountpoint = malloc(strlen(root->mountpoint) + strlen(path) + 1); if (!m->mountpoint) { r = -ENOMEM; goto error_args; } strcpy(m->mountpoint, root->mountpoint); strcpy(m->mountpoint + strlen(root->mountpoint), path); // add to mounts list mounts_insert(m); // helper_thread takes care of the channel_fd field and on down if (enqueue_helper_request(qe)) goto error_insert; if (ensure_helper_is_running() < 0) { // As it is not expected that ensure_helper_is_running() will error // and as recovering would require a single-use dequeue function, // for now we just error and let things go as they will. fprintf(stderr, "%s: ensure_helper_is_running failed. WARNING: request remains in the queue.\n", __FUNCTION__); goto error_insert; } return 0; error_insert: mounts_remove(m); free(m->mountpoint); error_args: fuse_opt_free_args(&m->args); error_parents: hash_map_destroy(m->parents); error_path: free(m->fstitch_path); error_qe: memset(qe, 0, sizeof(*qe)); free(qe); error_m: memset(m, 0, sizeof(*m)); free(m); return r; }
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; }
static int crashsim_bd_write_block(BD_t * object, bdesc_t * block, uint32_t number) { struct crashsim_info * info = (struct crashsim_info *) object; int value; /* make sure it's a valid block */ assert(block->length && number + block->length / object->blocksize <= object->numblocks); info->total++; if(!info->crashed) { uint32_t rval; if((rval = random32()) < info->threshold) { printf("Crash simulator simulating block device crash! (%u < %u)\n", rval, info->threshold); info->crashed = 1; } } if(info->crashed) { if(!hash_map_find_val(info->blocks, (void *) number)) { value = hash_map_insert(info->blocks, (void *) number, block); if(value < 0) return value; bdesc_retain(block); } #if REVISION_TAIL_INPLACE value = revision_tail_prepare(block, object); if(value < 0) return value; #else static uint8_t buffer[4096]; if(block->length > sizeof(buffer)) { printf("%s(): block size larger than buffer! (%d, %d)\n", __FUNCTION__, block->length, (int) sizeof(buffer)); return -EFAULT; } value = revision_tail_prepare(block, object, buffer); if(value < 0) return value; #endif value = revision_tail_acknowledge(block, object); if(value < 0) { kpanic("revision_tail_acknowledge gave error: %d\n", value); return value; } info->absorbed++; return 0; } /* this should never fail */ value = patch_push_down(block, object, info->bd); if(value < 0) return value; /* write it */ return CALL(info->bd, write_block, block, number); }
static inline int __container_hash_map_insert(container_t *ct, void *data) { return hash_map_insert(ct->priv.hmap,data); }
list* search_a_star(void* state, void* state_world, search_is_goal state_goal_func, search_gen_successors state_gen_func, search_link_parent state_link_func, search_goal_backtrace state_back_func, search_trans_cost state_trans_func, search_heuristic state_heur_func, search_set_f_cost state_f_cost_set_func, hash_func state_hash_alg, generic_comp state_comp_func, generic_cpy state_copy_func, generic_op state_free_func, heap_comp state_heap_func) { int* g_cost_ptr, *f_cost_ptr, f_cost, tmp_f, g_cost, found; void* current_state, *successor_state, *heap_memory_location; list* states_overflow, *successor_list, *path; hash_table* states_closed_set, *states_open_set; hash_map* states_g_cost, *states_f_cost, *states_heap_index; heap* states_heap; states_overflow = list_create(NULL, NULL, state_free_func); states_closed_set = hash_table_create(89, .75, state_hash_alg, state_comp_func, state_copy_func, state_free_func); states_open_set = hash_table_create(89, .75, state_hash_alg, state_comp_func, state_copy_func, state_free_func); states_g_cost = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, state_free_func, (generic_op)free); states_f_cost = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, state_free_func, (generic_op)free); states_heap_index = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, NULL, NULL); states_heap = heap_create(89, state_heap_func, state_comp_func, state_copy_func, state_free_func); current_state = state; f_cost = state_heur_func(current_state, NULL); state_f_cost_set_func(current_state, f_cost); g_cost = 0; g_cost_ptr = malloc(sizeof(int)); *g_cost_ptr = g_cost; f_cost_ptr = malloc(sizeof(int)); *f_cost_ptr = f_cost; hash_map_insert(states_g_cost, current_state, g_cost_ptr, 0); heap_memory_location = heap_add(states_heap, state_copy_func(current_state)); hash_table_insert(states_open_set, state_copy_func(current_state), 0); hash_map_insert(states_f_cost, state_copy_func(current_state), f_cost_ptr, 0); hash_map_insert(states_heap_index, current_state, heap_memory_location, 1); path = NULL; found = 0; while(!heap_is_empty(states_heap) && !found) { current_state = state_copy_func(heap_peek(states_heap)); heap_remove(states_heap); hash_table_remove(states_open_set, current_state); hash_map_remove(states_heap_index, current_state); if(state_goal_func(current_state, state_world)) { path = state_back_func(current_state); found = 1; } else { if(!hash_table_insert(states_closed_set, current_state, 0)) { list_push_front(states_overflow, current_state); } successor_list = state_gen_func(current_state, state_world); while(!list_is_empty(successor_list)) { successor_state = list_front(successor_list); g_cost = *(int*)hash_map_get(states_g_cost, current_state) + state_trans_func(current_state, successor_state, state_world); f_cost = g_cost + state_heur_func(successor_state, state_world); tmp_f = hash_map_contains_key(states_f_cost, successor_state) ? *(int*)hash_map_get(states_f_cost, successor_state) : UINT_MAX; if(hash_table_contains(states_closed_set, successor_state) && f_cost > tmp_f) { list_remove_front(successor_list); continue; } if(!hash_table_contains(states_open_set, successor_state) || f_cost < tmp_f) { state_f_cost_set_func(successor_state, f_cost); state_link_func(successor_state, current_state); g_cost_ptr = malloc(sizeof(int)); f_cost_ptr = malloc(sizeof(int)); *g_cost_ptr = g_cost; *f_cost_ptr = f_cost; if(!hash_table_contains(states_open_set, successor_state)) { hash_table_insert(states_open_set, successor_state, 0); heap_memory_location = heap_add(states_heap, state_copy_func(successor_state)); hash_map_insert(states_heap_index, successor_state, heap_memory_location, 1); } else { heap_memory_location = hash_map_get(states_heap_index, successor_state); heap_up_mod_data(states_heap, heap_memory_location, successor_state); } if(!hash_map_set(states_g_cost, successor_state, g_cost_ptr)) { hash_map_insert(states_g_cost, state_copy_func(successor_state), g_cost_ptr, 0); } if(!hash_map_set(states_f_cost, successor_state, f_cost_ptr)) { hash_map_insert(states_f_cost, state_copy_func(successor_state), f_cost_ptr, 0); } list_pop(successor_list); } else { list_remove_front(successor_list); } } list_kill(successor_list); } } heap_kill(states_heap); list_kill(states_overflow); hash_map_kill(states_g_cost); hash_map_kill(states_f_cost); hash_table_kill(states_open_set); hash_table_kill(states_closed_set); hash_map_dissolve(states_heap_index); return path; }
void run() { unsigned i, time; gasnett_tick_t start, end; hash_map_create(params[HASHMAP_SIZE], (grt_bool_t) params[ON_PTHREAD]); grt_barrier(); #ifdef LOCKS grt_lock_state_t state; #endif for (i = 0; i < MY_NUM_OPS; ++i) { grt_word_t key = keys[i], val = values[i]; #ifdef LOCKS hash_t hash = compute_hash(key); hash_map_lock(hash.proc, hash.offset, WRITE, &state); #endif hash_map_insert(key, val); #ifdef LOCKS hash_map_unlock(hash.proc, hash.offset); #endif } BARRIER(); start = gasnett_ticks_now(); #ifdef LOCKS grt_lock_state_t state1, state2; #endif for (i = 0; i < MY_NUM_OPS; ++i) { unsigned idx = grt_random_next() * MY_NUM_OPS; grt_word_t key1 = keys[i]; unsigned second_idx = grt_random_next() * MY_NUM_OPS; grt_word_t key2 = keys[second_idx]; #ifdef LOCKS lock(key1, key2, &state1, &state2); #endif grt_word_t val1, val2; #ifndef LOCKS #ifndef NOLOCKS stm_start(grt_id); #endif #endif grt_bool_t found1 = hash_map_find(key1, &val1); grt_bool_t found2 = hash_map_find(key2, &val2); hash_map_insert(key1, val2); hash_map_insert(key2, val1); #ifndef LOCKS #ifndef NOLOCKS stm_commit(grt_id); #endif #endif #if LOCKS unlock(key1, key2); #endif } end = gasnett_ticks_now(); time = ((unsigned) gasnett_ticks_to_us(end - start)); printf("processor %u: execution time=%f us\n", grt_id, (double) time); fflush(stdout); grt_write(0, time, ×[grt_id]); BARRIER(); if (grt_id == 0) { time = 0, max_time = 0; for (i = 0; i < grt_num_procs; ++i) { gasnett_tick_t this_time = times[i]; time += this_time; if (this_time >= max_time) max_time = this_time; } time_per_op = ((float) time) / params[NUM_OPS]; printf("total CPU time=%f us\n", (double) time); printf("time per operation=%f us\n", time_per_op); printf("max time=%f us\n", (double) max_time); } BARRIER(); hash_map_destroy(); BARRIER(); }
boolean explore_node_map_insert_node_with_key( explore_node_map* map, const void* key, const void* node ) { return hash_map_insert( &map->nodes, key, &node ); }