container.h 18.6 KB
Newer Older
1
2
/* Copyright (c) 2003-2004, Roger Dingledine
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3
 * Copyright (c) 2007-2008, The Tor Project, Inc. */
4
5
6
7
8
/* See LICENSE for licensing information */
/* $Id$ */

#ifndef __CONTAINER_H
#define __CONTAINER_H
9
10
#define CONTAINER_H_ID \
  "$Id$"
11

Nick Mathewson's avatar
Nick Mathewson committed
12
#include "util.h"
13

14
15
16
17
18
19
/** A resizeable list of pointers, with associated helpful functionality.
 *
 * The members of this struct are exposed only so that macros and inlines can
 * use them; all access to smartlist internals should go throuch the functions
 * and macros defined here.
 **/
20
typedef struct smartlist_t {
21
22
23
24
25
26
27
  /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
   * before it needs to be resized.  Only the first <b>num_used</b> (\<=
   * capacity) elements point to valid data.
   */
  void **list;
  int num_used;
  int capacity;
28
} smartlist_t;
29
30
31
32
33
34

smartlist_t *smartlist_create(void);
void smartlist_free(smartlist_t *sl);
void smartlist_clear(smartlist_t *sl);
void smartlist_add(smartlist_t *sl, void *element);
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
35
void smartlist_remove(smartlist_t *sl, const void *element);
36
37
void *smartlist_pop_last(smartlist_t *sl);
void smartlist_reverse(smartlist_t *sl);
38
void smartlist_string_remove(smartlist_t *sl, const char *element);
39
int smartlist_isin(const smartlist_t *sl, const void *element) ATTR_PURE;
40
41
int smartlist_string_isin(const smartlist_t *sl, const char *element)
  ATTR_PURE;
42
int smartlist_string_pos(const smartlist_t *, const char *elt) ATTR_PURE;
43
44
int smartlist_string_isin_case(const smartlist_t *sl, const char *element)
  ATTR_PURE;
45
int smartlist_string_num_isin(const smartlist_t *sl, int num) ATTR_PURE;
46
47
int smartlist_digest_isin(const smartlist_t *sl, const char *element)
  ATTR_PURE;
48
49
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
  ATTR_PURE;
50
51
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
52

53
/* smartlist_choose() is defined in crypto.[ch] */
54
#ifdef DEBUG_SMARTLIST
55
56
/** Return the number of items in sl.
 */
57
58
static INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE;
static INLINE int smartlist_len(const smartlist_t *sl) {
59
60
61
62
63
  tor_assert(sl);
  return (sl)->num_used;
}
/** Return the <b>idx</b>th element of sl.
 */
64
65
static INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE;
static INLINE void *smartlist_get(const smartlist_t *sl, int idx) {
66
67
  tor_assert(sl);
  tor_assert(idx>=0);
Peter Palfrader's avatar
Peter Palfrader committed
68
  tor_assert(sl->num_used > idx);
69
70
  return sl->list[idx];
}
71
static INLINE void smartlist_set(smartlist_t *sl, int idx, void *val) {
72
73
  tor_assert(sl);
  tor_assert(idx>=0);
Peter Palfrader's avatar
Peter Palfrader committed
74
  tor_assert(sl->num_used > idx);
75
76
  sl->list[idx] = val;
}
77
78
79
80
81
82
#else
#define smartlist_len(sl) ((sl)->num_used)
#define smartlist_get(sl, idx) ((sl)->list[idx])
#define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
#endif

83
84
/** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
 * smartlist <b>sl</b>. */
85
static INLINE void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
86
87
88
89
90
91
92
93
{
  if (idx1 != idx2) {
    void *elt = smartlist_get(sl, idx1);
    smartlist_set(sl, idx1, smartlist_get(sl, idx2));
    smartlist_set(sl, idx2, elt);
  }
}

94
95
void smartlist_del(smartlist_t *sl, int idx);
void smartlist_del_keeporder(smartlist_t *sl, int idx);
96
void smartlist_insert(smartlist_t *sl, int idx, void *val);
97
98
void smartlist_sort(smartlist_t *sl,
                    int (*compare)(const void **a, const void **b));
99
100
101
void smartlist_uniq(smartlist_t *sl,
                    int (*compare)(const void **a, const void **b),
                    void (*free_fn)(void *elt));
102
void smartlist_sort_strings(smartlist_t *sl);
103
void smartlist_sort_digests(smartlist_t *sl);
104
105
void smartlist_uniq_strings(smartlist_t *sl);
void smartlist_uniq_digests(smartlist_t *sl);
106
void *smartlist_bsearch(smartlist_t *sl, const void *key,
107
108
                        int (*compare)(const void *key, const void **member))
 ATTR_PURE;
109
110
111
112
int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
                          int (*compare)(const void *key, const void **member),
                          int *found_out)
 ATTR_PURE;
113

114
115
116
117
118
119
120
121
void smartlist_pqueue_add(smartlist_t *sl,
                          int (*compare)(const void *a, const void *b),
                          void *item);
void *smartlist_pqueue_pop(smartlist_t *sl,
                           int (*compare)(const void *a, const void *b));
void smartlist_pqueue_assert_ok(smartlist_t *sl,
                                int (*compare)(const void *a, const void *b));

122
123
124
125
#define SPLIT_SKIP_SPACE   0x01
#define SPLIT_IGNORE_BLANK 0x02
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
                           int flags, int max);
126
char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
127
                             size_t *len_out) ATTR_MALLOC;
128
char *smartlist_join_strings2(smartlist_t *sl, const char *join,
129
130
                              size_t join_len, int terminate, size_t *len_out)
  ATTR_MALLOC;
131

132
133
134
/** Iterate over the items in a smartlist <b>sl</b>, in order.  For each item,
 * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
 * execute the statement <b>cmd</b>.  Inside the loop, the loop index can
135
136
137
138
139
140
 * be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
 * as <b>var</b>_sl_len.
 *
 * NOTE: Do not change the length of the list while the loop is in progress,
 * unless you adjust the _sl_len variable correspondingly.  See second example
 * below.
141
142
143
144
145
146
147
148
149
 *
 * Example use:
 * <pre>
 *   smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
 *   SMARTLIST_FOREACH(list, char *, cp,
 *   {
 *     printf("%d: %s\n", cp_sl_idx, cp);
 *     tor_free(cp);
 *   });
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 *   smartlist_free(list);
 * </pre>
 *
 * Example use (advanced):
 * <pre>
 *   SMARTLIST_FOREACH(list, char *, cp,
 *   {
 *     if (!strcmp(cp, "junk")) {
 *       smartlist_del(list, cp_sl_idx);
 *       tor_free(cp);
 *       --cp_sl_len; // decrement length of list so we don't run off the end
 *       --cp_sl_idx; // decrement idx so we consider the item that moved here
 *     }
 *   });
164
165
 * </pre>
 */
166
#define SMARTLIST_FOREACH(sl, type, var, cmd)                   \
167
  STMT_BEGIN                                                    \
168
    int var ## _sl_idx, var ## _sl_len=(sl)->num_used;          \
169
    type var;                                                   \
170
    for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len;   \
171
         ++var ## _sl_idx) {                                    \
172
      var = (sl)->list[var ## _sl_idx];                         \
173
      cmd;                                                      \
174
    } STMT_END
175

176
/** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
177
 * with the variable <b>var</b>, remove the current element in a way that
178
179
 * won't confuse the loop. */
#define SMARTLIST_DEL_CURRENT(sl, var)          \
180
  STMT_BEGIN                                    \
181
182
183
    smartlist_del(sl, var ## _sl_idx);          \
    --var ## _sl_idx;                           \
    --var ## _sl_len;                           \
184
  STMT_END
185

186
187
#define DECLARE_MAP_FNS(maptype, keytype, prefix)                       \
  typedef struct maptype maptype;                                       \
188
  typedef struct prefix##entry_t *prefix##iter_t;                       \
189
190
  maptype* prefix##new(void);                                           \
  void* prefix##set(maptype *map, keytype key, void *val);              \
191
  void* prefix##get(const maptype *map, keytype key);                   \
192
193
  void* prefix##remove(maptype *map, keytype key);                      \
  void prefix##free(maptype *map, void (*free_val)(void*));             \
194
195
  int prefix##isempty(const maptype *map);                              \
  int prefix##size(const maptype *map);                                 \
196
197
198
199
  prefix##iter_t *prefix##iter_init(maptype *map);                      \
  prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
  prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
  void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
200
  int prefix##iter_done(prefix##iter_t *iter);                          \
201
  void prefix##assert_ok(const maptype *map)
202

203
/* Map from const char * to void *. Implemented with a hash table. */
204
DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
205
/* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
206
DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
207

208
#undef DECLARE_MAP_FNS
209

210
211
212
213
214
215
216
217
218
219
220
221
#define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar)      \
  STMT_BEGIN                                                            \
    prefix##iter_t *key##_iter;                                         \
    for (key##_iter = prefix##iter_init(map);                           \
         !prefix##iter_done(key##_iter);                                \
         key##_iter = prefix##iter_next(map, key##_iter)) {             \
      keytype keyvar;                                                   \
      void *valvar##_voidp;                                             \
      valtype valvar;                                                   \
      prefix##iter_get(key##_iter, &keyvar, &valvar##_voidp);           \
      valvar = valvar##_voidp;

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
  STMT_BEGIN                                                            \
    prefix##iter_t *key##_iter;                                         \
    int keyvar##_del=0;                                                 \
    for (key##_iter = prefix##iter_init(map);                           \
         !prefix##iter_done(key##_iter);                                \
         key##_iter = keyvar##_del ?                                    \
           prefix##iter_next_rmv(map, key##_iter) :                     \
           prefix##iter_next(map, key##_iter)) {                        \
      keytype keyvar;                                                   \
      void *valvar##_voidp;                                             \
      valtype valvar;                                                   \
      keyvar##_del=0;                                                   \
      prefix##iter_get(key##_iter, &keyvar, &valvar##_voidp);           \
      valvar = valvar##_voidp;

#define MAP_DEL_CURRENT(keyvar)                   \
  STMT_BEGIN                                      \
    keyvar##_del = 1;                             \
  STMT_END

243
244
245
246
#define MAP_FOREACH_END } STMT_END ;

#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar)                 \
  MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar)
247
248
#define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar)          \
  MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar)
249
250
#define DIGESTMAP_FOREACH_END MAP_FOREACH_END

251
void* strmap_set_lc(strmap_t *map, const char *key, void *val);
252
void* strmap_get_lc(const strmap_t *map, const char *key);
253
254
void* strmap_remove_lc(strmap_t *map, const char *key);

255
256
257
258
259
260
261
#define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype)           \
  typedef struct maptype maptype;                                       \
  typedef struct prefix##iter_t prefix##iter_t;                         \
  static INLINE maptype* prefix##new(void)                              \
  {                                                                     \
    return (maptype*)digestmap_new();                                   \
  }                                                                     \
262
263
264
265
  static INLINE digestmap_t* prefix##to_digestmap(maptype *map)         \
  {                                                                     \
    return (digestmap_t*)map;                                           \
  }                                                                     \
266
267
268
269
270
271
272
273
274
275
276
  static INLINE valtype* prefix##get(maptype *map, const char *key)     \
  {                                                                     \
    return (valtype*)digestmap_get((digestmap_t*)map, key);             \
  }                                                                     \
  static INLINE valtype* prefix##set(maptype *map, const char *key,     \
                                     valtype *val)                      \
  {                                                                     \
    return (valtype*)digestmap_set((digestmap_t*)map, key, val);        \
  }                                                                     \
  static INLINE valtype* prefix##remove(maptype *map, const char *key)  \
  {                                                                     \
277
    return (valtype*)digestmap_remove((digestmap_t*)map, key);          \
278
279
280
281
282
283
284
285
286
287
288
  }                                                                     \
  static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
  {                                                                     \
    digestmap_free((digestmap_t*)map, free_val);                        \
  }                                                                     \
  static INLINE int prefix##isempty(maptype *map)                       \
  {                                                                     \
    return digestmap_isempty((digestmap_t*)map);                        \
  }                                                                     \
  static INLINE int prefix##size(maptype *map)                          \
  {                                                                     \
289
    return digestmap_size((digestmap_t*)map);                           \
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  }                                                                     \
  static INLINE prefix##iter_t *prefix##iter_init(maptype *map)         \
  {                                                                     \
    return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map);    \
  }                                                                     \
  static INLINE prefix##iter_t *prefix##iter_next(maptype *map,         \
                                                  prefix##iter_t *iter) \
  {                                                                     \
    return (prefix##iter_t*) digestmap_iter_next(                       \
                       (digestmap_t*)map, (digestmap_iter_t*)iter);     \
  }                                                                     \
  static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map,     \
                                                  prefix##iter_t *iter) \
  {                                                                     \
    return (prefix##iter_t*) digestmap_iter_next_rmv(                   \
                       (digestmap_t*)map, (digestmap_iter_t*)iter);     \
  }                                                                     \
  static INLINE void prefix##iter_get(prefix##iter_t *iter,             \
                                      const char **keyp,                \
                                      valtype **valp)                   \
  {                                                                     \
    void *v;                                                            \
    digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v);             \
    *valp = v;                                                          \
  }                                                                     \
  static INLINE int prefix##iter_done(prefix##iter_t *iter)             \
  {                                                                     \
    return digestmap_iter_done((digestmap_iter_t*)iter);                \
  }

320
321
322
323
324
#if SIZEOF_INT == 4
#define BITARRAY_SHIFT 5
#elif SIZEOF_INT == 8
#define BITARRAY_SHIFT 6
#else
325
#error "int is neither 4 nor 8 bytes. I can't deal with that."
326
#endif
327
#define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
328

329
/** A random-access array of one-bit-wide elements. */
330
331
332
333
334
typedef unsigned int bitarray_t;
/** Create a new bit array that can hold <b>n_bits</b> bits. */
static INLINE bitarray_t *
bitarray_init_zero(int n_bits)
{
335
  size_t sz = (n_bits+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
336
337
  return tor_malloc_zero(sz*sizeof(unsigned int));
}
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
static INLINE bitarray_t *
bitarray_expand(bitarray_t *ba, int n_bits_old, int n_bits_new)
{
  size_t sz_old = (n_bits_old+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
  size_t sz_new = (n_bits_new+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
  char *ptr;
  if (sz_new <= sz_old)
    return ba;
  ptr = tor_realloc(ba, sz_new);
  memset(ptr+sz_old, 0, sz_new-sz_old); /* This does nothing to the older
                                         * excess bytes.  But they were
                                         * already set to 0 by
                                         * bitarry_init_zero. */
  return (bitarray_t*) ptr;
}
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/** Free the bit array <b>ba</b>. */
static INLINE void
bitarray_free(bitarray_t *ba)
{
  tor_free(ba);
}
/** Set the <b>bit</b>th bit in <b>b</b> to 1. */
static INLINE void
bitarray_set(bitarray_t *b, int bit)
{
  b[bit >> BITARRAY_SHIFT] |= (1u << (bit & BITARRAY_MASK));
}
/** Set the <b>bit</b>th bit in <b>b</b> to 0. */
static INLINE void
bitarray_clear(bitarray_t *b, int bit)
{
  b[bit >> BITARRAY_SHIFT] &= ~ (1u << (bit & BITARRAY_MASK));
}
/** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero.  NOTE: does
 * not necessarily return 1 on true. */
static INLINE unsigned int
bitarray_is_set(bitarray_t *b, int bit)
{
  return b[bit >> BITARRAY_SHIFT] & (1u << (bit & BITARRAY_MASK));
}

379
380
381
382
383
384
385
386
/* These functions, given an <b>array</b> of <b>n_elements</b>, return the
 * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
 * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
 * the median.  As a side effect, the elements of <b>array</b> are sorted. */
int find_nth_int(int *array, int n_elements, int nth);
time_t find_nth_time(time_t *array, int n_elements, int nth);
double find_nth_double(double *array, int n_elements, int nth);
uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
387
long find_nth_long(long *array, int n_elements, int nth);
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
static INLINE int
median_int(int *array, int n_elements)
{
  return find_nth_int(array, n_elements, (n_elements-1)/2);
}
static INLINE time_t
median_time(time_t *array, int n_elements)
{
  return find_nth_time(array, n_elements, (n_elements-1)/2);
}
static INLINE double
median_double(double *array, int n_elements)
{
  return find_nth_double(array, n_elements, (n_elements-1)/2);
}
static INLINE uint32_t
median_uint32(uint32_t *array, int n_elements)
{
  return find_nth_uint32(array, n_elements, (n_elements-1)/2);
}
408
409
410
411
412
static INLINE long
median_long(long *array, int n_elements)
{
  return find_nth_long(array, n_elements, (n_elements-1)/2);
}
413

414
#endif
415