00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef OMAPI_HASH_H
00030 #define OMAPI_HASH_H
00031
00032 #if !defined (DEFAULT_HASH_SIZE)
00033 # define DEFAULT_HASH_SIZE 9973
00034 #endif
00035
00036 #if !defined (KEY_HASH_SIZE)
00037 # define KEY_HASH_SIZE 1009
00038 #endif
00039
00040
00041 typedef struct {
00042 int foo;
00043 } hashed_object_t;
00044
00045 typedef isc_result_t (*hash_foreach_func)(const void *, unsigned, void *);
00046 typedef int (*hash_reference) (hashed_object_t **, hashed_object_t *,
00047 const char *, int);
00048 typedef int (*hash_dereference) (hashed_object_t **, const char *, int);
00049
00050 struct hash_bucket {
00051 struct hash_bucket *next;
00052 const unsigned char *name;
00053 unsigned len;
00054 hashed_object_t *value;
00055 };
00056
00057 typedef int (*hash_comparator_t)(const void *, const void *, size_t);
00058
00059 struct hash_table {
00060 unsigned hash_count;
00061 hash_reference referencer;
00062 hash_dereference dereferencer;
00063 hash_comparator_t cmp;
00064 unsigned (*do_hash)(const void *, unsigned, unsigned);
00065
00066
00067 struct hash_bucket *buckets [1];
00068 };
00069
00070 struct named_hash {
00071 struct named_hash *next;
00072 const char *name;
00073 struct hash_table *hash;
00074 };
00075
00076 #define HASH_FUNCTIONS_DECL(name, bufarg, type, hashtype) \
00077 void name##_hash_add (hashtype *, bufarg, unsigned, type *, \
00078 const char *, int); \
00079 void name##_hash_delete (hashtype *, bufarg, unsigned, \
00080 const char *, int); \
00081 int name##_hash_lookup (type **, hashtype *, bufarg, unsigned, \
00082 const char *, int); \
00083 unsigned char * name##_hash_report(hashtype *); \
00084 int name##_hash_foreach (hashtype *, hash_foreach_func); \
00085 int name##_new_hash (hashtype **, unsigned, const char *, int); \
00086 void name##_free_hash_table (hashtype **, const char *, int);
00087
00088
00089 #define HASH_FUNCTIONS(name, bufarg, type, hashtype, ref, deref, hasher) \
00090 void name##_hash_add (hashtype *table, \
00091 bufarg buf, unsigned len, type *ptr, \
00092 const char *file, int line) \
00093 { \
00094 add_hash ((struct hash_table *)table, buf, \
00095 len, (hashed_object_t *)ptr, file, line); \
00096 } \
00097 \
00098 void name##_hash_delete (hashtype *table, bufarg buf, unsigned len, \
00099 const char *file, int line) \
00100 { \
00101 delete_hash_entry ((struct hash_table *)table, buf, len, \
00102 file, line); \
00103 } \
00104 \
00105 int name##_hash_lookup (type **ptr, hashtype *table, \
00106 bufarg buf, unsigned len, const char *file, int line) \
00107 { \
00108 return hash_lookup ((hashed_object_t **)ptr, \
00109 (struct hash_table *)table, \
00110 buf, len, file, line); \
00111 } \
00112 \
00113 unsigned char * name##_hash_report(hashtype *table) \
00114 { \
00115 return hash_report((struct hash_table *)table); \
00116 } \
00117 \
00118 int name##_hash_foreach (hashtype *table, hash_foreach_func func) \
00119 { \
00120 return hash_foreach ((struct hash_table *)table, \
00121 func); \
00122 } \
00123 \
00124 int name##_new_hash (hashtype **tp, unsigned c, const char *file, int line) \
00125 { \
00126 return new_hash ((struct hash_table **)tp, \
00127 (hash_reference)ref, (hash_dereference)deref, c, \
00128 hasher, file, line); \
00129 } \
00130 \
00131 void name##_free_hash_table (hashtype **table, const char *file, int line) \
00132 { \
00133 free_hash_table ((struct hash_table **)table, file, line); \
00134 }
00135
00136 void relinquish_hash_bucket_hunks (void);
00137 int new_hash_table (struct hash_table **, unsigned, const char *, int);
00138 void free_hash_table (struct hash_table **, const char *, int);
00139 struct hash_bucket *new_hash_bucket (const char *, int);
00140 void free_hash_bucket (struct hash_bucket *, const char *, int);
00141 int new_hash(struct hash_table **,
00142 hash_reference, hash_dereference, unsigned,
00143 unsigned (*do_hash)(const void *, unsigned, unsigned),
00144 const char *, int);
00145 unsigned do_string_hash(const void *, unsigned, unsigned);
00146 unsigned do_case_hash(const void *, unsigned, unsigned);
00147 unsigned do_id_hash(const void *, unsigned, unsigned);
00148 unsigned do_number_hash(const void *, unsigned, unsigned);
00149 unsigned do_ip4_hash(const void *, unsigned, unsigned);
00150 unsigned char *hash_report(struct hash_table *);
00151 void add_hash (struct hash_table *,
00152 const void *, unsigned, hashed_object_t *,
00153 const char *, int);
00154 void delete_hash_entry (struct hash_table *, const void *,
00155 unsigned, const char *, int);
00156 int hash_lookup (hashed_object_t **, struct hash_table *,
00157 const void *, unsigned, const char *, int);
00158 int hash_foreach (struct hash_table *, hash_foreach_func);
00159 int casecmp (const void *s, const void *t, size_t len);
00160
00161 #endif