void test_htable_rehash() { htable *ht = htable_create(); assert(htable_capacity(ht) == MIN_HASH_SIZE && "Size is MIN_HASH_SIZE after creation."); htable_insert(ht, "one", "1st value"); htable_insert(ht, "two", "2nd value"); htable_insert(ht, "three", "3rd value"); htable_insert(ht, "four", "4th value"); htable_insert(ht, "five", "5th value"); htable_insert(ht, "six", "6th value"); htable_insert(ht, "seven", "7th value"); htable_insert(ht, "eight", "8th value"); htable_insert(ht, "nine", "9th value"); htable_insert(ht, "ten", "10th value"); htable_insert(ht, "eleven", "11th value"); htable_insert(ht, "twelve", "12th value"); assert(htable_capacity(ht) == 29 && "Size is 29 after rehash."); assert(0 == strcmp("1st value", htable_get(ht, "one")) && "First value should be equal to 1st value"); assert(0 == strcmp("2nd value", htable_get(ht, "two")) && "Second value should be equal to 2nd value"); assert(0 == strcmp("7th value", htable_get(ht, "seven")) && "Third value should be equal to 3rd value"); htable_destroy(ht); }
const char *htrace_conf_get(const struct htrace_conf *cnf, const char *key) { const char *val; val = htable_get(cnf->values, key); if (val) return val; val = htable_get(cnf->defaults, key); return val; }
void test_htable_get() { htable *ht = htable_create(); htable_insert(ht, "two", "2nd value"); assert(NULL == htable_get(ht, "one") && "First value should be NULL"); assert(NULL != htable_get(ht, "two") && "Second value should not equal NULL"); assert(0 == strcmp("2nd value", htable_get(ht, "two")) && "Second value should be equal to 2nd value"); htable_destroy(ht); }
static void register_first_touch(int pid, void * start_of_page) { size_t hash = hash_pointer(start_of_page, 0); /* Is this the first time we touch this page ? */ ft_entry * t = htable_get(&firsttouch, hash, ptrequ, start_of_page); /* If not record the touching thread to the firsttouch htable */ if (!t) { t = malloc(sizeof(ft_entry)); t->tid = pid; t->start_of_page = start_of_page; htable_add(&firsttouch, hash, t); debug_print("First touch by %d detected at %p\n", pid, start_of_page); } ft_entry * x = htable_get(&firsttouch, hash, ptrequ, start_of_page); }
int fifo_fetch(fifo_t *fifo, uint64_t key, void **ptr) { struct fifo_page *page; if (!htable_get(fifo->t, key, (void **)&page)) { *ptr = page->data; return 0; } if (fifo->active < fifo->nmemb) { page = fifo->page + fifo->active; page->data = fifo->data + fifo->active * fifo->size; page->key = key; fifo->active++; htable_set(fifo->t, key, (void *)page); *ptr = page->data; return 1; } page = fifo->page + fifo->head; if (++fifo->head >= fifo->nmemb) fifo->head = 0; htable_del(fifo->t, page->key); htable_set(fifo->t, key, page); page->key = key; *ptr = page->data; return 1; }
int main(int argc, char **argv) { struct htable ht; foobar_t *foo; foobar_t *ret; htable_init(&ht, rehash, NULL); foo = malloc(sizeof(foobar_t)); foo->name = strdup("fumier"); foo->blah = 123; htable_add(&ht, hash_string(foo->name), foo); ret = (foobar_t *)htable_get(&ht, hash_string("gugus"), streq, "gugus"); if (ret) { printf("Found\n"); } else { printf("Not found\n"); } free(foo->name); free(foo); return 0; }
void test_htable_probing() { htable *ht = htable_create(); size_t capacity = htable_capacity(ht); assert(_hash_func("one", capacity) == _hash_func("seven", capacity) && "Make sure that the keys gives the same key"); htable_insert(ht, "one", "1st value"); htable_insert(ht, "seven", "7th value"); assert(0 == strcmp("1st value", htable_get(ht, "one")) && "First value should be equal to 1st value"); assert(0 == strcmp("7th value", htable_get(ht, "seven")) && "Seventh value should be equal to 7th value"); htable_destroy(ht); }
client_entry_st *find_client_entry(sec_mod_st *sec, uint8_t sid[SID_SIZE]) { struct htable *db = sec->client_db; client_entry_st t; memcpy(t.sid, sid, SID_SIZE); return htable_get(db, rehash(&t, NULL), client_entry_cmp, &t); }
int main(void) { int value; bool done, result; char *cmd, *key, *end, *str_value; char input[256]; struct htable* tbl = htable_new(); if(tbl == NULL) { printf("ERROR: tbl == NULL\n"); return 1; } while(fgets(input, sizeof(input), stdin)) { end = strstr(input, "\n"); if(!end) break; *end = '\0'; if(strcmp(input, "quit") == 0) break; if(strcmp(input, "exit") == 0) break; done = false; value = 0; cmd = input; key = strstr(input, " "); if(key) { *key++ = '\0'; str_value = strstr(key, " "); if(str_value) { *str_value++ = '\0'; value = atoi(str_value); if(strcmp(cmd, "set") == 0) { done = true; result = htable_set(tbl, key, strlen(key), value); } } else { if(strcmp(cmd, "get") == 0) { done = true; result = htable_get(tbl, key, strlen(key), &value); } else if(strcmp(cmd, "del") == 0) { done = true; result = htable_del(tbl, key, strlen(key)); } } } if(done == true) { printf("+OK, cmd = %s, key = %s, value = %d, result = %d, last_error = %d, size = %d, items = %d\n", cmd, key, value, result, tbl->last_error, tbl->size, tbl->items); } else { printf("-INVALID COMMAND, 'set key value', 'get key', 'del key' or 'quit' expected\n"); } } printf("+GOODBYE\n"); htable_free(tbl); return 0; }
static void add_vals(struct htable *ht, const uint64_t val[], unsigned int off, unsigned int num) { uint64_t i; for (i = off; i < off+num; i++) { if (htable_get(ht, hash(&i, NULL), objcmp, &i)) { fail("%llu already in hash", (long long)i); return; } htable_add(ht, hash(&val[i], NULL), &val[i]); if (htable_get(ht, hash(&i, NULL), objcmp, &i) != &val[i]) { fail("%llu not added to hash", (long long)i); return; } } pass("Added %llu numbers to hash", (long long)i); }
int test_add_element_and_find_it(){ hashtable* myhtable; hsize size=101; myhtable=htable_create(size,NULL); htable_insert(myhtable,"Hugo","Yvaon"); assert(!strcmp("Yvaon",htable_get(myhtable,"Hugo"))); htable_destroy(myhtable); myhtable=NULL; return 1; }
static void refill_vals(struct htable *ht, const uint64_t val[], unsigned int num) { uint64_t i; for (i = 0; i < num; i++) { if (htable_get(ht, hash(&i, NULL), objcmp, &i)) continue; htable_add(ht, hash(&val[i], NULL), &val[i]); } }
faup_snapshot_value_count_t *faup_snapshot_value_count_get(faup_snapshot_item_t *item, char *value) { size_t counter; if (!item) { fprintf(stderr, "Item is empty!\n"); return NULL; } return (faup_snapshot_value_count_t *)htable_get(&item->values, hash_string(value), streq, value); }
uint64_t htrace_conf_get_u64(struct htrace_log *log, const struct htrace_conf *cnf, const char *key) { const char *val; uint64_t out = 0; val = htable_get(cnf->values, key); if (val) { if (convert_u64(log, key, val, &out)) { return out; } } val = htable_get(cnf->defaults, key); if (val) { if (convert_u64(log, key, val, &out)) { return out; } } return 0; }
static int ip_lease_exists(main_server_st* s, struct sockaddr_storage* ip, size_t sockaddrlen) { struct ip_lease_st t; t.rip_len = sockaddrlen; memcpy(&t.rip, ip, sizeof(*ip)); if (htable_get(&s->ip_leases.ht, rehash(&t, NULL), ip_lease_cmp, &t) != 0) return 1; return 0; }
static void find_vals(struct htable *ht, const uint64_t val[], unsigned int num) { uint64_t i; for (i = 0; i < num; i++) { if (htable_get(ht, hash(&i, NULL), objcmp, &i) != &val[i]) { fail("%llu not found in hash", (long long)i); return; } } pass("Found %llu numbers in hash", (long long)i); }
/* interface calling into the fortran routine */ static int lbfgs(index_t *x0, at *f, at *g, double gtol, htable_t *p, at *vargs) { /* argument checking and setup */ extern void lbfgs_(int *n, int *m, double *x, double *fval, double *gval, \ int *diagco, double *diag, int iprint[2], double *gtol, \ double *xtol, double *w, int *iflag); ifn (IND_STTYPE(x0) == ST_DOUBLE) error(NIL, "not an array of doubles", x0->backptr); ifn (Class(f)->listeval) error(NIL, "not a function", f); ifn (Class(f)->listeval) error(NIL, "not a function", g); ifn (gtol > 0) error(NIL, "threshold value not positive", NEW_NUMBER(gtol)); at *gx = copy_array(x0)->backptr; at *(*listeval_f)(at *, at *) = Class(f)->listeval; at *(*listeval_g)(at *, at *) = Class(g)->listeval; at *callf = new_cons(f, new_cons(x0->backptr, vargs)); at *callg = new_cons(g, new_cons(gx, new_cons(x0->backptr, vargs))); htable_t *params = lbfgs_params(); if (p) htable_update(params, p); int iprint[2]; iprint[0] = (int)Number(htable_get(params, NEW_SYMBOL("iprint-1"))); iprint[1] = (int)Number(htable_get(params, NEW_SYMBOL("iprint-2"))); lb3_.gtol = Number(htable_get(params, NEW_SYMBOL("ls-gtol"))); lb3_.stpmin = Number(htable_get(params, NEW_SYMBOL("ls-stpmin"))); lb3_.stpmax = Number(htable_get(params, NEW_SYMBOL("ls-stpmax"))); int m = (int)Number(htable_get(params, NEW_SYMBOL("lbfgs-m"))); int n = index_nelems(x0); double *x = IND_ST(x0)->data; double fval; double *gval = IND_ST(Mptr(gx))->data; int diagco = false; double *diag = mm_blob(n*sizeof(double)); double *w = mm_blob((n*(m+m+1)+m+m)*sizeof(double)); double xtol = eps(1); /* machine precision */ int iflag = 0; ifn (n>0) error(NIL, "empty array", x0->backptr); ifn (m>0) error(NIL, "m-parameter must be positive", NEW_NUMBER(m)); /* reverse communication loop */ do { fval = Number(listeval_f(Car(callf), callf)); listeval_g(Car(callg), callg); lbfgs_(&n, &m, x, &fval, gval, &diagco, diag, iprint, >ol, &xtol, w, &iflag); assert(iflag<2); } while (iflag > 0); return iflag; }
static struct aga_node *nr_to_n(struct agar_state *sr, const void *nr) { struct agar_node *nn; size_t hash = hash_pointer(nr, HASH_BASE); bool rc; nn = htable_get(&sr->nodes, hash, agar_node_cmp, nr); if (!nn) { nn = tal(sr, struct agar_node); assert(nn); nn->nr = nr; aga_node_init(&nn->n); rc = htable_add(&sr->nodes, hash, nn); assert(rc); }
int span_table_get(struct span_table *st, struct htrace_span **out, const char *desc, const char *trid) { struct htable *ht = (struct htable *)st; struct htrace_span *span; span = htable_get(ht, desc); EXPECT_NONNULL(span); EXPECT_STR_EQ(desc, span->desc); EXPECT_UINT64_GE(span->begin_ms, span->end_ms); EXPECT_TRUE(0 != htrace_span_id_compare(&INVALID_SPAN_ID, &span->span_id)); EXPECT_NONNULL(span->trid); EXPECT_STR_EQ(trid, span->trid); *out = span; return EXIT_SUCCESS; }
static void add_forkserv_pages(L4_Word_t start, L4_Word_t end) { for(L4_Word_t addr = start & ~PAGE_MASK; addr <= (end | PAGE_MASK); addr += PAGE_SIZE) { size_t hash = int_hash(addr); void *ptr = htable_get(&forkserv_pages, hash, &forkserv_page_cmp, &addr); if(ptr == NULL) { struct forkserv_page *p = malloc(sizeof(*p)); p->address = addr; if(!htable_add(&forkserv_pages, hash, p)) { fprintf(stderr, "htable_add() failed\n"); abort(); } } } }
long _likely_trace(bool cond, bool expect, const char *condstr, const char *file, unsigned int line) { struct trace *p, trace; if (!htable) htable = htable_new(rehash, NULL); init_trace(&trace, condstr, file, line, expect); p = htable_get(htable, hash_trace(&trace), hash_cmp, &trace); if (!p) p = add_trace(condstr, file, line, expect); p->count++; if (cond == expect) p->right++; return cond; }
static void check_addr(void *ip, void *addr) { if (!inst_enable) return; address *new_addr = (address*)malloc(sizeof(*new_addr)); *new_addr = (address)addr; size_t new_addr_hash = addr_hash(new_addr, NULL); if (htable_get(&addresses, new_addr_hash, cmp, new_addr)) { if (instr_count > max_instr_count) { max_instr_count = instr_count; instr_count = 0; } htable_clear(&addresses); } htable_add(&addresses, new_addr_hash, new_addr); }
/* Returns a random word from a dictionary. */ char *get_random_word(struct randgen *randgen, struct htable *dict) { struct htable_entry *entry; char id[6]; assert(randgen != NULL); assert(dict != NULL); if (get_random_id(randgen, id, sizeof(id)) == -1) { fprintf(stderr, "%s\n", "get_random_id failed"); return NULL; } entry = htable_get(dict, id); if (!entry) { fprintf(stderr, "no such word: %s\n", id); return NULL; } return entry->word; }
inline int htable_contains(htable_t * self, int hash, htable_key_t key) { htable_value_t v; return htable_get(self, hash, key, &v) != ERR_HTABLE_GET_KEY_MISSING; }
static void plain_group_list(void *pool, void *additional, char ***groupname, unsigned *groupname_size) { FILE *fp; char line[512]; ssize_t ll; char *p, *sp; unsigned i; size_t hval; struct htable_iter iter; char *tgroup[MAX_GROUPS]; unsigned tgroup_size; struct htable hash; struct plain_cfg_st *config = additional; htable_init(&hash, rehash, NULL); pool = talloc_init("plain"); fp = fopen(config->passwd, "r"); if (fp == NULL) { syslog(LOG_AUTH, "error in plain authentication; cannot open: %s", (char*)config->passwd); return; } line[sizeof(line)-1] = 0; while ((p=fgets(line, sizeof(line)-1, fp)) != NULL) { ll = strlen(p); if (ll <= 4) continue; if (line[ll - 1] == '\n') { ll--; line[ll] = 0; } if (line[ll - 1] == '\r') { ll--; line[ll] = 0; } #ifdef HAVE_STRSEP sp = line; p = strsep(&sp, ":"); if (p != NULL) { p = strsep(&sp, ":"); #else p = strtok_r(line, ":", &sp); if (p != NULL) { p = strtok_r(NULL, ":", &sp); #endif if (p != NULL) { break_group_list(pool, p, tgroup, &tgroup_size); for (i=0; i<tgroup_size; i++) { hval = rehash(tgroup[i], NULL); if (htable_get(&hash, hval, str_cmp, tgroup[i]) == NULL) { if (strlen(tgroup[i]) > 1) htable_add(&hash, hval, tgroup[i]); } } } } } *groupname_size = 0; *groupname = talloc_size(pool, sizeof(char*)*MAX_GROUPS); if (*groupname == NULL) { goto exit; } p = htable_first(&hash, &iter); while (p != NULL && (*groupname_size) < MAX_GROUPS) { (*groupname)[(*groupname_size)] = talloc_strdup(*groupname, p); p = htable_next(&hash, &iter); (*groupname_size)++; } /* always succeed */ exit: htable_clear(&hash); safe_memset(line, 0, sizeof(line)); fclose(fp); return; } const struct auth_mod_st plain_auth_funcs = { .type = AUTH_TYPE_PLAIN | AUTH_TYPE_USERNAME_PASS, .allows_retries = 1, .global_init = plain_global_init, .auth_init = plain_auth_init, .auth_deinit = plain_auth_deinit, .auth_msg = plain_auth_msg, .auth_pass = plain_auth_pass, .auth_user = plain_auth_user, .auth_group = plain_auth_group, .group_list = plain_group_list };
int main(int argc, char *argv[]) { unsigned int i; uintptr_t perfect_bit; struct htable ht; uint64_t val[NUM_VALS]; uint64_t dne; void *p; struct htable_iter iter; plan_tests(29); for (i = 0; i < NUM_VALS; i++) val[i] = i; dne = i; htable_init(&ht, hash, NULL); ok1(ht.max == 0); ok1(ht.bits == 0); /* We cannot find an entry which doesn't exist. */ ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* This should increase it once. */ add_vals(&ht, val, 0, 1); ok1(ht.bits == 1); ok1(ht.max == 1); ok1(ht.common_mask == -1); /* Mask should be set. */ ok1(check_mask(&ht, val, 1)); /* This should increase it again. */ add_vals(&ht, val, 1, 1); ok1(ht.bits == 2); ok1(ht.max == 3); /* Mask should be set. */ ok1(ht.common_mask != 0); ok1(ht.common_mask != -1); ok1(check_mask(&ht, val, 2)); /* Now do the rest. */ add_vals(&ht, val, 2, NUM_VALS - 2); /* Find all. */ find_vals(&ht, val, NUM_VALS); ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* Walk once, should get them all. */ i = 0; for (p = htable_first(&ht,&iter); p; p = htable_next(&ht, &iter)) i++; ok1(i == NUM_VALS); /* Delete all. */ del_vals(&ht, val, NUM_VALS); ok1(!htable_get(&ht, hash(&val[0], NULL), objcmp, &val[0])); /* Worst case, a "pointer" which doesn't have any matching bits. */ htable_add(&ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); htable_add(&ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]); ok1(ht.common_mask == 0); ok1(ht.common_bits == 0); /* Get rid of bogus pointer before we trip over it! */ htable_del(&ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); /* Add the rest. */ add_vals(&ht, val, 0, NUM_VALS-1); /* Check we can find them all. */ find_vals(&ht, val, NUM_VALS); ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* Corner cases: wipe out the perfect bit using bogus pointer. */ htable_clear(&ht); htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1])); ok1(ht.perfect_bit); perfect_bit = ht.perfect_bit; htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit)); ok1(ht.perfect_bit == 0); htable_del(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit)); /* Enlarging should restore it... */ add_vals(&ht, val, 0, NUM_VALS-1); ok1(ht.perfect_bit != 0); htable_clear(&ht); return exit_status(); }
//if there is no entry, then an entry is added and 1 is returned //if the entry in the routing table is already better, 0 is returned //if the given information is better, the table is updated and 1 is returned. //NOTE: this DOES NOT lock the routing table mutex. //printmode=0 to print a full line ("route: this happened"), 1 to print shortened line. int update_routing_table(struct in_addr destAddress, uint8_t cost, interface_t *supplier_int, int printmode) { routeentry_t *curentry, *newentry; if (cost >= ROUTE_INFINITY) cost = ROUTE_INFINITY; curentry = (routeentry_t *)htable_get(g_routeTable, destAddress.s_addr); if (curentry != NULL) { //if the supplier is the same... if (curentry->interface == supplier_int) { //if the cost is the same, we're verifying an existing entry. if (curentry->cost == cost) { if (printmode==0) dbg(DBG_ROUTE, "route: Refreshing entry for %s, cost still %d.\n", inet_ntoa_host(destAddress), curentry->cost); else dbg(DBG_ROUTE, "refreshed to %d.\n", curentry->cost); curentry->lastRefreshTime = time(NULL); return 0; } //otherwise, we're updating it - a route changed / link went down, etc. //NOTE: This seems to fix problems with split horizon //since if a node you connected to suddenly lost route, you also assume //you lose route unless you get an update from someone else. if (curentry->cost > cost) { if (printmode==0) dbg(DBG_ROUTE, "route: Found better route to %s from %d to %d.\n", inet_ntoa_host(destAddress), curentry->cost, cost); else dbg(DBG_ROUTE, "improved cost to %d\n", curentry->cost); curentry->cost = cost; curentry->lastRefreshTime = time(NULL); return 1; } /*if (curentry->cost == 1) { dbg(DBG_ROUTE, "route: Not worsening route of cost 1 - direct link.\n"); return 0; }*/ if (printmode==0) dbg(DBG_ROUTE, "route: Route to %s worsened from %d to %d\n", inet_ntoa_host(destAddress), curentry->cost, cost); else dbg(DBG_ROUTE, "worsened from %d\n", curentry->cost); curentry->cost = cost; curentry->lastRefreshTime = time(NULL); return 1; } //if it's not, maybe we have a better route from somewhere else. if (curentry->cost > cost) { if (printmode==0) dbg(DBG_ROUTE, "route: Found better route to %s from a different interface, cost from %d to %d.\n", inet_ntoa_host(destAddress), curentry->cost, cost); else dbg(DBG_ROUTE, "improved from %d (diff route)\n", curentry->cost); //we found a better route // Replace the old value with the new value curentry->cost = cost; curentry->interface = supplier_int; curentry->lastRefreshTime = time(NULL); return 1; } else { if (printmode==0) dbg(DBG_ROUTE, "route: Not updating with entry from different interfaces, ours is <= it.\n"); else dbg(DBG_ROUTE, ">= ours, not updating\n"); } } else { if (printmode==0) dbg(DBG_ROUTE, "route: Found new route to %s, cost=%d.\n", inet_ntoa_host(destAddress), cost); else dbg(DBG_ROUTE, "new route\n"); //we found a new route. // Put in a new value free(curentry); newentry = (routeentry_t *)malloc(sizeof(routeentry_t)); newentry->interface = supplier_int; newentry->cost = cost; newentry->lastRefreshTime = time(NULL); htable_put(g_routeTable, destAddress.s_addr, newentry); return 1; } return 0; }