container.c 26 KB

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