static bool single_test(void) { int i; ht_init(&hash1); ht_init(&hash2); for (i = 0; i < NUM_ELEMENTS; i++) { int k, klen; klen = (i % 8) + 1; for (k = 0; k < klen; k++) data[i][k] = keydomain[i % (sizeof(keydomain) - 1)]; data[i][k] = 0; ASSERT(ht_insert(&hash1, data[i])); ASSERT(ht_insert_str(&hash2, data[i], data[i])); } for (i = 0; i < NUM_ELEMENTS; i++) { const char *found1, *found2; found1 = ht_find_str(&hash1, data[i]); if (strcmp(found1, data[i])) return false; kprintf("hash1: found data[%d] = %s\n", i, found1); found2 = ht_find_str(&hash2, data[i]); if (strcmp(found2, data[i])) return false; kprintf("hash2: found data[%d] = %s\n", i, found2); } return true; }
int ud_init (struct udoc *ud) { bin_zero (ud, sizeof (*ud)); if (!ud_oht_init (&ud->ud_parts, sizeof (struct ud_part))) goto FAIL; if (!ud_oht_init (&ud->ud_link_exts, sizeof (struct ud_ref))) goto FAIL; if (!ud_oht_init (&ud->ud_refs, sizeof (struct ud_ref))) goto FAIL; if (!ud_oht_init (&ud->ud_ref_names, sizeof (struct ud_ref))) goto FAIL; if (!ud_oht_init (&ud->ud_footnotes, sizeof (struct ud_ref))) goto FAIL; if (!ud_oht_init (&ud->ud_styles, sizeof (struct ud_ref))) goto FAIL; if (!dstack_init (&ud->ud_errors, 16, sizeof (struct ud_err))) goto FAIL; if (!token_init (&ud->ud_tok)) goto FAIL; ud->ud_dirfd_pwd = open_ro ("."); if (ud->ud_dirfd_pwd == -1) goto FAIL; ud->ud_dirfd_src = -1; ud->ud_dirfd_out = -1; ud->ud_main_doc = ud; ud->ud_cur_doc = ud; if (!ht_init (&ud->ud_loopchecks)) goto FAIL; if (!ht_init (&ud->ud_documents)) goto FAIL; taia_now (&ud->ud_time_start); return 1; FAIL: ud_free (ud); return 0; }
void rx_init(void) { if(!initialized) { initialized=1; pattern=(int *)m_alloc(len_p=P_AVG_SIZE*LEN_P,sizeof(int)); r2p=(int (*)[2])m_alloc(len_2=LEN_2,sizeof(int[2])); regex=(char*)m_alloc(len_r=R_AVG_SIZE*LEN_R,sizeof(char)); memo=(int (*)[M_SIZE])m_alloc(len_m=LEN_M,sizeof(int[M_SIZE])); ht_init(&ht_p,LEN_P,&hash_p,&equal_p); ht_init(&ht_2,LEN_2,&hash_2,&equal_2); ht_init(&ht_r,LEN_R,&hash_r,&equal_r); ht_init(&ht_m,LEN_M,&hash_m,&equal_m); windup(); } }
worker_t worker_create() { worker_t pworker = (worker_t)malloc(sizeof(worker)); if (pworker == NULL) { print_log(LOG_TYPE_ERROR, "malloc worker error\n"); return NULL; } pworker->total_count = 0; pworker->closed_count = 0; pworker->neterr_count = 0; pworker->epfd = epoll_create(256); hash_table *ht = (hash_table *)malloc(sizeof(hash_table)); pworker->pht = ht; ht_init(pworker->pht, HT_VALUE_CONST, 0.05); pworker->redis = connector_create(INVALID_ID, pworker, CONN_TYPE_REDIS, REDIS_IP, REDIS_PORT); pworker->plist = list_create(); struct timeval tm_now; gettimeofday(&tm_now, NULL); pworker->ticktime = tm_now.tv_sec; return pworker; }
/* load the filter from the filter file */ static int load_filter(struct fkeys_conf *conf, dbfr_t *filter_reader) { char *t_keybuf; int i, acum_len; ht_init(&conf->filter, 1024, NULL, NULL); while (dbfr_getline(filter_reader) > 0) { t_keybuf = (char *) xmalloc(filter_reader->current_line_sz); for (acum_len = 0, i = 0; i < conf->key_count; i++) { acum_len += get_line_field(t_keybuf + acum_len, filter_reader->current_line, filter_reader->current_line_sz - acum_len, conf->aindexes[i], delim); if (i != conf->key_count -1) { strcat(t_keybuf + acum_len, delim); acum_len += delim_len; } } if (acum_len > 0) ht_put(&conf->filter, t_keybuf, (void*)0xDEADBEEF); //bst_insert(&conf->ftree, t_keybuf); } conf->key_buffer_sz = filter_reader->current_line_sz; return 0; }
SExp make_hash_table(void) { SHash* p = smalloc(sizeof(*p), FALSE); p->typeinfo = &TSHash; ht_init(&p->ht, &SHashVTbl); ht_resize(&p->ht, 129); return ptr2s(p); }
int main(void) { unsigned long al1; unsigned long al2; test_assert(ht_init(&ht)); add(&ht, "AAAAAAAAAAAA", "AAAAAAAAAAAA", 1); add(&ht, "BBBBBBBBBBBB", "BBBBBBBBBBBB", 1); add(&ht, "CCCCCCCCCCCC", "CCCCCCCCCCCC", 1); add(&ht, "DDDDDDDDDDDD", "DDDDDDDDDDDD", 1); add(&ht, "EEEEEEEEEEEE", "EEEEEEEEEEEE", 1); add(&ht, "FFFFFFFFFFFF", "FFFFFFFFFFFF", 1); add(&ht, "HHHHHHHHHHHH", "HHHHHHHHHHHH", 1); add(&ht, "IIIIIIIIIIII", "IIIIIIIIIIII", 1); add(&ht, "JJJJJJJJJJJJ", "JJJJJJJJJJJJ", 1); add(&ht, "KKKKKKKKKKKK", "KKKKKKKKKKKK", 1); add(&ht, "LLLLLLLLLLLL", "LLLLLLLLLLLL", 1); add(&ht, "MMMMMMMMMMMM", "MMMMMMMMMMMM", 1); al1 = count(); test_assert(al1 == 12); ht_clear_ext(&ht, X, 0); al2 = count(); test_assert(al1 == al2); ht_free(&ht); return 0; }
struct r_set* r_set_new( struct r_set_cfg const* cfg ) { set_dbg("Allocate set with config %p", (void*) cfg); /* * magic constant: We initialize the hashtable with 8 buckets, 2^3 == 8, so * we must set `ht_init_power` to 3 */ const size_t ht_init_power = 3; struct r_set* set = calloc(1, sizeof(*set)); if (likely(set)) { if (!ht_init(&set->ht, ht_init_power)) { set_dbg("Allocation failed: %p", (void*)set); free(set); set = NULL; } else { set->cfg = cfg; } } return set; }
guint mbb_session_new(struct mbb_session *ss, gchar *peer, guint port, mbb_session_type type) { guint sid; ss->user = NULL; ss->peer = g_strdup(peer); ss->port = port; ss->type = type; time(&ss->start); ss->mtime = ss->start; ss->killed = FALSE; ss->kill_msg = NULL; session_vars_init(ss); mbb_plock_writer_lock(); sid = session_id++; if (ht == NULL) ht_init(); g_hash_table_insert(ht, GINT_TO_POINTER(sid), ss); mbb_plock_writer_unlock(); return sid; }
void ht_grow(htab_desc *table) { ht_entry *p, *q, *s; char dummy; unsigned int prev_size; p = s = table->base; q = p + table->size; prev_size = table->size; /* Always grow according to the prime table. if maximum table size is reached and it is not full, ht_init() has not effect. if maximum table size is reached and it is full, ht_init() gives rts_error(). */ ht_init(table, prev_size + 1); if (table->size > prev_size) { for ( ; p < q; p++) { if ((p->nb.val.i1 != 0) || (p->nb.val.i2 !=0)) ht_put(table, &(p->nb), &dummy)->ptr = p->ptr; } free((char *)s); } return; }
/** * \brief Give the hash table a different block of memory to use. * * Tells the hash table to use the array \p buckets for storing all of its * elements. This will usually be used to increase the memory pool when the * number of collisions gets too high. * * \param [out] ht Pointer to the hash table that should use \p buckets. * \param [in] buckets Array of lists to use for storing buckets in the hash * table. * \param [in] num The length of the \p buckets array. * * \pre <tt>ht != NULL</tt> * \pre <tt>buckets != NULL</tt> * \pre <tt>num > 0</tt> * * \return Returns a pointer to the old array used to store the buckets. This * array may be freed after the call if it is no longer needed. */ struct list *ht_rehash(struct hash_table *ht, struct list *buckets, size_t num) { struct list *old_buckets; /* Old memory pool for the buckets. */ size_t old_num; /* Old number of buckets in the hash table. */ size_t i; /* Iterator over the old buckets. */ assert(ht != NULL); assert(buckets != NULL); assert(num > 0); /* Store the new buckets array in the hash table. */ old_buckets = ht->buckets; old_num = ht->len; ht_init(ht, buckets, num, ht->hash, ht->cmp); /* Iterate over all of the old buckets. */ for (i = 0; i < old_num; i++) { struct list_elem *le; struct list *b = old_buckets + i; /* Iterate over all the elements in each of the old buckets. */ for (le = list_begin(b); le != list_end(b); le = list_next(le)) { /* For each hash element, remove it from the old bucket and add it * to a new bucket. */ struct hash_elem *val = containerof(le, struct hash_elem, le); (void)ht_remove(val); ht_insert(ht, val); } } /* This array is no longer used by the hash table. */ return old_buckets; }
/* Destroy an entire hash table */ int ht_destroy(struct hashtable *t) { unsigned int i; struct hashtable copy = *t; /* Free all the elements */ for (i = 0; i < t->size && t->used > 0; i++) { if (t->table[i] != NULL && t->table[i] != ht_free_element) { if (t->key_destructor) t->key_destructor(t->table[i]->key); if (t->val_destructor) t->val_destructor(t->table[i]->data); #ifndef AHT_USE_SLAB free(t->table[i]); #endif t->used--; } } #ifdef AHT_USE_SLAB slab_destroy(t->cache); #endif /* Free the table and the allocated cache structure */ free(t->table); #ifdef AHT_USE_SLAB free(t->cache); #endif /* Re-initialize the table */ ht_init(t); /* Restore methods */ t->hashf = copy.hashf; t->key_destructor = copy.key_destructor; t->val_destructor = copy.val_destructor; t->key_compare = copy.key_compare; return HT_OK; /* It can't fail ht_destroy never fails */ }
bool registerClient(char *loc) { char *registerURL=NULL; asprintf(®isterURL,"%s/register?uuid=%s&mac=%s&fw_version=%s&arch=%s&platform=%s&ip=%s", ph->location, // TODO : More generic! ph->uuid, ht_get_simple(ph->configFlagsMap,"W_MAC"), ht_get_simple(ph->configFlagsMap,"VERSION"), ht_get_simple(ph->configFlagsMap,"WALLY_ARCH"), ht_get_simple(ph->configFlagsMap,"WALLY_PLATFORM"), ht_get_simple(ph->configFlagsMap,"W_IPADDR")); slog(DEBUG,DEBUG,"RegURL : %s",registerURL); registerMap = malloc(sizeof(hash_table)); ht_init(registerMap, HT_KEY_CONST | HT_VALUE_CONST, 0.05); ph->registered = parseJSON(registerMap,url_call(registerURL),NULL); if(registerURL){ free(registerURL); } if(ph->registered){ asprintf(&commandURL,"%s/command?uuid=%s", ph->location, ph->uuid); commandURL=replace(commandURL,"/register?","/command?"); } else { commandURL=NULL; } slog(DEBUG,DEBUG,"CmdURL : %s",commandURL); return ph->registered; }
// XXX TODO: loadFactor should be a float, 0.75 instead of 75 struct hashtable* create_hashtable2(int capacity, int load_factor) { struct hashtable *_ht = malloc(sizeof(struct hashtable)); assert(_ht != NULL); ht_init(_ht, capacity, load_factor); return _ht; }
int main() { ht_init(&hasht); ht_add(hasht, 0); ht_add(hasht, -10); ht_add(hasht, 10); assert(ht_contain(hasht, 10)); assert(ht_contain(hasht, 0)); assert(ht_contain(hasht, -10)); ht_remove(hasht, 0); assert(!ht_contain(hasht, 0)); ht_remove(hasht, 10); assert(!ht_contain(hasht, 10)); ht_remove(hasht, -10); assert(!ht_contain(hasht, -10)); assert(ht_contain(hasht, 0)); return 0; }
/* * Accessor method for context of the pre. */ hashtable_t * get_context() { if (!_envctx) { _envctx = malloc(sizeof(hashtable_t)); ht_init(_envctx); } return _envctx; }
int main() { ht_init(); test1(); ht_kill(); return 0; }
smb_ht *ht_create(HASH_FUNCTION hash_func, DATA_COMPARE equal) { // Allocate and create the table. smb_ht *table; table = smb_new(smb_ht, 1); ht_init(table, hash_func, equal); return table; }
void parser_init(void) { // Initialize the hashtable used to store the command description ht_init(&commands); #if CONFIG_INTERNAL_COMMANDS parser_register_cmd(&CMD_HUNK_TEMPLATE(help)); #endif }
/* Expand or create the hashtable */ int ht_expand(struct hashtable *t, size_t size) { struct hashtable n; /* the new hashtable */ unsigned int realsize = next_power(size), i; /* the size is invalid if it is smaller than the number of * elements already inside the hashtable */ if (t->used >= size) return HT_INVALID; ht_init(&n); n.size = realsize; n.sizemask = realsize-1; n.table = malloc(realsize*sizeof(struct ht_ele*)); if (n.table == NULL) return HT_NOMEM; /* Copy methods */ n.hashf = t->hashf; n.key_destructor = t->key_destructor; n.val_destructor = t->val_destructor; n.key_compare= t->key_compare; /* Initialize all the pointers to NULL */ memset(n.table, 0, realsize*sizeof(struct ht_ele*)); /* Copy all the elements from the old to the new table: * note that if the old hash table is empty t->size is zero, * so ht_expand() acts like an ht_create() */ n.used = t->used; for (i = 0; i < t->size && t->used > 0; i++) { if (t->table[i] != NULL && t->table[i] != ht_free_element) { u_int32_t h; /* Get the new element index: note that we * know that there aren't freed elements in 'n' */ h = n.hashf(t->table[i]->key) & n.sizemask; if (n.table[h]) { n.collisions++; while(1) { h = (h+1) & n.sizemask; if (!n.table[h]) break; n.collisions++; } } /* Move the element */ n.table[h] = t->table[i]; t->used--; } } assert(t->used == 0); free(t->table); /* Remap the new hashtable in the old */ *t = n; return HT_OK; }
config_t config_init() { // Create the hashtable hashtable *ht = ht_init(TABLE_SIZE, NULL); // Load the file if it exists ht_deserialize(ht, FILE_NAME, NULL); return (config_t)ht; }
void ht_clear(hash_table *table) { ht_destroy(table); ht_init(table, table->flags, table->max_load_factor #ifndef __WITH_MURMUR , table->hashfunc_x86_32, table->hashfunc_x86_128, table->hashfunc_x64_128 #endif //__WITH_MURMUR ); }
int main (void) { int key, val, retrive; ht_init(10); /* * Insert a new key */ key = 8; val = 1; ht_insert(key, val); /* * Lookup */ retrive = 0; ht_lookup(key, &retrive); printf("lookup for key[%d] val=[%d]\n", key, retrive); /* * Insert another key, this key will be dispatched to the same bucket. */ key = 18; val = 2; ht_insert(key, val); /* * Lookup */ retrive = 0; ht_lookup(key, &retrive); printf("lookup for key[%d] val=[%d]\n", key, retrive); ht_lookup(8, &retrive); printf("lookup for key[%d] val=[%d]\n", 8, retrive); /* * Insert with the same key */ key = 18; val = 3; ht_insert(key, val); /* * Lookup */ retrive = 0; ht_lookup(key, &retrive); printf("lookup for key[%d] val=[%d]\n", key, retrive); /* * Free object */ ht_cleanup(); }
int main(void) { struct hashtable ht; alloc_set_alloc(count_malloc); test_assert(ht_init(&ht)); replace1(&ht); ht_free(&ht); return 0; }
struct multimap* create_multimap(void) { struct multimap *_mm = malloc(sizeof(struct multimap)); assert(_mm != NULL); ht_init(&_mm->h, 11, 75); _mm->get_first = multimap_get_first; _mm->get_all = multimap_get_all; _mm->put = multimap_put; _mm->remove = multimap_remove; return _mm; }
/** * Creates a new optin object. By default, the new optin will accept the help option and print a * suitable message about available options as well as the usage text, if such text is set with optin_set_usage */ optin* optin_new() { optin* o = (optin*)malloc(sizeof(optin)); memset(o, 0, sizeof(optin)); o->options = (hashtable*)malloc(sizeof(hashtable)); ht_init(o->options, 17, _option_wrapper_hash, _option_wrapper_match, _option_wrapper_destroy); optin_add_switch(o, "help", "Displays help for the program"); optin_set_callback(o, "help", _help_fn); return o; }
END_TEST START_TEST (test_ht_init_no_cmp) { hashtable *ht; int res; res = ht_init(&ht, (ht_hashfunc_t)42, NULL); fail_unless(res == HT_ERROR && ht == NULL, "ht_init() should return HT_ERROR and set ht to NULL" "if a hash function is passed but no cmp function"); }
/** * Creates a new ngt Template Dictionary, ready to be filled with values */ ngt_dictionary* ngt_dictionary_new() { ngt_dictionary* d = (ngt_dictionary*)malloc(sizeof(ngt_dictionary)); memset(d, 0, sizeof(ngt_dictionary)); d->should_expand = NGT_SECTION_VISIBLE; // NOTE: Adjust the buckets parameter depending on how many markers are likely to be in a template // file (then adjust upward to the next prime number ht_init((hashtable*)d, 197, _dictionary_item_hash, _dictionary_item_match, _dictionary_item_destroy); return d; }
static void init(void) { if(!initialized) {initialized=1; rnl_init(); rnv_init(); rnv_verror_handler=&silent_verror_handler; string=(char*)m_alloc(len_v=LEN_S*S_AVG_SIZE,sizeof(char)); t2s=(int(*)[2])m_alloc(len_2=LEN_2,sizeof(int[2])); rules=(int(*)[3])m_alloc(len_r=LEN_R,sizeof(int[3])); ht_init(&ht_s,LEN_S,&hash_s,&equal_s); value=(char*)m_alloc(len_v=LEN_V,sizeof(char)); text=(char*)m_alloc(len_txt=LEN_T,sizeof(char)); windup(); } }
GrHash* GrHash_GcNewFlag(long flags) { GrHash* h=ht_malloc(flags); if(h==NULL) { return NULL; } if(ht_init(h,ht_normal_lookup_func)<0) { return NULL; } return h; }