* _add() adds an element, _remove() removes an element if it's there,
* _choose() returns a random element.
*/
-#define SL_DEFAULT_CAPACITY 32
-
-smartlist_t *smartlist_create(int max_elements) {
+smartlist_t *smartlist_create(int capacity) {
smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
sl->num_used = 0;
- sl->max = max_elements;
- if (max_elements <= SL_DEFAULT_CAPACITY)
- sl->capacity = max_elements;
- else
- sl->capacity = SL_DEFAULT_CAPACITY;
+ sl->capacity = capacity;
sl->list = tor_malloc(sizeof(void *) * sl->capacity);
return sl;
}
/* add element to the list, but only if there's room */
void smartlist_add(smartlist_t *sl, void *element) {
- if (sl->num_used < sl->max) {
- if (sl->num_used >= sl->capacity) {
- sl->capacity *= 2;
- sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
- }
- sl->list[sl->num_used++] = element;
- } else
- log_fn(LOG_WARN,"We've already got %d elements, discarding.",sl->max);
+ if (sl->num_used >= sl->capacity) {
+ sl->capacity *= 2;
+ sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
+ }
+ sl->list[sl->num_used++] = element;
}
void smartlist_remove(smartlist_t *sl, void *element) {
typedef struct {
void **list;
int num_used;
- int max;
int capacity;
} smartlist_t;
-smartlist_t *smartlist_create(int max_elements);
+smartlist_t *smartlist_create(int capacity);
void smartlist_free(smartlist_t *sl);
void smartlist_grow_capacity(smartlist_t *sl, int n);
void smartlist_add(smartlist_t *sl, void *element);
log_fn(LOG_INFO, "Found %d servers that might support %d/%d pending connections.",
n_best_support, best_support, n_pending_connections);
- preferredexits = smartlist_create(MAX_ROUTERS_IN_DIR);
+ preferredexits = smartlist_create(16);
add_nickname_list_to_smartlist(preferredexits,options.ExitNodes);
- excludedexits = smartlist_create(MAX_ROUTERS_IN_DIR);
+ excludedexits = smartlist_create(16);
add_nickname_list_to_smartlist(excludedexits,options.ExcludeNodes);
- sl = smartlist_create(MAX_ROUTERS_IN_DIR);
+ sl = smartlist_create(dir->n_routers);
/* If any routers definitely support any pending connections, choose one
* at random. */
log_fn(LOG_DEBUG, "Path is %d long; we want %d", cur_len,
state->desired_path_len);
- excludednodes = smartlist_create(MAX_ROUTERS_IN_DIR);
+ excludednodes = smartlist_create(16);
add_nickname_list_to_smartlist(excludednodes,options.ExcludeNodes);
if(cur_len == state->desired_path_len - 1) { /* Picking last node */
}
} else if(cur_len == 0) { /* picking first node */
/* try the nodes in EntryNodes first */
- sl = smartlist_create(MAX_ROUTERS_IN_DIR);
+ sl = smartlist_create(16);
add_nickname_list_to_smartlist(sl,options.EntryNodes);
/* XXX one day, consider picking chosen_exit knowing what's in EntryNodes */
remove_twins_from_smartlist(sl,router_get_by_nickname(state->chosen_exit));
choice = smartlist_choose(sl);
smartlist_free(sl);
if(!choice) {
- sl = smartlist_create(MAX_ROUTERS_IN_DIR);
+ sl = smartlist_create(32);
router_add_running_routers_to_smartlist(sl);
remove_twins_from_smartlist(sl,router_get_by_nickname(state->chosen_exit));
smartlist_subtract(sl,excludednodes);
}
} else {
log_fn(LOG_DEBUG, "Contemplating intermediate hop: random choice.");
- sl = smartlist_create(MAX_ROUTERS_IN_DIR);
+ sl = smartlist_create(32);
router_add_running_routers_to_smartlist(sl);
remove_twins_from_smartlist(sl,router_get_by_nickname(state->chosen_exit));
for (i = 0, cpath = *head_ptr; i < cur_len; ++i, cpath=cpath->next) {
if(!routerlist)
return NULL;
- sl = smartlist_create(MAX_ROUTERS_IN_DIR);
+ sl = smartlist_create(8);
for(i=0;i<routerlist->n_routers;i++) {
router = routerlist->routers[i];
if(router->dir_port > 0 && router->is_running)
} else {
end = str + strlen(str);
}
-
- tokens = smartlist_create(128);
+
+ tokens = smartlist_create(16);
if (tokenize_string(str,end,tokens,1)) {
log_fn(LOG_WARN, "Error tokenizing directory"); goto err;
}
tok->args[0]);
goto err;
}
-
+
tok = (directory_token_t*)tokens->list[0];
if (tok->tp != K_SIGNED_DIRECTORY) {
log_fn(LOG_WARN, "Directory doesn't start with signed-directory.");
token_free((directory_token_t*)tokens->list[i]);
}
smartlist_free(tokens);
- tokens = smartlist_create(128);
+ tokens = smartlist_create(16);
if (tokenize_string(str,str+strlen(str),tokens,1)<0) {
log_fn(LOG_WARN, "Error tokenizing signature"); goto err;
}
log_fn(LOG_WARN, "Couldn't compute router hash.");
return NULL;
}
- tokens = smartlist_create(128);
+ tokens = smartlist_create(16);
if (tokenize_string(s,end,tokens,0)) {
log_fn(LOG_WARN, "Error tokeninzing router descriptor."); goto err;
}