void gui_nicklist_send_hsignal (const char *signal, struct t_gui_buffer *buffer, struct t_gui_nick_group *group, struct t_gui_nick *nick) { if (!gui_nicklist_hsignal) { gui_nicklist_hsignal = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); } if (!gui_nicklist_hsignal) return; hashtable_remove_all (gui_nicklist_hsignal); hashtable_set (gui_nicklist_hsignal, "buffer", buffer); hashtable_set (gui_nicklist_hsignal, "parent_group", (group) ? group->parent : nick->group); if (group) hashtable_set (gui_nicklist_hsignal, "group", group); if (nick) hashtable_set (gui_nicklist_hsignal, "nick", nick); (void) hook_hsignal_send (signal, gui_nicklist_hsignal); }
PathSegment *raster_make_segment(Raster *raster, short eid, short v1, short h1, short h2, short v2) { PathSegment *s = MEM_calloc(sizeof(*s)); s->head.eid = eid; s->head.type = PATH_SEGMENT; s->v1 = (void*)hashtable_get(&raster->verts, v1); s->h1 = (void*)hashtable_get(&raster->verts, h1); s->h2 = (void*)hashtable_get(&raster->verts, h2); s->v2 = (void*)hashtable_get(&raster->verts, v2); if (!s->v1) { fprintf(stderr, "failed to find vert with id %d\n", v1); return NULL; } if (!s->h1) { fprintf(stderr, "failed to find vert with id %d\n", h1); return NULL; } if (!s->h2) { fprintf(stderr, "failed to find vert with id %d\n", h2); return NULL; } if (!s->v2) { fprintf(stderr, "failed to find vert with id %d\n", v2); return NULL; } hashtable_set(&raster->segments, eid, (intptr_t)s); hashtable_set(&raster->master, eid, (intptr_t)s); return s; }
Path *raster_make_path(Raster *raster, short eid, short style) { Path *p = MEM_calloc(sizeof(*p)); p->head.eid = eid; p->head.type = PATH_PATH; p->style = style; hashtable_set(&raster->paths, eid, (intptr_t)p); hashtable_set(&raster->master, eid, (intptr_t)p); list_append(&raster->renderlist, p); return p; }
PathVertex *raster_make_vertex(Raster *raster, short eid, short x, short y) { PathVertex *v = MEM_calloc(sizeof(*v)); v->head.eid = eid; v->head.type = PATH_VERTEX; v->x = x; v->y = y; hashtable_set(&raster->verts, eid, (intptr_t)v); hashtable_set(&raster->master, eid, (intptr_t)v); return v; }
void hashtable_set(HashTable *ht, uintptr_t key, intptr_t val) { uintptr_t hash, probe = 0; if (ht->used >= ht->size / 3) { int newsize = hashsizes[++ht->cursize]; struct _hashentry *newtable = MEM_malloc(sizeof(*newtable)*newsize), *oldtable = ht->table; int i, oldsize = ht->size; for (i = 0; i < newsize; i++) { newtable[i].key = KEY_UNUSED; newtable[i].val = VAL_UNUSED; } //set up new, bigger hash table ht->size = newsize; ht->table = newtable; ht->used = 0; //reinsert old items for (i = 0; i < oldsize; i++) { if (oldtable[i].key != KEY_UNUSED || oldtable[i].val != VAL_UNUSED) { hashtable_set(ht, oldtable[i].key, oldtable[i].val); } } if (oldtable != ht->_static_table) MEM_free(oldtable); hashtable_set(ht, key, val); return; } hash = (key + probe) % ht->size; while (ht->table[hash].key != KEY_UNUSED || ht->table[hash].val != VAL_UNUSED) { if (ht->table[hash].key == key) { ht->table[hash].val = val; return; } probe = (probe + 1) * 2; hash = (key + probe) % ht->size; } ht->table[hash].key = key; ht->table[hash].val = val; ht->used++; }
void gui_color_palette_add_alias_cb (void *data, struct t_hashtable *hashtable, const void *key, const void *value) { struct t_gui_color_palette *color_palette; char *error; int number; /* make C compiler happy */ (void) data; (void) hashtable; color_palette = (struct t_gui_color_palette *)value; if (color_palette && color_palette->alias) { error = NULL; number = (int)strtol ((char *)key, &error, 10); if (error && !error[0]) { hashtable_set (gui_color_hash_palette_alias, color_palette->alias, &number); } } }
void saveProcesssIp(char *process_hdl, char *ip, char *msgRet) { dbprint("save: %s -> %s\n", process_hdl, ip); //TODO - lock hashtable_set(&tblOrginalIp, process_hdl, ip); sprintf(msgRet, "ACK 0\n"); //TODO - unlock }
static char * cmd_set (bitu_app_t *app, char **params, int num_params) { char *error; if ((error = _validate_num_params ("set", 2, num_params)) != NULL) return error; hashtable_set (app->environment, strdup (params[0]), strdup (params[1])); return NULL; }
int main() { char key[9]; int i; int pos = 0; init_key(key, ARRAY_SIZE(key)); for (i = 0; i < HASHTABLE_SIZE; i++) { void *unchanged = NULL + 1234; void *actual = unchanged; void *value = NULL + i; assert(!find_next_collision(pos, key, strlen(key))); assert(hashtable_get(key, &actual) == 1); assert(actual == unchanged); assert(!hashtable_set(key, value)); assert(hashtable_test_key_at_pos(key, pos + i % HASHTABLE_SIZE)); } assert(!find_next_collision(pos, key, strlen(key))); /* table is full */ assert(hashtable_set(key, NULL) == 1); init_key(key, ARRAY_SIZE(key)); for (i = 0; i < HASHTABLE_SIZE; i++) { void *expect = NULL + i; void *actual; assert(!find_next_collision(pos, key, strlen(key))); assert(!hashtable_get(key, &actual)); assert(actual == expect); } assert(hashtable_remove("asdf")); assert(hashtable_remove("")); init_key(key, ARRAY_SIZE(key)); for (i = 0; i < HASHTABLE_SIZE; i++) { assert(!find_next_collision(pos, key, strlen(key))); assert(!hashtable_remove(key)); assert(hashtable_remove(key)); } init_key(key, ARRAY_SIZE(key)); assert(hashtable_remove(key)); return 0; }
int secure_decrypt_data_not_decrypted (const char *passphrase) { char **keys, *buffer, *decrypted; const char *value; int num_ok, num_keys, i, length_buffer, length_decrypted, rc; /* we need a passphrase to decrypt data! */ if (!passphrase || !passphrase[0]) return 0; num_ok = 0; keys = string_split (hashtable_get_string (secure_hashtable_data_encrypted, "keys"), ",", 0, 0, &num_keys); if (keys) { for (i = 0; i < num_keys; i++) { value = hashtable_get (secure_hashtable_data_encrypted, keys[i]); if (value && value[0]) { buffer = malloc (strlen (value) + 1); if (buffer) { length_buffer = string_decode_base16 (value, buffer); decrypted = NULL; length_decrypted = 0; rc = secure_decrypt_data (buffer, length_buffer, secure_hash_algo[CONFIG_INTEGER(secure_config_crypt_hash_algo)], secure_cipher[CONFIG_INTEGER(secure_config_crypt_cipher)], passphrase, &decrypted, &length_decrypted); if ((rc == 0) && decrypted) { hashtable_set (secure_hashtable_data, keys[i], decrypted); hashtable_remove (secure_hashtable_data_encrypted, keys[i]); num_ok++; } if (decrypted) free (decrypted); free (buffer); } } } string_free_split (keys); } return num_ok; }
hashtable_t* hashtable_load(FILE* file, void* (*loadValue)(const void* key,size_t key_length,off_t location,void* metadata),void* metadata) { int i,j; size_t size; int magic=-1; fread(&magic,sizeof(int),1,file); if(magic!=HASHTABLE_MAGIC) { warn("bad magic %d!=%d",magic,HASHTABLE_MAGIC); return NULL; } fread(&size,sizeof(size_t),1,file); hashtable_t* h=hashtable_new_size(size); off_t bucketLocation[size]; size_t bucketLength[size]; size_t bucketSize[size]; for(i=0;i<h->size;i++) { fread(&bucketLocation[i],sizeof(off_t),1,file); fread(&bucketLength[i],sizeof(size_t),1,file); fread(&bucketSize[i],sizeof(size_t),1,file); } for(i=0;i<h->size;i++) { //warn("%d %lld %d %d",i,bucketLocation[i],bucketLength[i],bucketSize[i]); if(bucketLength[i]!=0) { fseeko(file,bucketLocation[i],SEEK_SET); char buffer[bucketSize[i]]; fread(buffer,bucketSize[i],1,file); char* ptr=buffer; for(j=0;j<bucketLength[i];j++) { size_t key_length=*(size_t*)ptr; ptr+=sizeof(size_t); char* key=ptr; ptr+=key_length; void* value=NULL; if(loadValue!=NULL) { value=loadValue(key,key_length,*(off_t*)ptr,metadata); ptr+=sizeof(off_t); } else { value=*(void**)ptr; ptr+=sizeof(void*); } hashtable_set(h,key,key_length,value); } } } return h; }
TEST(String, EvalPathHome) { char *home, *result; int length_home, length_weechat_home; struct t_hashtable *extra_vars; home = getenv ("HOME"); length_home = strlen (home); length_weechat_home = strlen (weechat_home); POINTERS_EQUAL(NULL, string_eval_path_home (NULL, NULL, NULL, NULL)); result = string_eval_path_home ("/tmp/test", NULL, NULL, NULL); STRCMP_EQUAL(result, "/tmp/test"); free (result); result = string_eval_path_home ("~/test", NULL, NULL, NULL); CHECK(strncmp (result, home, length_home) == 0); LONGS_EQUAL(length_home + 5, strlen (result)); STRCMP_EQUAL(result + length_home, "/test"); free (result); result = string_eval_path_home ("%h/test", NULL, NULL, NULL); CHECK(strncmp (result, weechat_home, length_weechat_home) == 0); LONGS_EQUAL(length_weechat_home + 5, strlen (result)); STRCMP_EQUAL(result + length_weechat_home, "/test"); free (result); setenv ("WEECHAT_TEST_PATH", "path1", 1); result = string_eval_path_home ("%h/${env:WEECHAT_TEST_PATH}/path2", NULL, NULL, NULL); CHECK(strncmp (result, weechat_home, length_weechat_home) == 0); LONGS_EQUAL(length_weechat_home + 12, strlen (result)); STRCMP_EQUAL(result + length_weechat_home, "/path1/path2"); free (result); extra_vars = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); CHECK(extra_vars); hashtable_set (extra_vars, "path2", "value"); result = string_eval_path_home ("%h/${env:WEECHAT_TEST_PATH}/${path2}", NULL, extra_vars, NULL); CHECK(strncmp (result, weechat_home, length_weechat_home) == 0); LONGS_EQUAL(length_weechat_home + 12, strlen (result)); STRCMP_EQUAL(result + length_weechat_home, "/path1/value"); free (result); hashtable_free (extra_vars); }
char* lru_find(char* k) { cache_t* item=(cache_t*)hashtable_get(_ht,k); if(item) { hashtable_remove(_ht,k); hashtable_set(_ht,k,item); return item->v; } return NULL; }
int main(){ struct hashtable ht; hashtable_init(&ht,1024,sizeof(int),sizeof(int)); for(int i = 0; i < 20;i++){ int value = i * 2; hashtable_set(&ht,&i,&value); } /* test remove */ { int key = 5; hashtable_remove(&ht,&key); } /* test value retrieval */ for(int i = 0; i < 20; i++){ int value = 0; if(hashtable_get(&ht,&i,&value) == HT_KEY_NOT_FOUND){ printf("Key not found\n"); }else{ printf("%d -> %d\n",i,value); } } /* test overwriting */ { int key = 3; int value = 20; int ret = 0; hashtable_set(&ht,&key,&value); hashtable_get(&ht,&key,&ret); printf("%d -> %d\n",key,ret); } hashtable_destroy(&ht); return 0; }
/** * Resize the allocated memory. * Warning: clears the table of all entries. */ void hashtable_resize(hashtable* t, unsigned int capacity) { assert(capacity >= t->size); unsigned int old_capacity = t->capacity; hashtable_entry* old_body = t->body; t->body = hashtable_body_allocate(capacity); t->capacity = capacity; for (int i = 0; i < old_capacity; i++) { if (old_body[i].key != NULL) { hashtable_set(t, old_body[i].key, old_body[i].value); } } }
symbol_t* add_symbol(symtable_t *t, const char* name) { size_t len = strlen(name)+1; symbol_t *sym = get_symbol(t, name); if(sym) return sym; sym = (symbol_t*)calloc(1, sizeof(symbol_t)); sym->id = t->curid++; hashtable_set(&t->ht, name, len, sym); return sym; }
hashtable_t* string_array_to_hashtable(array_t* input) { hashtable_t* output=hashtable_new(); int i; if(input->length % 2 != 0)warn("hashtable_new_from_string_array(%p), odd number of element in array (%zd), last element ignored", input, input->length); for(i=0; i<input->length; i+=2) { string_t* key = (string_t*)array_get(input, i); string_t* value = (string_t*)array_get(input, i+1); hashtable_set(output, key->data, key->length, string_copy(value)); } return output; }
static int cache_put(lua_State * L) { size_t len1,len2; const char * key_l=lua_tolstring(L,1,&len1); char * key=malloc(len1+1); memcpy(key,key_l,len1); key[len1]='\0'; const char * val_l=lua_tolstring(L,2,&len2); char * val=malloc(len2); memcpy(val,val_l,len2); //printf("%s %s %u\n",val,val_l,len2); pthread_rwlock_wrlock(&rwlock); hashtable_set(ht,key,val,len2); pthread_rwlock_unlock(&rwlock); return 0; }
int bitu_plugin_ctx_load (bitu_plugin_ctx_t *plugin_ctx, const char *lib) { bitu_plugin_t *plugin; if ((plugin = bitu_plugin_load (lib)) == NULL) return TA_ERROR; if (hashtable_set (plugin_ctx->plugins, strdup (bitu_plugin_name (plugin)), plugin) == -1) { bitu_plugin_free (plugin); return TA_ERROR; } return TA_OK; }
service_t* service_set(hashtable_t *table, char *name, service_mothod method, uint16_t num_all_nodes, service_node_t *node_arr[]) { hashtable_data_t *data = hashtable_get(table, name); if(data != NULL) return NULL; service_t *service = calloc(sizeof(service_t), 1); if(service == NULL) return NULL; service->name = name; service->len = strlen(name); service->method = method; service->num_all_nodes = num_all_nodes; int i; service_node_t *snode, *sprev; for(i = 0; i < num_all_nodes; i++) { snode = node_arr[i]; if(service->all_next == NULL) { service->all_next = snode; }else { sprev->all_next = snode; } sprev = snode; } data = hashtable_data_init(name, (void *) service); if(data == NULL) { service_free(service); return NULL; } hashtable_set(table, data); return service; }
void getProcesssIp(char *process_hdl, char *new_ip, char *msgRet) { char *ip; dbprint("lookup: %s with %s ", process_hdl, new_ip); //TODO - lock hashtable_set(&tblNewIp, process_hdl, new_ip); if (hashtable_get(&tblOrginalIp, process_hdl, (void **) &ip) == 1) { dbprint("and get %s\n", ip); sprintf(msgRet, "ACK %s\n", ip); } else { dbprint("%s", " not found\n"); sprintf(msgRet, "NACK\n"); } //TODO - unlock }
void hdata_new_list (struct t_hdata *hdata, const char *name, void *pointer, int flags) { struct t_hdata_list *list; if (!hdata || !name) return; list = malloc (sizeof (*list)); if (list) { list->pointer = pointer; list->flags = flags; hashtable_set (hdata->hash_list, name, list); } }
void lru_add(char* k,char* v) { char* vtmp; cache_t* item,*tmp_item; item=(cache_t*)malloc(sizeof(cache_t)); vtmp=malloc(strlen(v)+1); strcpy(vtmp,v); item->k=k; item->v=vtmp; hashtable_set(_ht,k,item); if(hashtable_count(_ht)>=MAX_CACHE_SIZE) { //todo delete } }
struct t_hdata * hdata_new (struct t_dogechat_plugin *plugin, const char *hdata_name, const char *var_prev, const char *var_next, int create_allowed, int delete_allowed, int (*callback_update)(void *data, struct t_hdata *hdata, void *pointer, struct t_hashtable *hashtable), void *callback_update_data) { struct t_hdata *new_hdata; if (!hdata_name || !hdata_name[0]) return NULL; new_hdata = malloc (sizeof (*new_hdata)); if (new_hdata) { new_hdata->name = strdup (hdata_name); new_hdata->plugin = plugin; new_hdata->var_prev = (var_prev) ? strdup (var_prev) : NULL; new_hdata->var_next = (var_next) ? strdup (var_next) : NULL; new_hdata->hash_var = hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_POINTER, NULL, NULL); new_hdata->hash_var->callback_free_value = &hdata_free_var; new_hdata->hash_list = hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_POINTER, NULL, NULL); new_hdata->hash_list->callback_free_value = &hdata_free_list; hashtable_set (dogechat_hdata, hdata_name, new_hdata); new_hdata->create_allowed = create_allowed; new_hdata->delete_allowed = delete_allowed; new_hdata->callback_update = callback_update; new_hdata->callback_update_data = callback_update_data; new_hdata->update_pending = 0; } return new_hdata; }
int main(int argc, char **argv) { hash_t = hashtable_create(); array_a = arraylist_create(); hashtable_set(hash_t, "application name", "Test Application"); arraylist_add(array_a, "abcd"); LOG.debug = 1; //LOG.error_log_path = "error.log"; log_init(); log_debug("Starting server now...\n"); config_init(); parse_conf(); read_logfile("./test.log"); hashtable_destroy(hash_t); arraylist_destroy(array_a); return 0; }
void gui_focus_buffer_localvar_map_cb (void *data, struct t_hashtable *hashtable, const void *key, const void *value) { struct t_hashtable *hashtable_focus; char hash_key[512]; /* make C compiler happy */ (void) hashtable; hashtable_focus = (struct t_hashtable *)data; if (hashtable_focus && key && value) { snprintf (hash_key, sizeof (hash_key), "_buffer_localvar_%s", (const char *)key); hashtable_set (hashtable_focus, hash_key, (const char *)value); } }
void gui_color_palette_add (int number, const char *value) { struct t_gui_color_palette *new_color_palette; char str_number[64]; gui_color_palette_alloc_structs (); new_color_palette = gui_color_palette_new (number, value); if (!new_color_palette) return; snprintf (str_number, sizeof (str_number), "%d", number); hashtable_set (gui_color_hash_palette_color, str_number, new_color_palette); gui_color_palette_build_aliases (); if (gui_init_ok) gui_color_buffer_display (); }
static void _memory_tracker_track( void* addr, uint64_t size ) { if( addr ) do { int32_t tag = atomic_exchange_and_add32( &_memory_tag_next, 1 ); if( tag >= MAX_CONCURRENT_ALLOCATIONS ) { int32_t newtag = tag % MAX_CONCURRENT_ALLOCATIONS; atomic_cas32( &_memory_tag_next, newtag, tag + 1 ); tag = newtag; } if( !_memory_tags[ tag ].address && atomic_cas_ptr( &_memory_tags[ tag ].address, addr, 0 ) ) { _memory_tags[ tag ].size = (uintptr_t)size; stacktrace_capture( _memory_tags[ tag ].trace, 14, 3 ); hashtable_set( _memory_table, (uintptr_t)addr, (uintptr_t)( tag + 1 ) ); return; } } while( true ); }
int main( int argc, char *argv[] ) { struct hashtable hashtable; hashtable_init( &hashtable, 65536 ); printf("%p\n", hashtable_get(&hashtable, 1000)); printf("%p\n", hashtable_get(&hashtable, 500)); int i; for (i = 0; i <= 11; i++){ hashtable_value_t val = {.ptr = (void *)(i*i), .state=-1}; hashtable_set(&hashtable, i, val); } printf("%p\n", hashtable_get(&hashtable, 100)); printf("%p\n", hashtable_get(&hashtable, 10)); printf("%p\n", hashtable_get(&hashtable, 1)); hashtable_finalize(&hashtable); return 0; }
void hdata_new_var (struct t_hdata *hdata, const char *name, int offset, int type, int update_allowed, const char *array_size, const char *hdata_name) { struct t_hdata_var *var; if (!hdata || !name) return; var = malloc (sizeof (*var)); if (var) { var->offset = offset; var->type = type; var->update_allowed = update_allowed; var->array_size = (array_size && array_size[0]) ? strdup (array_size) : NULL; var->hdata_name = (hdata_name && hdata_name[0]) ? strdup (hdata_name) : NULL; hashtable_set (hdata->hash_var, name, var); } }