aboutsummaryrefslogtreecommitdiff
path: root/src/or/fp_pair.c
blob: 55e4c89a4292e64a24ed9af29bd7955a674507b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* Copyright (c) 2013, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#include "or.h"
#include "fp_pair.h"

/* Define fp_pair_map_t structures */

struct fp_pair_map_entry_s {
  HT_ENTRY(fp_pair_map_entry_s) node;
  void *val;
  fp_pair_t key;
};

struct fp_pair_map_s {
  HT_HEAD(fp_pair_map_impl, fp_pair_map_entry_s) head;
};

/*
 * Hash function and equality checker for fp_pair_map_t
 */

/** Compare fp_pair_entry_t objects by key value. */
static INLINE int
fp_pair_map_entries_eq(const fp_pair_map_entry_t *a,
                       const fp_pair_map_entry_t *b)
{
  return tor_memeq(&(a->key), &(b->key), sizeof(fp_pair_t));
}

/** Return a hash value for an fp_pair_entry_t. */
static INLINE unsigned int
fp_pair_map_entry_hash(const fp_pair_map_entry_t *a)
{
  tor_assert(sizeof(a->key) == DIGEST_LEN*2);
  return (unsigned) siphash24g(&a->key, DIGEST_LEN*2);
}

/*
 * Hash table functions for fp_pair_map_t
 */

HT_PROTOTYPE(fp_pair_map_impl, fp_pair_map_entry_s, node,
             fp_pair_map_entry_hash, fp_pair_map_entries_eq)
HT_GENERATE(fp_pair_map_impl, fp_pair_map_entry_s, node,
            fp_pair_map_entry_hash, fp_pair_map_entries_eq,
            0.6, tor_malloc, tor_realloc, tor_free)

/** Constructor to create a new empty map from fp_pair_t to void *
 */

fp_pair_map_t *
fp_pair_map_new(void)
{
  fp_pair_map_t *result;

  result = tor_malloc(sizeof(fp_pair_map_t));
  HT_INIT(fp_pair_map_impl, &result->head);
  return result;
}

/** Set the current value for key to val; returns the previous
 * value for key if one was set, or NULL if one was not.
 */

void *
fp_pair_map_set(fp_pair_map_t *map, const fp_pair_t *key, void *val)
{
  fp_pair_map_entry_t *resolve;
  fp_pair_map_entry_t search;
  void *oldval;

  tor_assert(map);
  tor_assert(key);
  tor_assert(val);

  memcpy(&(search.key), key, sizeof(*key));
  resolve = HT_FIND(fp_pair_map_impl, &(map->head), &search);
  if (resolve) {
    oldval = resolve->val;
    resolve->val = val;
  } else {
    resolve = tor_malloc_zero(sizeof(fp_pair_map_entry_t));
    memcpy(&(resolve->key), key, sizeof(*key));
    resolve->val = val;
    HT_INSERT(fp_pair_map_impl, &(map->head), resolve);
    oldval = NULL;
  }

  return oldval;
}

/** Set the current value for the key (first, second) to val; returns
 * the previous value for key if one was set, or NULL if one was not.
 */

void *
fp_pair_map_set_by_digests(fp_pair_map_t *map,
                           const char *first, const char *second,
                           void *val)
{
  fp_pair_t k;

  tor_assert(first);
  tor_assert(second);

  memcpy(k.first, first, DIGEST_LEN);
  memcpy(k.second, second, DIGEST_LEN);

  return fp_pair_map_set(map, &k, val);
}

/** Return the current value associated with key, or NULL if no value is set.
 */

void *
fp_pair_map_get(const fp_pair_map_t *map, const fp_pair_t *key)
{
  fp_pair_map_entry_t *resolve;
  fp_pair_map_entry_t search;
  void *val = NULL;

  tor_assert(map);
  tor_assert(key);

  memcpy(&(search.key), key, sizeof(*key));
  resolve = HT_FIND(fp_pair_map_impl, &(map->head), &search);
  if (resolve) val = resolve->val;

  return val;
}

/** Return the current value associated the key (first, second), or
 * NULL if no value is set.
 */

void *
fp_pair_map_get_by_digests(const fp_pair_map_t *map,
                           const char *first, const char *second)
{
  fp_pair_t k;

  tor_assert(first);
  tor_assert(second);

  memcpy(k.first, first, DIGEST_LEN);
  memcpy(k.second, second, DIGEST_LEN);

  return fp_pair_map_get(map, &k);
}

/** Remove the value currently associated with key from the map.
 * Return the value if one was set, or NULL if there was no entry for
 * key.  The caller must free any storage associated with the
 * returned value.
 */

void *
fp_pair_map_remove(fp_pair_map_t *map, const fp_pair_t *key)
{
  fp_pair_map_entry_t *resolve;
  fp_pair_map_entry_t search;
  void *val = NULL;

  tor_assert(map);
  tor_assert(key);

  memcpy(&(search.key), key, sizeof(*key));
  resolve = HT_REMOVE(fp_pair_map_impl, &(map->head), &search);
  if (resolve) {
    val = resolve->val;
    tor_free(resolve);
  }

  return val;
}

/** Remove all entries from map, and deallocate storage for those entries.
 * If free_val is provided, it is invoked on every value in map.
 */

void
fp_pair_map_free(fp_pair_map_t *map, void (*free_val)(void*))
{
  fp_pair_map_entry_t **ent, **next, *this;

  if (map) {
    for (ent = HT_START(fp_pair_map_impl, &(map->head));
         ent != NULL; ent = next) {
      this = *ent;
      next = HT_NEXT_RMV(fp_pair_map_impl, &(map->head), ent);
      if (free_val) free_val(this->val);
      tor_free(this);
    }
    tor_assert(HT_EMPTY(&(map->head)));
    HT_CLEAR(fp_pair_map_impl, &(map->head));
    tor_free(map);
  }
}

/** Return true iff map has no entries.
 */

int
fp_pair_map_isempty(const fp_pair_map_t *map)
{
  tor_assert(map);

  return HT_EMPTY(&(map->head));
}

/** Return the number of items in map.
 */

int
fp_pair_map_size(const fp_pair_map_t *map)
{
  tor_assert(map);

  return HT_SIZE(&(map->head));
}

/** return an iterator pointing to the start of map.
 */

fp_pair_map_iter_t *
fp_pair_map_iter_init(fp_pair_map_t *map)
{
  tor_assert(map);

  return HT_START(fp_pair_map_impl, &(map->head));
}

/** Advance iter a single step to the next entry of map, and return
 * its new value.
 */

fp_pair_map_iter_t *
fp_pair_map_iter_next(fp_pair_map_t *map, fp_pair_map_iter_t *iter)
{
  tor_assert(map);
  tor_assert(iter);

  return HT_NEXT(fp_pair_map_impl, &(map->head), iter);
}

/** Advance iter a single step to the next entry of map, removing the current
 * entry, and return its new value.
 */

fp_pair_map_iter_t *
fp_pair_map_iter_next_rmv(fp_pair_map_t *map, fp_pair_map_iter_t *iter)
{
  fp_pair_map_entry_t *rmv;

  tor_assert(map);
  tor_assert(iter);
  tor_assert(*iter);

  rmv = *iter;
  iter = HT_NEXT_RMV(fp_pair_map_impl, &(map->head), iter);
  tor_free(rmv);

  return iter;
}

/** Set *key_out and *val_out to the current entry pointed to by iter.
 */

void
fp_pair_map_iter_get(fp_pair_map_iter_t *iter,
                     fp_pair_t *key_out, void **val_out)
{
  tor_assert(iter);
  tor_assert(*iter);

  if (key_out) memcpy(key_out, &((*iter)->key), sizeof(fp_pair_t));
  if (val_out) *val_out = (*iter)->val;
}

/** Return true iff iter has advanced past the last entry of its map.
 */

int
fp_pair_map_iter_done(fp_pair_map_iter_t *iter)
{
  return (iter == NULL);
}

/** Assert if anything has gone wrong with the internal
 * representation of map.
 */

void
fp_pair_map_assert_ok(const fp_pair_map_t *map)
{
  tor_assert(!fp_pair_map_impl_HT_REP_IS_BAD_(&(map->head)));
}