hash_table * hash_create(size_t size) { size_t i; hash_table *ht = (hash_table *)calloc(1, sizeof(hash_table)); if (NULL == ht) { perror("can't calloc memory!"); log_info(LOG_ERR, "can't calloc memory for hash table:%s", strerror(errno)); sync(); exit(errno); } ht->size = size; ht->lists = (link_list **) calloc(size, sizeof(link_list *)); if (NULL == ht->lists) { perror("can't calloc memory!"); log_info(LOG_ERR, "can't calloc memory for hash lists:%s", strerror(errno)); sync(); exit(errno); } for (i=0; i < size; i++) { ht->lists[i] = link_list_create(); } ht->timeout = DEFAULT_TIMEOUT; return ht; }
int link_list_test_main(int argc, char **argv) { printf("----------------------------------------------\n"); printf("starts to have a test of list.\n"); printf("----------------------------------------------\n"); link_list *list = link_list_create(); link_list_insert_first(list, 1); link_list_insert_first(list, 2); link_list_insert_first(list, 3); link_list_insert_first(list, 4); link_list_append(list, 5); link_list_append(list, 6); link_list_delete(list, list->tail->prev->prev); link_list_insert_after(list, list->tail->prev->prev, 10); link_list_insert_before(list, list->tail->prev->prev, 9); link_list_traverse(list); /* printf("another is %d.\n", list->tail->prev->prev->next->data); */ printf("ok, now length is %d.\n", list->length); printf("----------------------------------------------\n"); printf("test is finished. everything is gone be alright.\n"); printf("----------------------------------------------\n"); printf("\n"); return EXIT_SUCCESS; }
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; }
void delay_table_add(uint64_t key, struct msg_server_s *msg) { tc_pool_t *pool; p_link_node ln; delay_sess_t *s; struct msg_server_s *cmsg; s = (delay_sess_t *) hash_find(table, key); if (s == NULL) { pool = tc_create_pool(TC_DEFAULT_POOL_SIZE, 0); if (pool != NULL) { s = (delay_sess_t *) tc_pcalloc(pool, sizeof(delay_sess_t)); if (s != NULL) { s->key = key; s->pool = pool; s->msg_list = link_list_create(s->pool); s->evt = tc_event_add_timer(s->pool, OUTPUT_INTERVAL, s, tc_delay_del_obs); msg_ls_cnt++; hash_add(table, s->pool, key, s); } else { return; } } else { return; } } cmsg = copy_message(s->pool, msg); if (cmsg != NULL) { ln = link_node_malloc(s->pool, (void *) cmsg); link_list_append(s->msg_list, ln); msg_item_cnt++; } }
/* add message to delay table */ void delay_table_add(uint64_t key, struct msg_server_s *msg) { link_list *msg_list; p_link_node ln; struct msg_server_s *cmsg; msg_list = (link_list *) hash_find(table, key); if (msg_list == NULL) { msg_ls_cnt++; msg_list = link_list_create(); hash_add(table, key, msg_list); } cmsg = copy_message(msg); if (cmsg != NULL) { ln = link_node_malloc((void *) cmsg); link_list_append(msg_list, ln); msg_item_cnt++; } return; }
hash_table * hash_create(tc_pool_t *pool, uint32_t size) { size_t i; hash_table *ht = (hash_table *) tc_pcalloc(pool, sizeof(hash_table)); if (ht != NULL) { ht->pool = pool; ht->size = size; ht->lists = (link_list **) tc_pcalloc(pool, size * sizeof(link_list *)); if (ht->lists != NULL) { for (i = 0; i < size; i++) { ht->lists[i] = link_list_create(pool); } } else { tc_log_info(LOG_ERR, errno, "can't calloc memory for hash lists"); ht = NULL; } } else { tc_log_info(LOG_ERR, errno, "can't calloc memory for hash table"); } return ht; }