smartlist_core.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef TOR_SMARTLIST_CORE_H
  6. #define TOR_SMARTLIST_CORE_H
  7. #include <stddef.h>
  8. #include "lib/cc/compat_compiler.h"
  9. #include "lib/cc/torint.h"
  10. #include "lib/testsupport/testsupport.h"
  11. /** A resizeable list of pointers, with associated helpful functionality.
  12. *
  13. * The members of this struct are exposed only so that macros and inlines can
  14. * use them; all access to smartlist internals should go through the functions
  15. * and macros defined here.
  16. **/
  17. typedef struct smartlist_t {
  18. /** @{ */
  19. /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
  20. * before it needs to be resized. Only the first <b>num_used</b> (\<=
  21. * capacity) elements point to valid data.
  22. */
  23. void **list;
  24. int num_used;
  25. int capacity;
  26. /** @} */
  27. } smartlist_t;
  28. MOCK_DECL(smartlist_t *, smartlist_new, (void));
  29. MOCK_DECL(void, smartlist_free_, (smartlist_t *sl));
  30. #define smartlist_free(sl) FREE_AND_NULL(smartlist_t, smartlist_free_, (sl))
  31. void smartlist_clear(smartlist_t *sl);
  32. void smartlist_add(smartlist_t *sl, void *element);
  33. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  34. void smartlist_add_strdup(struct smartlist_t *sl, const char *string);
  35. void smartlist_remove(smartlist_t *sl, const void *element);
  36. void smartlist_remove_keeporder(smartlist_t *sl, const void *element);
  37. void *smartlist_pop_last(smartlist_t *sl);
  38. int smartlist_contains(const smartlist_t *sl, const void *element);
  39. /* smartlist_choose() is defined in crypto.[ch] */
  40. #ifdef DEBUG_SMARTLIST
  41. #include "lib/err/torerr.h"
  42. #include <stdlib.h>
  43. /** Return the number of items in sl.
  44. */
  45. static inline int smartlist_len(const smartlist_t *sl);
  46. static inline int smartlist_len(const smartlist_t *sl) {
  47. raw_assert(sl);
  48. return (sl)->num_used;
  49. }
  50. /** Return the <b>idx</b>th element of sl.
  51. */
  52. static inline void *smartlist_get(const smartlist_t *sl, int idx);
  53. static inline void *smartlist_get(const smartlist_t *sl, int idx) {
  54. raw_assert(sl);
  55. raw_assert(idx>=0);
  56. raw_assert(sl->num_used > idx);
  57. return sl->list[idx];
  58. }
  59. static inline void smartlist_set(smartlist_t *sl, int idx, void *val) {
  60. raw_assert(sl);
  61. raw_assert(idx>=0);
  62. raw_assert(sl->num_used > idx);
  63. sl->list[idx] = val;
  64. }
  65. #else /* !(defined(DEBUG_SMARTLIST)) */
  66. #define smartlist_len(sl) ((sl)->num_used)
  67. #define smartlist_get(sl, idx) ((sl)->list[idx])
  68. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  69. #endif /* defined(DEBUG_SMARTLIST) */
  70. /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
  71. * smartlist <b>sl</b>. */
  72. static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
  73. {
  74. if (idx1 != idx2) {
  75. void *elt = smartlist_get(sl, idx1);
  76. smartlist_set(sl, idx1, smartlist_get(sl, idx2));
  77. smartlist_set(sl, idx2, elt);
  78. }
  79. }
  80. void smartlist_del(smartlist_t *sl, int idx);
  81. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  82. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  83. #endif /* !defined(TOR_CONTAINER_H) */