container.c 27 KB

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