tor  0.4.1.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-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
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 
26 typedef struct smartlist_t {
32  void **list;
33  int num_used;
34  int capacity;
36 } smartlist_t;
37 
38 MOCK_DECL(smartlist_t *, smartlist_new, (void));
39 MOCK_DECL(void, smartlist_free_, (smartlist_t *sl));
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 
47 void smartlist_remove(smartlist_t *sl, const void *element);
48 void smartlist_remove_keeporder(smartlist_t *sl, const void *element);
50 
51 int smartlist_contains(const smartlist_t *sl, const void *element);
52 
53 /* smartlist_choose() is defined in crypto.[ch] */
54 #ifdef DEBUG_SMARTLIST
55 #include "lib/err/torerr.h"
56 #include <stdlib.h>
59 static inline int smartlist_len(const smartlist_t *sl);
60 static inline int smartlist_len(const smartlist_t *sl) {
61  raw_assert(sl);
62  return (sl)->num_used;
63 }
66 static inline void *smartlist_get(const smartlist_t *sl, int idx);
67 static inline void *smartlist_get(const smartlist_t *sl, int idx) {
68  raw_assert(sl);
69  raw_assert(idx>=0);
70  raw_assert(sl->num_used > idx);
71  return sl->list[idx];
72 }
73 static inline void smartlist_set(smartlist_t *sl, int idx, void *val) {
74  raw_assert(sl);
75  raw_assert(idx>=0);
76  raw_assert(sl->num_used > idx);
77  sl->list[idx] = val;
78 }
79 #else /* !(defined(DEBUG_SMARTLIST)) */
80 #define smartlist_len(sl) ((sl)->num_used)
81 #define smartlist_get(sl, idx) ((sl)->list[idx])
82 #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
83 #endif /* defined(DEBUG_SMARTLIST) */
84 
87 static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
88 {
89  if (idx1 != idx2) {
90  void *elt = smartlist_get(sl, idx1);
91  smartlist_set(sl, idx1, smartlist_get(sl, idx2));
92  smartlist_set(sl, idx2, elt);
93  }
94 }
95 
96 void smartlist_del(smartlist_t *sl, int idx);
97 void smartlist_del_keeporder(smartlist_t *sl, int idx);
98 void smartlist_insert(smartlist_t *sl, int idx, void *val);
99 
100 #endif /* !defined(TOR_CONTAINER_H) */
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
int smartlist_contains(const smartlist_t *sl, const void *element)
void * smartlist_pop_last(smartlist_t *sl)
void smartlist_insert(smartlist_t *sl, int idx, void *val)
void ** list
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2)
Integer definitions used throughout Tor.
void smartlist_add(smartlist_t *sl, void *element)
static void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
Utility macros to handle different features and behavior in different compilers.
void smartlist_del_keeporder(smartlist_t *sl, int idx)
void smartlist_clear(smartlist_t *sl)
void smartlist_remove_keeporder(smartlist_t *sl, const void *element)
struct smartlist_t smartlist_t
void smartlist_del(smartlist_t *sl, int idx)
Macros to implement mocking and selective exposure for the test code.
Headers for torerr.c.
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
void smartlist_remove(smartlist_t *sl, const void *element)