1 /*
2  * Copyright (c) 2008-2020, OARC, Inc.
3  * Copyright (c) 2007-2008, Internet Systems Consortium, Inc.
4  * Copyright (c) 2003-2007, The Measurement Factory, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #ifndef __dsc_hashtbl_h
38 #define __dsc_hashtbl_h
39 
40 #include <stddef.h>
41 #include <stdint.h>
42 
43 typedef struct _hashitem {
44     const void*       key;
45     void*             data;
46     struct _hashitem* next;
47 } hashitem;
48 
49 typedef unsigned int hashfunc(const void* key);
50 typedef int          hashkeycmp(const void* a, const void* b);
51 typedef void         hashfree(void* p);
52 
53 typedef struct
54 {
55     unsigned int modulus;
56     hashitem**   items;
57     hashfunc*    hasher;
58     hashkeycmp*  keycmp;
59     int          use_arena;
60     hashfree*    keyfree;
61     hashfree*    datafree;
62     struct
63     {
64         hashitem*    next;
65         unsigned int slot;
66     } iter;
67 } hashtbl;
68 
69 hashtbl* hash_create(int N, hashfunc*, hashkeycmp*, int use_arena, hashfree*, hashfree*);
70 void     hash_destroy(hashtbl*);
71 int      hash_add(const void* key, void* data, hashtbl*);
72 void     hash_remove(const void* key, hashtbl* tbl);
73 void*    hash_find(const void* key, hashtbl*);
74 void     hash_iter_init(hashtbl*);
75 void*    hash_iterate(hashtbl*);
76 
77 /*
78  * found in lookup3.c
79  */
80 extern uint32_t hashlittle(const void* key, size_t length, uint32_t initval);
81 extern uint32_t hashbig(const void* key, size_t length, uint32_t initval);
82 extern uint32_t hashword(const uint32_t* k, size_t length, uint32_t initval);
83 
84 #ifdef HAVE_ENDIAN_H
85 #include <endian.h>
86 #endif
87 #ifdef HAVE_SYS_ENDIAN_H
88 #include <sys/endian.h>
89 #endif
90 #ifdef HAVE_MACHINE_ENDIAN_H
91 #include <machine/endian.h>
92 #endif
93 
94 #ifndef __BYTE_ORDER
95 #if defined(BYTE_ORDER)
96 #define __BYTE_ORDER BYTE_ORDER
97 #elif defined(_BYTE_ORDER)
98 #define __BYTE_ORDER _BYTE_ORDER
99 #else
100 #error "No byte order define, please fix"
101 #endif
102 #endif
103 #ifndef __LITTLE_ENDIAN
104 #if defined(LITTLE_ENDIAN)
105 #define __LITTLE_ENDIAN LITTLE_ENDIAN
106 #elif defined(_LITTLE_ENDIAN)
107 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
108 #else
109 #error "No little endian define, please fix"
110 #endif
111 #endif
112 #ifndef __BIG_ENDIAN
113 #if defined(BIG_ENDIAN)
114 #define __BIG_ENDIAN BIG_ENDIAN
115 #elif defined(_BIG_ENDIAN)
116 #define __BIG_ENDIAN _BIG_ENDIAN
117 #else
118 #error "No big endian define, please fix"
119 #endif
120 #endif
121 
122 #if __BYTE_ORDER == __LITTLE_ENDIAN
123 #define hashendian hashlittle
124 #elif __BYTE_ORDER == __BIG_ENDIAN
125 #define hashendian hashbig
126 #else
127 #error "No byte order define, please fix"
128 #endif
129 
130 #endif /* __dsc_hashtbl_h */
131