Home | History | Annotate | Line # | Download | only in omapip
      1 /*	$NetBSD: hash.h,v 1.3 2022/04/03 01:10:59 christos Exp $	*/
      2 
      3 /* hash.h
      4 
      5    Definitions for hashing... */
      6 
      7 /*
      8  * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
      9  * Copyright (c) 1995-2003 by Internet Software Consortium
     10  *
     11  * This Source Code Form is subject to the terms of the Mozilla Public
     12  * License, v. 2.0. If a copy of the MPL was not distributed with this
     13  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     16  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     17  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     18  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     20  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     21  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     22  *
     23  *   Internet Systems Consortium, Inc.
     24  *   PO Box 360
     25  *   Newmarket, NH 03857 USA
     26  *   <info (at) isc.org>
     27  *   https://www.isc.org/
     28  *
     29  */
     30 
     31 #ifndef OMAPI_HASH_H
     32 #define OMAPI_HASH_H
     33 
     34 #if !defined (DEFAULT_HASH_SIZE)
     35 # define DEFAULT_HASH_SIZE	9973
     36 #endif
     37 
     38 #if !defined (KEY_HASH_SIZE)
     39 # define KEY_HASH_SIZE		1009
     40 #endif
     41 
     42 /* The purpose of the hashed_object_t struct is to not match anything else. */
     43 typedef struct {
     44 	int foo;
     45 } hashed_object_t;
     46 
     47 typedef isc_result_t (*hash_foreach_func)(const void *, unsigned, void *);
     48 typedef int (*hash_reference) (hashed_object_t **, hashed_object_t *,
     49 			       const char *, int);
     50 typedef int (*hash_dereference) (hashed_object_t **, const char *, int);
     51 
     52 struct hash_bucket {
     53 	struct hash_bucket *next;
     54 	const unsigned char *name;
     55 	unsigned len;
     56 	hashed_object_t *value;
     57 };
     58 
     59 typedef int (*hash_comparator_t)(const void *, const void *, size_t);
     60 
     61 struct hash_table {
     62 	unsigned hash_count;
     63 	hash_reference referencer;
     64 	hash_dereference dereferencer;
     65 	hash_comparator_t cmp;
     66 	unsigned (*do_hash)(const void *, unsigned, unsigned);
     67 
     68 	/* This must remain the last entry in this table. */
     69 	struct hash_bucket *buckets [1];
     70 };
     71 
     72 struct named_hash {
     73 	struct named_hash *next;
     74 	const char *name;
     75 	struct hash_table *hash;
     76 };
     77 
     78 #define HASH_FUNCTIONS_DECL(name, bufarg, type, hashtype)		      \
     79 void name##_hash_add (hashtype *, bufarg, unsigned, type *,		      \
     80 		      const char *, int);				      \
     81 void name##_hash_delete (hashtype *, bufarg, unsigned,			      \
     82 			 const char *, int);				      \
     83 int name##_hash_lookup (type **, hashtype *, bufarg, unsigned,		      \
     84 			const char *, int);				      \
     85 unsigned char * name##_hash_report(hashtype *);				      \
     86 int name##_hash_foreach (hashtype *, hash_foreach_func);		      \
     87 int name##_new_hash (hashtype **, unsigned, const char *, int);		      \
     88 void name##_free_hash_table (hashtype **, const char *, int);
     89 
     90 
     91 #define HASH_FUNCTIONS(name, bufarg, type, hashtype, ref, deref, hasher)      \
     92 void name##_hash_add (hashtype *table,					      \
     93 		      bufarg buf, unsigned len, type *ptr,		      \
     94 		      const char *file, int line)			      \
     95 {									      \
     96 	add_hash ((struct hash_table *)table, buf,			      \
     97 		  len, (hashed_object_t *)ptr, file, line);		      \
     98 }									      \
     99 									      \
    100 void name##_hash_delete (hashtype *table, bufarg buf, unsigned len,	      \
    101 			 const char *file, int line)			      \
    102 {									      \
    103 	delete_hash_entry ((struct hash_table *)table, buf, len,	      \
    104 			   file, line);					      \
    105 }									      \
    106 									      \
    107 int name##_hash_lookup (type **ptr, hashtype *table,			      \
    108 			bufarg buf, unsigned len, const char *file, int line) \
    109 {									      \
    110 	return hash_lookup ((hashed_object_t **)ptr,			      \
    111 			    (struct hash_table *)table,			      \
    112 			    buf, len, file, line);			      \
    113 }									      \
    114 									      \
    115 unsigned char * name##_hash_report(hashtype *table)			      \
    116 {									      \
    117 	return hash_report((struct hash_table *)table);			      \
    118 }									      \
    119 									      \
    120 int name##_hash_foreach (hashtype *table, hash_foreach_func func)	      \
    121 {									      \
    122 	return hash_foreach ((struct hash_table *)table,		      \
    123 			     func);					      \
    124 }									      \
    125 									      \
    126 int name##_new_hash (hashtype **tp, unsigned c, const char *file, int line)   \
    127 {									      \
    128 	return new_hash ((struct hash_table **)tp,			      \
    129 			 (hash_reference)ref, (hash_dereference)deref, c,     \
    130 			 hasher, file, line);				      \
    131 }									      \
    132 									      \
    133 void name##_free_hash_table (hashtype **table, const char *file, int line)    \
    134 {									      \
    135 	free_hash_table ((struct hash_table **)table, file, line);	      \
    136 }
    137 
    138 void relinquish_hash_bucket_hunks (void);
    139 int new_hash_table (struct hash_table **, unsigned, const char *, int);
    140 void free_hash_table (struct hash_table **, const char *, int);
    141 struct hash_bucket *new_hash_bucket (const char *, int);
    142 void free_hash_bucket (struct hash_bucket *, const char *, int);
    143 int new_hash(struct hash_table **,
    144 	     hash_reference, hash_dereference, unsigned,
    145 	     unsigned (*do_hash)(const void *, unsigned, unsigned),
    146 	     const char *, int);
    147 unsigned do_string_hash(const void *, unsigned, unsigned);
    148 unsigned do_case_hash(const void *, unsigned, unsigned);
    149 unsigned do_id_hash(const void *, unsigned, unsigned);
    150 unsigned do_number_hash(const void *, unsigned, unsigned);
    151 unsigned do_ip4_hash(const void *, unsigned, unsigned);
    152 unsigned char *hash_report(struct hash_table *);
    153 void add_hash (struct hash_table *,
    154 		      const void *, unsigned, hashed_object_t *,
    155 		      const char *, int);
    156 void delete_hash_entry (struct hash_table *, const void *,
    157 			       unsigned, const char *, int);
    158 int hash_lookup (hashed_object_t **, struct hash_table *,
    159 			const void *, unsigned, const char *, int);
    160 int hash_foreach (struct hash_table *, hash_foreach_func);
    161 int casecmp (const void *s, const void *t, size_t len);
    162 
    163 #endif /* OMAPI_HASH_H */
    164