void hash_add_entry(hash_t *hash, void* key, unsigned int key_size, void *value, unsigned int value_size) { if (hash_lookup_entry(hash, key, key_size)) { fprintf(stderr, "duplicate hash key\n"); return; } hash_node_t *node = (hash_node_t*)malloc(sizeof(hash_node_t)); node->prev = NULL; node->next = NULL; node->key = malloc(key_size); memcpy(node->key, key, key_size); node->value = malloc(value_size); memcpy(node->value, value, value_size); hash_node_t **bucket = hash_get_bucket(hash, key); if (*bucket == NULL) { *bucket = node; } else { //头插法 将节点插入到链表头部 node->next = *bucket; (*bucket)->prev = node; *bucket = node; } }
void hash_add_entry(struct hash* p_hash, void* p_key, void* p_value) { struct hash_node** p_bucket; struct hash_node* p_new_node; if (hash_lookup_entry(p_hash, p_key)) { bug("duplicate hash key"); } p_bucket = hash_get_bucket(p_hash, p_key); p_new_node = vsf_sysutil_malloc(sizeof(*p_new_node)); p_new_node->p_prev = 0; p_new_node->p_next = 0; p_new_node->p_key = vsf_sysutil_malloc(p_hash->key_size); vsf_sysutil_memcpy(p_new_node->p_key, p_key, p_hash->key_size); p_new_node->p_value = vsf_sysutil_malloc(p_hash->value_size); vsf_sysutil_memcpy(p_new_node->p_value, p_value, p_hash->value_size); if (!*p_bucket) { *p_bucket = p_new_node; } else { p_new_node->p_next = *p_bucket; (*p_bucket)->p_prev = p_new_node; *p_bucket = p_new_node; } }
void hash_add_entry(hash_t *hash, void *key, unsigned int key_size, void *value, unsigned int value_size) { if (hash_lookup_entry(hash, key, key_size)) { fprintf(stderr, "duplicate hash key\n"); return; } unsigned int bucket = hash_get_bucket(hash, key); unsigned int i = bucket; while (hash->nodes[i].status == ACTIVE) { i = (i + 1) % hash->buckets; if (i == bucket) { return; // 没找到,并且表满 } } hash->nodes[i].status = ACTIVE; if (hash->nodes[i].key) { free(hash->nodes[i].key); } hash->nodes[i].key = malloc(key_size); memcpy(hash->nodes[i].key, key, key_size); if (hash->nodes[i].value) { free(hash->nodes[i].value); } hash->nodes[i].value = malloc(value_size); memcpy(hash->nodes[i].value, value, value_size); }
int main(void) { stu2_t stu_arr[] = { { 1234, "AAAA", 20 }, { 4568, "BBBB", 23 }, { 6729, "AAAA", 19 } }; hash_t *hash = hash_alloc(256, hash_int); int size = sizeof(stu_arr) / sizeof(stu_arr[0]); int i; for (i=0; i<size; i++) { hash_add_entry(hash, &(stu_arr[i].sno), sizeof(stu_arr[i].sno), &stu_arr[i], sizeof(stu_arr[i])); } int sno = 4568; stu2_t *s = (stu2_t *)hash_lookup_entry(hash, &sno, sizeof(sno)); if (s) { printf("%d %s %d\n", s->sno, s->name, s->age); } else { printf("not found\n"); } sno = 1234; hash_free_entry(hash, &sno, sizeof(sno)); s = (stu2_t *)hash_lookup_entry(hash, &sno, sizeof(sno)); if (s) { printf("%d %s %d\n", s->sno, s->name, s->age); } else { printf("not found\n"); } return 0; }
/* Liefert NULL wenn nicht gefunden. * Falls ein default zurueckgegeben wird, stimmt "tuple" nicht mit * entry->tuple ueberein. */ const Entry* symbol_lookup_entry(const Symbol* sym, const Tuple* tuple) { const Entry* entry; assert(symbol_is_valid(sym)); assert(tuple_is_valid(tuple)); entry = hash_lookup_entry(sym->hash, tuple); if (NULL == entry && sym->deflt != NULL && set_lookup(sym->set, tuple)) entry = sym->deflt; return entry; }
void handle_sigchld(int sig) { pid_t pid; // 如果多个子进程同时断开连接 while ((pid = waitpid(-1 ,NULL, WNOHANG)) > 0) { unsigned int* ip = hash_lookup_entry(s_pid_ip_hash, &pid, sizeof(pid)); if (ip == NULL) { continue; } drop_ip_count(ip); --cur_childrens; hash_free_entry(s_pid_ip_hash, &pid, sizeof(pid)); } }
static unsigned int handle_ip_count(struct vsf_sysutil_ipv4addr* p_accept_addr) { unsigned int* p_count = (unsigned int*)hash_lookup_entry(s_p_ip_count_hash, (void*)p_accept_addr); unsigned int count; if (!p_count) { count = 1; hash_add_entry(s_p_ip_count_hash, (void*)p_accept_addr, (void*)&count); } else { count = *p_count; count++; *p_count = count; } return count; }
static unsigned int handle_ip_count(void* p_ipaddr) { unsigned int* p_count = (unsigned int*)hash_lookup_entry(s_p_ip_count_hash, p_ipaddr); unsigned int count; if (!p_count) { count = 1; hash_add_entry(s_p_ip_count_hash, p_ipaddr, (void*)&count); } else { count = *p_count; count++; *p_count = count; } return count; }
unsigned int handle_ip_count(void *ip) { unsigned int count; unsigned int* ip_count; ip_count = hash_lookup_entry(s_ip_count_hash, ip, sizeof(unsigned int)); if (ip_count == NULL) { count = 1; hash_add_entry(s_ip_count_hash, ip, sizeof(unsigned int), &count, sizeof(unsigned int)); } else { count = *ip_count; count++; *ip_count = count; } return count; }
static void handle_sigchld(void* p_private) { unsigned int reap_one = 1; (void) p_private; while (reap_one) { reap_one = (unsigned int)vsf_sysutil_wait_reap_one(); if (reap_one) { struct vsf_sysutil_ipv4addr* p_ip; /* Account total number of instances */ --s_children; /* Account per-IP limit */ p_ip = (struct vsf_sysutil_ipv4addr*) hash_lookup_entry(s_p_pid_ip_hash, (void*)&reap_one); drop_ip_count(p_ip); hash_free_entry(s_p_pid_ip_hash, (void*)&reap_one); } } }
void drop_ip_count(void* ip) { unsigned int count; unsigned int* ip_count; ip_count = hash_lookup_entry(s_ip_count_hash, ip, sizeof(unsigned int)); if (ip_count == NULL) { return; } count = *ip_count; count--; if (count == 0) { hash_free_entry(s_ip_count_hash, ip, sizeof(unsigned int)); } *ip_count = count; }
static void drop_ip_count(struct vsf_sysutil_ipv4addr* p_ip) { unsigned int count; unsigned int* p_count = (unsigned int*)hash_lookup_entry(s_p_ip_count_hash, (void*)p_ip); if (!p_count) { bug("IP address missing from hash"); } count = *p_count; if (!count) { bug("zero count for IP address"); } count--; *p_count = count; if (!count) { hash_free_entry(s_p_ip_count_hash, (void*)p_ip); } }
static void drop_ip_count(void* p_raw_addr) { unsigned int count; unsigned int* p_count = (unsigned int*)hash_lookup_entry(s_p_ip_count_hash, p_raw_addr); if (!p_count) { bug("IP address missing from hash"); } count = *p_count; if (!count) { bug("zero count for IP address"); } count--; *p_count = count; if (!count) { hash_free_entry(s_p_ip_count_hash, p_raw_addr); } }
static void handle_sigchld(int duff) { unsigned int reap_one = 1; (void) duff; while (reap_one) { reap_one = (unsigned int)vsf_sysutil_wait_reap_one(); if (reap_one) { struct vsf_sysutil_ipaddr* p_ip; /* Account total number of instances */ --s_children; /* Account per-IP limit */ p_ip = (struct vsf_sysutil_ipaddr*) hash_lookup_entry(s_p_pid_ip_hash, (void*)&reap_one); /* Kitsune - old connections are not in the hash tables */ if (p_ip) { drop_ip_count(p_ip); hash_free_entry(s_p_pid_ip_hash, (void*)&reap_one); } } } }