container.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /* Copyright 2003-2004 Roger Dingledine
  2. Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char container_c_id[] = "$Id$";
  6. /**
  7. * \file container.c
  8. * \brief Implements a smartlist (a resizable array) along
  9. * with helper functions to use smartlists. Also includes a
  10. * splay-tree implementation of the string-to-void* map.
  11. **/
  12. #include "compat.h"
  13. #include "util.h"
  14. #include "log.h"
  15. #include "../or/tree.h"
  16. #include "container.h"
  17. #ifdef HAVE_CTYPE_H
  18. #include <ctype.h>
  19. #endif
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <assert.h>
  23. /* All newly allocated smartlists have this capacity.
  24. */
  25. #define SMARTLIST_DEFAULT_CAPACITY 32
  26. #ifndef FAST_SMARTLIST
  27. struct smartlist_t {
  28. /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
  29. * before it needs to be resized. Only the first <b>num_used</b> (\<=
  30. * capacity) elements point to valid data.
  31. */
  32. void **list;
  33. int num_used;
  34. int capacity;
  35. };
  36. #endif
  37. /** Allocate and return an empty smartlist.
  38. */
  39. smartlist_t *smartlist_create() {
  40. smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
  41. sl->num_used = 0;
  42. sl->capacity = SMARTLIST_DEFAULT_CAPACITY;
  43. sl->list = tor_malloc(sizeof(void *) * sl->capacity);
  44. return sl;
  45. }
  46. /** Deallocate a smartlist. Does not release storage associated with the
  47. * list's elements.
  48. */
  49. void smartlist_free(smartlist_t *sl) {
  50. free(sl->list);
  51. free(sl);
  52. }
  53. /** Change the capacity of the smartlist to <b>n</b>, so that we can grow
  54. * the list up to <b>n</b> elements with no further reallocation or wasted
  55. * space. If <b>n</b> is less than or equal to the number of elements
  56. * currently in the list, reduce the list's capacity as much as
  57. * possible without losing elements.
  58. */
  59. void smartlist_set_capacity(smartlist_t *sl, int n) {
  60. if (n < sl->num_used)
  61. n = sl->num_used;
  62. if (sl->capacity != n) {
  63. sl->capacity = n;
  64. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  65. }
  66. }
  67. /** Remove all elements from the list.
  68. */
  69. void smartlist_clear(smartlist_t *sl) {
  70. sl->num_used = 0;
  71. }
  72. /** Set the list's new length to <b>len</b> (which must be \<= the list's
  73. * current size). Remove the last smartlist_len(sl)-len elements from the
  74. * list.
  75. */
  76. void smartlist_truncate(smartlist_t *sl, int len)
  77. {
  78. tor_assert(len <= sl->num_used);
  79. sl->num_used = len;
  80. }
  81. /** Append element to the end of the list. */
  82. void smartlist_add(smartlist_t *sl, void *element) {
  83. if (sl->num_used >= sl->capacity) {
  84. int higher = sl->capacity * 2;
  85. tor_assert(higher > sl->capacity); /* detect overflow */
  86. sl->capacity = higher;
  87. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  88. }
  89. sl->list[sl->num_used++] = element;
  90. }
  91. /** Append each element from S2 to the end of S1. */
  92. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2)
  93. {
  94. SMARTLIST_FOREACH(s2, void *, element, smartlist_add(sl, element));
  95. }
  96. /** Remove all elements E from sl such that E==element. Preserve
  97. * the order of any elements before E, but elements after E can be
  98. * rearranged.
  99. */
  100. void smartlist_remove(smartlist_t *sl, void *element) {
  101. int i;
  102. if (element == NULL)
  103. return;
  104. for (i=0; i < sl->num_used; i++)
  105. if (sl->list[i] == element) {
  106. sl->list[i] = sl->list[--sl->num_used]; /* swap with the end */
  107. i--; /* so we process the new i'th element */
  108. }
  109. }
  110. /** If there are any strings in sl equal to element, remove them.
  111. * Does not preserve order. */
  112. void
  113. smartlist_string_remove(smartlist_t *sl, const char *element)
  114. {
  115. int i;
  116. tor_assert(sl);
  117. tor_assert(element);
  118. for (i = 0; i < sl->num_used; ++i) {
  119. if (!strcmp(element, sl->list[i])) {
  120. sl->list[i] = sl->list[--sl->num_used]; /* swap with the end */
  121. i--; /* so we process the new i'th element */
  122. }
  123. }
  124. }
  125. /** Return true iff some element E of sl has E==element.
  126. */
  127. int smartlist_isin(const smartlist_t *sl, void *element) {
  128. int i;
  129. for (i=0; i < sl->num_used; i++)
  130. if (sl->list[i] == element)
  131. return 1;
  132. return 0;
  133. }
  134. int smartlist_string_isin(const smartlist_t *sl, const char *element) {
  135. int i;
  136. if (!sl) return 0;
  137. for (i=0; i < sl->num_used; i++)
  138. if (strcmp((const char*)sl->list[i],element)==0)
  139. return 1;
  140. return 0;
  141. }
  142. int smartlist_string_num_isin(const smartlist_t *sl, int num) {
  143. char buf[16];
  144. tor_snprintf(buf,sizeof(buf),"%d", num);
  145. return smartlist_string_isin(sl, buf);
  146. }
  147. /** Return true iff some element E of sl2 has smartlist_isin(sl1,E).
  148. */
  149. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2) {
  150. int i;
  151. for (i=0; i < sl2->num_used; i++)
  152. if (smartlist_isin(sl1, sl2->list[i]))
  153. return 1;
  154. return 0;
  155. }
  156. /** Remove every element E of sl1 such that !smartlist_isin(sl2,E).
  157. * Does not preserve the order of sl1.
  158. */
  159. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2) {
  160. int i;
  161. for (i=0; i < sl1->num_used; i++)
  162. if (!smartlist_isin(sl2, sl1->list[i])) {
  163. sl1->list[i] = sl1->list[--sl1->num_used]; /* swap with the end */
  164. i--; /* so we process the new i'th element */
  165. }
  166. }
  167. /** Remove every element E of sl1 such that smartlist_isin(sl2,E).
  168. * Does not preserve the order of sl1.
  169. */
  170. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
  171. int i;
  172. for (i=0; i < sl2->num_used; i++)
  173. smartlist_remove(sl1, sl2->list[i]);
  174. }
  175. #ifndef FAST_SMARTLIST
  176. /** Return the <b>idx</b>th element of sl.
  177. */
  178. void *smartlist_get(const smartlist_t *sl, int idx)
  179. {
  180. tor_assert(sl);
  181. tor_assert(idx>=0);
  182. tor_assert(idx < sl->num_used);
  183. return sl->list[idx];
  184. }
  185. /** Change the value of the <b>idx</b>th element of sl to <b>val</b>.
  186. */
  187. void smartlist_set(smartlist_t *sl, int idx, void *val)
  188. {
  189. tor_assert(sl);
  190. tor_assert(idx>=0);
  191. tor_assert(idx < sl->num_used);
  192. sl->list[idx] = val;
  193. }
  194. /** Return the number of items in sl.
  195. */
  196. int smartlist_len(const smartlist_t *sl)
  197. {
  198. return sl->num_used;
  199. }
  200. #endif
  201. /** Remove the <b>idx</b>th element of sl; if idx is not the last
  202. * element, swap the last element of sl into the <b>idx</b>th space.
  203. * Return the old value of the <b>idx</b>th element.
  204. */
  205. void smartlist_del(smartlist_t *sl, int idx)
  206. {
  207. tor_assert(sl);
  208. tor_assert(idx>=0);
  209. tor_assert(idx < sl->num_used);
  210. sl->list[idx] = sl->list[--sl->num_used];
  211. }
  212. /** Remove the <b>idx</b>th element of sl; if idx is not the last element,
  213. * moving all subsequent elements back one space. Return the old value
  214. * of the <b>idx</b>th element.
  215. */
  216. void smartlist_del_keeporder(smartlist_t *sl, int idx)
  217. {
  218. tor_assert(sl);
  219. tor_assert(idx>=0);
  220. tor_assert(idx < sl->num_used);
  221. --sl->num_used;
  222. if (idx < sl->num_used)
  223. memmove(sl->list+idx, sl->list+idx+1, sizeof(void*)*(sl->num_used-idx));
  224. }
  225. /** Insert the value <b>val</b> as the new <b>idx</b>th element of
  226. * <b>sl</b>, moving all items previously at <b>idx</b> or later
  227. * forward one space.
  228. */
  229. void smartlist_insert(smartlist_t *sl, int idx, void *val)
  230. {
  231. tor_assert(sl);
  232. tor_assert(idx>=0);
  233. tor_assert(idx <= sl->num_used);
  234. if (idx == sl->num_used) {
  235. smartlist_add(sl, val);
  236. } else {
  237. /* Ensure sufficient capacity */
  238. if (sl->num_used >= sl->capacity) {
  239. sl->capacity *= 2;
  240. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  241. }
  242. /* Move other elements away */
  243. if (idx < sl->num_used)
  244. memmove(sl->list + idx + 1, sl->list + idx,
  245. sizeof(void*)*(sl->num_used-idx));
  246. sl->num_used++;
  247. sl->list[idx] = val;
  248. }
  249. }
  250. /**
  251. * Split a string <b>str</b> along all occurrences of <b>sep</b>,
  252. * adding the split strings, in order, to <b>sl</b>. If
  253. * <b>flags</b>&amp;SPLIT_SKIP_SPACE is true, remove initial and
  254. * trailing space from each entry. If
  255. * <b>flags</b>&amp;SPLIT_IGNORE_BLANK is true, remove any entries of
  256. * length 0. If max>0, divide the string into no more than <b>max</b>
  257. * pieces. If <b>sep</b> is NULL, split on any sequence of horizontal space.
  258. */
  259. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  260. int flags, int max)
  261. {
  262. const char *cp, *end, *next;
  263. int n = 0;
  264. tor_assert(sl);
  265. tor_assert(str);
  266. cp = str;
  267. while (1) {
  268. if (flags&SPLIT_SKIP_SPACE) {
  269. while (TOR_ISSPACE(*cp)) ++cp;
  270. }
  271. if (max>0 && n == max-1) {
  272. end = strchr(cp,'\0');
  273. } else if (sep) {
  274. end = strstr(cp,sep);
  275. if (!end)
  276. end = strchr(cp,'\0');
  277. } else {
  278. for (end = cp; *end && *end != '\t' && *end != ' '; ++end)
  279. ;
  280. }
  281. if (!*end) {
  282. next = NULL;
  283. } else if (sep) {
  284. next = end+strlen(sep);
  285. } else {
  286. next = end+1;
  287. while (*next == '\t' || *next == ' ')
  288. ++next;
  289. }
  290. if (flags&SPLIT_SKIP_SPACE) {
  291. while (end > cp && TOR_ISSPACE(*(end-1)))
  292. --end;
  293. }
  294. if (end != cp || !(flags&SPLIT_IGNORE_BLANK)) {
  295. smartlist_add(sl, tor_strndup(cp, end-cp));
  296. ++n;
  297. }
  298. if (!next)
  299. break;
  300. cp = next;
  301. }
  302. return n;
  303. }
  304. /** Allocate and return a new string containing the concatenation of
  305. * the elements of <b>sl</b>, in order, separated by <b>join</b>. If
  306. * <b>terminate</b> is true, also terminate the string with <b>join</b>.
  307. * If <b>len_out</b> is not NULL, set <b>len_out</b> to the length of
  308. * the returned string. Requires that every element of <b>sl</b> is
  309. * NUL-terminated string.
  310. */
  311. char *smartlist_join_strings(smartlist_t *sl, const char *join,
  312. int terminate, size_t *len_out)
  313. {
  314. return smartlist_join_strings2(sl,join,strlen(join),terminate,len_out);
  315. }
  316. /** As smartlist_join_strings, but instead of separating/terminated with a
  317. * NUL-terminated string <b>join</b>, uses the <b>join_len</b>-byte sequence
  318. * at <b>join</b>. (Useful for generating a sequence of NUL-terminated
  319. * strings.)
  320. */
  321. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  322. size_t join_len, int terminate, size_t *len_out)
  323. {
  324. int i;
  325. size_t n = 0;
  326. char *r = NULL, *dst, *src;
  327. tor_assert(sl);
  328. tor_assert(join);
  329. if (terminate)
  330. n = join_len;
  331. for (i = 0; i < sl->num_used; ++i) {
  332. n += strlen(sl->list[i]);
  333. if (i+1 < sl->num_used) /* avoid double-counting the last one */
  334. n += join_len;
  335. }
  336. dst = r = tor_malloc(n+1);
  337. for (i = 0; i < sl->num_used; ) {
  338. for (src = sl->list[i]; *src; )
  339. *dst++ = *src++;
  340. if (++i < sl->num_used) {
  341. memcpy(dst, join, join_len);
  342. dst += join_len;
  343. }
  344. }
  345. if (terminate) {
  346. memcpy(dst, join, join_len);
  347. dst += join_len;
  348. }
  349. *dst = '\0';
  350. if (len_out)
  351. *len_out = dst-r;
  352. return r;
  353. }
  354. /** Sort the members of <b>sl</b> into an order defined by
  355. * the ordering function <b>compare</b>, which returns less then 0 if a
  356. * precedes b, greater than 0 if b precedes a, and 0 if a 'equals' b.
  357. */
  358. void
  359. smartlist_sort(smartlist_t *sl, int (*compare)(const void **a, const void **b))
  360. {
  361. if (!sl->num_used)
  362. return;
  363. qsort(sl->list, sl->num_used, sizeof(void*),
  364. (int (*)(const void *,const void*))compare);
  365. }
  366. /** Assuming the members of <b>sl</b> are in order, return a pointer to the
  367. * member which matches <b>key</b>. Ordering and matching are defined by a
  368. * <b>compare</b> function, which returns 0 on a match; less than 0 if key is
  369. * less than member, and greater than 0 if key is greater then member.
  370. */
  371. void *
  372. smartlist_bsearch(smartlist_t *sl, const void *key,
  373. int (*compare)(const void *key, const void **member))
  374. {
  375. void ** r;
  376. if (!sl->num_used)
  377. return NULL;
  378. r = bsearch(key, sl->list, sl->num_used, sizeof(void*),
  379. (int (*)(const void *, const void *))compare);
  380. return r ? *r : NULL;
  381. }
  382. /* Splay-tree implementation of string-to-void* map
  383. */
  384. typedef struct strmap_entry_t {
  385. SPLAY_ENTRY(strmap_entry_t) node;
  386. char *key;
  387. void *val;
  388. } strmap_entry_t;
  389. struct strmap_t {
  390. SPLAY_HEAD(strmap_tree, strmap_entry_t) head;
  391. };
  392. static int compare_strmap_entries(strmap_entry_t *a,
  393. strmap_entry_t *b)
  394. {
  395. return strcmp(a->key, b->key);
  396. }
  397. SPLAY_PROTOTYPE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
  398. SPLAY_GENERATE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
  399. /** Create a new empty map from strings to void*'s.
  400. */
  401. strmap_t* strmap_new(void)
  402. {
  403. strmap_t *result;
  404. result = tor_malloc(sizeof(strmap_t));
  405. SPLAY_INIT(&result->head);
  406. return result;
  407. }
  408. /** Set the current value for <b>key</b> to <b>val</b>. Returns the previous
  409. * value for <b>key</b> if one was set, or NULL if one was not.
  410. *
  411. * This function makes a copy of <b>key</b> if necessary, but not of <b>val</b>.
  412. */
  413. void* strmap_set(strmap_t *map, const char *key, void *val)
  414. {
  415. strmap_entry_t *resolve;
  416. strmap_entry_t search;
  417. void *oldval;
  418. tor_assert(map);
  419. tor_assert(key);
  420. tor_assert(val);
  421. search.key = (char*)key;
  422. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  423. if (resolve) {
  424. oldval = resolve->val;
  425. resolve->val = val;
  426. return oldval;
  427. } else {
  428. resolve = tor_malloc_zero(sizeof(strmap_entry_t));
  429. resolve->key = tor_strdup(key);
  430. resolve->val = val;
  431. SPLAY_INSERT(strmap_tree, &map->head, resolve);
  432. return NULL;
  433. }
  434. }
  435. /** Return the current value associated with <b>key</b>, or NULL if no
  436. * value is set.
  437. */
  438. void* strmap_get(strmap_t *map, const char *key)
  439. {
  440. strmap_entry_t *resolve;
  441. strmap_entry_t search;
  442. tor_assert(map);
  443. tor_assert(key);
  444. search.key = (char*)key;
  445. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  446. if (resolve) {
  447. return resolve->val;
  448. } else {
  449. return NULL;
  450. }
  451. }
  452. /** Remove the value currently associated with <b>key</b> from the map.
  453. * Return the value if one was set, or NULL if there was no entry for
  454. * <b>key</b>.
  455. *
  456. * Note: you must free any storage associated with the returned value.
  457. */
  458. void* strmap_remove(strmap_t *map, const char *key)
  459. {
  460. strmap_entry_t *resolve;
  461. strmap_entry_t search;
  462. void *oldval;
  463. tor_assert(map);
  464. tor_assert(key);
  465. search.key = (char*)key;
  466. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  467. if (resolve) {
  468. oldval = resolve->val;
  469. SPLAY_REMOVE(strmap_tree, &map->head, resolve);
  470. tor_free(resolve->key);
  471. tor_free(resolve);
  472. return oldval;
  473. } else {
  474. return NULL;
  475. }
  476. }
  477. /** Same as strmap_set, but first converts <b>key</b> to lowercase. */
  478. void* strmap_set_lc(strmap_t *map, const char *key, void *val)
  479. {
  480. /* We could be a little faster by using strcasecmp instead, and a separate
  481. * type, but I don't think it matters. */
  482. void *v;
  483. char *lc_key = tor_strdup(key);
  484. tor_strlower(lc_key);
  485. v = strmap_set(map,lc_key,val);
  486. tor_free(lc_key);
  487. return v;
  488. }
  489. /** Same as strmap_get, but first converts <b>key</b> to lowercase. */
  490. void* strmap_get_lc(strmap_t *map, const char *key)
  491. {
  492. void *v;
  493. char *lc_key = tor_strdup(key);
  494. tor_strlower(lc_key);
  495. v = strmap_get(map,lc_key);
  496. tor_free(lc_key);
  497. return v;
  498. }
  499. /** Same as strmap_remove, but first converts <b>key</b> to lowercase */
  500. void* strmap_remove_lc(strmap_t *map, const char *key)
  501. {
  502. void *v;
  503. char *lc_key = tor_strdup(key);
  504. tor_strlower(lc_key);
  505. v = strmap_remove(map,lc_key);
  506. tor_free(lc_key);
  507. return v;
  508. }
  509. /** Invoke fn() on every entry of the map, in order. For every entry,
  510. * fn() is invoked with that entry's key, that entry's value, and the
  511. * value of <b>data</b> supplied to strmap_foreach. fn() must return a new
  512. * (possibly unmodified) value for each entry: if fn() returns NULL, the
  513. * entry is removed.
  514. *
  515. * Example:
  516. * \code
  517. * static void* upcase_and_remove_empty_vals(const char *key, void *val,
  518. * void* data) {
  519. * char *cp = (char*)val;
  520. * if (!*cp) { // val is an empty string.
  521. * free(val);
  522. * return NULL;
  523. * } else {
  524. * for (; *cp; cp++)
  525. * *cp = toupper(*cp);
  526. * }
  527. * return val;
  528. * }
  529. * }
  530. *
  531. * ...
  532. *
  533. * strmap_foreach(map, upcase_and_remove_empty_vals, NULL);
  534. * \endcode
  535. */
  536. void strmap_foreach(strmap_t *map,
  537. void* (*fn)(const char *key, void *val, void *data),
  538. void *data)
  539. {
  540. strmap_entry_t *ptr, *next;
  541. tor_assert(map);
  542. tor_assert(fn);
  543. for (ptr = SPLAY_MIN(strmap_tree, &map->head); ptr != NULL; ptr = next) {
  544. /* This remove-in-place usage is specifically blessed in tree(3). */
  545. next = SPLAY_NEXT(strmap_tree, &map->head, ptr);
  546. ptr->val = fn(ptr->key, ptr->val, data);
  547. if (!ptr->val) {
  548. SPLAY_REMOVE(strmap_tree, &map->head, ptr);
  549. tor_free(ptr->key);
  550. tor_free(ptr);
  551. }
  552. }
  553. }
  554. /** return an <b>iterator</b> pointer to the front of a map.
  555. *
  556. * Iterator example:
  557. *
  558. * \code
  559. * // uppercase values in "map", removing empty values.
  560. *
  561. * strmap_iter_t *iter;
  562. * const char *key;
  563. * void *val;
  564. * char *cp;
  565. *
  566. * for (iter = strmap_iter_init(map); !strmap_iter_done(iter); ) {
  567. * strmap_iter_get(iter, &key, &val);
  568. * cp = (char*)val;
  569. * if (!*cp) {
  570. * iter = strmap_iter_next_rmv(iter);
  571. * free(val);
  572. * } else {
  573. * for (;*cp;cp++) *cp = toupper(*cp);
  574. * iter = strmap_iter_next(iter);
  575. * }
  576. * }
  577. * \endcode
  578. *
  579. */
  580. strmap_iter_t *strmap_iter_init(strmap_t *map)
  581. {
  582. tor_assert(map);
  583. return SPLAY_MIN(strmap_tree, &map->head);
  584. }
  585. /** Advance the iterator <b>iter</b> for map a single step to the next entry.
  586. */
  587. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
  588. {
  589. tor_assert(map);
  590. tor_assert(iter);
  591. return SPLAY_NEXT(strmap_tree, &map->head, iter);
  592. }
  593. /** Advance the iterator <b>iter</b> a single step to the next entry, removing
  594. * the current entry.
  595. */
  596. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
  597. {
  598. strmap_iter_t *next;
  599. tor_assert(map);
  600. tor_assert(iter);
  601. next = SPLAY_NEXT(strmap_tree, &map->head, iter);
  602. SPLAY_REMOVE(strmap_tree, &map->head, iter);
  603. tor_free(iter->key);
  604. tor_free(iter);
  605. return next;
  606. }
  607. /** Set *keyp and *valp to the current entry pointed to by iter.
  608. */
  609. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
  610. {
  611. tor_assert(iter);
  612. tor_assert(keyp);
  613. tor_assert(valp);
  614. *keyp = iter->key;
  615. *valp = iter->val;
  616. }
  617. /** Return true iff iter has advanced past the last entry of map.
  618. */
  619. int strmap_iter_done(strmap_iter_t *iter)
  620. {
  621. return iter == NULL;
  622. }
  623. /** Remove all entries from <b>map</b>, and deallocate storage for those entries.
  624. * If free_val is provided, it is invoked on every value in <b>map</b>.
  625. */
  626. void
  627. strmap_free(strmap_t *map, void (*free_val)(void*))
  628. {
  629. strmap_entry_t *ent, *next;
  630. for (ent = SPLAY_MIN(strmap_tree, &map->head); ent != NULL; ent = next) {
  631. next = SPLAY_NEXT(strmap_tree, &map->head, ent);
  632. SPLAY_REMOVE(strmap_tree, &map->head, ent);
  633. tor_free(ent->key);
  634. if (free_val)
  635. free_val(ent->val);
  636. tor_free(ent);
  637. }
  638. tor_assert(SPLAY_EMPTY(&map->head));
  639. tor_free(map);
  640. }
  641. int strmap_isempty(strmap_t *map)
  642. {
  643. return SPLAY_EMPTY(&map->head);
  644. }