container.c 16 KB

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