1 Star 0 Fork 6

erci2047 / iceftpd

forked from 冰雪不语 / iceftpd 
Create your Gitee Account
Explore and code with more than 8 million developers,Free private repositories !:)
Sign up
Clone or Download
hash.c 2.65 KB
Copy Edit Web IDE Raw Blame History
冰雪不语 authored 2015-08-31 13:07 . ~
#include "hash.h"
#include "common.h"
#include <assert.h>
typedef struct hash_node {
void *key;
void *value;
struct hash_node *prev;
struct hash_node *next;
} hash_node_t;
struct hash {
unsigned int buckets;
hashfunc_t hash_func;
hash_node_t **nodes;
};
hash_node_t** hash_get_bucket(hash_t *hash, void *key);
hash_node_t* hash_get_node_by_key(hash_t *hash, void *key, unsigned int key_size);
hash_t *hash_alloc(unsigned int buckets, hashfunc_t hash_func)
{
hash_t *hash = (hash_t *)malloc(sizeof(hash_t));
//assert(hash != NULL);
hash->buckets = buckets;
hash->hash_func = hash_func;
int size = buckets * sizeof(hash_node_t *);
hash->nodes = (hash_node_t **)malloc(size);
memset(hash->nodes, 0, size);
return hash;
}
void* hash_lookup_entry(hash_t *hash, void* key, unsigned int key_size)
{
hash_node_t *node = hash_get_node_by_key(hash, key, key_size);
if (node == NULL) {
return NULL;
}
return node->value;
}
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 = 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_free_entry(hash_t *hash, void *key, unsigned int key_size)
{
hash_node_t *node = hash_get_node_by_key(hash, key, key_size);
if (node == NULL) {
return;
}
free(node->key);
free(node->value);
if (node->prev) {
node->prev->next = node->next;
} else {
hash_node_t **bucket = hash_get_bucket(hash, key);
*bucket = node->next;
}
if (node->next)
node->next->prev = node->prev;
free(node);
}
hash_node_t** hash_get_bucket(hash_t *hash, void *key)
{
unsigned int bucket = hash->hash_func(hash->buckets, key);
if (bucket >= hash->buckets) {
fprintf(stderr, "bad bucket lookup\n");
exit(EXIT_FAILURE);
}
return &(hash->nodes[bucket]);
}
hash_node_t* hash_get_node_by_key(hash_t *hash, void *key, unsigned int key_size)
{
hash_node_t **bucket = hash_get_bucket(hash, key);
hash_node_t *node = *bucket;
if (node == NULL) {
return NULL;
}
while (node != NULL && memcmp(node->key, key, key_size) != 0) {
node = node->next;
}
return node;
}

Comment ( 0 )

Sign in to post a comment

C
1
https://git.oschina.net/erci2047/iceftpd.git
git@git.oschina.net:erci2047/iceftpd.git
erci2047
iceftpd
iceftpd
master

Search

103111 552b83b3 1850385 103110 ed87a847 1850385