smartlist_core.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file smartlist_core.h
  7. * \brief Top-level declarations for the smartlist_t dynamic array type.
  8. **/
  9. #ifndef TOR_SMARTLIST_CORE_H
  10. #define TOR_SMARTLIST_CORE_H
  11. #include <stddef.h>
  12. #include "lib/cc/compat_compiler.h"
  13. #include "lib/cc/torint.h"
  14. #include "lib/testsupport/testsupport.h"
  15. /** A resizeable list of pointers, with associated helpful functionality.
  16. *
  17. * The members of this struct are exposed only so that macros and inlines can
  18. * use them; all access to smartlist internals should go through the functions
  19. * and macros defined here.
  20. **/
  21. typedef struct smartlist_t {
  22. /** @{ */
  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. } smartlist_t;
  32. MOCK_DECL(smartlist_t *, smartlist_new, (void));
  33. MOCK_DECL(void, smartlist_free_, (smartlist_t *sl));
  34. #define smartlist_free(sl) FREE_AND_NULL(smartlist_t, smartlist_free_, (sl))
  35. void smartlist_clear(smartlist_t *sl);
  36. void smartlist_add(smartlist_t *sl, void *element);
  37. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  38. void smartlist_add_strdup(struct smartlist_t *sl, const char *string);
  39. void smartlist_remove(smartlist_t *sl, const void *element);
  40. void smartlist_remove_keeporder(smartlist_t *sl, const void *element);
  41. void *smartlist_pop_last(smartlist_t *sl);
  42. int smartlist_contains(const smartlist_t *sl, const void *element);
  43. /* smartlist_choose() is defined in crypto.[ch] */
  44. #ifdef DEBUG_SMARTLIST
  45. #include "lib/err/torerr.h"
  46. #include <stdlib.h>
  47. /** Return the number of items in sl.
  48. */
  49. static inline int smartlist_len(const smartlist_t *sl);
  50. static inline int smartlist_len(const smartlist_t *sl) {
  51. raw_assert(sl);
  52. return (sl)->num_used;
  53. }
  54. /** Return the <b>idx</b>th element of sl.
  55. */
  56. static inline void *smartlist_get(const smartlist_t *sl, int idx);
  57. static inline void *smartlist_get(const smartlist_t *sl, int idx) {
  58. raw_assert(sl);
  59. raw_assert(idx>=0);
  60. raw_assert(sl->num_used > idx);
  61. return sl->list[idx];
  62. }
  63. static inline void smartlist_set(smartlist_t *sl, int idx, void *val) {
  64. raw_assert(sl);
  65. raw_assert(idx>=0);
  66. raw_assert(sl->num_used > idx);
  67. sl->list[idx] = val;
  68. }
  69. #else /* !(defined(DEBUG_SMARTLIST)) */
  70. #define smartlist_len(sl) ((sl)->num_used)
  71. #define smartlist_get(sl, idx) ((sl)->list[idx])
  72. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  73. #endif /* defined(DEBUG_SMARTLIST) */
  74. /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
  75. * smartlist <b>sl</b>. */
  76. static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
  77. {
  78. if (idx1 != idx2) {
  79. void *elt = smartlist_get(sl, idx1);
  80. smartlist_set(sl, idx1, smartlist_get(sl, idx2));
  81. smartlist_set(sl, idx2, elt);
  82. }
  83. }
  84. void smartlist_del(smartlist_t *sl, int idx);
  85. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  86. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  87. #endif /* !defined(TOR_CONTAINER_H) */