blob: 62aee365fa31efd5f9cea3f456efce9916d6ccb3 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef included_clib_mhash_h
16#define included_clib_mhash_h
17
18/*
19 Copyright (c) 2010 Eliot Dresselhaus
20
21 Permission is hereby granted, free of charge, to any person obtaining
22 a copy of this software and associated documentation files (the
23 "Software"), to deal in the Software without restriction, including
24 without limitation the rights to use, copy, modify, merge, publish,
25 distribute, sublicense, and/or sell copies of the Software, and to
26 permit persons to whom the Software is furnished to do so, subject to
27 the following conditions:
28
29 The above copyright notice and this permission notice shall be
30 included in all copies or substantial portions of the Software.
31
32 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39*/
40
41#include <vppinfra/format.h>
42#include <vppinfra/hash.h>
43#include <vppinfra/heap.h>
44
45/* Hash table plus vector of keys. */
Dave Barachc3799992016-08-15 11:12:27 -040046typedef struct
47{
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 /* Vector or heap used to store keys. Hash table stores keys as byte
49 offsets into this vector. */
Dave Barachc3799992016-08-15 11:12:27 -040050 u8 *key_vector_or_heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
52 /* Byte offsets of free keys in vector (used to store free keys when
53 n_key_bytes > 1). */
Dave Barachc3799992016-08-15 11:12:27 -040054 u32 *key_vector_free_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Dave Barachc3799992016-08-15 11:12:27 -040056 u8 **key_tmps;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
58 /* Possibly fixed size of key.
59 0 means keys are vectors of u8's.
60 1 means keys are null terminated c strings. */
61#define MHASH_VEC_STRING_KEY 0
62#define MHASH_C_STRING_KEY 1
63 u32 n_key_bytes;
64
65 /* Seed value for Jenkins hash. */
66 u32 hash_seed;
67
68 /* Hash table mapping key -> value. */
Dave Barachc3799992016-08-15 11:12:27 -040069 uword *hash;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
71 /* Format function for keys. */
Dave Barachc3799992016-08-15 11:12:27 -040072 format_function_t *format_key;
Ed Warnickecb9cada2015-12-08 15:45:58 -070073} mhash_t;
74
75void mhash_init (mhash_t * h, uword n_value_bytes, uword n_key_bytes);
76
77always_inline void
78mhash_init_c_string (mhash_t * h, uword n_value_bytes)
Dave Barachc3799992016-08-15 11:12:27 -040079{
80 mhash_init (h, n_value_bytes, MHASH_C_STRING_KEY);
81}
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83always_inline void
84mhash_init_vec_string (mhash_t * h, uword n_value_bytes)
Dave Barachc3799992016-08-15 11:12:27 -040085{
86 mhash_init (h, n_value_bytes, MHASH_VEC_STRING_KEY);
87}
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
89always_inline void *
90mhash_key_to_mem (mhash_t * h, uword key)
91{
92 if (key == ~0)
93 {
Dave Barachc3799992016-08-15 11:12:27 -040094 u8 *key_tmp;
95
Damjan Marionf55f9b82017-05-10 21:06:28 +020096 int my_cpu = os_get_thread_index ();
Dave Barachc3799992016-08-15 11:12:27 -040097 vec_validate (h->key_tmps, my_cpu);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 key_tmp = h->key_tmps[my_cpu];
99 return key_tmp;
100 }
101 return vec_elt_at_index (h->key_vector_or_heap, key);
102}
103
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200104hash_pair_t *mhash_get_pair (mhash_t * h, const void *key);
Dave Barachc3799992016-08-15 11:12:27 -0400105uword mhash_set_mem (mhash_t * h, void *key, uword * new_value,
106 uword * old_value);
107uword mhash_unset (mhash_t * h, void *key, uword * old_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109always_inline uword *
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200110mhash_get (mhash_t * h, const void *key)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111{
Dave Barachc3799992016-08-15 11:12:27 -0400112 hash_pair_t *p = mhash_get_pair (h, key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 return p ? &p->value[0] : 0;
114}
115
116always_inline uword
Dave Barachc3799992016-08-15 11:12:27 -0400117mhash_set (mhash_t * h, void *key, uword new_value, uword * old_value)
118{
119 return mhash_set_mem (h, key, &new_value, old_value);
120}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122always_inline uword
123mhash_unset_key (mhash_t * h, uword key, uword * old_value)
124{
Dave Barachc3799992016-08-15 11:12:27 -0400125 void *k = mhash_key_to_mem (h, key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 return mhash_unset (h, k, old_value);
127}
128
129always_inline uword
130mhash_value_bytes (mhash_t * m)
131{
Dave Barachc3799992016-08-15 11:12:27 -0400132 hash_t *h = hash_header (m->hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 return hash_value_bytes (h);
134}
135
136always_inline uword
137mhash_elts (mhash_t * m)
Dave Barachc3799992016-08-15 11:12:27 -0400138{
139 return hash_elts (m->hash);
140}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
142always_inline uword
143mhash_key_vector_is_heap (mhash_t * h)
Dave Barachc3799992016-08-15 11:12:27 -0400144{
145 return h->n_key_bytes <= 1;
146}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
148always_inline void
149mhash_free (mhash_t * h)
150{
151 if (mhash_key_vector_is_heap (h))
152 heap_free (h->key_vector_or_heap);
153 else
154 vec_free (h->key_vector_or_heap);
155 vec_free (h->key_vector_free_indices);
156 hash_free (h->hash);
157}
158
159#define mhash_foreach(k,v,mh,body) \
160do { \
161 hash_pair_t * _mhash_foreach_p; \
162 hash_foreach_pair (_mhash_foreach_p, (mh)->hash, ({ \
163 (k) = mhash_key_to_mem ((mh), _mhash_foreach_p->key); \
164 (v) = &_mhash_foreach_p->value[0]; \
165 body; \
166 })); \
167} while (0)
168
Vladislav Grishenko70328922023-12-14 17:48:28 +0100169u8 *format_mhash (u8 *s, va_list *va);
170
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171format_function_t format_mhash_key;
172
Vladislav Grishenko70328922023-12-14 17:48:28 +0100173/* Main test routine. */
174int test_mhash_main (unformat_input_t *input);
175
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176#endif /* included_clib_mhash_h */
Dave Barachc3799992016-08-15 11:12:27 -0400177
178/*
179 * fd.io coding-style-patch-verification: ON
180 *
181 * Local Variables:
182 * eval: (c-set-style "gnu")
183 * End:
184 */