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; }
/* * Remove an option. */ void removeoption(const char *name) { struct nvlist *nv, *nvt; char *p, *low, c; const char *n; if ((nv = ht_lookup(opttab, name)) != NULL) { if (options == nv) { options = nv->nv_next; nvfree(nv); } else { nvt = options; while (nvt->nv_next != NULL) { if (nvt->nv_next == nv) { nvt->nv_next = nvt->nv_next->nv_next; nvfree(nv); break; } else nvt = nvt->nv_next; } } } (void)ht_remove(opttab, name); low = emalloc(strlen(name) + 1); /* make lowercase, then remove from select table */ for (n = name, p = low; (c = *n) != '\0'; n++) *p++ = isupper(c) ? tolower(c) : c; *p = 0; n = intern(low); free(low); (void)ht_remove(selecttab, n); }
DataRecord* bc_get(Bitcask *bc, const char* key) { Item *item = ht_get(bc->tree, key); if (NULL == item) return NULL; if (item->ver < 0){ free(item); return NULL; } int bucket = item->pos & 0xff; uint32_t pos = item->pos & 0xffffff00; if (bucket > bc->curr) { fprintf(stderr, "BUG: invalid bucket %d > %d\n", bucket, bc->curr); ht_remove(bc->tree, key); free(item); return NULL; } DataRecord* r = NULL; if (bucket == bc->curr) { pthread_mutex_lock(&bc->buffer_lock); if (bucket == bc->curr && pos >= bc->wbuf_start_pos){ int p = pos - bc->wbuf_start_pos; r = decode_record(bc->write_buffer + p, bc->wbuf_curr_pos - p, true); } pthread_mutex_unlock(&bc->buffer_lock); if (r != NULL){ free(item); return r; } } char fname[20], data[255]; const char * path = mgr_base(bc->mgr); sprintf(fname, DATA_FILE, bucket); sprintf(data, "%s/%s", path, fname); int fd = open(data, O_RDONLY); if (-1 == fd){ goto GET_END; } r = fast_read_record(fd, pos, true); if (NULL == r){ fprintf(stderr, "Bug: get %s failed in %s %d %d\n", key, path, bucket, pos); }else{ // check key if (strcmp(key, r->key) != 0){ fprintf(stderr, "Bug: record %s is not expected %s\n", r->key, key); free_record(r); r = NULL; } } GET_END: if (NULL == r) ht_remove(bc->tree, key); if (fd != -1) close(fd); free(item); return r; }
void test_ht_remove(void **state) { Hashtable *ht; int i; ht = ht_create(0, uint16_hash, uint16_compare, uint16_copy, uint16_free, uint16_compare, uint16_copy, uint16_free); for (i=0; i<LIMIT; i++) { uint16_t *data; uint16_t *key; data = malloc(sizeof(uint16_t)); *data = i; key = malloc(sizeof(uint16_t)); *key = i; ht_insert(ht, key, data); } for (i=0; i<LIMIT; i++) { uint16_t *data; data = ht_remove(ht, &i); assert_true(data != NULL); assert_int_equal(*data, i); free(data); data = ht_remove(ht, &i); assert_true(data == NULL); } ht_free(ht); }
void ht_clear(int free_data) { hash_elem_it it = HT_ITERATOR(hash); uint8_t * k = ht_iterate_keys(&it); while(k != NULL) { free_data ? free(ht_remove(k)) : ht_remove(k); k = ht_iterate_keys(&it); } }
void handle_usb_unplugged(char* device){ must_exit = 1; pid_t pid = fork(); if (pid ==0){ /*child process*/ execl("/bin/umount", "umount", "-l", device, (char *)0); }else{ waitpid(pid,0,0); MacRec *head = (MacRec *) ht_lookup(macaddrs, device); char* signal = (char*) malloc(32); if (head != NULL){ sprintf(signal, head->addr, 0, 17); strcat(signal, " deny"); sendsignal(signal); MacRec *current = head; while ( (current = current->next) != NULL){ printf("deny --> %s\n", current->addr); sprintf(signal, current->addr, 0, 17); strcat(signal, " deny"); sendsignal(signal); } ht_remove(macaddrs, device); free_macrec(head); free(signal); } } }
unsigned long _st_remove(smat_table_t *table, smat_entry_t *entry, unsigned int remflag) { unsigned long retval; if (remflag & ST_REM_HASH) { /* remove from hash table */ if ((retval = ht_remove(&table->st_table, &entry->se_hash))) return retval; } if (remflag & ST_REM_FIRST) { /* remove from first linked list */ if ((retval = ll_remove(entry->se_link[SMAT_LOC_FIRST].le_head, &entry->se_link[SMAT_LOC_FIRST]))) return retval; } if (remflag & ST_REM_SECOND) { /* remove from second linked list */ if ((retval = ll_remove(entry->se_link[SMAT_LOC_SECOND].le_head, &entry->se_link[SMAT_LOC_SECOND]))) return retval; } if (remflag & ST_REM_FREE) /* free entry */ _smat_free(entry); return 0; }
static int nd_pool_destroy (__do pool) { if ( NULL != pool->p_pool) { if (ht_remove ((hashtable_t*) pool->p_pool->d, (unsigned char*) pool->path_c, strlen (pool->path_c) + 1)) { print_str ("ERROR: nd_pool_destroy: orphaned pool, fix this!\n"); abort (); } } if (NULL != pool->link) { mutex_lock (&di_base.index_linked.mutex); md_unlink_le (&di_base.index_linked, pool->link); pthread_mutex_unlock (&di_base.index_linked.mutex); } print_str ("DEBUG: destroyed pool: %s\n", pool->path_c); free (pool->d); free (pool->path_c); free (pool); return 0; }
int main (int argc, char **argv) { if (argc != 3) { printf("Error on usage: %s <key> <value>\n", argv[0]); return EXIT_FAILURE; } char *key = argv[1]; char *value = argv[2]; // testing add function ht_add(key, value); ht_add("my", "diego"); ht_add("baby", "looks"); ht_add("rocks", "great!"); printf("- Entity added for key %s is %s.\n", key, ht_find(key)); // printing function printf("\nPrinting all entries:\n"); ht_print(); // testing remove function printf("\n- Trying to remove entry for key %s: %s.\n", key, (ht_remove(key) ? "done" : "fail")); // printing function printf("\nPrinting all entries:\n"); ht_print(); return EXIT_SUCCESS; }
int main(int argc, char** argv) { printf("Creating...\n"); HashTable* t = ht_make(3); HashFun h = jenkin1_hash; test_table(t, h, 0, 1); for (int i = 0; i < sizeof(test_a)/sizeof(test_a[0]); ++i) { const char* s = test_a[i]; unsigned int hash = h(s); printf("Add \"%s\" (%u)...\n", s, hash); ht_put(t, hash, s); test_table(t, h, i+1, 1); } for (int i = 0; i < sizeof(test_b)/sizeof(test_b[0]); ++i) { const char* s = test_b[i]; unsigned int hash = h(s); printf("Remove \"%s\" (%u)...\n", s, hash); ht_remove(t, hash, s); test_table(t, h, i+1, 0); } printf("All ok!\n"); ht_destroy(t); return 0; }
void os_free(struct os *os) { int i; #ifdef HAS_HTAB if (!os->cached_partition_info[1].sfw_tlb) { htab_free(os); } #endif /* HAS_HTAB */ if (os->po_psm) os_psm_destroy(os->po_psm); for (i = 0; i < os->installed_cpus; i++) { cpu_free(os->cpu[i]); os->cpu[i] = NULL; } ht_remove(&os_hash, os->po_lpid); os->po_lpid = -1; hfree(os, sizeof(*os)); }
/** * \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; }
// remove expired items from connection_list void _hp_clean_expired(hashtable_t *ht, time_t max_age) { hashtable_item_t *i; _connection_info_t *ci; _connection_list_t *cl; strlist_t *rl = 0; strlist_iterator_t *rli; char *tip; for (ht_reset(ht); ht_hasnext(ht); ) { i = ht_next(ht); ci = (_connection_info_t*) i->value; // remove expired connection times ci->connects = _hp_cl_remove(ci->connects, max_age); // if there are none left, remove the ip from the table. if (_hp_cl_size(ci->connects) < 1) rl = str_add(rl, ci->ip); } // remove ips with no connection-times from hashtable. for (rli = str_iterator(rl); str_iterator_hasnext(rli); ) { tip = str_iterator_next(rli); // printf("DEBUG: expiring %s\n", tip); ht_remove(ht, tip); } str_close(rl); free(rli); }
void level_free_last(struct level *level, struct ht *h) { struct level_node *n; n = level->last; ht_remove(h, n->sk.data); level_free_node(level, n); }
int sba_jfs_remove_journaled_blocks(int blocknr) { if (ht_remove(h_jfs_journaled_blocks, blocknr) < 0) { sba_debug(1, "Error removing the blocknr %d\n", blocknr); return -1; } return 1; }
/** * Saffire method: */ SAFFIRE_METHOD(hash, remove) { t_string_object *key; if (! object_parse_arguments(SAFFIRE_METHOD_ARGS, "s", &key)) { saffire_warning("Error while parsing argument list\n"); RETURN_NUMERICAL(0); } ht_remove(self->ht, key->value); RETURN_SELF; }
void channel_handle_client_read(connector_t pconn, int event) { //由于和客户端只有一次交互,不用一直读取 if (connector_read(pconn, event) > 0) { char *val = buffer_get_read(pconn->preadbuf); message_t pmsg = (message_t)malloc(sizeof(message)); memset(pmsg, 0, sizeof(pmsg)); size_t len1 = get_client_msg(val, pmsg); if (len1 == 0) { print_log(LOG_TYPE_ERROR, "Read Client Msg Error %s", val); free(pmsg); return; } char data[20] = {0}; memcpy(data, pmsg->uid, pmsg->len); buffer_read(pconn->preadbuf, len1, TRUE); memcpy(pconn->uid, data, pmsg->len); int len2 = sizeof(connector_t); ht_insert(pconn->pworker->pht, data, (pmsg->len)+1, pconn, len2+1); context_t pcontext = (context_t)malloc(sizeof(context)); memset(pcontext, 0, sizeof(context)); memcpy(pcontext->data, data, pmsg->len); list_push_tail(pconn->pworker->plist, pcontext); //print_log(LOG_TYPE_DEBUG, "Hash key %s, Len %d", pcontext->data, pmsg->len); char cmd[REDIS_CMD_LEN] = {'\0'}; get_request_str(data, cmd); int len = strlen(cmd); if (pconn->pworker->redis->state == CONN_STATE_RUN) { buffer_write(pconn->pworker->redis->pwritebuf, cmd, len); connector_write(pconn->pworker->redis); } else { print_log(LOG_TYPE_ERROR, "Redis not run"); list_pop_head(pconn->pworker->plist); ht_remove(pconn->pworker->pht, data, (pmsg->len)+1); pconn->pworker->neterr_count++; } free(pmsg); } }
/** * Removes an interface with the given name from the table * * @param[in] ifname * Interface name * * @param[in] af * Address family * * @param[in] interface_exists * TRUE if the interface still exists (we need to remvoe filters from it); * FALSE if it was deleted so we don't need to worry about deleting filters */ void policy_table_delete_policy(char *ifname, uint8_t af, boolean interface_exists) { hash_key_t k; policy_table_entry_t * policy; ped_policy_route_t * route = NULL, * route_tmp = NULL; INSIST(if_table != NULL); INSIST(ifname != NULL); INSIST(strlen(ifname) <= MAX_IF_NAME_LEN); junos_trace(PED_TRACEFLAG_HT, "%s(%s, %d)", __func__, ifname, af); strcpy(k.ifname, ifname); k.af = af; policy = ht_remove(if_table, &k); // frees the key if(policy == NULL) return; // remove and delete filters only if the interface still exists // (if interface is deleted so are all attached filters) if(interface_exists) { if(policy->filter) { remove_filters_from_interface(ifname); free(policy->filter); } if(policy->pfd_filter) { remove_pfd_filter_from_interface(policy->ifname); policy->pfd_filter = FALSE; } } // remove and delete routes route = policy->route; while(route) { route_tmp = route; // Save pointer to current route. route = route->next; // Move to the next route. if(route->status != ROUTE_FAILED) { remove_route(route_tmp); if(route->status == ROUTE_PENDING) { --changes_pending; } } free(route_tmp); // Free the current route data. } free(policy); }
int config_set_string(config_t config, char *name, char *value) { hashtable *ht; char *key; int rc; ht = (hashtable *)config; key = name; ht_remove(ht, key, 1); rc = ht_add(ht, key, strdup(value)); return rc; }
void channel_handle_redis_read(connector_t pconn, int event) { //修复一个问题,Redis返回的数据已经读取到了缓冲区,不能只读取一个业务包 if (connector_read(pconn, event) > 0) { while (buffer_readable(pconn->preadbuf) > 0) { char *origin = buffer_get_read(pconn->preadbuf); char analyse[100] = {0}; int originlen = get_analyse_data(origin, analyse); if (originlen == 0) { print_log(LOG_TYPE_DEBUG, "buffer no value"); break; } buffer_read(pconn->preadbuf, originlen, TRUE); if (strcmp(analyse, REDIS_HBVAL) == 0) return; context_t pcontext = (context_t)pconn->pworker->plist->head->value; //print_log(LOG_TYPE_DEBUG, "Redis Read %s List Head Uid %s", analyse, pcontext->data); char key[UID_MAX_LEN] = {0}; memcpy(key, pcontext->data, strlen(pcontext->data)); MEM_FREE(pcontext); list_pop_head(pconn->pworker->plist); size_t len1 = strlen(key); size_t len2 = 0; connector_t pclientcon = (connector_t)ht_get(pconn->pworker->pht, key, len1+1, &len2); if (pclientcon) { ht_remove(pconn->pworker->pht, key, len1+1); char val[100] = {0}; get_response_str(val, key, analyse); size_t size = strlen(val); buffer_write(pclientcon->pwritebuf, val, size); connector_write(pclientcon); } } } }
// update pos in HTree void update_items(Item *it, void *args) { HTree *tree = (HTree*) args; Item *p = ht_get(tree, it->key); if (p) { if (it->pos != p->pos && it->ver == p->ver) { if (it->ver > 0) { ht_add(tree, p->key, it->pos, p->hash, p->ver); } else { ht_remove(tree, p->key); } } free(p); } else { ht_add(tree, it->key, it->pos, it->hash, it->ver); } }
void vtable_remove(var_t *table, char *name) { ht_t *ht = table->v_data; var_t *v; v = vtable_lookup(table, name); if (v == NULL) { log_debug("vtable_remove: \"%s\" not found", name); return; } ht_remove(ht, v); return; }
void mhandle_free(void *ptr, char *at) { unsigned long size; /* initialization */ if (!ptr) return; initialize(); /* delete pointer from database & check corruption*/ ptr -= CORRUPT_RANGE; size = ht_remove(ptr, at); /* clear memory & free pointer */ bzero(ptr, size + CORRUPT_TOTAL); free(ptr); }
int config_set_dword(config_t config, char *name, int value) { hashtable *ht; char *key; int rc; ht = (hashtable *)config; key = name; char *s = malloc(10 + 1); /* maximum length of 2^32 is 10 */ sprintf(s, "%d", value); ht_remove(ht, key, 1); rc = ht_add(ht, key, s); return rc; }
void shiki_remove(struct shiki *lru, struct slice *sk) { struct level_node *n; if (lru->buffer == 0) return; n = (struct level_node *)ht_get(lru->ht, sk->data); if (n != NULL) { ht_remove(lru->ht, sk->data); if (n->hits == -1) level_free_node(&lru->level_new, n); else level_free_node(&lru->level_old, n); } }
int vtable_set(var_t *table, var_t *v) { ht_t *ht = table->v_data; if (ht_lookup(ht, v) != NULL) { ht_remove(ht, v); } if (ht_insert(ht, v)) { log_error("vtable_set: ht_insert failed"); return -1; } return 0; }
void unregister_message_proc(MessageMap* _map, u32 _msg) { ht_node_t* pm = 0; ls_node_t* p = 0; union { Ptr ptr; u32 uint; } u; assert(_map); if(_msg < MESSAGE_TABLE_SIZE) { _map->fast_table[_msg] = 0; } else { pm = _map->proc_map; u.uint = _msg; p = ht_find(pm, u.ptr); if(p) { ht_remove(pm, p->extra); } } }
void *mhandle_realloc(void *ptr, unsigned long size, char *at) { /* equivalent to malloc or free*/ if (!ptr) return mhandle_malloc(size, at); if (!size) { mhandle_free(ptr, at); return NULL; } /* realloc */ initialize(); ptr -= CORRUPT_RANGE; ht_remove(ptr, at); ptr = realloc(ptr, size + CORRUPT_TOTAL); if (!ptr) outofmem(at); mark_corruption(ptr, size); ht_insert(ptr, size, at); return ptr + CORRUPT_RANGE; }
void *test(void *data) { int val2, numtx, r, last = -1; val_t val = 0; int unext, mnext, cnext; thread_data_t *d = (thread_data_t *)data; /* Create transaction */ TM_THREAD_ENTER(); /* Wait on barrier */ barrier_cross(d->barrier); /* Is the first op an update, a move? */ r = rand_range_re(&d->seed, 100) - 1; unext = (r < d->update); mnext = (r < d->move); cnext = (r >= d->update + d->snapshot); #ifdef ICC while (stop == 0) { #else while (AO_load_full(&stop) == 0) { #endif /* ICC */ if (unext) { // update if (mnext) { // move if (last == -1) val = rand_range_re(&d->seed, d->range); val2 = rand_range_re(&d->seed, d->range); if (ht_move(d->set, val, val2, TRANSACTIONAL)) { d->nb_moved++; last = val2; } d->nb_move++; } else if (last < 0) { // add val = rand_range_re(&d->seed, d->range); if (ht_add(d->set, val, TRANSACTIONAL)) { d->nb_added++; last = val; } d->nb_add++; } else { // remove if (d->alternate) { // alternate mode if (ht_remove(d->set, last, TRANSACTIONAL)) { d->nb_removed++; last = -1; } } else { /* Random computation only in non-alternated cases */ val = rand_range_re(&d->seed, d->range); /* Remove one random value */ if (ht_remove(d->set, val, TRANSACTIONAL)) { d->nb_removed++; /* Repeat until successful, to avoid size variations */ last = -1; } } d->nb_remove++; } } else { // reads if (cnext) { // contains (no snapshot) if (d->alternate) { if (d->update == 0) { if (last < 0) { val = d->first; last = val; } else { // last >= 0 val = rand_range_re(&d->seed, d->range); last = -1; } } else { // update != 0 if (last < 0) { val = rand_range_re(&d->seed, d->range); //last = val; } else { val = last; } } } else val = rand_range_re(&d->seed, d->range); if (ht_contains(d->set, val, TRANSACTIONAL)) d->nb_found++; d->nb_contains++; } else { // snapshot if (ht_snapshot(d->set, TRANSACTIONAL)) d->nb_snapshoted++; d->nb_snapshot++; } } /* Is the next op an update, a move, a contains? */ if (d->effective) { // a failed remove/add is a read-only tx numtx = d->nb_contains + d->nb_add + d->nb_remove + d->nb_move + d->nb_snapshot; unext = ((100.0 * (d->nb_added + d->nb_removed + d->nb_moved)) < (d->update * numtx)); mnext = ((100.0 * d->nb_moved) < (d->move * numtx)); cnext = !((100.0 * d->nb_snapshoted) < (d->snapshot * numtx)); } else { // remove/add (even failed) is considered as an update r = rand_range_re(&d->seed, 100) - 1; unext = (r < d->update); mnext = (r < d->move); cnext = (r >= d->update + d->snapshot); } #ifdef ICC } #else } #endif /* ICC */ /* Free transaction */ TM_THREAD_EXIT(); return NULL; }
void delsym(const char *s) { ht_remove(symtab, s); }