int main() { struct hash *my_hash; int pot; int i; char *dummy; pot = 16; my_hash = hash_new( pot ); for ( i = 1; i <= ( 1 << pot ); i++ ) { sprintf( dummy, "dummy_%d", i ); hash_add( my_hash, dummy, "FOO" ); } hash_collision_stats( my_hash ); // if ( ( val = ( char * ) hash_get( my_hash, key ) ) != NULL ) { // printf( "Key: %s, Val: %s\n", key, val ); // } else { // printf( "Key: %s, Val: Not Found\n", key ); // } hash_destroy( my_hash ); return 0; }
int cip_net_indexer(const void *vp) { const dns_message *m = vp; ipnetobj *obj; inX_addr masked_addr; if (m->malformed) return -1; if (NULL == theHash) { theHash = hash_create(MAX_ARRAY_SZ, ipnet_hashfunc, ipnet_cmpfunc, 1, NULL, afree); if (NULL == theHash) return -1; } #if USE_IPV6 if (6 == inXaddr_version(&m->client_ip_addr)) masked_addr = inXaddr_mask(&m->client_ip_addr, &v6mask); else #endif masked_addr = inXaddr_mask(&m->client_ip_addr, &v4mask); if ((obj = hash_find(&masked_addr, theHash))) return obj->index; obj = acalloc(1, sizeof(*obj)); if (NULL == obj) return -1; obj->addr = masked_addr; obj->index = next_idx; if (0 != hash_add(&obj->addr, obj, theHash)) { afree(obj); return -1; } next_idx++; return obj->index; }
static int hash_grow(hash *h) { int i, rc; struct record *old_recs; unsigned int old_recs_length; old_recs_length = sizes[h->size_index]; old_recs = h->records; if (h->size_index == sizes_count - 1) return -1; if ((h->records = calloc(sizes[++h->size_index],sizeof(struct record))) == NULL) { h->records = old_recs; return -1; } h->records_count = 0; // rehash table for (i=0; i < old_recs_length; i++) { if (old_recs[i].key) { rc = _timeout(old_recs[i].lifetime); if(rc) { hash_add(h, old_recs[i].key, old_recs[i].value, old_recs[i].lifetime); } } } free(old_recs); return 0; }
void enlarge_table(Hash_Table *t) { int i = 0; int new_size = find_greater_prime(t->size); Label *old_table = t->table; int old_size = t->size; t->table = (Label *) malloc(sizeof(Label) * new_size); for (i = 0; i < new_size; i++) { t->table[i].name = NULL; } t->size = new_size; t->elements = 0; for (i = 0; i < old_size; i++) { if (old_table[i].name != NULL) { hash_add(t, old_table[i]); } } free(old_table); }
/** Delete an id based stanza handler. * * @param conn a Strophe connection object * @param handler a function pointer to a stanza handler * @param id a string containing the id the handler is for * * @ingroup Handlers */ void xmpp_id_handler_delete(xmpp_conn_t * const conn, xmpp_handler handler, const char * const id) { xmpp_handlist_t *item, *prev; prev = NULL; item = (xmpp_handlist_t *)hash_get(conn->id_handlers, id); if (!item) return; while (item) { if (item->handler == (void *)handler) break; prev = item; item = item->next; } if (item) { if (prev) prev->next = item->next; else { hash_drop(conn->id_handlers, id); hash_add(conn->id_handlers, id, item->next); } xmpp_free(conn->ctx, item->id); xmpp_free(conn->ctx, item); } }
void hash_file(hash *hashtable[], char *file, unsigned int tablesize) { FILE *fp; bstring buffer; bstring value; int poseq; fp = fopen(file, "r"); if (fp == NULL) { fp = fopen(file, "w+"); /* create the file */ if (fp == NULL) fprintf(stderr, "Could not create file (%s)\n", file); return; } while ((buffer = bgets((bNgetc) fgetc, fp, '\n')) != NULL) { /* seriously, wtf is a \10 doing in my buffer you c********r? let's get rid of it: */ poseq = bstrchrp(buffer, '=', 0); value = bmidstr(buffer, poseq+1, blength(buffer) - poseq - 2); btrunc(buffer, poseq); /*printf("%s=%s\n", bdata(buffer), bdata(value));*/ hash_add(hashtable, buffer, value, tablesize); } fclose(fp); bdestroy(buffer); bdestroy(value); }
static bool check_pack_needed_for_recons(tc_sess_t *s, tc_iph_t *ip, tc_tcph_t *tcp) { uint16_t size_tcp; p_link_node ln; unsigned char *payload, command, *pkt; mysql_table_item_t *item; if (s->cur_pack.cont_len > 0) { size_tcp = tcp->doff << 2; payload = (unsigned char *) ((char *) tcp + size_tcp); /* skip packet length */ payload = payload + 3; /* skip packet number */ payload = payload + 1; command = payload[0]; if (command != COM_STMT_PREPARE) { return false; } item = hash_find(ctx.table, s->hash_key); if (!item) { item = tc_pcalloc(ctx.pool, sizeof(mysql_table_item_t)); if (item != NULL) { item->list = link_list_create(ctx.pool); if (item->list != NULL) { hash_add(ctx.table, ctx.pool, s->hash_key, item); if (ctx.table->total > MAX_TABLE_ITEM_NUM) { tc_log_info(LOG_INFO, 0, "too many items in ctx.table"); } } else { tc_log_info(LOG_ERR, 0, "list create err"); return false; } } else { tc_log_info(LOG_ERR, 0, "mysql item create err"); return false; } } if (item->list->size > MAX_SP_SIZE) { tc_log_info(LOG_INFO, 0, "too many prepared stmts for a session"); return false; } tc_log_debug1(LOG_INFO, 0, "push packet:%u", ntohs(s->src_port)); pkt = (unsigned char *) cp_fr_ip_pack(ctx.pool, ip); ln = link_node_malloc(ctx.pool, pkt); ln->key = ntohl(tcp->seq); link_list_append_by_order(item->list, ln); item->tot_cont_len += s->cur_pack.cont_len; return true; } return false; }
static void * hash_insert(unsigned char *keyp, void *datap, Hash_table *tablep) { if (!hash_find(tablep,keyp)) hash_add(tablep,pool_copy(keyp),datap); return datap; }
/* split key, value pairs into a hash */ static hash_t *_parse_digest_challenge(xmpp_ctx_t *ctx, const char *msg) { hash_t *result; unsigned char *text; char *key, *value; unsigned char *s, *t; text = (unsigned char *)xmpp_base64_decode_str(ctx, msg, strlen(msg)); if (text == NULL) { xmpp_error(ctx, "SASL", "couldn't Base64 decode challenge!"); return NULL; } result = hash_new(ctx, 10, xmpp_free); if (result != NULL) { s = text; while (*s != '\0') { /* skip any leading commas and spaces */ while ((*s == ',') || (*s == ' ')) s++; /* accumulate a key ending at '=' */ t = s; while ((*t != '=') && (*t != '\0')) t++; if (*t == '\0') break; /* bad string */ key = _make_string(ctx, (char *)s, (t-s)); if (key == NULL) break; /* advance our start pointer past the key */ s = t + 1; t = s; /* if we see quotes, grab the string in between */ if ((*s == '\'') || (*s == '"')) { t++; while ((*t != *s) && (*t != '\0')) t++; value = _make_string(ctx, (char *)s+1, (t-s-1)); if (*t == *s) { s = t + 1; } else { s = t; } /* otherwise, accumulate a value ending in ',' or '\0' */ } else { while ((*t != ',') && (*t != '\0')) t++; value = _make_string(ctx, (char *)s, (t-s)); s = t; } if (value == NULL) { xmpp_free(ctx, key); break; } /* TODO: check for collisions per spec */ hash_add(result, key, value); /* hash table now owns the value, free the key */ xmpp_free(ctx, key); } } xmpp_free(ctx, text); return result; }
void retrieve_mysql_user_pwd_info(char *pairs) { char *p, *end, *q, *next, *pair_end; size_t len; uint64_t key; mysql_user *p_user_info, *p_tmp_user_info; user_pwd_table = hash_create(256); strcpy(user_pwd_table->name, "user password table"); p = pairs; len = strlen(p); end = p + len; if (len <= 1) { log_info(LOG_WARN, "use password error:%s", pairs); exit(1); } do{ next = strchr(p, ':'); q = strchr(p, '@'); if ( next != NULL) { if (next != p) { pair_end = next - 1; } else { log_info(LOG_WARN, "use password info error:%s", pairs); exit(1); } } else { pair_end = p + strlen(p) - 1; } if ((q-p) >= 256 || (pair_end - q) >= 256) { log_info(LOG_WARN, "too long for user or password"); exit(1); } p_user_info = (mysql_user*)calloc(1, sizeof(mysql_user)); strncpy(p_user_info->user, p, q-p); strncpy(p_user_info->password, q + 1, pair_end - q); key = get_key_from_user(p_user_info->user); p_tmp_user_info = hash_find(user_pwd_table, key); if (NULL == p_tmp_user_info) { hash_add(user_pwd_table, key, (void *)p_user_info); } else { p_tmp_user_info->next = p_user_info; } if (next != NULL) { p = next + 1; } else { break; } }while (p < end) ; }
static void lwan_module_register(lwan_t *l, const lwan_module_t *module) { if (!module->name) lwan_status_critical("Module at %p has no name", module); lwan_status_debug("Registering module \"%s\"", module->name); hash_add(l->module_registry, module->name, module); }
/* Add item to the router table */ void router_add(uint32_t ip, uint16_t port, int fd) { uint64_t key = get_key(ip, port); hash_add(table, key, (void *)(long)fd); delay_table_send(key, fd); }
static void pretrim_setup(void) { unsigned char *p; static int true = 1; pretrim = hash_create(1000); if (pretrim_args) for (p = list_first(pretrim_args); p; p = list_next(pretrim_args)) hash_add(pretrim, p, &true); else { int i; pretrim_lines = loadfile_lines3((unsigned char *)pretrim_file, NULL, &pretrim_content); for (i = 0; pretrim_lines[i]; ++i) hash_add(pretrim, pretrim_lines[i], &true); } }
void quad_hash_table::insert(int x){ if (!isFull()){ if (!contains(x)) { main_list[hash_add(x, 0)].setItem(x); counter++; } } }
shm_h() { v = (long) (SHM_RDONLY); hash_add("SHM_RDONLY", (char *) &v, sizeof(long)); v = (long) (SHM_RND); hash_add("SHM_RND", (char *) &v, sizeof(long)); v = (long) (SHM_LOCK); hash_add("SHM_LOCK", (char *) &v, sizeof(long)); v = (long) (SHM_UNLOCK); hash_add("SHM_UNLOCK", (char *) &v, sizeof(long)); v = (long) (SHMLBA); hash_add("SHMLBA", (char *) &v, sizeof(long)); }
/* * Update instance with new peer address */ void update_floated(struct multi_context *m, struct multi_instance *mi, struct mroute_addr real, uint32_t hv) { struct mroute_addr real_old; real_old = mi->real; generate_prefix (mi); /* remove before modifying mi->real, since it also modifies key in hash */ hash_remove(m->hash, &real_old); hash_remove(m->iter, &real_old); /* update address */ memcpy(&mi->real, &real, sizeof(real)); mi->context.c2.from = m->top.c2.from; mi->context.c2.to_link_addr = &mi->context.c2.from; /* switch to new log prefix */ generate_prefix (mi); /* inherit buffers */ mi->context.c2.buffers = m->top.c2.buffers; /* inherit parent link_socket and link_socket_info */ mi->context.c2.link_socket = m->top.c2.link_socket; mi->context.c2.link_socket_info->lsa->actual = m->top.c2.from; /* fix remote_addr in tls structure */ tls_update_remote_addr (mi->context.c2.tls_multi, &mi->context.c2.from); mi->did_open_context = true; hash_add(m->hash, &mi->real, mi, false); hash_add(m->iter, &mi->real, mi, false); mi->did_real_hash = true; #ifdef MANAGEMENT_DEF_AUTH hash_remove (m->cid_hash, &mi->context.c2.mda_context.cid); hash_add (m->cid_hash, &mi->context.c2.mda_context.cid, mi, false); #endif #ifdef MANAGEMENT_DEF_AUTH mi->did_cid_hash = true; #endif }
int main(void) { HashTable ht; hash_init(&ht, 2); int a = 118; int b = 119; hash_add(&ht, "订单2355", &a); hash_add(&ht, "订单2399", &a); hash_add(&ht, "订单2388", &a); hash_add(&ht, "订单2333", &a); printf("哈希表大小为%d\n",ht.nTableSize); printf("哈希表已使用元素个数为%d\n",ht.nNumUsed); Bucket *find_bucket,*b2; find_bucket = hash_get(&ht, "订单2333"); hash_del(&ht, find_bucket); hash_del_by_key(&ht, "订单2388"); hash_add(&ht, "订单9999", &a); hash_add(&ht, "订单8888", &a); hash_add(&ht, "订单7777", &a); hash_foreach(&ht); return 0; }
void initsoundhashnames(void) { int32_t i; hash_init(&h_names); for (i=0; g_dynSoundList[i].staticval >= 0; i++) hash_add(&h_names, g_dynSoundList[i].str, i, 0); }
/** Copy a stanza and its children. * This function copies a stanza along with all its children and returns * the new stanza and children with a reference count of 1. The returned * stanza will have no parent and no siblings. This function is useful * for extracting a child stanza for inclusion in another tree. * * @param stanza a Strophe stanza object * * @return a new Strophe stanza object * * @ingroup Stanza */ xmpp_stanza_t *xmpp_stanza_copy(const xmpp_stanza_t * const stanza) { xmpp_stanza_t *copy, *child, *copychild, *tail; hash_iterator_t *iter; const char *key; void *val; copy = xmpp_stanza_new(stanza->ctx); if (!copy) goto copy_error; copy->type = stanza->type; if (stanza->data) { copy->data = xmpp_strdup(stanza->ctx, stanza->data); if (!copy->data) goto copy_error; } if (stanza->attributes) { copy->attributes = hash_new(stanza->ctx, 8, xmpp_free); if (!copy->attributes) goto copy_error; iter = hash_iter_new(stanza->attributes); if (!iter) { printf("DEBUG HERE\n"); goto copy_error; } while ((key = hash_iter_next(iter))) { val = xmpp_strdup(stanza->ctx, (char *)hash_get(stanza->attributes, key)); if (!val) goto copy_error; if (hash_add(copy->attributes, key, val)) goto copy_error; } hash_iter_release(iter); } tail = copy->children; for (child = stanza->children; child; child = child->next) { copychild = xmpp_stanza_copy(child); if (!copychild) goto copy_error; copychild->parent = copy; if (tail) { copychild->prev = tail; tail->next = copychild; } else copy->children = copychild; tail = copychild; } return copy; copy_error: /* release all the hitherto allocated memory */ if (copy) xmpp_stanza_release(copy); return NULL; }
void nTM_see_word(nTM *model, WordIndex word) { hash_element *element = hash_get(model->unique_words, word); if (element == NULL) { // This is a new word! hash_add(model->unique_words, word, 1); model->words++; } }
void alist_add(alist_t *alist, void *name, void *value) { alist_el_t *el; el = xmalloc(sizeof (alist_el_t)); el->ale_name = name; el->ale_value = value; hash_add(alist->al_elements, el); }
static void add_phone (char *phn, int32 id, int32 base_id, int32 type, int32 len) { char *diphn = (char *) salloc (phn); hash_add (&phones, diphn, (caddr_t )id); list_add (&phones_list, (caddr_t )diphn, id); list_add (&phone_base_map, (caddr_t )base_id, id); list_add (&phone_model_len, (caddr_t )len, id); list_add (&phone_type_map, (caddr_t )type, id); }
void set(Hash *h, List *l, char *key, int value) { Node *n = NULL; if(NULL != (n = hash_get(h, key))) { list_remove_node(l, n); } list_push(l, key, value, NULL); hash_add(h, key, value, l->root); }
void frame_add(Hash *frame, char *key, boxed_value *value){ if (hash_contains(frame, key)){ boxed_value *current_value = hash_value(frame, key); if (value == current_value) return; else dec_ref_count(current_value); } inc_ref_count(value); hash_add(frame, strdup(key), value); }
int main() { char *data = "add"; char *data0 = "add"; char *data1 = "add12"; char *data2 = "add123"; hashmap *mymap; mymap = hash_init(); printf("data0 = %s = %lu\ndata = %s = %lu\ndata1 = %s = %lu\ndata2 = %s = %lu\n",data0, hash(data0), data, hash(data), data1, hash(data1), data2, hash(data2)); printf("Adding data to the map, 0 if successful, -1 if it already exists: %d\n", hash_add(data, mymap)); printf("Adding data0 to the map, 0 if successful, -1 if it already exists: %d\n", hash_add(data0, mymap)); printf("Adding data1 to the map, 0 if successful, -1 if it already exists: %d\n", hash_add(data1, mymap)); printf("Adding data2 to the map, 0 if successful, -1 if it already exists: %d\n", hash_add(data2, mymap)); printf("Deleting data from the map, 0 if successful, -1 if it doesn't exist: %d\n", hash_delete(data, mymap)); printf("Deleting data from the map, 0 if successful, -1 if it doesn't exist: %d\n", hash_delete(data, mymap)); printf("Searching for data1 in the map, 0 if found, -1 if not found: %d\n", hash_search(data1, mymap)); printf("Deleting data1 from the map, 0 if successful, -1 if it doesn't exist: %d\n", hash_delete(data1, mymap)); printf("Searching for data1 in the map, 0 if found, -1 if not found: %d\n", hash_search(data1, mymap)); printf("Removing the map, 0 if successful: %d\n", hash_removemap(mymap)); }
/*! Register log notification stream * @param[in] h Clicon handle * @param[in] stream Event stream. CLICON is predefined, others are application-defined * @param[in] filter Filter. For xml notification ie xpath: .[name="kalle"] * @param[in] status 0 for stop, 1 to start * @param[in] fn Callback function called when notification occurs * @param[in] arg Argument to function note * Note this calls cligen_regfd which may callback on cli command interpretator */ int cli_notification_register(clicon_handle h, char *stream, enum format_enum format, char *filter, int status, int (*fn)(int, void*), void *arg) { int retval = -1; char *logname = NULL; void *p; int s; clicon_hash_t *cdat = clicon_data(h); size_t len; int s_exist = -1; len = strlen("log_socket_") + strlen(stream) + 1; if ((logname = malloc(len)) == NULL){ clicon_err(OE_UNIX, errno, "malloc"); goto done; } snprintf(logname, len, "log_socket_%s", stream); if ((p = hash_value(cdat, logname, &len)) != NULL) s_exist = *(int*)p; if (status){ /* start */ if (s_exist!=-1){ clicon_err(OE_PLUGIN, 0, "Result log socket already exists"); goto done; } if (clicon_rpc_create_subscription(h, stream, filter, &s) < 0) goto done; if (cligen_regfd(s, fn, arg) < 0) goto done; if (hash_add(cdat, logname, &s, sizeof(s)) == NULL) goto done; } else{ /* stop */ if (s_exist != -1){ cligen_unregfd(s_exist); } hash_del(cdat, logname); #if 0 /* cant turn off */ if (clicon_rpc_create_subscription(h, status, stream, format, filter, NULL) < 0) goto done; #endif } retval = 0; done: if (logname) free(logname); return retval; }
static int __bcache_insert(struct bcentry *bce) { int ret; ret = hash_add(&bc_hash, bce, &bce->our_addr, &bce->peer_addr); if (ret) return ret; bcache_count++; return 0; }
//--------------------------------------------------------------------------------------------------------------------- static int __pmipcache_insert(pmip_entry_t * bce) { int ret; ret = hash_add(&g_pmip_hash, bce, &bce->our_addr, &bce->mn_hw_address); if (ret) { return ret; } g_pmip_cache_count++; //dbg("PMIP cache entry is inserted for: %x:%x:%x:%x:%x:%x:%x:%x <-> %x:%x:%x:%x:%x:%x:%x:%x\n", NIP6ADDR(&bce->our_addr), NIP6ADDR(&bce->mn_hw_address)); return 0; }
/** ** Init button. */ global void InitUnitButtons(void) { int i; // for( i=0; i<sizeof(UnitButtons)/sizeof(*UnitButtons); ++i ) { for( i=0; UnitButtons[i].Ident; ++i ) { // DebugLevel0("%s\n",UnitButtons[i].Ident); *(UnitButton**)hash_add(ButtonHash,UnitButtons[i].Ident)= &UnitButtons[i]; } }
// Update an element in the hash by adding <change>; if the element doesn't exist, it takes on the value <change> hash_element *hash_update(ct_hash *map, int key, int change) { hash_element *elem = hash_get(map,key); if(elem == NULL){ elem = hash_add(map,key,change); } else { elem->value += change; map->sum += change; } return elem; }