container.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /* Copyright 2003-2004 Roger Dingledine
  2. Copyright 2004-2006 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char container_c_id[] =
  6. "$Id$";
  7. /**
  8. * \file container.c
  9. * \brief Implements a smartlist (a resizable array) along
  10. * with helper functions to use smartlists. Also includes
  11. * hash table implementations of a string-to-void* map, and of
  12. * a digest-to-void* map.
  13. **/
  14. #include "compat.h"
  15. #include "util.h"
  16. #include "log.h"
  17. #include "container.h"
  18. #include "crypto.h"
  19. #ifdef HAVE_CTYPE_H
  20. #include <ctype.h>
  21. #endif
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include "ht.h"
  26. /* All newly allocated smartlists have this capacity.
  27. */
  28. #define SMARTLIST_DEFAULT_CAPACITY 32
  29. /** Allocate and return an empty smartlist.
  30. */
  31. smartlist_t *
  32. smartlist_create(void)
  33. {
  34. smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
  35. sl->num_used = 0;
  36. sl->capacity = SMARTLIST_DEFAULT_CAPACITY;
  37. sl->list = tor_malloc(sizeof(void *) * sl->capacity);
  38. return sl;
  39. }
  40. /** Deallocate a smartlist. Does not release storage associated with the
  41. * list's elements.
  42. */
  43. void
  44. smartlist_free(smartlist_t *sl)
  45. {
  46. tor_assert(sl != NULL);
  47. tor_free(sl->list);
  48. tor_free(sl);
  49. }
  50. /** Change the capacity of the smartlist to <b>n</b>, so that we can grow
  51. * the list up to <b>n</b> elements with no further reallocation or wasted
  52. * space. If <b>n</b> is less than or equal to the number of elements
  53. * currently in the list, reduce the list's capacity as much as
  54. * possible without losing elements.
  55. */
  56. void
  57. smartlist_set_capacity(smartlist_t *sl, int n)
  58. {
  59. if (n < sl->num_used)
  60. n = sl->num_used;
  61. if (sl->capacity != n) {
  62. sl->capacity = n;
  63. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  64. }
  65. }
  66. /** Remove all elements from the list.
  67. */
  68. void
  69. smartlist_clear(smartlist_t *sl)
  70. {
  71. sl->num_used = 0;
  72. }
  73. /** Make sure that <b>sl</b> can hold at least <b>size</b> entries. */
  74. static INLINE void
  75. smartlist_ensure_capacity(smartlist_t *sl, int size)
  76. {
  77. if (size > sl->capacity) {
  78. int higher = sl->capacity * 2;
  79. while (size > higher)
  80. higher *= 2;
  81. tor_assert(higher > sl->capacity); /* detect overflow */
  82. sl->capacity = higher;
  83. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  84. }
  85. }
  86. /** Append element to the end of the list. */
  87. void
  88. smartlist_add(smartlist_t *sl, void *element)
  89. {
  90. smartlist_ensure_capacity(sl, sl->num_used+1);
  91. sl->list[sl->num_used++] = element;
  92. }
  93. /** Append each element from S2 to the end of S1. */
  94. void
  95. smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
  96. {
  97. smartlist_ensure_capacity(s1, s1->num_used + s2->num_used);
  98. memcpy(s1->list + s1->num_used, s2->list, s2->num_used*sizeof(void*));
  99. s1->num_used += s2->num_used;
  100. }
  101. /** Remove all elements E from sl such that E==element. Preserve
  102. * the order of any elements before E, but elements after E can be
  103. * rearranged.
  104. */
  105. void
  106. smartlist_remove(smartlist_t *sl, const void *element)
  107. {
  108. int i;
  109. if (element == NULL)
  110. return;
  111. for (i=0; i < sl->num_used; i++)
  112. if (sl->list[i] == element) {
  113. sl->list[i] = sl->list[--sl->num_used]; /* swap with the end */
  114. i--; /* so we process the new i'th element */
  115. }
  116. }
  117. /** If <b>sl</b> is nonempty, remove and return the final element. Otherwise,
  118. * return NULL. */
  119. void *
  120. smartlist_pop_last(smartlist_t *sl)
  121. {
  122. tor_assert(sl);
  123. if (sl->num_used)
  124. return sl->list[--sl->num_used];
  125. else
  126. return NULL;
  127. }
  128. /** Reverse the order of the items in <b>sl</b>. */
  129. void
  130. smartlist_reverse(smartlist_t *sl)
  131. {
  132. int i, j;
  133. void *tmp;
  134. tor_assert(sl);
  135. for (i = 0, j = sl->num_used-1; i < j; ++i, --j) {
  136. tmp = sl->list[i];
  137. sl->list[i] = sl->list[j];
  138. sl->list[j] = tmp;
  139. }
  140. }
  141. /** If there are any strings in sl equal to element, remove and free them.
  142. * Does not preserve order. */
  143. void
  144. smartlist_string_remove(smartlist_t *sl, const char *element)
  145. {
  146. int i;
  147. tor_assert(sl);
  148. tor_assert(element);
  149. for (i = 0; i < sl->num_used; ++i) {
  150. if (!strcmp(element, sl->list[i])) {
  151. tor_free(sl->list[i]);
  152. sl->list[i] = sl->list[--sl->num_used]; /* swap with the end */
  153. i--; /* so we process the new i'th element */
  154. }
  155. }
  156. }
  157. /** Return true iff some element E of sl has E==element.
  158. */
  159. int
  160. smartlist_isin(const smartlist_t *sl, const void *element)
  161. {
  162. int i;
  163. for (i=0; i < sl->num_used; i++)
  164. if (sl->list[i] == element)
  165. return 1;
  166. return 0;
  167. }
  168. /** Return true iff <b>sl</b> has some element E such that
  169. * !strcmp(E,<b>element</b>)
  170. */
  171. int
  172. smartlist_string_isin(const smartlist_t *sl, const char *element)
  173. {
  174. int i;
  175. if (!sl) return 0;
  176. for (i=0; i < sl->num_used; i++)
  177. if (strcmp((const char*)sl->list[i],element)==0)
  178. return 1;
  179. return 0;
  180. }
  181. /** Return true iff <b>sl</b> has some element E such that
  182. * !strcasecmp(E,<b>element</b>)
  183. */
  184. int
  185. smartlist_string_isin_case(const smartlist_t *sl, const char *element)
  186. {
  187. int i;
  188. if (!sl) return 0;
  189. for (i=0; i < sl->num_used; i++)
  190. if (strcasecmp((const char*)sl->list[i],element)==0)
  191. return 1;
  192. return 0;
  193. }
  194. /** Return true iff <b>sl</b> has some element E such that E is equal
  195. * to the decimal encoding of <b>num</b>.
  196. */
  197. int
  198. smartlist_string_num_isin(const smartlist_t *sl, int num)
  199. {
  200. char buf[16];
  201. tor_snprintf(buf,sizeof(buf),"%d", num);
  202. return smartlist_string_isin(sl, buf);
  203. }
  204. /** Return true iff some element E of sl2 has smartlist_isin(sl1,E).
  205. */
  206. int
  207. smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
  208. {
  209. int i;
  210. for (i=0; i < sl2->num_used; i++)
  211. if (smartlist_isin(sl1, sl2->list[i]))
  212. return 1;
  213. return 0;
  214. }
  215. /** Remove every element E of sl1 such that !smartlist_isin(sl2,E).
  216. * Does not preserve the order of sl1.
  217. */
  218. void
  219. smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2)
  220. {
  221. int i;
  222. for (i=0; i < sl1->num_used; i++)
  223. if (!smartlist_isin(sl2, sl1->list[i])) {
  224. sl1->list[i] = sl1->list[--sl1->num_used]; /* swap with the end */
  225. i--; /* so we process the new i'th element */
  226. }
  227. }
  228. /** Remove every element E of sl1 such that smartlist_isin(sl2,E).
  229. * Does not preserve the order of sl1.
  230. */
  231. void
  232. smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2)
  233. {
  234. int i;
  235. for (i=0; i < sl2->num_used; i++)
  236. smartlist_remove(sl1, sl2->list[i]);
  237. }
  238. /** Remove the <b>idx</b>th element of sl; if idx is not the last
  239. * element, swap the last element of sl into the <b>idx</b>th space.
  240. * Return the old value of the <b>idx</b>th element.
  241. */
  242. void
  243. smartlist_del(smartlist_t *sl, int idx)
  244. {
  245. tor_assert(sl);
  246. tor_assert(idx>=0);
  247. tor_assert(idx < sl->num_used);
  248. sl->list[idx] = sl->list[--sl->num_used];
  249. }
  250. /** Remove the <b>idx</b>th element of sl; if idx is not the last element,
  251. * moving all subsequent elements back one space. Return the old value
  252. * of the <b>idx</b>th element.
  253. */
  254. void
  255. smartlist_del_keeporder(smartlist_t *sl, int idx)
  256. {
  257. tor_assert(sl);
  258. tor_assert(idx>=0);
  259. tor_assert(idx < sl->num_used);
  260. --sl->num_used;
  261. if (idx < sl->num_used)
  262. memmove(sl->list+idx, sl->list+idx+1, sizeof(void*)*(sl->num_used-idx));
  263. }
  264. /** Insert the value <b>val</b> as the new <b>idx</b>th element of
  265. * <b>sl</b>, moving all items previously at <b>idx</b> or later
  266. * forward one space.
  267. */
  268. void
  269. smartlist_insert(smartlist_t *sl, int idx, void *val)
  270. {
  271. tor_assert(sl);
  272. tor_assert(idx>=0);
  273. tor_assert(idx <= sl->num_used);
  274. if (idx == sl->num_used) {
  275. smartlist_add(sl, val);
  276. } else {
  277. smartlist_ensure_capacity(sl, sl->num_used+1);
  278. /* Move other elements away */
  279. if (idx < sl->num_used)
  280. memmove(sl->list + idx + 1, sl->list + idx,
  281. sizeof(void*)*(sl->num_used-idx));
  282. sl->num_used++;
  283. sl->list[idx] = val;
  284. }
  285. }
  286. /**
  287. * Split a string <b>str</b> along all occurrences of <b>sep</b>,
  288. * adding the split strings, in order, to <b>sl</b>. If
  289. * <b>flags</b>&amp;SPLIT_SKIP_SPACE is true, remove initial and
  290. * trailing space from each entry. If
  291. * <b>flags</b>&amp;SPLIT_IGNORE_BLANK is true, remove any entries of
  292. * length 0. If max>0, divide the string into no more than <b>max</b>
  293. * pieces. If <b>sep</b> is NULL, split on any sequence of horizontal space.
  294. */
  295. int
  296. smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  297. int flags, int max)
  298. {
  299. const char *cp, *end, *next;
  300. int n = 0;
  301. tor_assert(sl);
  302. tor_assert(str);
  303. cp = str;
  304. while (1) {
  305. if (flags&SPLIT_SKIP_SPACE) {
  306. while (TOR_ISSPACE(*cp)) ++cp;
  307. }
  308. if (max>0 && n == max-1) {
  309. end = strchr(cp,'\0');
  310. } else if (sep) {
  311. end = strstr(cp,sep);
  312. if (!end)
  313. end = strchr(cp,'\0');
  314. } else {
  315. for (end = cp; *end && *end != '\t' && *end != ' '; ++end)
  316. ;
  317. }
  318. if (!*end) {
  319. next = NULL;
  320. } else if (sep) {
  321. next = end+strlen(sep);
  322. } else {
  323. next = end+1;
  324. while (*next == '\t' || *next == ' ')
  325. ++next;
  326. }
  327. if (flags&SPLIT_SKIP_SPACE) {
  328. while (end > cp && TOR_ISSPACE(*(end-1)))
  329. --end;
  330. }
  331. if (end != cp || !(flags&SPLIT_IGNORE_BLANK)) {
  332. smartlist_add(sl, tor_strndup(cp, end-cp));
  333. ++n;
  334. }
  335. if (!next)
  336. break;
  337. cp = next;
  338. }
  339. return n;
  340. }
  341. /** Allocate and return a new string containing the concatenation of
  342. * the elements of <b>sl</b>, in order, separated by <b>join</b>. If
  343. * <b>terminate</b> is true, also terminate the string with <b>join</b>.
  344. * If <b>len_out</b> is not NULL, set <b>len_out</b> to the length of
  345. * the returned string. Requires that every element of <b>sl</b> is
  346. * NUL-terminated string.
  347. */
  348. char *
  349. smartlist_join_strings(smartlist_t *sl, const char *join,
  350. int terminate, size_t *len_out)
  351. {
  352. return smartlist_join_strings2(sl,join,strlen(join),terminate,len_out);
  353. }
  354. /** As smartlist_join_strings, but instead of separating/terminated with a
  355. * NUL-terminated string <b>join</b>, uses the <b>join_len</b>-byte sequence
  356. * at <b>join</b>. (Useful for generating a sequence of NUL-terminated
  357. * strings.)
  358. */
  359. char *
  360. smartlist_join_strings2(smartlist_t *sl, const char *join,
  361. size_t join_len, int terminate, size_t *len_out)
  362. {
  363. int i;
  364. size_t n = 0;
  365. char *r = NULL, *dst, *src;
  366. tor_assert(sl);
  367. tor_assert(join);
  368. if (terminate)
  369. n = join_len;
  370. for (i = 0; i < sl->num_used; ++i) {
  371. n += strlen(sl->list[i]);
  372. if (i+1 < sl->num_used) /* avoid double-counting the last one */
  373. n += join_len;
  374. }
  375. dst = r = tor_malloc(n+1);
  376. for (i = 0; i < sl->num_used; ) {
  377. for (src = sl->list[i]; *src; )
  378. *dst++ = *src++;
  379. if (++i < sl->num_used) {
  380. memcpy(dst, join, join_len);
  381. dst += join_len;
  382. }
  383. }
  384. if (terminate) {
  385. memcpy(dst, join, join_len);
  386. dst += join_len;
  387. }
  388. *dst = '\0';
  389. if (len_out)
  390. *len_out = dst-r;
  391. return r;
  392. }
  393. /** Sort the members of <b>sl</b> into an order defined by
  394. * the ordering function <b>compare</b>, which returns less then 0 if a
  395. * precedes b, greater than 0 if b precedes a, and 0 if a 'equals' b.
  396. */
  397. void
  398. smartlist_sort(smartlist_t *sl, int (*compare)(const void **a, const void **b))
  399. {
  400. if (!sl->num_used)
  401. return;
  402. qsort(sl->list, sl->num_used, sizeof(void*),
  403. (int (*)(const void *,const void*))compare);
  404. }
  405. /** Given a sorted smartlist <b>sl</b> and the comparison function used to
  406. * sort it, remove all duplicate members. If free_fn is provided, calls
  407. * free_fn on each duplicate. Otherwise, frees them with tor_free(), which
  408. * may not be what you want.. Preserves order.
  409. */
  410. void
  411. smartlist_uniq(smartlist_t *sl,
  412. int (*compare)(const void **a, const void **b),
  413. void (*free_fn)(void *a))
  414. {
  415. int i;
  416. for (i=1; i < sl->num_used; ++i) {
  417. if (compare((const void **)&(sl->list[i-1]),
  418. (const void **)&(sl->list[i])) == 0) {
  419. if (free_fn)
  420. free_fn(sl->list[i]);
  421. else
  422. tor_free(sl->list[i]);
  423. smartlist_del_keeporder(sl, i--);
  424. }
  425. }
  426. }
  427. /** Assuming the members of <b>sl</b> are in order, return a pointer to the
  428. * member which matches <b>key</b>. Ordering and matching are defined by a
  429. * <b>compare</b> function, which returns 0 on a match; less than 0 if key is
  430. * less than member, and greater than 0 if key is greater then member.
  431. */
  432. void *
  433. smartlist_bsearch(smartlist_t *sl, const void *key,
  434. int (*compare)(const void *key, const void **member))
  435. {
  436. void ** r;
  437. if (!sl->num_used)
  438. return NULL;
  439. r = bsearch(key, sl->list, sl->num_used, sizeof(void*),
  440. (int (*)(const void *, const void *))compare);
  441. return r ? *r : NULL;
  442. }
  443. /** Helper: compare two const char **s. */
  444. static int
  445. _compare_string_ptrs(const void **_a, const void **_b)
  446. {
  447. return strcmp((const char*)*_a, (const char*)*_b);
  448. }
  449. /** Sort a smartlist <b>sl</b> containing strings into lexically ascending
  450. * order. */
  451. void
  452. smartlist_sort_strings(smartlist_t *sl)
  453. {
  454. smartlist_sort(sl, _compare_string_ptrs);
  455. }
  456. /** Remove duplicate strings from a sorted list, and free them with tor_free().
  457. */
  458. void
  459. smartlist_uniq_strings(smartlist_t *sl)
  460. {
  461. smartlist_uniq(sl, _compare_string_ptrs, NULL);
  462. }
  463. /* Heap-based priority queue implementation for O(lg N) insert and remove.
  464. * Recall that the heap property is that, for every index I, h[I] <
  465. * H[LEFT_CHILD[I]] and h[I] < H[RIGHT_CHILD[I]].
  466. */
  467. /* For a 1-indexed array, we would use LEFT_CHILD[x] = 2*x and RIGHT_CHILD[x]
  468. * = 2*x + 1. But this is C, so we have to adjust a little. */
  469. //#define LEFT_CHILD(i) ( ((i)+1)*2 - 1)
  470. //#define RIGHT_CHILD(i) ( ((i)+1)*2 )
  471. //#define PARENT(i) ( ((i)+1)/2 - 1)
  472. #define LEFT_CHILD(i) ( 2*(i) + 1 )
  473. #define RIGHT_CHILD(i) ( 2*(i) + 2 )
  474. #define PARENT(i) ( ((i)-1) / 2 )
  475. /** Helper. <b>sl</b> may have at most one violation of the heap property:
  476. * the item at <b>idx</b> may be greater than one or both of its children.
  477. * Restore the heap property. */
  478. static INLINE void
  479. smartlist_heapify(smartlist_t *sl,
  480. int (*compare)(const void *a, const void *b),
  481. int idx)
  482. {
  483. while (1) {
  484. int left_idx = LEFT_CHILD(idx);
  485. int best_idx;
  486. if (left_idx >= sl->num_used)
  487. return;
  488. if (compare(sl->list[idx],sl->list[left_idx]) < 0)
  489. best_idx = idx;
  490. else
  491. best_idx = left_idx;
  492. if (left_idx+1 < sl->num_used &&
  493. compare(sl->list[left_idx+1],sl->list[best_idx]) < 0)
  494. best_idx = left_idx + 1;
  495. if (best_idx == idx) {
  496. return;
  497. } else {
  498. void *tmp = sl->list[idx];
  499. sl->list[idx] = sl->list[best_idx];
  500. sl->list[best_idx] = tmp;
  501. idx = best_idx;
  502. }
  503. }
  504. }
  505. /** Insert <b>item</b> into the heap stored in <b>sl</b>, where order
  506. * is determined by <b>compare</b>. */
  507. void
  508. smartlist_pqueue_add(smartlist_t *sl,
  509. int (*compare)(const void *a, const void *b),
  510. void *item)
  511. {
  512. int idx;
  513. smartlist_add(sl,item);
  514. for (idx = sl->num_used - 1; idx; ) {
  515. int parent = PARENT(idx);
  516. if (compare(sl->list[idx], sl->list[parent]) < 0) {
  517. void *tmp = sl->list[parent];
  518. sl->list[parent] = sl->list[idx];
  519. sl->list[idx] = tmp;
  520. idx = parent;
  521. } else {
  522. return;
  523. }
  524. }
  525. }
  526. /** Remove and return the top-priority item from the heap stored in <b>sl</b>,
  527. * where order is determined by <b>compare</b>. <b>sl</b> must not be
  528. * empty. */
  529. void *
  530. smartlist_pqueue_pop(smartlist_t *sl,
  531. int (*compare)(const void *a, const void *b))
  532. {
  533. void *top;
  534. tor_assert(sl->num_used);
  535. top = sl->list[0];
  536. if (--sl->num_used) {
  537. sl->list[0] = sl->list[sl->num_used];
  538. smartlist_heapify(sl, compare, 0);
  539. }
  540. return top;
  541. }
  542. /** Assert that the heap property is correctly maintained by the heap stored
  543. * in <b>sl</b>, where order is determined by <b>compare</b>. */
  544. void
  545. smartlist_pqueue_assert_ok(smartlist_t *sl,
  546. int (*compare)(const void *a, const void *b))
  547. {
  548. int i;
  549. for (i = sl->num_used - 1; i > 0; --i) {
  550. tor_assert(compare(sl->list[PARENT(i)], sl->list[i]) <= 0);
  551. }
  552. }
  553. /** Helper: compare two DIGEST_LEN digests. */
  554. static int
  555. _compare_digests(const void **_a, const void **_b)
  556. {
  557. return memcmp((const char*)*_a, (const char*)*_b, DIGEST_LEN);
  558. }
  559. /** Sort the list of DIGEST_LEN-byte digests into ascending order. */
  560. void
  561. smartlist_sort_digests(smartlist_t *sl)
  562. {
  563. smartlist_sort(sl, _compare_digests);
  564. }
  565. /** Remove duplicate digests from a sorted list, and free them with tor_free().
  566. */
  567. void
  568. smartlist_uniq_digests(smartlist_t *sl)
  569. {
  570. smartlist_uniq(sl, _compare_digests, NULL);
  571. }
  572. #define DEFINE_MAP_STRUCTS(maptype, keydecl, prefix) \
  573. typedef struct prefix ## entry_t { \
  574. HT_ENTRY(prefix ## entry_t) node; \
  575. void *val; \
  576. keydecl; \
  577. } prefix ## entry_t; \
  578. struct maptype { \
  579. HT_HEAD(prefix ## impl, prefix ## entry_t) head; \
  580. };
  581. DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_);
  582. DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_);
  583. /** Helper: compare strmap_entry_t objects by key value. */
  584. static INLINE int
  585. strmap_entries_eq(strmap_entry_t *a, strmap_entry_t *b)
  586. {
  587. return !strcmp(a->key, b->key);
  588. }
  589. /** Helper: return a hash value for a strmap_entry_t. */
  590. static INLINE unsigned int
  591. strmap_entry_hash(strmap_entry_t *a)
  592. {
  593. return ht_string_hash(a->key);
  594. }
  595. /** Helper: compare digestmap_entry_t objects by key value. */
  596. static INLINE int
  597. digestmap_entries_eq(digestmap_entry_t *a, digestmap_entry_t *b)
  598. {
  599. return !memcmp(a->key, b->key, DIGEST_LEN);
  600. }
  601. /** Helper: return a hash value for a digest_map_t. */
  602. static INLINE unsigned int
  603. digestmap_entry_hash(digestmap_entry_t *a)
  604. {
  605. uint32_t *p = (uint32_t*)a->key;
  606. return ht_improve_hash(p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4]);
  607. }
  608. HT_PROTOTYPE(strmap_impl, strmap_entry_t, node, strmap_entry_hash,
  609. strmap_entries_eq);
  610. HT_GENERATE(strmap_impl, strmap_entry_t, node, strmap_entry_hash,
  611. strmap_entries_eq, 0.6, malloc, realloc, free);
  612. HT_PROTOTYPE(digestmap_impl, digestmap_entry_t, node, digestmap_entry_hash,
  613. digestmap_entries_eq);
  614. HT_GENERATE(digestmap_impl, digestmap_entry_t, node, digestmap_entry_hash,
  615. digestmap_entries_eq, 0.6, malloc, realloc, free);
  616. /** Constructor to create a new empty map from strings to void*'s.
  617. */
  618. strmap_t *
  619. strmap_new(void)
  620. {
  621. strmap_t *result;
  622. result = tor_malloc(sizeof(strmap_t));
  623. HT_INIT(strmap_impl, &result->head);
  624. return result;
  625. }
  626. /** Constructor to create a new empty map from digests to void*'s.
  627. */
  628. digestmap_t *
  629. digestmap_new(void)
  630. {
  631. digestmap_t *result;
  632. result = tor_malloc(sizeof(digestmap_t));
  633. HT_INIT(digestmap_impl, &result->head);
  634. return result;
  635. }
  636. /** Set the current value for <b>key</b> to <b>val</b>. Returns the previous
  637. * value for <b>key</b> if one was set, or NULL if one was not.
  638. *
  639. * This function makes a copy of <b>key</b> if necessary, but not of
  640. * <b>val</b>.
  641. */
  642. void *
  643. strmap_set(strmap_t *map, const char *key, void *val)
  644. {
  645. strmap_entry_t *resolve;
  646. strmap_entry_t search;
  647. void *oldval;
  648. tor_assert(map);
  649. tor_assert(key);
  650. tor_assert(val);
  651. search.key = (char*)key;
  652. resolve = HT_FIND(strmap_impl, &map->head, &search);
  653. if (resolve) {
  654. oldval = resolve->val;
  655. resolve->val = val;
  656. return oldval;
  657. } else {
  658. resolve = tor_malloc_zero(sizeof(strmap_entry_t));
  659. resolve->key = tor_strdup(key);
  660. resolve->val = val;
  661. tor_assert(!HT_FIND(strmap_impl, &map->head, resolve));
  662. HT_INSERT(strmap_impl, &map->head, resolve);
  663. return NULL;
  664. }
  665. }
  666. /** Like strmap_set() above but for digestmaps. */
  667. void *
  668. digestmap_set(digestmap_t *map, const char *key, void *val)
  669. {
  670. digestmap_entry_t *resolve;
  671. digestmap_entry_t search;
  672. void *oldval;
  673. tor_assert(map);
  674. tor_assert(key);
  675. tor_assert(val);
  676. memcpy(&search.key, key, DIGEST_LEN);
  677. resolve = HT_FIND(digestmap_impl, &map->head, &search);
  678. if (resolve) {
  679. oldval = resolve->val;
  680. resolve->val = val;
  681. return oldval;
  682. } else {
  683. resolve = tor_malloc_zero(sizeof(digestmap_entry_t));
  684. memcpy(resolve->key, key, DIGEST_LEN);
  685. resolve->val = val;
  686. HT_INSERT(digestmap_impl, &map->head, resolve);
  687. return NULL;
  688. }
  689. }
  690. /** Return the current value associated with <b>key</b>, or NULL if no
  691. * value is set.
  692. */
  693. void *
  694. strmap_get(strmap_t *map, const char *key)
  695. {
  696. strmap_entry_t *resolve;
  697. strmap_entry_t search;
  698. tor_assert(map);
  699. tor_assert(key);
  700. search.key = (char*)key;
  701. resolve = HT_FIND(strmap_impl, &map->head, &search);
  702. if (resolve) {
  703. return resolve->val;
  704. } else {
  705. return NULL;
  706. }
  707. }
  708. /** Like strmap_get() above but for digestmaps. */
  709. void *
  710. digestmap_get(digestmap_t *map, const char *key)
  711. {
  712. digestmap_entry_t *resolve;
  713. digestmap_entry_t search;
  714. tor_assert(map);
  715. tor_assert(key);
  716. memcpy(&search.key, key, DIGEST_LEN);
  717. resolve = HT_FIND(digestmap_impl, &map->head, &search);
  718. if (resolve) {
  719. return resolve->val;
  720. } else {
  721. return NULL;
  722. }
  723. }
  724. /** Remove the value currently associated with <b>key</b> from the map.
  725. * Return the value if one was set, or NULL if there was no entry for
  726. * <b>key</b>.
  727. *
  728. * Note: you must free any storage associated with the returned value.
  729. */
  730. void *
  731. strmap_remove(strmap_t *map, const char *key)
  732. {
  733. strmap_entry_t *resolve;
  734. strmap_entry_t search;
  735. void *oldval;
  736. tor_assert(map);
  737. tor_assert(key);
  738. search.key = (char*)key;
  739. resolve = HT_REMOVE(strmap_impl, &map->head, &search);
  740. if (resolve) {
  741. oldval = resolve->val;
  742. tor_free(resolve->key);
  743. tor_free(resolve);
  744. return oldval;
  745. } else {
  746. return NULL;
  747. }
  748. }
  749. /** Like strmap_remove() above but for digestmaps. */
  750. void *
  751. digestmap_remove(digestmap_t *map, const char *key)
  752. {
  753. digestmap_entry_t *resolve;
  754. digestmap_entry_t search;
  755. void *oldval;
  756. tor_assert(map);
  757. tor_assert(key);
  758. memcpy(&search.key, key, DIGEST_LEN);
  759. resolve = HT_REMOVE(digestmap_impl, &map->head, &search);
  760. if (resolve) {
  761. oldval = resolve->val;
  762. tor_free(resolve);
  763. return oldval;
  764. } else {
  765. return NULL;
  766. }
  767. }
  768. /** Same as strmap_set, but first converts <b>key</b> to lowercase. */
  769. void *
  770. strmap_set_lc(strmap_t *map, const char *key, void *val)
  771. {
  772. /* We could be a little faster by using strcasecmp instead, and a separate
  773. * type, but I don't think it matters. */
  774. void *v;
  775. char *lc_key = tor_strdup(key);
  776. tor_strlower(lc_key);
  777. v = strmap_set(map,lc_key,val);
  778. tor_free(lc_key);
  779. return v;
  780. }
  781. /** Same as strmap_get, but first converts <b>key</b> to lowercase. */
  782. void *
  783. strmap_get_lc(strmap_t *map, const char *key)
  784. {
  785. void *v;
  786. char *lc_key = tor_strdup(key);
  787. tor_strlower(lc_key);
  788. v = strmap_get(map,lc_key);
  789. tor_free(lc_key);
  790. return v;
  791. }
  792. /** Same as strmap_remove, but first converts <b>key</b> to lowercase */
  793. void *
  794. strmap_remove_lc(strmap_t *map, const char *key)
  795. {
  796. void *v;
  797. char *lc_key = tor_strdup(key);
  798. tor_strlower(lc_key);
  799. v = strmap_remove(map,lc_key);
  800. tor_free(lc_key);
  801. return v;
  802. }
  803. /** return an <b>iterator</b> pointer to the front of a map.
  804. *
  805. * Iterator example:
  806. *
  807. * \code
  808. * // uppercase values in "map", removing empty values.
  809. *
  810. * strmap_iter_t *iter;
  811. * const char *key;
  812. * void *val;
  813. * char *cp;
  814. *
  815. * for (iter = strmap_iter_init(map); !strmap_iter_done(iter); ) {
  816. * strmap_iter_get(iter, &key, &val);
  817. * cp = (char*)val;
  818. * if (!*cp) {
  819. * iter = strmap_iter_next_rmv(iter);
  820. * free(val);
  821. * } else {
  822. * for (;*cp;cp++) *cp = TOR_TOUPPER(*cp);
  823. * iter = strmap_iter_next(iter);
  824. * }
  825. * }
  826. * \endcode
  827. *
  828. */
  829. strmap_iter_t *
  830. strmap_iter_init(strmap_t *map)
  831. {
  832. tor_assert(map);
  833. return HT_START(strmap_impl, &map->head);
  834. }
  835. digestmap_iter_t *
  836. digestmap_iter_init(digestmap_t *map)
  837. {
  838. tor_assert(map);
  839. return HT_START(digestmap_impl, &map->head);
  840. }
  841. /** Advance the iterator <b>iter</b> for map a single step to the next entry.
  842. */
  843. strmap_iter_t *
  844. strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
  845. {
  846. tor_assert(map);
  847. tor_assert(iter);
  848. return HT_NEXT(strmap_impl, &map->head, iter);
  849. }
  850. digestmap_iter_t *
  851. digestmap_iter_next(digestmap_t *map, digestmap_iter_t *iter)
  852. {
  853. tor_assert(map);
  854. tor_assert(iter);
  855. return HT_NEXT(digestmap_impl, &map->head, iter);
  856. }
  857. /** Advance the iterator <b>iter</b> a single step to the next entry, removing
  858. * the current entry.
  859. */
  860. strmap_iter_t *
  861. strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
  862. {
  863. strmap_entry_t *rmv;
  864. tor_assert(map);
  865. tor_assert(iter);
  866. tor_assert(*iter);
  867. rmv = *iter;
  868. iter = HT_NEXT_RMV(strmap_impl, &map->head, iter);
  869. tor_free(rmv->key);
  870. tor_free(rmv);
  871. return iter;
  872. }
  873. digestmap_iter_t *
  874. digestmap_iter_next_rmv(digestmap_t *map, digestmap_iter_t *iter)
  875. {
  876. digestmap_entry_t *rmv;
  877. tor_assert(map);
  878. tor_assert(iter);
  879. tor_assert(*iter);
  880. rmv = *iter;
  881. iter = HT_NEXT_RMV(digestmap_impl, &map->head, iter);
  882. tor_free(rmv);
  883. return iter;
  884. }
  885. /** Set *keyp and *valp to the current entry pointed to by iter.
  886. */
  887. void
  888. strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
  889. {
  890. tor_assert(iter);
  891. tor_assert(*iter);
  892. tor_assert(keyp);
  893. tor_assert(valp);
  894. *keyp = (*iter)->key;
  895. *valp = (*iter)->val;
  896. }
  897. void
  898. digestmap_iter_get(digestmap_iter_t *iter, const char **keyp, void **valp)
  899. {
  900. tor_assert(iter);
  901. tor_assert(*iter);
  902. tor_assert(keyp);
  903. tor_assert(valp);
  904. *keyp = (*iter)->key;
  905. *valp = (*iter)->val;
  906. }
  907. /** Return true iff iter has advanced past the last entry of map.
  908. */
  909. int
  910. strmap_iter_done(strmap_iter_t *iter)
  911. {
  912. return iter == NULL;
  913. }
  914. int
  915. digestmap_iter_done(digestmap_iter_t *iter)
  916. {
  917. return iter == NULL;
  918. }
  919. /** Remove all entries from <b>map</b>, and deallocate storage for those
  920. * entries. If free_val is provided, it is invoked on every value in
  921. * <b>map</b>.
  922. */
  923. void
  924. strmap_free(strmap_t *map, void (*free_val)(void*))
  925. {
  926. strmap_entry_t **ent, **next, *this;
  927. for (ent = HT_START(strmap_impl, &map->head); ent != NULL; ent = next) {
  928. this = *ent;
  929. next = HT_NEXT_RMV(strmap_impl, &map->head, ent);
  930. tor_free(this->key);
  931. if (free_val)
  932. free_val(this->val);
  933. tor_free(this);
  934. }
  935. tor_assert(HT_EMPTY(&map->head));
  936. HT_CLEAR(strmap_impl, &map->head);
  937. tor_free(map);
  938. }
  939. void
  940. digestmap_free(digestmap_t *map, void (*free_val)(void*))
  941. {
  942. digestmap_entry_t **ent, **next, *this;
  943. for (ent = HT_START(digestmap_impl, &map->head); ent != NULL; ent = next) {
  944. this = *ent;
  945. next = HT_NEXT_RMV(digestmap_impl, &map->head, ent);
  946. if (free_val)
  947. free_val(this->val);
  948. tor_free(this);
  949. }
  950. tor_assert(HT_EMPTY(&map->head));
  951. HT_CLEAR(digestmap_impl, &map->head);
  952. tor_free(map);
  953. }
  954. void
  955. strmap_assert_ok(strmap_t *map)
  956. {
  957. tor_assert(!_strmap_impl_HT_REP_IS_BAD(&map->head));
  958. }
  959. void
  960. digestmap_assert_ok(digestmap_t *map)
  961. {
  962. tor_assert(!_digestmap_impl_HT_REP_IS_BAD(&map->head));
  963. }
  964. /** Return true iff <b>map</b> has no entries. */
  965. int
  966. strmap_isempty(strmap_t *map)
  967. {
  968. return HT_EMPTY(&map->head);
  969. }
  970. int
  971. digestmap_isempty(digestmap_t *map)
  972. {
  973. return HT_EMPTY(&map->head);
  974. }
  975. /** Return the number of items in <b>map</b>. */
  976. int
  977. strmap_size(strmap_t *map)
  978. {
  979. return HT_SIZE(&map->head);
  980. }
  981. int
  982. digestmap_size(digestmap_t *map)
  983. {
  984. return HT_SIZE(&map->head);
  985. }