fix most of compilation warnings
This commit is contained in:
+50
-39
@@ -1,20 +1,19 @@
|
||||
/* =============================================================================
|
||||
* CDL (Configuration Definition Language) validator $Revision: 1.4 $
|
||||
* (C)2004-2007 Nikolai Nosov. All rights reserved.
|
||||
* (C)2004-2007 Nikolay Nosov. All rights reserved.
|
||||
*
|
||||
* File: $RCSfile: hash_multimap.c,v $
|
||||
* Purpose: Hash Multimap implementation
|
||||
* Written by: Nikolai Nosov nnosov@gmail.com
|
||||
* Written by: Nikolay Nosov
|
||||
* Last modified: $Date: 2008/06/21 12:15:53 $ by $Author: nnosov $.
|
||||
*
|
||||
* For more information please visit
|
||||
* http://nsoft.volgocity.ru/cdl or
|
||||
* http://cdl.sourceforge.net
|
||||
* ===========================================================================*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "hash_multimap.h"
|
||||
|
||||
/* Default hash function
|
||||
@@ -22,16 +21,19 @@
|
||||
@param size hash key size in bytes
|
||||
@return hash value, depends of the hash key
|
||||
*/
|
||||
static unsigned int default_hash_fn(const char *key, int size)
|
||||
static unsigned int default_hash_fn(const void *key, int size)
|
||||
{
|
||||
register int i, j;
|
||||
unsigned int cs = 0;
|
||||
unsigned int ps = 0;
|
||||
|
||||
if(key) {
|
||||
for(i = 0, j = 0; i < size; i++, j++) {
|
||||
ps |= ((unsigned int)key[i]) << (j * 8);
|
||||
if(j == 3 || j == (size - 1)) {
|
||||
if(key)
|
||||
{
|
||||
for(i = 0, j = 0; i < size; i++, j++)
|
||||
{
|
||||
ps |= ((unsigned int)((const char*)key)[i]) << (j * 8);
|
||||
if(j == 3 || j == (size - 1))
|
||||
{
|
||||
cs ^= ps;
|
||||
ps = 0;
|
||||
j = 0;
|
||||
@@ -42,12 +44,13 @@ static unsigned int default_hash_fn(const char *key, int size)
|
||||
return cs;
|
||||
}
|
||||
|
||||
static int default_compare_fn(const char *key1, const char *key2, const int size)
|
||||
static int default_compare_fn(const void *key1, const void *key2, const int size)
|
||||
{
|
||||
return !memcmp(key1, key2, size);
|
||||
}
|
||||
|
||||
/** Initialize hash node. Must be called before first usage of the node.
|
||||
|
||||
@param node pointer to the hash node descriptor
|
||||
*/
|
||||
void hash_node_init(struct hash_node *node)
|
||||
@@ -58,6 +61,7 @@ void hash_node_init(struct hash_node *node)
|
||||
}
|
||||
|
||||
/** Cleanup hash node
|
||||
|
||||
@param node pointer to the hash node descriptor
|
||||
*/
|
||||
void hash_node_cleanup(struct hash_node *node)
|
||||
@@ -69,6 +73,7 @@ void hash_node_cleanup(struct hash_node *node)
|
||||
}
|
||||
|
||||
/** Initialize hash table
|
||||
|
||||
@param map pointer to the hash table descriptor
|
||||
@param hash_shift size of the hash table in bits. Should be greater than
|
||||
HASH_MIN_SHIFT and less than HASH_MAX_SHIFT. In case of less than HASH_MIN_SHIFT,
|
||||
@@ -83,7 +88,8 @@ int hash_head_init(struct hash_head *map, unsigned int hash_shift, _hash_fn hf,
|
||||
{
|
||||
int i;
|
||||
|
||||
if(map) {
|
||||
if(map)
|
||||
{
|
||||
if(hash_shift <= HASH_MIN_SHIFT) {
|
||||
map->hash_shift = HASH_MIN_SHIFT;
|
||||
} else if(hash_shift >= HASH_MAX_SHIFT) {
|
||||
@@ -95,9 +101,11 @@ int hash_head_init(struct hash_head *map, unsigned int hash_shift, _hash_fn hf,
|
||||
map->hash_size = 1UL << map->hash_shift;
|
||||
map->hash_mask = (map->hash_size - 1);
|
||||
|
||||
map->bucket = (list_head*)malloc(sizeof(list_head) * map->hash_size);
|
||||
if(map->bucket) {
|
||||
for(uint16_t i = 0; i < map->hash_size; i++) {
|
||||
map->bucket = (struct list_head *)malloc(sizeof(struct list_head) * map->hash_size);
|
||||
if(map->bucket)
|
||||
{
|
||||
for(i = 0; i < map->hash_size; i++)
|
||||
{
|
||||
list_head_init(map->bucket + i);
|
||||
}
|
||||
|
||||
@@ -112,12 +120,12 @@ int hash_head_init(struct hash_head *map, unsigned int hash_shift, _hash_fn hf,
|
||||
} else {
|
||||
map->compare_fn = default_compare_fn;
|
||||
}
|
||||
} else {
|
||||
return ENOMEM;
|
||||
}
|
||||
} else {
|
||||
return ENODEV;
|
||||
else
|
||||
return ENOMEM;
|
||||
}
|
||||
else
|
||||
return ENODEV;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -132,10 +140,14 @@ void hash_head_cleanup(struct hash_head *map)
|
||||
struct hash_node *node;
|
||||
int i;
|
||||
|
||||
if(map) {
|
||||
if(map->bucket) {
|
||||
for(i = 0; i < map->hash_size; i++) {
|
||||
list_for_each_safe(p, n, map->bucket + i) {
|
||||
if(map)
|
||||
{
|
||||
if(map->bucket)
|
||||
{
|
||||
for(i = 0; i < map->hash_size; i++)
|
||||
{
|
||||
list_for_each_safe(p, n, map->bucket + i)
|
||||
{
|
||||
node = list_entry(p, struct hash_node, node);
|
||||
hash_node_cleanup(node);
|
||||
}
|
||||
@@ -160,12 +172,12 @@ struct hash_node* hash_find(struct hash_head *map, const void *key, int key_size
|
||||
struct hash_node *node;
|
||||
|
||||
list = map->bucket + (map->hash_fn(key, key_size) & map->hash_mask);
|
||||
list_for_each(n, list) {
|
||||
list_for_each(n, list)
|
||||
{
|
||||
node = list_entry(n, struct hash_node, node);
|
||||
if(key_size == node->key_size) {
|
||||
if(map->compare_fn(key, node->key, key_size)) {
|
||||
return node;
|
||||
}
|
||||
if(key_size == node->key_size)
|
||||
{
|
||||
if(map->compare_fn(key, node->key, key_size)) return node;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,9 +196,11 @@ struct hash_node* hash_find_next(struct hash_head *map, struct hash_node *hn1)
|
||||
struct hash_node *hn2;
|
||||
|
||||
|
||||
while((n = list_next(n)) != NULL) {
|
||||
while((n = list_next(n)) != NULL)
|
||||
{
|
||||
hn2 = list_entry(n, struct hash_node, node);
|
||||
if(hn1->key_size == hn2->key_size) {
|
||||
if(hn1->key_size == hn2->key_size)
|
||||
{
|
||||
if(map->compare_fn(hn1->key, hn2->key, hn2->key_size)) return hn2;
|
||||
}
|
||||
}
|
||||
@@ -203,10 +217,11 @@ struct hash_node* hash_find_next(struct hash_head *map, struct hash_node *hn1)
|
||||
|
||||
@return zero in case of success, ENOMEM in case of error
|
||||
*/
|
||||
int hash_add(struct hash_head *map, struct hash_node *node, void *key, int key_size)
|
||||
int hash_add(struct hash_head *map, struct hash_node *node, const void *key, int key_size)
|
||||
{
|
||||
node->key = (char*)malloc(key_size);
|
||||
if(node->key) {
|
||||
if(node->key)
|
||||
{
|
||||
memcpy(node->key, key, key_size);
|
||||
node->key_size = key_size;
|
||||
list_add_bottom(map->bucket + (map->hash_fn(key, key_size) & map->hash_mask), &node->node);
|
||||
@@ -249,7 +264,8 @@ struct hash_node * hash_node_first(struct hash_iterator *iter)
|
||||
iter->current = list_first(iter->map->bucket);
|
||||
iter->map_idx = 0;
|
||||
|
||||
if (iter->current) {
|
||||
if (iter->current)
|
||||
{
|
||||
return list_entry(iter->current, struct hash_node, node);
|
||||
}
|
||||
|
||||
@@ -263,14 +279,11 @@ struct hash_node * hash_node_first(struct hash_iterator *iter)
|
||||
*/
|
||||
struct hash_node * hash_node_next(struct hash_iterator *iter)
|
||||
{
|
||||
if(iter->map_idx >= iter->map->hash_size) {
|
||||
return NULL;
|
||||
}
|
||||
if(iter->map_idx >= iter->map->hash_size) return NULL;
|
||||
|
||||
if(iter->current) {
|
||||
iter->current = list_next(iter->current);
|
||||
}
|
||||
|
||||
if(iter->current) {
|
||||
return list_entry(iter->current, struct hash_node, node);
|
||||
}
|
||||
@@ -308,9 +321,7 @@ int hash_count(struct hash_head *head)
|
||||
int i;
|
||||
int count = 0;
|
||||
|
||||
for(i = 0; i < head->hash_size; i++) {
|
||||
count += list_count(head->bucket + i);
|
||||
}
|
||||
for(i = 0; i < head->hash_size; i++) count += list_count(head->bucket + i);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user