container.c 17 KB

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