1 /*
2 * %CopyrightBegin%
3 *
4 * Copyright Ericsson AB 1996-2016. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * %CopyrightEnd%
19 */
20
21 /**
22 * General hash functions
23 *
24 **/
25 #ifndef __HASH_H__
26 #define __HASH_H__
27
28 #include "sys.h"
29
30 typedef UWord HashValue;
31 typedef struct hash Hash;
32
33 typedef int (*HCMP_FUN)(void*, void*);
34 typedef HashValue (*H_FUN)(void*);
35 typedef void* (*HALLOC_FUN)(void*);
36 typedef void (*HFREE_FUN)(void*);
37 /* Meta functions */
38 typedef void* (*HMALLOC_FUN)(int,size_t);
39 typedef void (*HMFREE_FUN)(int,void*);
40 typedef int (*HMPRINT_FUN)(fmtfn_t,void*,char*, ...);
41 typedef void (*HFOREACH_FUN)(void *, void *);
42
43 /*
44 ** This bucket must be placed in top of
45 ** every object that uses hashing!!!
46 ** (Object*) == (Object*) &bucket
47 */
48 typedef struct hash_bucket
49 {
50 struct hash_bucket* next; /* Next bucket */
51 HashValue hvalue; /* Store hash value for get, rehash */
52 } HashBucket;
53
54 typedef struct hash_functions
55 {
56 H_FUN hash;
57 HCMP_FUN cmp;
58 HALLOC_FUN alloc;
59 HFREE_FUN free;
60 HMALLOC_FUN meta_alloc;
61 HMFREE_FUN meta_free;
62 HMPRINT_FUN meta_print;
63 } HashFunctions;
64
65 typedef struct {
66 char *name;
67 int size;
68 int used;
69 int objs;
70 int depth;
71 } HashInfo;
72
73 struct hash
74 {
75 HashFunctions fun; /* Function block */
76 int is_allocated; /* 0 iff hash structure is on stack or is static */
77 int meta_alloc_type; /* argument to pass to meta_alloc and meta_free */
78 char* name; /* Table name (static string, for debugging) */
79 int shift; /* How much to shift the hash value */
80 int max_shift; /* Never shift more than this value */
81 int shrink_threshold;
82 int grow_threshold;
83 int nobjs; /* Number of objects in table */
84 HashBucket** bucket; /* Vector of bucket pointers (objects) */
85 };
86
87 Hash* hash_new(int, char*, int, HashFunctions);
88 Hash* hash_init(int, Hash*, char*, int, HashFunctions);
89
90 void hash_delete(Hash*);
91 void hash_get_info(HashInfo*, Hash*);
92 void hash_info(fmtfn_t, void *, Hash*);
93 int hash_table_sz(Hash *);
94
95 void* hash_get(Hash*, void*);
96 void* hash_put(Hash*, void*);
97 void* hash_erase(Hash*, void*);
98 void* hash_remove(Hash*, void*);
99 void hash_foreach(Hash*, HFOREACH_FUN, void *);
100
101 ERTS_GLB_INLINE Uint hash_get_slot(Hash *h, HashValue hv);
102 ERTS_GLB_INLINE void* hash_fetch(Hash *, void*, H_FUN, HCMP_FUN);
103
104 #if ERTS_GLB_INLINE_INCL_FUNC_DEF
105
106 ERTS_GLB_INLINE Uint
hash_get_slot(Hash * h,HashValue hv)107 hash_get_slot(Hash *h, HashValue hv)
108 {
109 /* This slot mapping function uses fibonacci hashing in order to
110 * protect itself against a very bad hash function. This is not
111 * a hash function, so the user of hash.h should still spend time
112 * to figure out a good hash function for its data.
113 *
114 * See https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/
115 * for some thoughts and ideas about fibonacci hashing.
116 */
117
118 /* This is not strictly part of the fibonacci hashing algorithm
119 * but it does help to spread the values of the mapping function better.
120 */
121 hv ^= hv >> h->shift;
122 #ifdef ARCH_64
123 /* 2^64 / 1.61803398875 = 11400714819323198485.... */
124 return (UWORD_CONSTANT(11400714819323198485) * hv) >> h->shift;
125 #else
126 /* 2^32 / 1.61803398875 = 2654435769.... */
127 return (UWORD_CONSTANT(2654435769) * hv) >> h->shift;
128 #endif
129 }
130
hash_fetch(Hash * h,void * tmpl,H_FUN hash,HCMP_FUN cmp)131 ERTS_GLB_INLINE void* hash_fetch(Hash *h, void* tmpl, H_FUN hash, HCMP_FUN cmp)
132 {
133 HashValue hval = hash(tmpl);
134 Uint ix = hash_get_slot(h, hval);
135 HashBucket* b = h->bucket[ix];
136 ASSERT(h->fun.hash == hash);
137 ASSERT(h->fun.cmp == cmp);
138
139 while(b != (HashBucket*) 0) {
140 if ((b->hvalue == hval) && (cmp(tmpl, (void*)b) == 0))
141 return (void*) b;
142 b = b->next;
143 }
144 return (void*) 0;
145 }
146
147 #endif /* ERTS_GLB_INLINE_INCL_FUNC_DEF */
148
149 #endif
150