void notify(){ int fd; struct sockaddr_in server_addr; char buf_buf_msg[MAX_LINE]; if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ perror("Create socket error:"); return; } server_addr.sin_addr.s_addr = inet_addr(ip); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(successor->port); if(connect(fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){ successor->port = successor2->port; successor->key = hash_str(successor->port); successor2->port = node.port; successor2->key = hash_str(successor2->port); server_addr.sin_port = htons(successor->port); predecessor_update(successor->port); if(connect(fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){ perror("Connect error:"); return; } } memset(buf_buf_msg,0,sizeof(buf_buf_msg)); sprintf(buf_buf_msg,"notify %d",node.port); if(send(fd, buf_buf_msg, MAX_LINE, 0) < 0){ perror("Send error:"); return; } close(fd); }
void stabilize(){ int sock; struct sockaddr_in server_addr; char buf_buf_msg[MAX_LINE], buf_reply[MAX_LINE]; if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){ perror("Create socket error:"); return; } server_addr.sin_addr.s_addr = inet_addr(ip); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(successor->port); if(connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){ successor->port = successor2->port; successor->key = hash_str(successor->port); successor2->port = node.port; successor2->key = hash_str(successor2->port); server_addr.sin_port = htons(successor->port); predecessor_update(successor->port); if(connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){ perror("Connect error:"); return; } } memset(buf_buf_msg,0,sizeof(buf_buf_msg)); sprintf(buf_buf_msg,"stabilize %d",node.port); if(send(sock, buf_buf_msg, MAX_LINE, 0) < 0){ perror("Send error:"); return; } memset(buf_reply,0,sizeof(buf_reply)); if(recv(sock, buf_reply, MAX_LINE, 0) < 0){ perror("Recv error: "); return; } int recvPort; unsigned recvHash; recvPort=atoi(buf_reply); recvHash=hash_str(recvPort); if(recvPort>0){ if((successor->key > node.key) && (node.key < recvHash) && (recvHash < successor->key)){ successor->port = recvPort; successor->key = hash_str(successor->port); } else if((successor->key < node.key) && (recvPort!=node.port) && ((recvHash > node.key) || (recvHash < successor->key))){ successor->port = atoi(buf_reply); successor->key = hash_str(successor->port); } } close(sock); }
static void hash_object(object_t* object, uint32_t* hash) { switch (object->type) { case type_nil: *hash = hash_nil(*hash); return; case type_bool: *hash = hash_bool(*hash, object->b); return; case type_double: *hash = hash_double(*hash, object->d); return; case type_int: *hash = hash_i64(*hash, object->i); return; case type_uint: *hash = hash_u64(*hash, object->u); return; case type_str: *hash = hash_str(*hash, object->str, object->l); return; // unused types in this benchmark #if 0 case type_float: write_float(hash, node_float(node)); return; case type_bin: *hash = hash_str(*hash, node_data(node), node_data_len(node)); return; case type_ext: *hash = hash_u8(*hash, node_exttype(node)); *hash = hash_str(*hash, node_data(node), node_data_len(node)); return; #endif case type_array: { uint32_t count = object->l; for (uint32_t i = 0; i < count; ++i) hash_object(object->children + i, hash); *hash = hash_u32(*hash, count); return; } case type_map: { uint32_t count = object->l; for (uint32_t i = 0; i < count; ++i) { // we expect keys to be short strings object_t* key = object->children + (i * 2); *hash = hash_str(*hash, key->str, key->l); hash_object(object->children + (i * 2) + 1, hash); } *hash = hash_u32(*hash, count); return; } default: break; } abort(); }
/* Lookup a particular node in hash; return pointer to node if found, NULL otherwise */ struct host_stats *get_host(char *str) { struct host_stats *node; #ifdef DEBUG ASSERT(str); ASSERT(strlen(str) > 0); ASSERT((hash_str(str, HASHSIZE) >= 0) && (hash_str(str, HASHSIZE) < HASHSIZE)); #endif for (node = stats[hash_str(str, HASHSIZE)]; node != NULL; node = node->next) if (str_compare(str, node->host) == 0) return node; return NULL; }
/* Lookup a particular node in hash; return pointer to node if found, NULL otherwise */ FORMAT_NODE *get_field(char *str) { FORMAT_NODE *node; #ifdef DEBUG ASSERT(str); ASSERT(strlen(str) > 0); ASSERT((hash_str(str, HASHSIZE) >= 0) && (hash_str(str, HASHSIZE) < HASHSIZE)); #endif for (node = fields[hash_str(str, HASHSIZE)]; node != NULL; node = node->next) if (str_compare(str, node->name) == 0) return node; return NULL; }
static unsigned long calc_hash(const struct obj *obj) { unsigned long hash = obj->type; switch (obj->type) { case TYPE_NIL: break; case TYPE_NUM: hash += (unsigned long) obj->val.num; break; case TYPE_STR: hash = hash_str(hash, &obj->val.str); break; case TYPE_CFUNC: hash += (unsigned long) obj->val.ofs; break; case TYPE_FUNC: hash += (unsigned long) obj->val.cfunc; break; case TYPE_HASH: break; default: abort(); break; } return (hash); }
bool run_test(uint32_t* hash_out) { bson_t bson = BSON_INITIALIZER; bool ok; if (root_object->type == type_map) ok = append_document(&bson, root_object); else ok = append_array(&bson, root_object); if (!ok) { fprintf(stderr, "libbson error writing data!\n"); bson_destroy(&bson); return false; } *hash_out = hash_str(*hash_out, (const char*)bson_get_data(&bson), bson.len); // The documentation says that bson_destroy() should be called // regardless of whether the bson_t was initialized via bson_init() // bson_new() or BSON_INITIALIZER. This is because it stores a flag // to say whether it should be freed when destroyed. // This causes a warning under -flto about freeing a stack object // even though the bson_t is set for static. bson_destroy(&bson); return true; }
void dictionary_set_int_str(Dictionary_t *dic, const char *key, int64_t int_value, const char *str_val) { pthread_mutex_lock(&dic->mutex); struct dic_node *np = __get_dictionary_node_with_key(dic, key); if (np) { np->int_value = int_value; if (np->str_value) free(np->str_value); np->str_value = NULL; if (str_val) { size_t len = strlen(str_val); np->str_value = malloc(len+1); memcpy(np->str_value, str_val, len+1); } pthread_mutex_unlock(&dic->mutex); return; } np = __new_dic_node(key); if (!np) { pthread_mutex_unlock(&dic->mutex); return; } np->int_value = int_value; if (str_val) { size_t len = strlen(str_val); np->str_value = malloc(len+1); memcpy(np->str_value, str_val, len+1); } unsigned int hashval = hash_str(key, dic->bucket_count); np->next = dic->buckets[hashval]; dic->buckets[hashval] = np; pthread_mutex_unlock(&dic->mutex); }
reshandle_t rs_add_resource(const char* filepath, void* ptr, reshandle_t override_hdl, pfn_unload_res unload_func) { reshandle_t res_hdl = override_hdl; /* if rs_resource handle is overrided, release previous rs_resource and put new one into * current rs_resource slot */ if (res_hdl != INVALID_HANDLE) { struct rs_resource* rss = (struct rs_resource*)g_rs.ress.buffer; struct rs_resource* rs = &rss[GET_INDEX(res_hdl)]; if (rs->hdl != INVALID_HANDLE) rs->unload_func(rs->ptr); rs->ptr = ptr; rs->hdl = res_hdl; rs->unload_func = unload_func; ASSERT(str_isequal(rs->filepath, filepath)); } else { /* just add it to rs_resource database */ res_hdl = rs_add_todb(filepath, ptr, unload_func); if (res_hdl == INVALID_HANDLE) return INVALID_HANDLE; hashtable_chained_add(&g_rs.dict, hash_str(filepath), GET_INDEX(res_hdl)); } return res_hdl; }
int main ( int argc, char *argv[] ) { uint32_t userid,id; char email[65]; FILE * fp; char * line = NULL; size_t len = 0; ssize_t read; fp = fopen("f.in", "r"); //fp = fopen("/etc/mtab", "r"); if (fp == NULL) exit(1); while ((read = getline(&line, &len, fp)) != -1) { sscanf(line,"%u|%s",&userid,email); //int i=0; //char *p=line; //while ( *p !='|' && *p!='\0' ) p++; id=hash_str(email); // printf ("%u\n",id); printf ("%u%u:('%s',%u),\n", id%10,(id/10)%10, email, userid); } if (line) free(line); return 0; } /* ---------- end of function main ---------- */
/* Remove the given node from the hash and return it to the free stack; returns the correct node for continuing to traverse the hash */ struct host_stats *remove_node(struct host_stats *node, struct host_stats *prev) { struct host_stats *next; unsigned int hashval; /* Unlink the node from the hash */ if (prev == NULL) { hashval = hash_str(node->host, HASHSIZE); if (node->next) { stats[hashval] = node->next; } else { stats[hashval] = NULL; } next = stats[hashval]; } else { if (node->next) { prev->next = node->next; } else { prev->next = NULL; } next = prev->next; } /* Add the node to the head of the free stack */ node->next = free_stack; free_stack = node; return next; }
uint wld_register_var(uint section_id, const char* name, enum variant_type type, pfn_wld_varchanged change_fn, void* param) { ASSERT(section_id != 0); ASSERT(strlen(name) < 32); ASSERT(type != VAR_TYPE_NULL); struct wld_section* s = &((struct wld_section*)g_wld.sections.buffer)[section_id - 1]; uint hashval = hash_str(name); struct hashtable_item* item = hashtable_open_find(&s->vtable, hashval); if (item != NULL) { err_printf(__FILE__, __LINE__, "world-mgr: var '%s' already exists in '%s'", name, s->name); return 0; } struct wld_var* v = (struct wld_var*)arr_add(&s->vars); uint id = s->vars.item_cnt; if (v == NULL || IS_FAIL(hashtable_open_add(&s->vtable, hashval, id))) { err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY); return 0; } memset(v, 0x00, sizeof(struct wld_var)); str_safecpy(v->name, sizeof(v->name), name); v->change_fn = change_fn; v->param = param; v->v.type = type; return id; }
/* intern token TOKEN_STR */ struct exo_token_t * exo_intern(char *token_str) /* string to intern */ { int index; struct exo_token_t *ent; index = hash_str(token_str); for (ent=token_hash[index]; ent != NULL; ent=ent->next) { if (!strcmp(token_str, ent->str)) { /* got a match, return token entry */ return ent; } } /* not found, create a new entry */ ent = (struct exo_token_t *)calloc(1, sizeof(struct exo_token_t)); if (!ent) fatal("out of virtual memory"); ent->str = mystrdup(token_str); ent->token = token_id++; ent->next = token_hash[index]; token_hash[index] = ent; return ent; }
static void process_cached_repolist(const char *path) { struct stat st; char *cached_rc; time_t age; cached_rc = xstrdup(fmt("%s/rc-%8x", ctx.cfg.cache_root, hash_str(path))); if (stat(cached_rc, &st)) { /* Nothing is cached, we need to scan without forking. And * if we fail to generate a cached repolist, we need to * invoke scan_tree manually. */ if (generate_cached_repolist(path, cached_rc)) scan_tree(path, repo_config); return; } parse_configfile(cached_rc, config_cb); /* If the cached configfile hasn't expired, lets exit now */ age = time(NULL) - st.st_mtime; if (age <= (ctx.cfg.cache_scanrc_ttl * 60)) return; /* The cached repolist has been parsed, but it was old. So lets * rescan the specified path and generate a new cached repolist * in a child-process to avoid latency for the current request. */ if (fork()) return; exit(generate_cached_repolist(path, cached_rc)); }
Atom XInternAtom(Display * display, _Xconst char *atom_name, Bool only_if_exists) { unsigned char hash = hash_str(atom_name); struct hash_t *val; for (val = hash_list[hash]; val; val = val->next) if (strcmp(val->name, atom_name) == 0) { return val->atom; } if (only_if_exists == True) return None; if (!hash_list[hash]) val = hash_list[hash] = (struct hash_t *) Xcalloc(1, sizeof(struct hash_t)); else { struct hash_t *h = hash_list[hash]; while (h->next) h = h->next; val = h->next = (struct hash_t *) Xcalloc(1, sizeof(struct hash_t)); } val->name = strdup(atom_name); val->atom = atom_id++; return val->atom; }
int lc_arg_register(lc_arg_env_t *env, const char *name, char letter, const lc_arg_handler_t *handler) { lc_arg_t arg; arg.name = name; arg.letter = letter; arg.handler = handler; lc_arg_t **map = NULL; int base = 0; if (isupper((unsigned char)letter)) { map = env->upper; base = 'A'; } else if (islower((unsigned char)letter)) { map = env->lower; base = 'a'; } lc_arg_t *ent = set_insert(lc_arg_t, env->args, &arg, sizeof(arg), hash_str(name)); if (ent && base != 0) map[letter - base] = ent; return ent != NULL; }
JzonValue* jzon_get(JzonValue* object, const char* key) { if (!object->is_object) return NULL; if (object->size == 0) return NULL; uint64_t key_hash = hash_str(key); unsigned first = 0; unsigned last = object->size - 1; unsigned middle = (first + last) / 2; while (first <= last) { if (object->object_values[middle]->key_hash < key_hash) first = middle + 1; else if (object->object_values[middle]->key_hash == key_hash) return object->object_values[middle]->value; else last = middle - 1; middle = (first + last) / 2; } return NULL; }
result_t cmp_anim_modify(struct cmp_obj* obj, struct allocator* alloc, struct allocator* tmp_alloc, void* data, cmphandle_t cur_hdl) { result_t r; struct cmp_anim* a = (struct cmp_anim*)data; uint filehash = hash_str(a->filepath); int reload = (a->filepathhash == filehash); cmp_anim_destroydata(obj, a, cur_hdl, !reload); a->filepathhash = filehash; a->alloc = alloc; if (str_isempty(a->filepath)) return RET_OK; if (!reload) a->clip_hdl = rs_load_animreel(a->filepath, 0); if (a->clip_hdl == INVALID_HANDLE) return RET_FAIL; /* bind */ r = cmp_anim_bind(obj, a, alloc, tmp_alloc, cur_hdl); if (IS_FAIL(r)) { err_sendtolog(TRUE); log_print(LOG_WARNING, "binding anim-set failed"); return RET_FAIL; } return RET_OK; }
void mode_list(note_db_t *db, int longout, enum sort_policy sort, int invert_order, char *tag){ int i; note_t note; sort_notes(db, sort); for(i=0;i<db->len;i++){ int tag_i; if(invert_order) note=*get_note(db, db->len-1-i); else note=*get_note(db, i); if(tag!=NULL && !has_tag(¬e, tag)) continue; if(note.importance>5){ //ANSI red printf("\x1b[31m"); if(note.importance>7) //ANSI bright printf("\x1b[1m"); } if(longout){ char date_string[20]; struct tm *time=localtime(¬e.created); strftime(date_string, 20, "%F %H:%M:%S",time); printf("%2d %d %s %s", note.id, note.importance, date_string, note.text); }else{ printf("%2d %s", note.id, note.text); } //Disable all ANSI printf("\x1b[0m"); for(tag_i=0;tag_i<note.ntags;tag_i++){ int fg, bg; putchar(' '); fg=(hash_str(note.tags[tag_i], 22))%8; bg=(hash_str(note.tags[tag_i], 19))%8; if(fg==bg) bg=(bg==7 ? bg+1 : bg-1); //ANSI: set foreground and background colors printf("\x1b[%dm\x1b[%dm", 30+fg, 40+bg); printf("#%s", note.tags[tag_i]); printf("\x1b[0m"); //Disable all ANSI } putchar('\n'); } }
uint wld_find_section(const char* name) { struct hashtable_item* item = hashtable_open_find(&g_wld.stable, hash_str(name)); if (item != NULL) return (uint)item->value; return 0; }
static struct dic_node *__get_dictionary_node_with_key(Dictionary_t *dic, const char *key) { struct dic_node *np; for (np = dic->buckets[hash_str(key, dic->bucket_count)]; np != NULL; np = np->next) { if (strcmp(key, np->key) == 0) { return np; /* found */ } } return NULL; /* not found */ }
uint gfx_model_findnode(const struct gfx_model* model, const char* name) { uint namehash = hash_str(name); for (uint i = 0, cnt = model->node_cnt; i < cnt; i++) { if (namehash == model->nodes[i].name_hash) return i; } return INVALID_INDEX; }
//4. join and update void fix_fingers(int i){ int return_port; return_port = closest_port_find(fingertable[i].start); while(return_port == 0){ return_port=closest_port_find(fingertable[i].start); } fingertable[i].ftnode->port = return_port; fingertable[i].ftnode->key = hash_str(fingertable[i].ftnode->port); }
HASH key_from_wordlist(const list *l) { char *k = word_list_to_str(l); if (!k) return 0; HASH x = hash_str(k); free(k); return x; }
uint wld_find_var(uint section_id, const char* name) { ASSERT(section_id != 0); struct wld_section* s = &((struct wld_section*)g_wld.sections.buffer)[section_id - 1]; struct hashtable_item* item = hashtable_open_find(&s->vtable, hash_str(name)); if (item != NULL) return (uint)item->value; return 0; }
uint gfx_model_findjoint(const struct gfx_model_skeleton* skeleton, const char* name) { uint namehash = hash_str(name); for (uint i = 0, cnt = skeleton->joint_cnt; i < cnt; i++) { if (namehash == skeleton->joints[i].name_hash) return i; } return INVALID_INDEX; }
static struct bus_type_nb *__get_cell(const void *bus) { struct bus_type_nb *cell; unsigned int h = hash_str(bus); list_for_each_entry(cell, _bus_buckets + h, list) if (cell->bus == bus) return cell; return NULL; }
int parse_object(const char** input, JzonValue* output, bool root_object, JzonAllocator* allocator) { if (current(input) == '{') next(input); else if (!root_object) return -1; output->is_object = true; // Empty object. if (current(input) == '}') { output->size = 0; return 0; } Array object_values = { 0 }; while (current(input)) { JzonKeyValuePair* pair = (JzonKeyValuePair*)allocator->allocate(sizeof(JzonKeyValuePair)); skip_whitespace(input); char* key = parse_keyname(input, allocator); skip_whitespace(input); if (key == NULL || current(input) != ':') return -1; next(input); JzonValue* value = (JzonValue*)allocator->allocate(sizeof(JzonValue)); memset(value, 0, sizeof(JzonValue)); int error = parse_value(input, value, allocator); if (error != 0) return error; pair->key = key; pair->key_hash = hash_str(key); pair->value = value; arr_insert(&object_values, pair, find_object_pair_insertion_index((JzonKeyValuePair**)object_values.arr, object_values.size, pair->key_hash), allocator); skip_whitespace(input); if (current(input) == '}') { next(input); break; } } output->size = object_values.size; output->object_values = (JzonKeyValuePair**)object_values.arr; return 0; }
/* * Lookup a symbol by name in table. * No scope levels yet. */ Symbol * lookup_symbol(SymbolTable * st, const char * name) { Symbol * sym; unsigned int i = hash_str(name) % SYMTAB_HASHSIZE; for (sym = st->table[i]; sym; sym = sym->nextintable) { if (!strcmp(name, sym->name)) return sym; } return NULL; }
int main(int ac, char **av) { char buf[1024]; while( fgets(buf, sizeof buf, stdin) ) { buf[strcspn(buf,"\r\n")]=0; unsigned h = hash_str(buf); printf("%08x %s\n", h, buf); } return 0; }