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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef included_clib_mhash_h
16 #define included_clib_mhash_h
19 Copyright (c) 2010 Eliot Dresselhaus
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:
29 The above copyright notice and this permission notice shall be
30 included in all copies or substantial portions of the Software.
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.
41 #include <vppinfra/format.h>
42 #include <vppinfra/hash.h>
43 #include <vppinfra/heap.h>
45 /* Hash table plus vector of keys. */
47 /* Vector or heap used to store keys. Hash table stores keys as byte
48 offsets into this vector. */
49 u8 * key_vector_or_heap;
51 /* Byte offsets of free keys in vector (used to store free keys when
53 u32 * key_vector_free_indices;
57 /* Possibly fixed size of key.
58 0 means keys are vectors of u8's.
59 1 means keys are null terminated c strings. */
60 #define MHASH_VEC_STRING_KEY 0
61 #define MHASH_C_STRING_KEY 1
64 /* Seed value for Jenkins hash. */
67 /* Hash table mapping key -> value. */
70 /* Format function for keys. */
71 format_function_t * format_key;
74 void mhash_init (mhash_t * h, uword n_value_bytes, uword n_key_bytes);
77 mhash_init_c_string (mhash_t * h, uword n_value_bytes)
78 { mhash_init (h, n_value_bytes, MHASH_C_STRING_KEY); }
81 mhash_init_vec_string (mhash_t * h, uword n_value_bytes)
82 { mhash_init (h, n_value_bytes, MHASH_VEC_STRING_KEY); }
85 mhash_key_to_mem (mhash_t * h, uword key)
91 int my_cpu = os_get_cpu_number();
92 vec_validate(h->key_tmps, my_cpu);
93 key_tmp = h->key_tmps[my_cpu];
96 return vec_elt_at_index (h->key_vector_or_heap, key);
99 hash_pair_t * mhash_get_pair (mhash_t * h, void * key);
100 uword mhash_set_mem (mhash_t * h, void * key, uword * new_value, uword * old_value);
101 uword mhash_unset (mhash_t * h, void * key, uword * old_value);
103 always_inline uword *
104 mhash_get (mhash_t * h, void * key)
106 hash_pair_t * p = mhash_get_pair (h, key);
107 return p ? &p->value[0] : 0;
111 mhash_set (mhash_t * h, void * key, uword new_value, uword * old_value)
112 { return mhash_set_mem (h, key, &new_value, old_value); }
115 mhash_unset_key (mhash_t * h, uword key, uword * old_value)
117 void * k = mhash_key_to_mem (h, key);
118 return mhash_unset (h, k, old_value);
122 mhash_value_bytes (mhash_t * m)
124 hash_t * h = hash_header (m->hash);
125 return hash_value_bytes (h);
129 mhash_elts (mhash_t * m)
130 { return hash_elts (m->hash); }
133 mhash_key_vector_is_heap (mhash_t * h)
134 { return h->n_key_bytes <= 1; }
137 mhash_free (mhash_t * h)
139 if (mhash_key_vector_is_heap (h))
140 heap_free (h->key_vector_or_heap);
142 vec_free (h->key_vector_or_heap);
143 vec_free (h->key_vector_free_indices);
147 #define mhash_foreach(k,v,mh,body) \
149 hash_pair_t * _mhash_foreach_p; \
150 hash_foreach_pair (_mhash_foreach_p, (mh)->hash, ({ \
151 (k) = mhash_key_to_mem ((mh), _mhash_foreach_p->key); \
152 (v) = &_mhash_foreach_p->value[0]; \
157 format_function_t format_mhash_key;
159 #endif /* included_clib_mhash_h */