static void run_test(HTable* htable) { int i, j; /* fill table */ for(i = 1; i <= N; i++) { for(j = 1; j <= N; j++) { bool isNewNode; ExpressionTableNodeData new_node_data; sprintf(new_node_data.expression, "%d + %d", i, j); new_node_data.value = (i + j); htable_insert(htable, (HTableNode*)&new_node_data, &isNewNode); assert(isNewNode); } } assert(htable_nitems(htable) == (N*N)); /* check hash table is filled right */ for(i = 1; i <= N; i++) { for(j = 1; j <= N; j++) { ExpressionTableNode found_node; ExpressionTableNodeData query; sprintf(query.expression, "%d + %d", i, j); found_node = (ExpressionTableNode)htable_find(htable, (HTableNode*)&query); assert(found_node != NULL); assert(found_node->value == (i + j)); } } /* try to delete a non-existing node */ { bool result; ExpressionTableNodeData query; sprintf(query.expression, "ololo trololo"); result = htable_delete(htable, (HTableNode*)&query); assert(result == false); } /* clean table */ for(i = 1; i <= N; i++) { for(j = 1; j <= N; j++) { bool result; ExpressionTableNodeData query; sprintf(query.expression, "%d + %d", i, j); result = htable_delete(htable, (HTableNode*)&query); assert(result == true); } } assert(htable_nitems(htable) == 0); }
/** * Creates a hashtable, and inserts words into it. The table is then printed before we free the memory allocated to it. Note: The tablesize of the hashtable is determined by the first command line argument if there is one, with a default table tablesize of 113. The number of statistical snapshots to print is determined by the second command line argument if there is one, with a default number of 10. tablesize = the maximum number of positions in the hash table. word = the string to be inserted into the hash table. ht = the hash table using eith double hashing or linear probing. snapshots = the number of statistical snapshots to be used. @param argc the number of command-line arguments. @param argv an array of strings containing the command-line arguments. @return EXIT_SUCCESS if the program is successful. */ int main(int argc, char **argv) { time_t start,end; bool_t entire_table = FALSE, double_hashing = FALSE, print_stats = FALSE, do_spell_check = FALSE; int tablesize = 113, snapshots = 10; char word[256]; char *filename; htable ht; set_flags(argc, argv, &do_spell_check, &filename, &double_hashing, &entire_table, &print_stats, &snapshots, &tablesize); ht = htable_new(tablesize, (double_hashing) ? DOUBLE_H : LINEAR_P); start = clock(); while (getword(word, sizeof word, stdin) != EOF) { htable_insert(ht, word); } end = clock(); if(do_spell_check) { spell_check(ht,filename,(end-start)/(double)CLOCKS_PER_SEC); } if (entire_table) { htable_print_entire_table(ht, stderr); } if (print_stats) { htable_print_stats(ht, stdout, snapshots); } else if (!do_spell_check){ /* print words and frequencies */ htable_print(ht, stdout); } htable_delete(ht); return EXIT_SUCCESS; }
/** * @fn void virtual_cache_delete(virtual_cache_t vc) * @brief Release the virtual cache pointer. * @param vc The virtual cache pointer. */ void virtual_cache_delete(virtual_cache_t vc) { struct virtual_cache_s *v = (struct virtual_cache_s*)vc; if(!v) return; virtual_cache_clear(v); if(v->table) htable_delete(v->table), v->table = NULL; free(v); }
int psc_dnsbl_retrieve(const char *client_addr, const char **dnsbl_name, int dnsbl_index) { const char *myname = "psc_dnsbl_retrieve"; PSC_DNSBL_SCORE *score; int result_score; /* * Sanity check. */ if ((score = (PSC_DNSBL_SCORE *) htable_find(dnsbl_score_cache, client_addr)) == 0) msg_panic("%s: no blocklist score for %s", myname, client_addr); /* * Disable callbacks. */ PSC_CALL_BACK_CANCEL(score, dnsbl_index); /* * Reads are destructive. */ result_score = score->total; *dnsbl_name = score->dnsbl_name; score->refcount -= 1; if (score->refcount < 1) { if (msg_verbose > 1) msg_info("%s: delete blocklist score for %s", myname, client_addr); htable_delete(dnsbl_score_cache, client_addr, myfree); } return (result_score); }
int main(int argc, char **argv) { int i, len, res; struct htable *table; table = htable_new(16, 0, &htable_cstring_cmpfn, &string_copyfn, &string_freefn); assert(table != NULL); len = sizeof(string_data)/sizeof(string_data[0]); for (i = 0; i < 4; i++) { res = htable_add(table, strlen(string_data[i]), string_data[i], NULL); assert(res == 1); assert(strcmp(table->entries[i]->key, string_data[i]) == 0); } assert(htable_resize(table, 0, 1024) == 1); for (i = 4; i < len; i++) { res = htable_add(table, strlen(string_data[i]), string_data[i], NULL); assert(res == 1); assert(strcmp(table->entries[i]->key, string_data[i]) == 0); } assert(htable_resize(table, 0, 512) == 1); for (i = 0; i < len; i++) { assert(strcmp(table->entries[i]->key, string_data[i]) == 0); } htable_delete(table); return 0; }
void qmgr_queue_done(QMGR_QUEUE *queue) { const char *myname = "qmgr_queue_done"; QMGR_TRANSPORT *transport = queue->transport; /* * Sanity checks. It is an error to delete an in-core queue with pending * messages or timers. */ if (queue->busy_refcount != 0 || queue->todo_refcount != 0) msg_panic("%s: refcount: %d", myname, queue->busy_refcount + queue->todo_refcount); if (queue->todo.next || queue->busy.next) msg_panic("%s: queue not empty: %s", myname, queue->name); if (!QMGR_QUEUE_READY(queue)) msg_panic("%s: bad queue status: %s", myname, QMGR_QUEUE_STATUS(queue)); if (queue->dsn) msg_panic("%s: queue %s: spurious reason %s", myname, queue->name, queue->dsn->reason); /* * Clean up this in-core queue. */ QMGR_LIST_UNLINK(transport->queue_list, QMGR_QUEUE *, queue, peers); htable_delete(transport->queue_byname, queue->name, (void (*) (char *)) 0); myfree(queue->name); myfree(queue->nexthop); qmgr_queue_count--; myfree((char *) queue); }
void psc_free_session_state(PSC_STATE *state) { const char *myname = "psc_free_session_state"; HTABLE_INFO *ht; /* * Update the per-client session count. */ if ((ht = htable_locate(psc_client_concurrency, state->smtp_client_addr)) == 0) msg_panic("%s: unknown client address: %s", myname, state->smtp_client_addr); if (--(ht->value) == 0) htable_delete(psc_client_concurrency, state->smtp_client_addr, (void (*) (char *)) 0); if (state->smtp_client_stream != 0) { event_server_disconnect(state->smtp_client_stream); psc_check_queue_length--; } if (state->smtp_server_fd >= 0) { close(state->smtp_server_fd); psc_post_queue_length--; } if (state->send_buf != 0) state->send_buf = vstring_free(state->send_buf); myfree(state->smtp_client_addr); myfree(state->smtp_client_port); myfree(state->smtp_server_addr); myfree(state->smtp_server_port); if (state->dnsbl_reply) vstring_free(state->dnsbl_reply); if (state->helo_name) myfree(state->helo_name); if (state->sender) myfree(state->sender); if (state->cmd_buffer) vstring_free(state->cmd_buffer); if (state->expand_buf) vstring_free(state->expand_buf); myfree((char *) state); if (psc_check_queue_length < 0 || psc_post_queue_length < 0) msg_panic("bad queue length: check_queue=%d, post_queue=%d", psc_check_queue_length, psc_post_queue_length); /* * Update the stress level. */ if (psc_stress != 0 && psc_check_queue_length <= psc_lowat_check_queue_length) { psc_stress = 0; msg_info("leaving STRESS mode with %d connections", psc_check_queue_length); } }
int tls_mgr_delete(const char *unused_type, const char *key) { if (tls_cache == 0) return TLS_MGR_STAT_ERR; if (htable_locate(tls_cache, key)) { htable_delete(tls_cache, key, free_value); --cache_count; } return (TLS_MGR_STAT_OK); }
void dict_unregister(const char *dict_name) { const char *myname = "dict_unregister"; DICT_NODE *node; if ((node = dict_node(dict_name)) == 0) msg_panic("non-existing dictionary: %s", dict_name); if (msg_verbose > 1) msg_info("%s: %s %d", myname, dict_name, node->refcount); if (--(node->refcount) == 0) htable_delete(dict_table, dict_name, dict_node_free); }
void test_memory_copy() { int i, len, res; struct htable *table; table = htable_new(512, 0, &htable_cstring_cmpfn, &string_copyfn, &string_freefn); assert(table != NULL); len = sizeof(string_data)/sizeof(string_data[0]); for (i = 0; i < len; i++) { res = htable_add(table, strlen(string_data[i]), string_data[i], NULL); assert(res == 1); assert(strcmp((char *)table->entries[i]->key, string_data[i]) == 0); } htable_delete(table); }
void qmgr_peer_free(QMGR_PEER *peer) { const char *myname = "qmgr_peer_free"; QMGR_JOB *job = peer->job; QMGR_QUEUE *queue = peer->queue; /* * Sanity checks. It is an error to delete a referenced peer structure. */ if (peer->refcount != 0) msg_panic("%s: refcount: %d", myname, peer->refcount); if (peer->entry_list.next != 0) msg_panic("%s: entry list not empty: %s", myname, queue->name); QMGR_LIST_UNLINK(job->peer_list, QMGR_PEER *, peer, peers); htable_delete(job->peer_byname, queue->name, (void (*) (char *)) 0); myfree((char *) peer); }
static void dict_sockmap_close(DICT *dict) { const char *myname = "dict_sockmap_close"; DICT_SOCKMAP *dp = (DICT_SOCKMAP *) dict; if (dict_sockmap_handles == 0 || dict_sockmap_handles->used == 0) msg_panic("%s: attempt to close a non-existent map", myname); vstring_free(dp->rdwr_buf); myfree(dp->sockmap_name); if (--DICT_SOCKMAP_RH_REFCOUNT(dp->client_info) == 0) { auto_clnt_free(DICT_SOCKMAP_RH_HANDLE(dp->client_info)); htable_delete(dict_sockmap_handles, DICT_SOCKMAP_RH_NAME(dp->client_info), myfree); } if (dict->fold_buf) vstring_free(dict->fold_buf); dict_free(dict); }
void * st_th(void *arg) { int i, idx; uchar key[50] = { 0 }; int klen; uchar *val = NULL; int pre = 0; // struct hentry *he = NULL; uchar *oval; struct htable *ht; struct st_hlp *sh = (struct st_hlp *) arg; hashval_t hash; idx = sh->idx; ht = sh->ht; for (i = idx * NUMX; i < (idx + 1) * NUMX; i++) { hash = 0; sprintf((char *)key, "%dkey", i); val = malloc(50); sprintf((char *)val, "%dval", i); //printf("%d,%s,%s\n",idx,key,val); klen = strlen((const char *)key) + 1; pre = get_pre_mem_hash(key, klen, &hash); htable_insert(ht + pre, key, klen, A, val, 0, NULL, &hash); } if (idx == (THREADX - 1)) idx = -1; sleep(2); for (i = (idx + 1) * NUMX; i < (idx + 2) * NUMX; i++) { hash = 0; sprintf((char *)key, "%dkey", i); klen = strlen((const char *)key) + 1; pre = get_pre_mem_hash(key, klen, &hash); oval = htable_delete(ht + pre, key, klen, A, hash); if (oval == NULL) { printf("error in test %s,%d,%d\n", key, idx, i); } else free(oval); } sleep(5); return NULL; }
int main(int argc, char **argv) { int i, len, res; struct htable *table; table = htable_new(512, 0, &htable_int32_cmpfn, ©fn, &freefn); assert(table != NULL); len = sizeof(data)/sizeof(data[0]); for (i = 0; i < len; i++) { res = htable_add(table, 4, &data[i], NULL); assert(res == 1); assert(*(int*)table->entries[i]->key == data[i]); } htable_delete(table); return 0; }
int find_record_with_ttl(struct htable *ht, uchar * key, int klen, int type, uchar *val, int vlen, struct mvalue *md, hashval_t *hash) { int idx, ret; uchar *oval; idx = get_pre_mem_hash(key, klen, hash); ret = htable_find(ht + idx, key, klen, type, val, vlen, md, hash); if (ret > 0) { if (ttl_expired(val) == 1) { oval = htable_delete(ht + idx, key, klen, type, *hash); if (oval != NULL) free(oval); } else { return ret; } } return -1; }
int psc_dnsbl_retrieve(const char *client_addr, const char **dnsbl_name, int dnsbl_index, int *dnsbl_ttl) { const char *myname = "psc_dnsbl_retrieve"; PSC_DNSBL_SCORE *score; int result_score; int result_ttl; /* * Sanity check. */ if ((score = (PSC_DNSBL_SCORE *) htable_find(dnsbl_score_cache, client_addr)) == 0) msg_panic("%s: no blocklist score for %s", myname, client_addr); /* * Disable callbacks. */ PSC_CALL_BACK_CANCEL(score, dnsbl_index); /* * Reads are destructive. */ result_score = score->total; *dnsbl_name = score->dnsbl_name; result_ttl = (result_score > 0) ? score->fail_ttl : score->pass_ttl; /* As with dnsblog(8), a value < 0 means no reply TTL. */ if (result_ttl < var_psc_dnsbl_min_ttl) result_ttl = var_psc_dnsbl_min_ttl; if (result_ttl > var_psc_dnsbl_max_ttl) result_ttl = var_psc_dnsbl_max_ttl; *dnsbl_ttl = result_ttl; if (msg_verbose) msg_info("%s: addr=%s score=%d ttl=%d", myname, client_addr, result_score, result_ttl); score->refcount -= 1; if (score->refcount < 1) { if (msg_verbose > 1) msg_info("%s: delete blocklist score for %s", myname, client_addr); htable_delete(dnsbl_score_cache, client_addr, myfree); } return (result_score); }
static void anvil_remote_expire(int unused_event, char *context) { ANVIL_REMOTE *anvil_remote = (ANVIL_REMOTE *) context; const char *myname = "anvil_remote_expire"; if (msg_verbose) msg_info("%s %s", myname, anvil_remote->ident); if (anvil_remote->count != 0) msg_panic("%s: bad connection count: %d", myname, anvil_remote->count); htable_delete(anvil_remote_map, anvil_remote->ident, (void (*) (char *)) 0); ANVIL_REMOTE_FREE(anvil_remote); if (msg_verbose) msg_info("%s: anvil_remote_map used=%d", myname, anvil_remote_map->used); }
void mypwfree(struct mypasswd * mypwd) { if (mypwd->refcount < 1) msg_panic("mypwfree: refcount %d", mypwd->refcount); /* * See mypwenter() for the reason behind the binhash_locate() test. */ if (--mypwd->refcount == 0) { htable_delete(mypwcache_name, mypwd->pw_name, (void (*) (char *)) 0); if (binhash_locate(mypwcache_uid, (char *) &mypwd->pw_uid, sizeof(mypwd->pw_uid))) binhash_delete(mypwcache_uid, (char *) &mypwd->pw_uid, sizeof(mypwd->pw_uid), (void (*) (char *)) 0); myfree(mypwd->pw_name); myfree(mypwd->pw_passwd); myfree(mypwd->pw_gecos); myfree(mypwd->pw_dir); myfree(mypwd->pw_shell); myfree((char *) mypwd); } }
void qmgr_job_free(QMGR_JOB *job) { const char *myname = "qmgr_job_free"; QMGR_MESSAGE *message = job->message; QMGR_TRANSPORT *transport = job->transport; if (msg_verbose) msg_info("%s: %s %s", myname, message->queue_id, transport->name); /* * Sanity checks. */ if (job->rcpt_count) msg_panic("%s: non-zero recipient count (%d)", myname, job->rcpt_count); /* * Pop the job from the job stack if necessary. */ if (job->stack_level > 0) qmgr_job_pop(job); /* * Return any remaining recipient slots back to the recipient slots pool. */ qmgr_job_move_limits(job); if (job->rcpt_limit) msg_panic("%s: recipient slots leak (%d)", myname, job->rcpt_limit); /* * Unlink and discard the structure. Check if the job is still linked on * the job lists or if it was already retired before unlinking it. */ if (job->stack_level >= 0) qmgr_job_unlink(job); QMGR_LIST_UNLINK(message->job_list, QMGR_JOB *, job, message_peers); htable_delete(transport->job_byname, message->queue_id, (void (*) (void *)) 0); htable_free(job->peer_byname, (void (*) (void *)) 0); myfree((void *) job); }
void free_variables(void) { htable_delete(var_table); }