21 #define SMARTLIST_DEFAULT_CAPACITY 16
31 sl->
list = tor_calloc(
sizeof(
void *), sl->capacity);
52 memset(sl->
list, 0,
sizeof(
void *) * sl->num_used);
56 #if SIZE_MAX < INT_MAX
57 #error "We don't support systems where size_t is smaller than int."
65 #if (SIZE_MAX/SIZEOF_VOID_P) > INT_MAX
66 #define MAX_CAPACITY (INT_MAX)
68 #define MAX_CAPACITY (int)((SIZE_MAX / (sizeof(void*))))
71 raw_assert(size <= MAX_CAPACITY);
73 if (size > (
size_t) sl->capacity) {
74 size_t higher = (size_t) sl->capacity;
75 if (PREDICT_UNLIKELY(size > MAX_CAPACITY/2)) {
76 higher = MAX_CAPACITY;
81 sl->
list = tor_reallocarray(sl->
list,
sizeof(
void *),
83 memset(sl->
list + sl->capacity, 0,
84 sizeof(
void *) * (higher - sl->capacity));
85 sl->capacity = (int) higher;
87 #undef ASSERT_CAPACITY
101 if (new_size > (
size_t)sl->num_used) {
106 memset(sl->
list + sl->num_used, 0,
107 sizeof(
void *) * (new_size - sl->num_used));
111 sl->num_used = (int)new_size;
120 sl->
list[sl->num_used++] = element;
127 size_t new_size = (size_t)s1->num_used + (
size_t)s2->num_used;
128 raw_assert(new_size >= (
size_t) s1->num_used);
130 memcpy(s1->
list + s1->num_used, s2->
list, s2->num_used*
sizeof(
void*));
131 raw_assert(new_size <= INT_MAX);
132 s1->num_used = (int) new_size;
141 copy = tor_strdup(
string);
156 for (i=0; i < sl->num_used; i++)
157 if (sl->
list[i] == element) {
158 sl->
list[i] = sl->
list[--sl->num_used];
160 sl->
list[sl->num_used] = NULL;
169 int i, j, num_used_orig = sl->num_used;
173 for (i=j=0; j < num_used_orig; ++j) {
174 if (sl->
list[j] == element) {
180 memset(sl->
list + sl->num_used, 0,
181 sizeof(
void *) * (num_used_orig - sl->num_used));
191 void *tmp = sl->
list[--sl->num_used];
192 sl->
list[sl->num_used] = NULL;
204 for (i=0; i < sl->num_used; i++)
205 if (sl->
list[i] == element)
218 raw_assert(idx < sl->num_used);
219 sl->
list[idx] = sl->
list[--sl->num_used];
220 sl->
list[sl->num_used] = NULL;
232 raw_assert(idx < sl->num_used);
234 if (idx < sl->num_used)
235 memmove(sl->
list+idx, sl->
list+idx+1,
sizeof(
void*)*(sl->num_used-idx));
236 sl->
list[sl->num_used] = NULL;
248 raw_assert(idx <= sl->num_used);
249 if (idx == sl->num_used) {
254 if (idx < sl->num_used)
255 memmove(sl->
list + idx + 1, sl->
list + idx,
256 sizeof(
void*)*(sl->num_used-idx));
Headers for util_malloc.c.
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_ensure_capacity(smartlist_t *sl, size_t size)
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
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)
#define SMARTLIST_DEFAULT_CAPACITY
void smartlist_remove(smartlist_t *sl, const void *element)
void smartlist_free_(smartlist_t *sl)
void smartlist_del(smartlist_t *sl, int idx)
void smartlist_del_keeporder(smartlist_t *sl, int idx)
Top-level declarations for the smartlist_t dynamic array type.
#define MOCK_IMPL(rv, funcname, arglist)