smartlist_core.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, 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_grow(smartlist_t *sl, size_t new_size);
  40. void smartlist_remove(smartlist_t *sl, const void *element);
  41. void smartlist_remove_keeporder(smartlist_t *sl, const void *element);
  42. void *smartlist_pop_last(smartlist_t *sl);
  43. int smartlist_contains(const smartlist_t *sl, const void *element);
  44. /* smartlist_choose() is defined in crypto.[ch] */
  45. #ifdef DEBUG_SMARTLIST
  46. #include "lib/err/torerr.h"
  47. #include <stdlib.h>
  48. /** Return the number of items in sl.
  49. */
  50. static inline int smartlist_len(const smartlist_t *sl);
  51. static inline int smartlist_len(const smartlist_t *sl) {
  52. raw_assert(sl);
  53. return (sl)->num_used;
  54. }
  55. /** Return the <b>idx</b>th element of sl.
  56. */
  57. static inline void *smartlist_get(const smartlist_t *sl, int idx);
  58. static inline void *smartlist_get(const smartlist_t *sl, int idx) {
  59. raw_assert(sl);
  60. raw_assert(idx>=0);
  61. raw_assert(sl->num_used > idx);
  62. return sl->list[idx];
  63. }
  64. static inline void smartlist_set(smartlist_t *sl, int idx, void *val) {
  65. raw_assert(sl);
  66. raw_assert(idx>=0);
  67. raw_assert(sl->num_used > idx);
  68. sl->list[idx] = val;
  69. }
  70. #else /* !defined(DEBUG_SMARTLIST) */
  71. #define smartlist_len(sl) ((sl)->num_used)
  72. #define smartlist_get(sl, idx) ((sl)->list[idx])
  73. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  74. #endif /* defined(DEBUG_SMARTLIST) */
  75. /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
  76. * smartlist <b>sl</b>. */
  77. static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
  78. {
  79. if (idx1 != idx2) {
  80. void *elt = smartlist_get(sl, idx1);
  81. smartlist_set(sl, idx1, smartlist_get(sl, idx2));
  82. smartlist_set(sl, idx2, elt);
  83. }
  84. }
  85. void smartlist_del(smartlist_t *sl, int idx);
  86. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  87. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  88. #endif /* !defined(TOR_SMARTLIST_CORE_H) */