tor  0.4.2.1-alpha-dev
fp_pair.c
Go to the documentation of this file.
1 /* Copyright (c) 2013-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
20 #include "core/or/or.h"
22 
23 /* Define fp_pair_map_t structures */
24 
26  HT_ENTRY(fp_pair_map_entry_s) node;
27  void *val;
28  fp_pair_t key;
29 };
30 
31 struct fp_pair_map_s {
32  HT_HEAD(fp_pair_map_impl, fp_pair_map_entry_s) head;
33 };
34 
35 /*
36  * Hash function and equality checker for fp_pair_map_t
37  */
38 
40 static inline int
42  const fp_pair_map_entry_t *b)
43 {
44  return tor_memeq(&(a->key), &(b->key), sizeof(fp_pair_t));
45 }
46 
48 static inline unsigned int
50 {
51  tor_assert(sizeof(a->key) == DIGEST_LEN*2);
52  return (unsigned) siphash24g(&a->key, DIGEST_LEN*2);
53 }
54 
55 /*
56  * Hash table functions for fp_pair_map_t
57  */
58 
59 HT_PROTOTYPE(fp_pair_map_impl, fp_pair_map_entry_s, node,
61 HT_GENERATE2(fp_pair_map_impl, fp_pair_map_entry_s, node,
64 
65 
69 fp_pair_map_new(void)
70 {
71  fp_pair_map_t *result;
72 
73  result = tor_malloc(sizeof(fp_pair_map_t));
74  HT_INIT(fp_pair_map_impl, &result->head);
75  return result;
76 }
77 
82 void *
83 fp_pair_map_set(fp_pair_map_t *map, const fp_pair_t *key, void *val)
84 {
85  fp_pair_map_entry_t *resolve;
86  fp_pair_map_entry_t search;
87  void *oldval;
88 
89  tor_assert(map);
90  tor_assert(key);
91  tor_assert(val);
92 
93  memcpy(&(search.key), key, sizeof(*key));
94  resolve = HT_FIND(fp_pair_map_impl, &(map->head), &search);
95  if (resolve) {
96  oldval = resolve->val;
97  resolve->val = val;
98  } else {
99  resolve = tor_malloc_zero(sizeof(fp_pair_map_entry_t));
100  memcpy(&(resolve->key), key, sizeof(*key));
101  resolve->val = val;
102  HT_INSERT(fp_pair_map_impl, &(map->head), resolve);
103  oldval = NULL;
104  }
105 
106  return oldval;
107 }
108 
113 void *
115  const char *first, const char *second,
116  void *val)
117 {
118  fp_pair_t k;
119 
120  tor_assert(first);
121  tor_assert(second);
122 
123  memcpy(k.first, first, DIGEST_LEN);
124  memcpy(k.second, second, DIGEST_LEN);
125 
126  return fp_pair_map_set(map, &k, val);
127 }
128 
132 void *
133 fp_pair_map_get(const fp_pair_map_t *map, const fp_pair_t *key)
134 {
135  fp_pair_map_entry_t *resolve;
136  fp_pair_map_entry_t search;
137  void *val = NULL;
138 
139  tor_assert(map);
140  tor_assert(key);
141 
142  memcpy(&(search.key), key, sizeof(*key));
143  resolve = HT_FIND(fp_pair_map_impl, &(map->head), &search);
144  if (resolve) val = resolve->val;
145 
146  return val;
147 }
148 
153 void *
155  const char *first, const char *second)
156 {
157  fp_pair_t k;
158 
159  tor_assert(first);
160  tor_assert(second);
161 
162  memcpy(k.first, first, DIGEST_LEN);
163  memcpy(k.second, second, DIGEST_LEN);
164 
165  return fp_pair_map_get(map, &k);
166 }
167 
174 void *
176 {
177  fp_pair_map_entry_t *resolve;
178  fp_pair_map_entry_t search;
179  void *val = NULL;
180 
181  tor_assert(map);
182  tor_assert(key);
183 
184  memcpy(&(search.key), key, sizeof(*key));
185  resolve = HT_REMOVE(fp_pair_map_impl, &(map->head), &search);
186  if (resolve) {
187  val = resolve->val;
188  tor_free(resolve);
189  }
190 
191  return val;
192 }
193 
198 void
199 fp_pair_map_free_(fp_pair_map_t *map, void (*free_val)(void*))
200 {
201  fp_pair_map_entry_t **ent, **next, *this;
202 
203  if (map) {
204  for (ent = HT_START(fp_pair_map_impl, &(map->head));
205  ent != NULL; ent = next) {
206  this = *ent;
207  next = HT_NEXT_RMV(fp_pair_map_impl, &(map->head), ent);
208  if (free_val) free_val(this->val);
209  tor_free(this);
210  }
211  tor_assert(HT_EMPTY(&(map->head)));
212  HT_CLEAR(fp_pair_map_impl, &(map->head));
213  tor_free(map);
214  }
215 }
216 
220 int
222 {
223  tor_assert(map);
224 
225  return HT_EMPTY(&(map->head));
226 }
227 
231 int
233 {
234  tor_assert(map);
235 
236  return HT_SIZE(&(map->head));
237 }
238 
244 {
245  tor_assert(map);
246 
247  return HT_START(fp_pair_map_impl, &(map->head));
248 }
249 
256 {
257  tor_assert(map);
258  tor_assert(iter);
259 
260  return HT_NEXT(fp_pair_map_impl, &(map->head), iter);
261 }
262 
269 {
270  fp_pair_map_entry_t *rmv;
271 
272  tor_assert(map);
273  tor_assert(iter);
274  tor_assert(*iter);
275 
276  rmv = *iter;
277  iter = HT_NEXT_RMV(fp_pair_map_impl, &(map->head), iter);
278  tor_free(rmv);
279 
280  return iter;
281 }
282 
286 void
288  fp_pair_t *key_out, void **val_out)
289 {
290  tor_assert(iter);
291  tor_assert(*iter);
292 
293  if (key_out) memcpy(key_out, &((*iter)->key), sizeof(fp_pair_t));
294  if (val_out) *val_out = (*iter)->val;
295 }
296 
300 int
302 {
303  return (iter == NULL);
304 }
305 
310 void
312 {
313  tor_assert(!fp_pair_map_impl_HT_REP_IS_BAD_(&(map->head)));
314 }
315 
void * fp_pair_map_set(fp_pair_map_t *map, const fp_pair_t *key, void *val)
Definition: fp_pair.c:83
void * fp_pair_map_get(const fp_pair_map_t *map, const fp_pair_t *key)
Definition: fp_pair.c:133
void * fp_pair_map_get_by_digests(const fp_pair_map_t *map, const char *first, const char *second)
Definition: fp_pair.c:154
void * fp_pair_map_remove(fp_pair_map_t *map, const fp_pair_t *key)
Definition: fp_pair.c:175
void fp_pair_map_free_(fp_pair_map_t *map, void(*free_val)(void *))
Definition: fp_pair.c:199
#define tor_free(p)
Definition: malloc.h:52
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2)
Definition: malloc.c:146
void fp_pair_map_assert_ok(const fp_pair_map_t *map)
Definition: fp_pair.c:311
fp_pair_map_iter_t * fp_pair_map_iter_next_rmv(fp_pair_map_t *map, fp_pair_map_iter_t *iter)
Definition: fp_pair.c:268
tor_assert(buffer)
void * fp_pair_map_set_by_digests(fp_pair_map_t *map, const char *first, const char *second, void *val)
Definition: fp_pair.c:114
static int fp_pair_map_entries_eq(const fp_pair_map_entry_t *a, const fp_pair_map_entry_t *b)
Definition: fp_pair.c:41
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
static unsigned int fp_pair_map_entry_hash(const fp_pair_map_entry_t *a)
Definition: fp_pair.c:49
#define DIGEST_LEN
Definition: digest_sizes.h:20
Master header file for Tor-specific functionality.
void fp_pair_map_iter_get(fp_pair_map_iter_t *iter, fp_pair_t *key_out, void **val_out)
Definition: fp_pair.c:287
void tor_free_(void *mem)
Definition: malloc.c:227
Definition: fp_pair.c:25
int fp_pair_map_size(const fp_pair_map_t *map)
Definition: fp_pair.c:232
int fp_pair_map_isempty(const fp_pair_map_t *map)
Definition: fp_pair.c:221
Header file for fp_pair.c.
fp_pair_map_iter_t * fp_pair_map_iter_init(fp_pair_map_t *map)
Definition: fp_pair.c:243
fp_pair_map_iter_t * fp_pair_map_iter_next(fp_pair_map_t *map, fp_pair_map_iter_t *iter)
Definition: fp_pair.c:255
HT_PROTOTYPE(HT_GENERATE2(fp_pair_map_impl, HT_GENERATE2(fp_pair_map_entry_s, HT_GENERATE2(node, HT_GENERATE2(fp_pair_map_entry_hash, HT_GENERATE2(fp_pair_map_entries_eq)
Definition: fp_pair.c:59
int fp_pair_map_iter_done(fp_pair_map_iter_t *iter)
Definition: fp_pair.c:301