tor  0.4.2.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 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>
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 }
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 
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) */
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)
void smartlist_grow(smartlist_t *sl, size_t new_size)
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)