container.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. for (i=0; i < sl->num_used; i++)
  137. if (strcmp((const char*)sl->list[i],element)==0)
  138. return 1;
  139. return 0;
  140. }
  141. int smartlist_string_num_isin(const smartlist_t *sl, int num) {
  142. char buf[16];
  143. tor_snprintf(buf,sizeof(buf),"%d", num);
  144. return smartlist_string_isin(sl, buf);
  145. }
  146. /** Return true iff some element E of sl2 has smartlist_isin(sl1,E).
  147. */
  148. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2) {
  149. int i;
  150. for (i=0; i < sl2->num_used; i++)
  151. if (smartlist_isin(sl1, sl2->list[i]))
  152. return 1;
  153. return 0;
  154. }
  155. /** Remove every element E of sl1 such that !smartlist_isin(sl2,E).
  156. * Does not preserve the order of sl1.
  157. */
  158. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2) {
  159. int i;
  160. for (i=0; i < sl1->num_used; i++)
  161. if (!smartlist_isin(sl2, sl1->list[i])) {
  162. sl1->list[i] = sl1->list[--sl1->num_used]; /* swap with the end */
  163. i--; /* so we process the new i'th element */
  164. }
  165. }
  166. /** Remove every element E of sl1 such that smartlist_isin(sl2,E).
  167. * Does not preserve the order of sl1.
  168. */
  169. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
  170. int i;
  171. for (i=0; i < sl2->num_used; i++)
  172. smartlist_remove(sl1, sl2->list[i]);
  173. }
  174. #ifndef FAST_SMARTLIST
  175. /** Return the <b>idx</b>th element of sl.
  176. */
  177. void *smartlist_get(const smartlist_t *sl, int idx)
  178. {
  179. tor_assert(sl);
  180. tor_assert(idx>=0);
  181. tor_assert(idx < sl->num_used);
  182. return sl->list[idx];
  183. }
  184. /** Change the value of the <b>idx</b>th element of sl to <b>val</b>.
  185. */
  186. void smartlist_set(smartlist_t *sl, int idx, void *val)
  187. {
  188. tor_assert(sl);
  189. tor_assert(idx>=0);
  190. tor_assert(idx < sl->num_used);
  191. sl->list[idx] = val;
  192. }
  193. /** Return the number of items in sl.
  194. */
  195. int smartlist_len(const smartlist_t *sl)
  196. {
  197. return sl->num_used;
  198. }
  199. #endif
  200. /** Remove the <b>idx</b>th element of sl; if idx is not the last
  201. * element, swap the last element of sl into the <b>idx</b>th space.
  202. * Return the old value of the <b>idx</b>th element.
  203. */
  204. void smartlist_del(smartlist_t *sl, int idx)
  205. {
  206. tor_assert(sl);
  207. tor_assert(idx>=0);
  208. tor_assert(idx < sl->num_used);
  209. sl->list[idx] = sl->list[--sl->num_used];
  210. }
  211. /** Remove the <b>idx</b>th element of sl; if idx is not the last element,
  212. * moving all subsequent elements back one space. Return the old value
  213. * of the <b>idx</b>th element.
  214. */
  215. void smartlist_del_keeporder(smartlist_t *sl, int idx)
  216. {
  217. tor_assert(sl);
  218. tor_assert(idx>=0);
  219. tor_assert(idx < sl->num_used);
  220. --sl->num_used;
  221. if (idx < sl->num_used)
  222. memmove(sl->list+idx, sl->list+idx+1, sizeof(void*)*(sl->num_used-idx));
  223. }
  224. /** Insert the value <b>val</b> as the new <b>idx</b>th element of
  225. * <b>sl</b>, moving all items previously at <b>idx</b> or later
  226. * forward one space.
  227. */
  228. void smartlist_insert(smartlist_t *sl, int idx, void *val)
  229. {
  230. tor_assert(sl);
  231. tor_assert(idx>=0);
  232. tor_assert(idx <= sl->num_used);
  233. if (idx == sl->num_used) {
  234. smartlist_add(sl, val);
  235. } else {
  236. /* Ensure sufficient capacity */
  237. if (sl->num_used >= sl->capacity) {
  238. sl->capacity *= 2;
  239. sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
  240. }
  241. /* Move other elements away */
  242. if (idx < sl->num_used)
  243. memmove(sl->list + idx + 1, sl->list + idx,
  244. sizeof(void*)*(sl->num_used-idx));
  245. sl->num_used++;
  246. sl->list[idx] = val;
  247. }
  248. }
  249. /**
  250. * Split a string <b>str</b> along all occurrences of <b>sep</b>,
  251. * adding the split strings, in order, to <b>sl</b>. If
  252. * <b>flags</b>&amp;SPLIT_SKIP_SPACE is true, remove initial and
  253. * trailing space from each entry. If
  254. * <b>flags</b>&amp;SPLIT_IGNORE_BLANK is true, remove any entries of
  255. * length 0. If max>0, divide the string into no more than <b>max</b>
  256. * pieces. If <b>sep</b> is NULL, split on any sequence of horizontal space.
  257. */
  258. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  259. int flags, int max)
  260. {
  261. const char *cp, *end, *next;
  262. int n = 0;
  263. tor_assert(sl);
  264. tor_assert(str);
  265. cp = str;
  266. while (1) {
  267. if (flags&SPLIT_SKIP_SPACE) {
  268. while (TOR_ISSPACE(*cp)) ++cp;
  269. }
  270. if (max>0 && n == max-1) {
  271. end = strchr(cp,'\0');
  272. } else if (sep) {
  273. end = strstr(cp,sep);
  274. if (!end)
  275. end = strchr(cp,'\0');
  276. } else {
  277. for (end = cp; *end && *end != '\t' && *end != ' '; ++end)
  278. ;
  279. }
  280. if (!*end) {
  281. next = NULL;
  282. } else if (sep) {
  283. next = end+strlen(sep);
  284. } else {
  285. next = end+1;
  286. while (*next == '\t' || *next == ' ')
  287. ++next;
  288. }
  289. if (flags&SPLIT_SKIP_SPACE) {
  290. while (end > cp && TOR_ISSPACE(*(end-1)))
  291. --end;
  292. }
  293. if (end != cp || !(flags&SPLIT_IGNORE_BLANK)) {
  294. smartlist_add(sl, tor_strndup(cp, end-cp));
  295. ++n;
  296. }
  297. if (!next)
  298. break;
  299. cp = next;
  300. }
  301. return n;
  302. }
  303. /** Allocate and return a new string containing the concatenation of
  304. * the elements of <b>sl</b>, in order, separated by <b>join</b>. If
  305. * <b>terminate</b> is true, also terminate the string with <b>join</b>.
  306. * If <b>len_out</b> is not NULL, set <b>len_out</b> to the length of
  307. * the returned string. Requires that every element of <b>sl</b> is
  308. * NUL-terminated string.
  309. */
  310. char *smartlist_join_strings(smartlist_t *sl, const char *join,
  311. int terminate, size_t *len_out)
  312. {
  313. return smartlist_join_strings2(sl,join,strlen(join),terminate,len_out);
  314. }
  315. /** As smartlist_join_strings, but instead of separating/terminated with a
  316. * NUL-terminated string <b>join</b>, uses the <b>join_len</b>-byte sequence
  317. * at <b>join</b>. (Useful for generating a sequence of NUL-terminated
  318. * strings.)
  319. */
  320. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  321. size_t join_len, int terminate, size_t *len_out)
  322. {
  323. int i;
  324. size_t n = 0;
  325. char *r = NULL, *dst, *src;
  326. tor_assert(sl);
  327. tor_assert(join);
  328. if (terminate)
  329. n = join_len;
  330. for (i = 0; i < sl->num_used; ++i) {
  331. n += strlen(sl->list[i]);
  332. if (i+1 < sl->num_used) /* avoid double-counting the last one */
  333. n += join_len;
  334. }
  335. dst = r = tor_malloc(n+1);
  336. for (i = 0; i < sl->num_used; ) {
  337. for (src = sl->list[i]; *src; )
  338. *dst++ = *src++;
  339. if (++i < sl->num_used) {
  340. memcpy(dst, join, join_len);
  341. dst += join_len;
  342. }
  343. }
  344. if (terminate) {
  345. memcpy(dst, join, join_len);
  346. dst += join_len;
  347. }
  348. *dst = '\0';
  349. if (len_out)
  350. *len_out = dst-r;
  351. return r;
  352. }
  353. /* Splay-tree implementation of string-to-void* map
  354. */
  355. typedef struct strmap_entry_t {
  356. SPLAY_ENTRY(strmap_entry_t) node;
  357. char *key;
  358. void *val;
  359. } strmap_entry_t;
  360. struct strmap_t {
  361. SPLAY_HEAD(strmap_tree, strmap_entry_t) head;
  362. };
  363. static int compare_strmap_entries(strmap_entry_t *a,
  364. strmap_entry_t *b)
  365. {
  366. return strcmp(a->key, b->key);
  367. }
  368. SPLAY_PROTOTYPE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
  369. SPLAY_GENERATE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
  370. /** Create a new empty map from strings to void*'s.
  371. */
  372. strmap_t* strmap_new(void)
  373. {
  374. strmap_t *result;
  375. result = tor_malloc(sizeof(strmap_t));
  376. SPLAY_INIT(&result->head);
  377. return result;
  378. }
  379. /** Set the current value for <b>key</b> to <b>val</b>. Returns the previous
  380. * value for <b>key</b> if one was set, or NULL if one was not.
  381. *
  382. * This function makes a copy of <b>key</b> if necessary, but not of <b>val</b>.
  383. */
  384. void* strmap_set(strmap_t *map, const char *key, void *val)
  385. {
  386. strmap_entry_t *resolve;
  387. strmap_entry_t search;
  388. void *oldval;
  389. tor_assert(map);
  390. tor_assert(key);
  391. tor_assert(val);
  392. search.key = (char*)key;
  393. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  394. if (resolve) {
  395. oldval = resolve->val;
  396. resolve->val = val;
  397. return oldval;
  398. } else {
  399. resolve = tor_malloc_zero(sizeof(strmap_entry_t));
  400. resolve->key = tor_strdup(key);
  401. resolve->val = val;
  402. SPLAY_INSERT(strmap_tree, &map->head, resolve);
  403. return NULL;
  404. }
  405. }
  406. /** Return the current value associated with <b>key</b>, or NULL if no
  407. * value is set.
  408. */
  409. void* strmap_get(strmap_t *map, const char *key)
  410. {
  411. strmap_entry_t *resolve;
  412. strmap_entry_t search;
  413. tor_assert(map);
  414. tor_assert(key);
  415. search.key = (char*)key;
  416. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  417. if (resolve) {
  418. return resolve->val;
  419. } else {
  420. return NULL;
  421. }
  422. }
  423. /** Remove the value currently associated with <b>key</b> from the map.
  424. * Return the value if one was set, or NULL if there was no entry for
  425. * <b>key</b>.
  426. *
  427. * Note: you must free any storage associated with the returned value.
  428. */
  429. void* strmap_remove(strmap_t *map, const char *key)
  430. {
  431. strmap_entry_t *resolve;
  432. strmap_entry_t search;
  433. void *oldval;
  434. tor_assert(map);
  435. tor_assert(key);
  436. search.key = (char*)key;
  437. resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
  438. if (resolve) {
  439. oldval = resolve->val;
  440. SPLAY_REMOVE(strmap_tree, &map->head, resolve);
  441. tor_free(resolve->key);
  442. tor_free(resolve);
  443. return oldval;
  444. } else {
  445. return NULL;
  446. }
  447. }
  448. /** Same as strmap_set, but first converts <b>key</b> to lowercase. */
  449. void* strmap_set_lc(strmap_t *map, const char *key, void *val)
  450. {
  451. /* We could be a little faster by using strcasecmp instead, and a separate
  452. * type, but I don't think it matters. */
  453. void *v;
  454. char *lc_key = tor_strdup(key);
  455. tor_strlower(lc_key);
  456. v = strmap_set(map,lc_key,val);
  457. tor_free(lc_key);
  458. return v;
  459. }
  460. /** Same as strmap_get, but first converts <b>key</b> to lowercase. */
  461. void* strmap_get_lc(strmap_t *map, const char *key)
  462. {
  463. void *v;
  464. char *lc_key = tor_strdup(key);
  465. tor_strlower(lc_key);
  466. v = strmap_get(map,lc_key);
  467. tor_free(lc_key);
  468. return v;
  469. }
  470. /** Same as strmap_remove, but first converts <b>key</b> to lowercase */
  471. void* strmap_remove_lc(strmap_t *map, const char *key)
  472. {
  473. void *v;
  474. char *lc_key = tor_strdup(key);
  475. tor_strlower(lc_key);
  476. v = strmap_remove(map,lc_key);
  477. tor_free(lc_key);
  478. return v;
  479. }
  480. /** Invoke fn() on every entry of the map, in order. For every entry,
  481. * fn() is invoked with that entry's key, that entry's value, and the
  482. * value of <b>data</b> supplied to strmap_foreach. fn() must return a new
  483. * (possibly unmodified) value for each entry: if fn() returns NULL, the
  484. * entry is removed.
  485. *
  486. * Example:
  487. * \code
  488. * static void* upcase_and_remove_empty_vals(const char *key, void *val,
  489. * void* data) {
  490. * char *cp = (char*)val;
  491. * if (!*cp) { // val is an empty string.
  492. * free(val);
  493. * return NULL;
  494. * } else {
  495. * for (; *cp; cp++)
  496. * *cp = toupper(*cp);
  497. * }
  498. * return val;
  499. * }
  500. * }
  501. *
  502. * ...
  503. *
  504. * strmap_foreach(map, upcase_and_remove_empty_vals, NULL);
  505. * \endcode
  506. */
  507. void strmap_foreach(strmap_t *map,
  508. void* (*fn)(const char *key, void *val, void *data),
  509. void *data)
  510. {
  511. strmap_entry_t *ptr, *next;
  512. tor_assert(map);
  513. tor_assert(fn);
  514. for (ptr = SPLAY_MIN(strmap_tree, &map->head); ptr != NULL; ptr = next) {
  515. /* This remove-in-place usage is specifically blessed in tree(3). */
  516. next = SPLAY_NEXT(strmap_tree, &map->head, ptr);
  517. ptr->val = fn(ptr->key, ptr->val, data);
  518. if (!ptr->val) {
  519. SPLAY_REMOVE(strmap_tree, &map->head, ptr);
  520. tor_free(ptr->key);
  521. tor_free(ptr);
  522. }
  523. }
  524. }
  525. /** return an <b>iterator</b> pointer to the front of a map.
  526. *
  527. * Iterator example:
  528. *
  529. * \code
  530. * // uppercase values in "map", removing empty values.
  531. *
  532. * strmap_iter_t *iter;
  533. * const char *key;
  534. * void *val;
  535. * char *cp;
  536. *
  537. * for (iter = strmap_iter_init(map); !strmap_iter_done(iter); ) {
  538. * strmap_iter_get(iter, &key, &val);
  539. * cp = (char*)val;
  540. * if (!*cp) {
  541. * iter = strmap_iter_next_rmv(iter);
  542. * free(val);
  543. * } else {
  544. * for (;*cp;cp++) *cp = toupper(*cp);
  545. * iter = strmap_iter_next(iter);
  546. * }
  547. * }
  548. * \endcode
  549. *
  550. */
  551. strmap_iter_t *strmap_iter_init(strmap_t *map)
  552. {
  553. tor_assert(map);
  554. return SPLAY_MIN(strmap_tree, &map->head);
  555. }
  556. /** Advance the iterator <b>iter</b> for map a single step to the next entry.
  557. */
  558. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
  559. {
  560. tor_assert(map);
  561. tor_assert(iter);
  562. return SPLAY_NEXT(strmap_tree, &map->head, iter);
  563. }
  564. /** Advance the iterator <b>iter</b> a single step to the next entry, removing
  565. * the current entry.
  566. */
  567. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
  568. {
  569. strmap_iter_t *next;
  570. tor_assert(map);
  571. tor_assert(iter);
  572. next = SPLAY_NEXT(strmap_tree, &map->head, iter);
  573. SPLAY_REMOVE(strmap_tree, &map->head, iter);
  574. tor_free(iter->key);
  575. tor_free(iter);
  576. return next;
  577. }
  578. /** Set *keyp and *valp to the current entry pointed to by iter.
  579. */
  580. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
  581. {
  582. tor_assert(iter);
  583. tor_assert(keyp);
  584. tor_assert(valp);
  585. *keyp = iter->key;
  586. *valp = iter->val;
  587. }
  588. /** Return true iff iter has advanced past the last entry of map.
  589. */
  590. int strmap_iter_done(strmap_iter_t *iter)
  591. {
  592. return iter == NULL;
  593. }
  594. /** Remove all entries from <b>map</b>, and deallocate storage for those entries.
  595. * If free_val is provided, it is invoked on every value in <b>map</b>.
  596. */
  597. void
  598. strmap_free(strmap_t *map, void (*free_val)(void*))
  599. {
  600. strmap_entry_t *ent, *next;
  601. for (ent = SPLAY_MIN(strmap_tree, &map->head); ent != NULL; ent = next) {
  602. next = SPLAY_NEXT(strmap_tree, &map->head, ent);
  603. SPLAY_REMOVE(strmap_tree, &map->head, ent);
  604. tor_free(ent->key);
  605. if (free_val)
  606. free_val(ent->val);
  607. tor_free(ent);
  608. }
  609. tor_assert(SPLAY_EMPTY(&map->head));
  610. tor_free(map);
  611. }
  612. int strmap_isempty(strmap_t *map)
  613. {
  614. return SPLAY_EMPTY(&map->head);
  615. }