Index: client/attribute.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/attribute.c,v retrieving revision 1.11 diff -u -r1.11 attribute.c --- client/attribute.c 2002/05/25 17:44:05 1.11 +++ client/attribute.c 2002/06/07 16:36:53 @@ -11,7 +11,7 @@ #include "attribute.h" -#define ATTRIBUTE_LOG_LEVEL LOG_DEBUG +#define ATTRIBUTE_LOG_LEVEL LOG_NORMAL static struct hash_table *attribute_hash = NULL; @@ -26,8 +26,11 @@ unsigned int num_buckets) { const struct attr_key *pkey = (const struct attr_key *) key; - - return (pkey->id ^ pkey->x ^ pkey->y ^ pkey->key) % num_buckets; + unsigned int result = + (pkey->id ^ pkey->x ^ pkey->y ^ pkey->key) % num_buckets; + freelog(LOG_NORMAL, "hash(key=%d, id=%d, x=%d, y=%d) and buckets=%d = %d", + pkey->key, pkey->id, pkey->x, pkey->y, num_buckets, result); + return result; } /**************************************************************************** @@ -35,7 +38,16 @@ *****************************************************************************/ static int attr_hash_cmp_fn(const void *key1, const void *key2) { - return memcmp(key1, key2, sizeof(struct attr_key)); + int result = memcmp(key1, key2, sizeof(struct attr_key)); + const struct attr_key *pkey1 = (const struct attr_key *) key1; + const struct attr_key *pkey2 = (const struct attr_key *) key2; + + + freelog(LOG_NORMAL, + "cmp(key1=(key=%d, id=%d, x=%d, y=%d), key2=(key=%d, id=%d, x=%d, y=%d))=%d", + pkey1->key, pkey1->id, pkey1->x, pkey1->y, pkey2->key, pkey2->id, + pkey2->x, pkey2->y,result); + return result; } /**************************************************************************** @@ -44,6 +56,8 @@ void attribute_init() { assert(attribute_hash == NULL); + /* If there is padding memcmp can't be used. */ + assert(sizeof(struct attr_key) == 4 * sizeof(int)); attribute_hash = hash_new(attr_hash_val_fn, attr_hash_cmp_fn); } @@ -259,7 +273,7 @@ if (data_length != 0) { pvalue = fc_malloc(data_length + sizeof(int)); ((int *) pvalue)[0] = data_length; - memcpy((char *)pvalue + sizeof(int), data, data_length); + memcpy(ADD_TO_POINTER(pvalue, sizeof(int)), data, data_length); } if (hash_key_exists(attribute_hash, pkey)) { @@ -301,6 +315,21 @@ if (!pvalue) { freelog(ATTRIBUTE_LOG_LEVEL, " not found"); + { + int i; + + for (i = 0; i < hash_num_entries(attribute_hash); i++) { + const struct attr_key *pkey = hash_key_by_number(attribute_hash, i); + + pvalue = hash_lookup_data(attribute_hash, pkey); + assert(pvalue); + length = ((int *) pvalue)[0]; + + freelog(LOG_NORMAL, + " [%d], key=(key=%d, id=%d, x=%d, y=%d) value_len=%d", i, + pkey->key, pkey->id, pkey->x, pkey->y, length); + } + } return 0; } @@ -320,7 +349,7 @@ exit(EXIT_FAILURE); } - memcpy(data, (char *)pvalue + sizeof(int), length); + memcpy(data, ADD_TO_POINTER(pvalue, sizeof(int)), length); freelog(ATTRIBUTE_LOG_LEVEL, " found length=%d", length); return length;