container.c 17 KB

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