container.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. #define LEFT_CHILD(i) ( ((i)+1)*2 - 1)
  451. #define RIGHT_CHILD(i) ( ((i)+1)*2 )
  452. #define PARENT(i) ( ((i)+1)/2 - 1)
  453. static INLINE void
  454. smartlist_heapify(smartlist_t *sl,
  455. int (*compare)(const void *a, const void *b),
  456. int idx)
  457. {
  458. while (1) {
  459. int left_idx = LEFT_CHILD(idx);
  460. int best_idx;
  461. if (left_idx >= sl->num_used)
  462. return;
  463. if (compare(sl->list[idx],sl->list[left_idx]) < 0)
  464. best_idx = idx;
  465. else
  466. best_idx = left_idx;
  467. if (left_idx+1 < sl->num_used &&
  468. compare(sl->list[left_idx+1],sl->list[best_idx]) < 0)
  469. best_idx = left_idx + 1;
  470. if (best_idx == idx) {
  471. return;
  472. } else {
  473. void *tmp = sl->list[idx];
  474. sl->list[idx] = sl->list[best_idx];
  475. sl->list[best_idx] = tmp;
  476. idx = best_idx;
  477. }
  478. }
  479. }
  480. void
  481. smartlist_pqueue_add(smartlist_t *sl,
  482. int (*compare)(const void *a, const void *b),
  483. void *item)
  484. {
  485. int idx;
  486. smartlist_add(sl,item);
  487. for (idx = sl->num_used - 1; idx; ) {
  488. int parent = PARENT(idx);
  489. if (compare(sl->list[idx], sl->list[parent]) < 0) {
  490. void *tmp = sl->list[parent];
  491. sl->list[parent] = sl->list[idx];
  492. sl->list[idx] = tmp;
  493. idx = parent;
  494. } else {
  495. return;
  496. }
  497. }
  498. }
  499. void *
  500. smartlist_pqueue_pop(smartlist_t *sl,
  501. int (*compare)(const void *a, const void *b))
  502. {
  503. void *top;
  504. tor_assert(sl->num_used);
  505. top = sl->list[0];
  506. if (--sl->num_used) {
  507. sl->list[0] = sl->list[sl->num_used];
  508. smartlist_heapify(sl, compare, 0);
  509. }
  510. return top;
  511. }
  512. void
  513. smartlist_pqueue_assert_ok(smartlist_t *sl,
  514. int (*compare)(const void *a, const void *b))
  515. {
  516. int i;
  517. for (i = sl->num_used - 1; i > 0; --i) {
  518. tor_assert(compare(sl->list[PARENT(i)], sl->list[i]) <= 0);
  519. }
  520. }
  521. /** Helper: compare two DIGEST_LEN digests. */
  522. static int
  523. _compare_digests(const void **_a, const void **_b)
  524. {
  525. return memcmp((const char*)*_a, (const char*)*_b, DIGEST_LEN);
  526. }
  527. /** Sort the list of DIGEST_LEN-byte digests into ascending order. */
  528. void
  529. smartlist_sort_digests(smartlist_t *sl)
  530. {
  531. smartlist_sort(sl, _compare_digests);
  532. }
  533. /** Remove duplicate digests from a sorted list, and free them with tor_free().
  534. */
  535. void
  536. smartlist_uniq_digests(smartlist_t *sl)
  537. {
  538. smartlist_uniq(sl, _compare_digests, NULL);
  539. }
  540. #define DEFINE_MAP_STRUCTS(maptype, keydecl, prefix) \
  541. typedef struct prefix ## entry_t { \
  542. HT_ENTRY(prefix ## entry_t) node; \
  543. void *val; \
  544. keydecl; \
  545. } prefix ## entry_t; \
  546. struct maptype { \
  547. HT_HEAD(prefix ## impl, prefix ## entry_t) head; \
  548. };
  549. DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_);
  550. DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_);
  551. /** Helper: compare strmap_t_entry objects by key value. */
  552. static INLINE int
  553. strmap_entries_eq(strmap_entry_t *a, strmap_entry_t *b)
  554. {
  555. return !strcmp(a->key, b->key);
  556. }
  557. static INLINE unsigned int
  558. strmap_entry_hash(strmap_entry_t *a)
  559. {
  560. return ht_string_hash(a->key);
  561. }
  562. /** Helper: compare digestmap_entry_t objects by key value. */
  563. static INLINE int
  564. digestmap_entries_eq(digestmap_entry_t *a, digestmap_entry_t *b)
  565. {
  566. return !memcmp(a->key, b->key, DIGEST_LEN);
  567. }
  568. static INLINE unsigned int
  569. digestmap_entry_hash(digestmap_entry_t *a)
  570. {
  571. uint32_t *p = (uint32_t*)a->key;
  572. return ht_improve_hash(p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4]);
  573. }
  574. HT_PROTOTYPE(strmap_impl, strmap_entry_t, node, strmap_entry_hash,
  575. strmap_entries_eq);
  576. HT_GENERATE(strmap_impl, strmap_entry_t, node, strmap_entry_hash,
  577. strmap_entries_eq, 0.6, malloc, realloc, free);
  578. HT_PROTOTYPE(digestmap_impl, digestmap_entry_t, node, digestmap_entry_hash,
  579. digestmap_entries_eq);
  580. HT_GENERATE(digestmap_impl, digestmap_entry_t, node, digestmap_entry_hash,
  581. digestmap_entries_eq, 0.6, malloc, realloc, free);
  582. /** Constructor to create a new empty map from strings to void*'s.
  583. */
  584. strmap_t *
  585. strmap_new(void)
  586. {
  587. strmap_t *result;
  588. result = tor_malloc(sizeof(strmap_t));
  589. HT_INIT(&result->head);
  590. return result;
  591. }
  592. /** Constructor to create a new empty map from digests to void*'s.
  593. */
  594. digestmap_t *
  595. digestmap_new(void)
  596. {
  597. digestmap_t *result;
  598. result = tor_malloc(sizeof(digestmap_t));
  599. HT_INIT(&result->head);
  600. return result;
  601. }
  602. /** Set the current value for <b>key</b> to <b>val</b>. Returns the previous
  603. * value for <b>key</b> if one was set, or NULL if one was not.
  604. *
  605. * This function makes a copy of <b>key</b> if necessary, but not of
  606. * <b>val</b>.
  607. */
  608. void *
  609. strmap_set(strmap_t *map, const char *key, void *val)
  610. {
  611. strmap_entry_t *resolve;
  612. strmap_entry_t search;
  613. void *oldval;
  614. tor_assert(map);
  615. tor_assert(key);
  616. tor_assert(val);
  617. search.key = (char*)key;
  618. resolve = HT_FIND(strmap_impl, &map->head, &search);
  619. if (resolve) {
  620. oldval = resolve->val;
  621. resolve->val = val;
  622. return oldval;
  623. } else {
  624. resolve = tor_malloc_zero(sizeof(strmap_entry_t));
  625. resolve->key = tor_strdup(key);
  626. resolve->val = val;
  627. tor_assert(!HT_FIND(strmap_impl, &map->head, resolve));
  628. HT_INSERT(strmap_impl, &map->head, resolve);
  629. return NULL;
  630. }
  631. }
  632. /** Like strmap_set() above but for digestmaps. */
  633. void *
  634. digestmap_set(digestmap_t *map, const char *key, void *val)
  635. {
  636. digestmap_entry_t *resolve;
  637. digestmap_entry_t search;
  638. void *oldval;
  639. tor_assert(map);
  640. tor_assert(key);
  641. tor_assert(val);
  642. memcpy(&search.key, key, DIGEST_LEN);
  643. resolve = HT_FIND(digestmap_impl, &map->head, &search);
  644. if (resolve) {
  645. oldval = resolve->val;
  646. resolve->val = val;
  647. return oldval;
  648. } else {
  649. resolve = tor_malloc_zero(sizeof(digestmap_entry_t));
  650. memcpy(resolve->key, key, DIGEST_LEN);
  651. resolve->val = val;
  652. HT_INSERT(digestmap_impl, &map->head, resolve);
  653. return NULL;
  654. }
  655. }
  656. /** Return the current value associated with <b>key</b>, or NULL if no
  657. * value is set.
  658. */
  659. void *
  660. strmap_get(strmap_t *map, const char *key)
  661. {
  662. strmap_entry_t *resolve;
  663. strmap_entry_t search;
  664. tor_assert(map);
  665. tor_assert(key);
  666. search.key = (char*)key;
  667. resolve = HT_FIND(strmap_impl, &map->head, &search);
  668. if (resolve) {
  669. return resolve->val;
  670. } else {
  671. return NULL;
  672. }
  673. }
  674. /** Like strmap_get() above but for digestmaps. */
  675. void *
  676. digestmap_get(digestmap_t *map, const char *key)
  677. {
  678. digestmap_entry_t *resolve;
  679. digestmap_entry_t search;
  680. tor_assert(map);
  681. tor_assert(key);
  682. memcpy(&search.key, key, DIGEST_LEN);
  683. resolve = HT_FIND(digestmap_impl, &map->head, &search);
  684. if (resolve) {
  685. return resolve->val;
  686. } else {
  687. return NULL;
  688. }
  689. }
  690. /** Remove the value currently associated with <b>key</b> from the map.
  691. * Return the value if one was set, or NULL if there was no entry for
  692. * <b>key</b>.
  693. *
  694. * Note: you must free any storage associated with the returned value.
  695. */
  696. void *
  697. strmap_remove(strmap_t *map, const char *key)
  698. {
  699. strmap_entry_t *resolve;
  700. strmap_entry_t search;
  701. void *oldval;
  702. tor_assert(map);
  703. tor_assert(key);
  704. search.key = (char*)key;
  705. resolve = HT_REMOVE(strmap_impl, &map->head, &search);
  706. if (resolve) {
  707. oldval = resolve->val;
  708. tor_free(resolve->key);
  709. tor_free(resolve);
  710. return oldval;
  711. } else {
  712. return NULL;
  713. }
  714. }
  715. /** Like strmap_remove() above but for digestmaps. */
  716. void *
  717. digestmap_remove(digestmap_t *map, const char *key)
  718. {
  719. digestmap_entry_t *resolve;
  720. digestmap_entry_t search;
  721. void *oldval;
  722. tor_assert(map);
  723. tor_assert(key);
  724. memcpy(&search.key, key, DIGEST_LEN);
  725. resolve = HT_REMOVE(digestmap_impl, &map->head, &search);
  726. if (resolve) {
  727. oldval = resolve->val;
  728. tor_free(resolve);
  729. return oldval;
  730. } else {
  731. return NULL;
  732. }
  733. }
  734. /** Same as strmap_set, but first converts <b>key</b> to lowercase. */
  735. void *
  736. strmap_set_lc(strmap_t *map, const char *key, void *val)
  737. {
  738. /* We could be a little faster by using strcasecmp instead, and a separate
  739. * type, but I don't think it matters. */
  740. void *v;
  741. char *lc_key = tor_strdup(key);
  742. tor_strlower(lc_key);
  743. v = strmap_set(map,lc_key,val);
  744. tor_free(lc_key);
  745. return v;
  746. }
  747. /** Same as strmap_get, but first converts <b>key</b> to lowercase. */
  748. void *
  749. strmap_get_lc(strmap_t *map, const char *key)
  750. {
  751. void *v;
  752. char *lc_key = tor_strdup(key);
  753. tor_strlower(lc_key);
  754. v = strmap_get(map,lc_key);
  755. tor_free(lc_key);
  756. return v;
  757. }
  758. /** Same as strmap_remove, but first converts <b>key</b> to lowercase */
  759. void *
  760. strmap_remove_lc(strmap_t *map, const char *key)
  761. {
  762. void *v;
  763. char *lc_key = tor_strdup(key);
  764. tor_strlower(lc_key);
  765. v = strmap_remove(map,lc_key);
  766. tor_free(lc_key);
  767. return v;
  768. }
  769. /** return an <b>iterator</b> pointer to the front of a map.
  770. *
  771. * Iterator example:
  772. *
  773. * \code
  774. * // uppercase values in "map", removing empty values.
  775. *
  776. * strmap_iter_t *iter;
  777. * const char *key;
  778. * void *val;
  779. * char *cp;
  780. *
  781. * for (iter = strmap_iter_init(map); !strmap_iter_done(iter); ) {
  782. * strmap_iter_get(iter, &key, &val);
  783. * cp = (char*)val;
  784. * if (!*cp) {
  785. * iter = strmap_iter_next_rmv(iter);
  786. * free(val);
  787. * } else {
  788. * for (;*cp;cp++) *cp = TOR_TOUPPER(*cp);
  789. * iter = strmap_iter_next(iter);
  790. * }
  791. * }
  792. * \endcode
  793. *
  794. */
  795. strmap_iter_t *
  796. strmap_iter_init(strmap_t *map)
  797. {
  798. tor_assert(map);
  799. return HT_START(strmap_impl, &map->head);
  800. }
  801. digestmap_iter_t *
  802. digestmap_iter_init(digestmap_t *map)
  803. {
  804. tor_assert(map);
  805. return HT_START(digestmap_impl, &map->head);
  806. }
  807. /** Advance the iterator <b>iter</b> for map a single step to the next entry.
  808. */
  809. strmap_iter_t *
  810. strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
  811. {
  812. tor_assert(map);
  813. tor_assert(iter);
  814. return HT_NEXT(strmap_impl, &map->head, iter);
  815. }
  816. digestmap_iter_t *
  817. digestmap_iter_next(digestmap_t *map, digestmap_iter_t *iter)
  818. {
  819. tor_assert(map);
  820. tor_assert(iter);
  821. return HT_NEXT(digestmap_impl, &map->head, iter);
  822. }
  823. /** Advance the iterator <b>iter</b> a single step to the next entry, removing
  824. * the current entry.
  825. */
  826. strmap_iter_t *
  827. strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
  828. {
  829. strmap_entry_t *rmv;
  830. tor_assert(map);
  831. tor_assert(iter);
  832. tor_assert(*iter);
  833. rmv = *iter;
  834. iter = HT_NEXT_RMV(strmap_impl, &map->head, iter);
  835. tor_free(rmv->key);
  836. tor_free(rmv);
  837. return iter;
  838. }
  839. digestmap_iter_t *
  840. digestmap_iter_next_rmv(digestmap_t *map, digestmap_iter_t *iter)
  841. {
  842. digestmap_entry_t *rmv;
  843. tor_assert(map);
  844. tor_assert(iter);
  845. tor_assert(*iter);
  846. rmv = *iter;
  847. iter = HT_NEXT_RMV(digestmap_impl, &map->head, iter);
  848. tor_free(rmv);
  849. return iter;
  850. }
  851. /** Set *keyp and *valp to the current entry pointed to by iter.
  852. */
  853. void
  854. strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
  855. {
  856. tor_assert(iter);
  857. tor_assert(*iter);
  858. tor_assert(keyp);
  859. tor_assert(valp);
  860. *keyp = (*iter)->key;
  861. *valp = (*iter)->val;
  862. }
  863. void
  864. digestmap_iter_get(digestmap_iter_t *iter, const char **keyp, void **valp)
  865. {
  866. tor_assert(iter);
  867. tor_assert(*iter);
  868. tor_assert(keyp);
  869. tor_assert(valp);
  870. *keyp = (*iter)->key;
  871. *valp = (*iter)->val;
  872. }
  873. /** Return true iff iter has advanced past the last entry of map.
  874. */
  875. int
  876. strmap_iter_done(strmap_iter_t *iter)
  877. {
  878. return iter == NULL;
  879. }
  880. int
  881. digestmap_iter_done(digestmap_iter_t *iter)
  882. {
  883. return iter == NULL;
  884. }
  885. /** Remove all entries from <b>map</b>, and deallocate storage for those
  886. * entries. If free_val is provided, it is invoked on every value in
  887. * <b>map</b>.
  888. */
  889. void
  890. strmap_free(strmap_t *map, void (*free_val)(void*))
  891. {
  892. strmap_entry_t **ent, **next, *this;
  893. for (ent = HT_START(strmap_impl, &map->head); ent != NULL; ent = next) {
  894. this = *ent;
  895. next = HT_NEXT_RMV(strmap_impl, &map->head, ent);
  896. tor_free(this->key);
  897. if (free_val)
  898. free_val(this->val);
  899. tor_free(this);
  900. }
  901. tor_assert(HT_EMPTY(&map->head));
  902. HT_CLEAR(strmap_impl, &map->head);
  903. tor_free(map);
  904. }
  905. void
  906. digestmap_free(digestmap_t *map, void (*free_val)(void*))
  907. {
  908. digestmap_entry_t **ent, **next, *this;
  909. for (ent = HT_START(digestmap_impl, &map->head); ent != NULL; ent = next) {
  910. this = *ent;
  911. next = HT_NEXT_RMV(digestmap_impl, &map->head, ent);
  912. if (free_val)
  913. free_val(this->val);
  914. tor_free(this);
  915. }
  916. tor_assert(HT_EMPTY(&map->head));
  917. HT_CLEAR(digestmap_impl, &map->head);
  918. tor_free(map);
  919. }
  920. void
  921. strmap_assert_ok(strmap_t *map)
  922. {
  923. tor_assert(!_strmap_impl_HT_REP_IS_BAD(&map->head));
  924. }
  925. void
  926. digestmap_assert_ok(digestmap_t *map)
  927. {
  928. tor_assert(!_digestmap_impl_HT_REP_IS_BAD(&map->head));
  929. }
  930. /** Return true iff <b>map</b> has no entries. */
  931. int
  932. strmap_isempty(strmap_t *map)
  933. {
  934. return HT_EMPTY(&map->head);
  935. }
  936. int
  937. digestmap_isempty(digestmap_t *map)
  938. {
  939. return HT_EMPTY(&map->head);
  940. }
  941. int
  942. strmap_size(strmap_t *map)
  943. {
  944. return HT_SIZE(&map->head);
  945. }
  946. int
  947. digestmap_size(digestmap_t *map)
  948. {
  949. return HT_SIZE(&map->head);
  950. }