Tor  0.4.7.0-alpha-dev
smartlist_core.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * \file smartlist_core.h
8  * \brief Top-level declarations for the smartlist_t dynamic array type.
9  **/
10 
11 #ifndef TOR_SMARTLIST_CORE_H
12 #define TOR_SMARTLIST_CORE_H
13 
14 #include <stddef.h>
15 
16 #include "lib/cc/compat_compiler.h"
17 #include "lib/cc/torint.h"
19 
20 /** A resizeable list of pointers, with associated helpful functionality.
21  *
22  * The members of this struct are exposed only so that macros and inlines can
23  * use them; all access to smartlist internals should go through the functions
24  * and macros defined here.
25  **/
26 typedef struct smartlist_t {
27  /** @{ */
28  /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
29  * before it needs to be resized. Only the first <b>num_used</b> (<=
30  * capacity) elements point to valid data.
31  */
32  void **list;
33  int num_used;
34  int capacity;
35  /** @} */
36 } smartlist_t;
37 
40 #define smartlist_free(sl) FREE_AND_NULL(smartlist_t, smartlist_free_, (sl))
41 
43 void smartlist_add(smartlist_t *sl, void *element);
44 void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
45 void smartlist_add_strdup(struct smartlist_t *sl, const char *string);
46 void smartlist_grow(smartlist_t *sl, size_t new_size);
47 
48 void smartlist_remove(smartlist_t *sl, const void *element);
49 void smartlist_remove_keeporder(smartlist_t *sl, const void *element);
51 
52 int smartlist_contains(const smartlist_t *sl, const void *element);
53 
54 /* smartlist_choose() is defined in crypto.[ch] */
55 #ifdef DEBUG_SMARTLIST
56 #include "lib/err/torerr.h"
57 #include <stdlib.h>
58 /** Return the number of items in sl.
59  */
60 static inline int smartlist_len(const smartlist_t *sl);
61 static inline int smartlist_len(const smartlist_t *sl) {
62  raw_assert(sl);
63  return (sl)->num_used;
64 }
65 /** Return the <b>idx</b>th element of sl.
66  */
67 static inline void *smartlist_get(const smartlist_t *sl, int idx);
68 static inline void *smartlist_get(const smartlist_t *sl, int idx) {
69  raw_assert(sl);
70  raw_assert(idx>=0);
71  raw_assert(sl->num_used > idx);
72  return sl->list[idx];
73 }
74 static inline void smartlist_set(smartlist_t *sl, int idx, void *val) {
75  raw_assert(sl);
76  raw_assert(idx>=0);
77  raw_assert(sl->num_used > idx);
78  sl->list[idx] = val;
79 }
80 #else /* !defined(DEBUG_SMARTLIST) */
81 #define smartlist_len(sl) ((sl)->num_used)
82 #define smartlist_get(sl, idx) ((sl)->list[idx])
83 #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
84 #endif /* defined(DEBUG_SMARTLIST) */
85 
86 /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
87  * smartlist <b>sl</b>. */
88 static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
89 {
90  if (idx1 != idx2) {
91  void *elt = smartlist_get(sl, idx1);
92  smartlist_set(sl, idx1, smartlist_get(sl, idx2));
93  smartlist_set(sl, idx2, elt);
94  }
95 }
96 
97 void smartlist_del(smartlist_t *sl, int idx);
98 void smartlist_del_keeporder(smartlist_t *sl, int idx);
99 void smartlist_insert(smartlist_t *sl, int idx, void *val);
100 
101 #endif /* !defined(TOR_SMARTLIST_CORE_H) */
Utility macros to handle different features and behavior in different compilers.
void smartlist_grow(smartlist_t *sl, size_t new_size)
void smartlist_insert(smartlist_t *sl, int idx, void *val)
void smartlist_remove_keeporder(smartlist_t *sl, const void *element)
void * smartlist_pop_last(smartlist_t *sl)
static void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
smartlist_t * smartlist_new(void)
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
int smartlist_contains(const smartlist_t *sl, const void *element)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_clear(smartlist_t *sl)
void smartlist_remove(smartlist_t *sl, const void *element)
void smartlist_free_(smartlist_t *sl)
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2)
void smartlist_del(smartlist_t *sl, int idx)
void smartlist_del_keeporder(smartlist_t *sl, int idx)
void ** list
Macros to implement mocking and selective exposure for the test code.
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
Headers for torerr.c.
Integer definitions used throughout Tor.