FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
mhash.h
Go to the documentation of this file.
1 /*
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. */
46 typedef struct
47 {
48  /* Vector or heap used to store keys. Hash table stores keys as byte
49  offsets into this vector. */
51 
52  /* Byte offsets of free keys in vector (used to store free keys when
53  n_key_bytes > 1). */
55 
57 
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
64 
65  /* Seed value for Jenkins hash. */
67 
68  /* Hash table mapping key -> value. */
70 
71  /* Format function for keys. */
73 } mhash_t;
74 
75 void mhash_init (mhash_t * h, uword n_value_bytes, uword n_key_bytes);
76 
77 always_inline void
78 mhash_init_c_string (mhash_t * h, uword n_value_bytes)
79 {
80  mhash_init (h, n_value_bytes, MHASH_C_STRING_KEY);
81 }
82 
83 always_inline void
84 mhash_init_vec_string (mhash_t * h, uword n_value_bytes)
85 {
86  mhash_init (h, n_value_bytes, MHASH_VEC_STRING_KEY);
87 }
88 
89 always_inline void *
91 {
92  if (key == ~0)
93  {
94  u8 *key_tmp;
95 
96  int my_cpu = os_get_cpu_number ();
97  vec_validate (h->key_tmps, my_cpu);
98  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 
104 hash_pair_t *mhash_get_pair (mhash_t * h, const void *key);
105 uword mhash_set_mem (mhash_t * h, void *key, uword * new_value,
106  uword * old_value);
107 uword mhash_unset (mhash_t * h, void *key, uword * old_value);
108 
110 mhash_get (mhash_t * h, const void *key)
111 {
112  hash_pair_t *p = mhash_get_pair (h, key);
113  return p ? &p->value[0] : 0;
114 }
115 
117 mhash_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 }
121 
123 mhash_unset_key (mhash_t * h, uword key, uword * old_value)
124 {
125  void *k = mhash_key_to_mem (h, key);
126  return mhash_unset (h, k, old_value);
127 }
128 
131 {
132  hash_t *h = hash_header (m->hash);
133  return hash_value_bytes (h);
134 }
135 
138 {
139  return hash_elts (m->hash);
140 }
141 
144 {
145  return h->n_key_bytes <= 1;
146 }
147 
148 always_inline void
150 {
151  if (mhash_key_vector_is_heap (h))
153  else
156  hash_free (h->hash);
157 }
158 
159 #define mhash_foreach(k,v,mh,body) \
160 do { \
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 
170 
171 #endif /* included_clib_mhash_h */
172 
173 /*
174  * fd.io coding-style-patch-verification: ON
175  *
176  * Local Variables:
177  * eval: (c-set-style "gnu")
178  * End:
179  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
Definition: mhash.h:46
static uword mhash_value_bytes(mhash_t *m)
Definition: mhash.h:130
hash_pair_t * mhash_get_pair(mhash_t *h, const void *key)
Definition: mhash.c:254
format_function_t * format_key
Definition: mhash.h:72
u32 n_key_bytes
Definition: mhash.h:63
static void mhash_init_vec_string(mhash_t *h, uword n_value_bytes)
Definition: mhash.h:84
static uword mhash_key_vector_is_heap(mhash_t *h)
Definition: mhash.h:143
#define heap_free(v)
Definition: heap.h:347
u32 hash_seed
Definition: mhash.h:66
uword value[0]
Definition: hash.h:164
u8 ** key_tmps
Definition: mhash.h:56
#define always_inline
Definition: clib.h:84
void mhash_init(mhash_t *h, uword n_value_bytes, uword n_key_bytes)
Definition: mhash.c:168
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static uword hash_value_bytes(hash_t *h)
Definition: hash.h:292
static hash_t * hash_header(void *v)
Definition: hash.h:110
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
#define hash_free(h)
Definition: hash.h:286
format_function_t format_mhash_key
Definition: mhash.h:169
static uword mhash_set(mhash_t *h, void *key, uword new_value, uword *old_value)
Definition: mhash.h:117
u32 * key_vector_free_indices
Definition: mhash.h:54
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:353
static void mhash_init_c_string(mhash_t *h, uword n_value_bytes)
Definition: mhash.h:78
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
static uword mhash_elts(mhash_t *m)
Definition: mhash.h:137
static uword hash_elts(void *v)
Definition: hash.h:117
unsigned int u32
Definition: types.h:88
u8 * key_vector_or_heap
Definition: mhash.h:50
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
static void mhash_free(mhash_t *h)
Definition: mhash.h:149
uword * hash
Definition: mhash.h:69
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
static uword mhash_unset_key(mhash_t *h, uword key, uword *old_value)
Definition: mhash.h:123
static void * mhash_key_to_mem(mhash_t *h, uword key)
Definition: mhash.h:90
#define MHASH_C_STRING_KEY
Definition: mhash.h:62
uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:271
#define MHASH_VEC_STRING_KEY
Definition: mhash.h:61