void bk_create(bk *b, void *src) { sd_bk_file *sdbk = (sd_bk_file*)src; // File ID b->file_id = sdbk->file_id; // Copy VGA image surface_create_from_data( &b->background, SURFACE_TYPE_PALETTE, sdbk->background->w, sdbk->background->h, sdbk->background->data); // Copy sound translation table memcpy(b->sound_translation_table, sdbk->soundtable, 30); // Copy palettes vector_create(&b->palettes, sizeof(palette)); for(int i = 0; i < sdbk->palette_count; i++) { vector_append(&b->palettes, (palette*)sdbk->palettes[i]); } // Copy info structs hashmap_create(&b->infos, 7); bk_info tmp_bk_info; for(int i = 0; i < 50; i++) { if(sdbk->anims[i] != NULL) { bk_info_create(&tmp_bk_info, (void*)sdbk->anims[i], i); hashmap_iput(&b->infos, i, &tmp_bk_info, sizeof(bk_info)); } } }
/** * Compute the range's length if it was a milestone * @param df the dest file whose queue is to be scanned * @param tlen the length of the underlying text * @return true if it worked else 0 */ static int compute_range_lengths( dest_file *df, int tlen ) { int res = 1; hashmap *map = hashmap_create(); if ( map != NULL ) { range *r = df->queue; while ( r != NULL ) { range *s = hashmap_get( map, range_get_name(r) ); if ( s != NULL ) range_set_len( s, range_get_start(r)-range_get_start(s) ); hashmap_put( map, range_get_name(r), r ); r = range_get_next( r ); } hashmap_iterator *iter = hashmap_iterator_create( map ); if ( iter != NULL ) { while ( hashmap_iterator_has_next(iter) ) { char *key = hashmap_iterator_next(iter); range *t = hashmap_get( map, key ); range_set_len( t, tlen-range_get_start(t) ); } hashmap_iterator_dispose( iter ); } else res = 0; hashmap_dispose( map ); } else res = 0; return res; }
void init_handlers() { b.channels = calloc(1, sizeof(b.channels)); VEC_INIT(b.channels, 4); b.plugins = hashmap_create(4); b.handlers = hashmap_create(8); b.commands = hashmap_create(8); b.tick_functions = calloc(1, sizeof(struct double_link)); if (!load_plugin(&b, "common")) { fprintf(stderr, "[core\tfail] error loading common plugin, exiting..\n"); } if (!load_plugin(&b, "uncommon")) { printf("[core\tinfo] the uncommon plugin is not present.\n"); } }
static void _init_caches(void) { icon_cache_16 = hashmap_create(10); { /* Generic fallback icon */ sprite_t * app_icon = malloc(sizeof(sprite_t)); load_sprite(app_icon, "/usr/share/icons/16/applications-generic.bmp"); app_icon->alpha = ALPHA_EMBEDDED; hashmap_set(icon_cache_16, "generic", app_icon); } icon_cache_48 = hashmap_create(10); { /* Generic fallback icon */ sprite_t * app_icon = malloc(sizeof(sprite_t)); load_sprite(app_icon, "/usr/share/icons/48/applications-generic.bmp"); app_icon->alpha = ALPHA_EMBEDDED; hashmap_set(icon_cache_48, "generic", app_icon); } }
int main(int argc, char **argv) { struct pna_hashmap *map; struct my_pair *pair; /* local pair */ struct my_pair p0, p1, p2; p0.key = 0x00; p0.value.a = 4*1; p0.value.b = 4*11; p0.value.c = 4*111; p1.key = 0x01; p1.value.a = 5*1; p1.value.b = 5*11; p1.value.c = 5*111; map = hashmap_create(16, sizeof(p0.key), sizeof(p0.value)); printf("key size: %d\n", sizeof(p0.key)); pair = hashmap_put(map, &(p0.key), &(p0.value)); printf("Put p0 @ 0x%llx\n", pair); pair = hashmap_put(map, &(p1.key), &(p1.value)); printf("Put p1 @ 0x%llx\n", pair); printf("p0@0x%08x: {%llx, %d, %d, %d}\n", &p0, p0.key, p0.value.a, p0.value.b, p0.value.c); printf("pair(p1)@0x%08x: {%llx, %d, %d, %d}\n", pair, pair->key, pair->value.a, pair->value.b, pair->value.c); pair = hashmap_get(map, &(p0.key)); if (pair) { /* update */ printf("Found pair @ 0x%llx\n", pair); } else { pair = hashmap_put(map, &(p0.key), &(p0.value)); printf("Put pair @ 0x%llx\n", pair); } printf("pair(p0)@0x%08x: {%llx, %d, %d, %d}\n", pair, pair->key, pair->value.a, pair->value.b, pair->value.c); pair = hashmap_get(map, &(p1.key)); if (pair) { /* update */ printf("Found pair @ 0x%llx\n", pair); } else { pair = hashmap_put(map, &(p1.key), &(p1.value)); printf("Put pair @ 0x%llx\n", pair); } printf("pair(p1)@0x%08x: {%llx, %d, %d, %d}\n", pair, pair->key, pair->value.a, pair->value.b, pair->value.c); }
/* initialization routine for flow monitoring */ int flowmon_init(void) { int i; struct flowtab_info *info; char table_str[PNA_MAX_STR]; struct proc_dir_entry *proc_node; struct flow_entry e; /* create the /proc base dir for pna tables */ proc_parent = proc_mkdir(PNA_PROCDIR, NULL); /* make memory for table meta-information */ flowtab_info = (struct flowtab_info *) vmalloc(pna_tables * sizeof(struct flowtab_info)); if (!flowtab_info) { pr_err("insufficient memory for flowtab_info\n"); flowmon_cleanup(); return -ENOMEM; } memset(flowtab_info, 0, pna_tables * sizeof(struct flowtab_info)); /* configure each table for use */ for (i = 0; i < pna_tables; i++) { info = &flowtab_info[i]; info->map = hashmap_create(pna_flow_entries, sizeof(e.key), sizeof(e.data)); if (!info->map) { pr_err("Could not allocate hashmap (%d/%d tables, %u sessions)\n", i, pna_tables, pna_flow_entries); flowmon_cleanup(); return -ENOMEM; } flowtab_clean(info); /* initialize the read_mutec */ mutex_init(&info->read_mutex); snprintf(table_str, PNA_MAX_STR, PNA_PROCFILE, i); strncpy(info->table_name, table_str, PNA_MAX_STR); proc_node = create_proc_entry(info->table_name, 0644, proc_parent); if (!proc_node) { pr_err("failed to make proc entry: %s\n", table_str); flowmon_cleanup(); return -ENOMEM; } proc_node->proc_fops = &flowtab_fops; proc_node->mode = S_IFREG | S_IRUGO | S_IWUSR | S_IWGRP; proc_node->uid = 0; proc_node->gid = 0; proc_node->size = PAIRS_BYTES(info->map); } /* get packet arrival timestamps */ net_enable_timestamp(); return 0; }
int main(int argc, char *argv[]) { hashmap_create(); hashmap_test_strings(); hashmap_test_numbers(); hashmap_test_objects(); hashmap_test_size(); return 0; }
error_t cache_create(cache_t* c, int cache_size, hashmap_hash_t hash, hashmap_cmp_t cmp, int data_size, void* context, cache_fault_t fault, cache_evict_t evict, cache_copy_id_t copy_id, cache_free_id_t free_id ) { error_t err; int i; memset(c, 0, sizeof(cache_t)); // allocate the data section. c->data = malloc(cache_size * data_size); if( ! c->data ) { err = ERR_MEM; goto error; } // allocate the list section c->list = (cache_list_entry_t*) malloc(cache_size * sizeof(cache_list_entry_t)); if( ! c->list ) { err = ERR_MEM; goto error; } // allocate the mapping. err = hashmap_create(&c->mapping, hashmap_size_for_elements(cache_size), hash, cmp); if( err ) goto error; c->fault = fault; c->evict = evict; c->copy_id = copy_id; c->free_id = free_id; c->context = context; c->cache_size = cache_size; c->data_size = data_size; for( i = 0; i < c->cache_size; i++ ) { c->list[i].prev = i - 1; c->list[i].next = i + 1; c->list[i].id = NULL; //c->list[i].data_number = i; } c->list[0].prev = -1; c->list[c->cache_size-1].next = -1; c->head = 0; c->tail = c->cache_size-1; return ERR_NOERR; error: free( c->data ); free( c->list ); return err; }
void tcache_init(SDL_Renderer *renderer, int scale_factor, scaler_plugin *scaler) { cache = malloc(sizeof(tcache)); hashmap_create(&cache->entries, 6); cache->renderer = renderer; cache->scaler = scaler; cache->scale_factor = scale_factor; cache->hits = 0; cache->old_frees = 0; cache->misses = 0; DEBUG("Texture cache initialized."); }
/* Performs any initialization of the miniroute layer, if required. */ void miniroute_initialize() { int i; // Cache routes to hosts we've already found - it's of a static size route_cache = hashmap_create(SIZE_OF_ROUTE_CACHE, 0); // Cache the ID of the last discovery request we've seen from each host discovery_packets_seen = hashmap_create(10, 1); // The current discovery requests that are running current_discovery_requests = hashmap_create(10, 1); // The starting ID for discovery requests emanating from this host route_request_id = 0; // Setup a semaphore to control access to the route cache // also used to ensure the cache cleanup thread doesn't delete a route while // we're using it route_cache_sem = semaphore_create(); semaphore_initialize(route_cache_sem, 1); // Create a semaphore to control access to the request ID variable request_id_sem = semaphore_create(); semaphore_initialize(request_id_sem, 1); // Create a malloc'd chunk of route_req_seen structs to store info on // route requests we've recently seen route_reqs_seen = (route_req_seen_t*) malloc(sizeof(route_req_seen_t) * ROUTE_REQS_TO_STORE); for (i = 0; i < ROUTE_REQS_TO_STORE; i++) { route_reqs_seen[i] = (route_req_seen_t) malloc(sizeof(struct route_req_seen)); route_reqs_seen[i]->in_use = 0; // used to indicate this isn't in use } route_reqs_idx = 0; // Register an alarm to prune the route cache every 3 seconds register_alarm(3000, &prune_route_cache, NULL); }
END_TEST START_TEST(test_hashmap_contains) { HashMap *map = hashmap_create(5); hashmap_put(map, "foo", "bar"); ck_assert_int_eq(hashmap_contains(map, "foo"), 1); ck_assert_int_eq(hashmap_contains(map, "baz"), 0); hashmap_destroy(map); }
int add_error_variable (struct conn_s *connptr, const char *key, const char *val) { if (!connptr->error_variables) if (! (connptr->error_variables = hashmap_create (ERRVAR_BUCKETCOUNT))) return (-1); return hashmap_insert (connptr->error_variables, key, val, strlen (val) + 1); }
// Code to handle sequential queries. error_t create_sequential_query( sequential_query_t** q, struct ast_node* ast_node, result_type_t type) { error_t err = ERR_NOERR; switch( ast_node->type ) { case AST_NODE_BOOL: { struct boolean_node* boolean_node = (struct boolean_node*) ast_node; sequential_boolean_query_t* ret; ret = calloc(1, sizeof(sequential_boolean_query_t)); if( ! ret ) return ERR_MEM; ret->query.type = SEQ_BOOLEAN; ret->query.ast_node = ast_node; if( err ) return err; err = create_sequential_query(&ret->left, boolean_node->left, type); if( err ) return err; err = create_sequential_query(&ret->right, boolean_node->right, type); if( err ) return err; *q = &ret->query; } break; case AST_NODE_REGEXP: case AST_NODE_STRING: { sequential_regexp_query_t* ret; ret = calloc(1, sizeof(sequential_regexp_query_t)); if( ! ret ) return ERR_MEM; ret->query.type = SEQ_REGEXP; ret->query.ast_node = ast_node; //err = results_writer_create(&ret->results_writer, RESULT_TYPE_DOC_OFFSETS | RESULT_TYPE_REV_OFFSETS ); // reverse the AST //reverse_regexp(ast_node); /* err = compile_regexp_from_ast(&ret->nfa, ast_node); if( err ) return err; */ err = seq_compile_regexp_from_ast(& ret->matcher, ast_node); if( err ) return err; // create the hashmap. err = hashmap_create(& ret->matches, 128, hash_seq_search_match, cmp_seq_search_match); if( err ) return err; *q = &ret->query; } break; default: err = ERR_INVALID_STR("Unknown AST node"); } return err; }
END_TEST START_TEST(test_hashmap_clear_index) { HashMap *map = hashmap_create(5); hashmap_put(map, "a", "test"); hashmap_clear_index(map, 0); ck_assert_int_eq(map->size, 0); ck_assert_int_eq(hashmap_contains(map, "a"), 0); hashmap_destroy(map); }
END_TEST START_TEST(test_hashmap_index) { HashMap *map = hashmap_create(5); ck_assert_int_eq(hashmap_index(map, "a"), 0); ck_assert_int_eq(hashmap_index(map, "b"), 1); ck_assert_int_eq(hashmap_index(map, "c"), 2); ck_assert_int_eq(hashmap_index(map, "d"), 3); ck_assert_int_eq(hashmap_index(map, "e"), 4); ck_assert_int_eq(hashmap_index(map, "f"), 0); hashmap_destroy(map); }
END_TEST START_TEST(test_hashmap_locate_entry) { HashMap *map = hashmap_create(5); const char *key = "foo"; const char *value = "bar"; hashmap_put(map, key, value); Entry *entry = hashmap_locate_entry(map, key); ck_assert_str_eq(entry->key, key); ck_assert_str_eq(entry->value, value); hashmap_destroy(map); }
static void memory_store( void *block, const char *file, int line ) { if ( allocations == NULL ) allocations = hashmap_create(); if ( allocations != NULL ) { char block_addr[32]; char block_location[32]; snprintf( block_addr, 31, "%ld", (long) block ); snprintf( block_location, 31, "%s: %d", file, line ); char *value = string_store(block_location); hashmap_put( allocations, block_addr, value ); } else debug_report("failed to allocate memory allocations table\n"); }
int add_new_errorpage (char *filepath, unsigned int errornum) { char errornbuf[ERRORNUM_BUFSIZE]; config.errorpages = hashmap_create (ERRPAGES_BUCKETCOUNT); if (!config.errorpages) return (-1); snprintf (errornbuf, ERRORNUM_BUFSIZE, "%u", errornum); if (hashmap_insert (config.errorpages, errornbuf, filepath, strlen (filepath) + 1) < 0) return (-1); return (0); }
int cache_init(struct cache_s **cache) { *cache = (struct cache_s*)Malloc(sizeof(struct cache_s)); if(*cache == NULL) return -1; (*cache) -> map = hashmap_create(CACHE_BUCKET); if((*cache) -> map == NULL) return -1; (*cache) -> curr_size = 0; if(!pthread_rwlock_init(&((*cache) -> lock), NULL)) return -1; return 0; }
int main() { HashMap* hashmap = hashmap_create(); hashmap_add_node(hashmap, "trykey", "tryval", NULL, NULL); hashmap_add_node(hashmap, "trykey2", "tryval2", NULL, NULL); HashMapNode* node = hashmap_get(hashmap, "trykey"); assert(!strcmp("trykey", node->key)); assert(!strcmp("tryval", node->value)); node = hashmap_get(hashmap, "trykey2"); assert(!strcmp("trykey2", node->key)); assert(!strcmp("tryval2", node->value)); assert(hashmap->count == 2); hashmap_destroy(hashmap); assert(hashmap->count == 0); return 0; }
END_TEST START_TEST(test_hashmap_get) { HashMap *map = hashmap_create(5); const char *key = "foo"; const char *value = "bar"; unsigned int retrieved; const char *result; hashmap_put(map, key, value); retrieved = hashmap_get(map, key, &result); ck_assert_str_eq(result, value); ck_assert_int_eq(retrieved, 1); hashmap_destroy(map); }
int main(int argc, char **argv) { hashmap_t *map = NULL; map = hashmap_create(102400); if(map == NULL) { printf("create hashmap error:%m"); return -1; } hash_test_func(map); hashmap_free(map); return 0; }
END_TEST START_TEST(test_hashmap_entry_for_key) { HashMap *map = hashmap_create(5); Entry *entry; const char *key = "foo"; hashmap_put(map, key, "bar"); // Existing entry entry = hashmap_entry_for_key(map, key); ck_assert_str_eq(entry->key, key); // Expecting a new entry entry = hashmap_entry_for_key(map, "baz"); ck_assert(entry->key == NULL); hashmap_destroy(map); }
/** * Check if a list is broken by being circular * @param c a card pointer somewhere in the list * @return 1 if it has two ends else 0 */ int card_list_circular( card *c ) { int res = 0; hashmap *hm = hashmap_create( 12, 0 ); card *right = c->right; char junk[16]; UChar key[32]; char ckey[32]; strcpy( junk, "junk" ); snprintf(ckey,32,"%lx",(long)right); u_print( key, ckey, strlen(ckey) ); hashmap_put( hm, key, junk ); while ( !res && right != NULL ) { snprintf(ckey,32,"%lx",(long)right); //printf("%s\n",ckey); ascii_to_uchar( ckey, key, 32 ); if ( hashmap_contains(hm,key) ) res = 1; else { hashmap_put( hm, key, junk ); right = right->right; } } card *left = c->left; while ( !res && left != NULL ) { snprintf(ckey,32,"%lx",(long)left); //printf("%s\n",ckey); ascii_to_uchar( ckey, key, 32 ); if ( hashmap_contains(hm,key) ) res = 1; else { hashmap_put( hm, key, junk ); left = left->left; } } hashmap_dispose( hm, NULL ); return res; }
minithread_t minithread_create(proc_t proc, arg_t arg) { // Create the TCB minithread_t tcb = (minithread_t) malloc(sizeof(minithread)); // ISO C90... interrupt_level_t prev_level; // create the variables for the stack // these are **voids, b/c stack_pointer_t is a void* stack_pointer_t* stack_top = (stack_pointer_t*) malloc(sizeof(stack_pointer_t)); stack_pointer_t* stack_base = (stack_pointer_t*) malloc(sizeof(stack_pointer_t)); // Ensure this is true since we rely on this in a few lines *stack_top = NULL; *stack_base = NULL; // Initialize the TCB prev_level = set_interrupt_level(DISABLED); tcb->id = id_count++; set_interrupt_level(prev_level); tcb->priority = 0; tcb->proc = proc; tcb->arg = arg; tcb->dir_block_id = 1; // todo - make this come from the superblock tcb->open_files = hashmap_create(10, 1); // Allocate the stack minithread_allocate_stack(stack_base, stack_top); // Ensure the stack allocation succeeded if (*stack_base == NULL) return NULL; tcb->stack_base = stack_base; tcb->stack_top = stack_top; // Initialize the stack minithread_initialize_stack(stack_top, proc, arg, (proc_t) minithread_finished, (arg_t) tcb); return tcb; }
static int ini_init_context(ini_context_t *context) { const int buckets = 511; int result; memset(context, 0, sizeof(ini_context_t)); context->current_section = &context->global; context->sections = hashmap_create(buckets, HM_FLAG_POOL, time33_hash_func, ini_compare_key); if (context->sections == NULL) { result = errno != 0 ? errno : ENOMEM; fprintf(stderr, "file: "__FILE__", line: %d, " "hashmap_create fail, errno: %d, error info: %s", __LINE__, result, strerror(result)); return result; } return 0; }
END_TEST START_TEST(test_hashmap_remove) { HashMap *map = hashmap_create(5); const char *key = "foo"; unsigned int result; result = hashmap_remove(map, key); ck_assert_int_eq(result, 0); hashmap_put(map, key, "bar"); ck_assert_int_eq(hashmap_contains(map, key), 1); ck_assert_int_eq(map->size, 1); result = hashmap_remove(map, key); ck_assert_int_eq(result, 1); ck_assert_int_eq(hashmap_contains(map, key), 0); ck_assert_int_eq(map->size, 0); hashmap_destroy(map); }
END_TEST START_TEST(test_hashmap_put) { HashMap *map = hashmap_create(5); const char *key = "foo"; const char *value = "bar"; const char *value_replacement = "baz"; const char *result; ck_assert_int_eq(hashmap_put(map, key, value), 1); ck_assert_int_eq(hashmap_get(map, key, &result), 1); ck_assert_str_eq(result, value); ck_assert_int_eq(map->size, 1); ck_assert_int_eq(hashmap_put(map, key, value_replacement), 1); ck_assert_int_eq(hashmap_get(map, key, &result), 1); ck_assert_str_eq(result, value_replacement); ck_assert_int_eq(map->size, 1); hashmap_destroy(map); }
// OK void hashmap_resize(t_hashmap* map){ printf("\n---------------MAP_RESIZE-------------------\n"); int slots_before = map->slots; t_hashmap_entry** entries = map->entries; // Calcul du nouveau nombre de slots grâce à grow factor int slots_after = map->slots * map->grow_factor; map = hashmap_create(slots_after, map->load_factor, map->grow_factor); int i; for(i = 0; i < slots_before; i++){ t_hashmap_entry* entry = entries[i]; while(entry != NULL){ // printf("DEBUG: i=%d, key = %s\tvalue = %s\n", i, entry->key, (char*)entry->value); hashmap_put(map, entry->key, entry->value,entry->type); entry = entry->next; } } }
hashmap_t* hashmap_load(char * db) { hashmap_t* hashmap=hashmap_create(); FILE* fp; size_t len = 0; ssize_t read; char* line=NULL; fp = fopen(db, "r"); if (fp == NULL) return NULL; while ((read = getline(&line, &len, fp)) != -1) { if (line[read - 1] == '\n') line[read - 1] = '\0'; list_value_t* addlist=JSON_parse(line); hashmap_add_list(hashmap,addlist); } fclose(fp); if (line) free(line); return hashmap; }