From ad4999488eadac3a10de99caf50b732af8b771b9 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 22 Aug 2014 17:47:51 +0200 Subject: Remove VECTOR_ALLOC It is done automatically now if the VECTOR is zeroed before. --- src/fastd.c | 4 ---- src/peer.c | 2 -- src/peer_hashtable.c | 6 +----- src/poll.c | 2 +- src/shell.c | 5 +---- src/vector.c | 21 +++++---------------- src/vector.h | 11 ----------- 7 files changed, 8 insertions(+), 43 deletions(-) diff --git a/src/fastd.c b/src/fastd.c index 9be4290..589a293 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -533,10 +533,6 @@ static inline void init(int argc, char *argv[]) { ctx.next_maintenance = fastd_in_seconds(MAINTENANCE_INTERVAL); ctx.unknown_handshakes[0].timeout = ctx.now; - VECTOR_ALLOC(ctx.eth_addrs, 0); - VECTOR_ALLOC(ctx.peers, 0); - VECTOR_ALLOC(ctx.async_pids, 0); - #ifdef WITH_DYNAMIC_PEERS fastd_sem_init(&ctx.verify_limit, VERIFY_LIMIT); #endif diff --git a/src/peer.c b/src/peer.c index 19b10d8..1ae7b3c 100644 --- a/src/peer.c +++ b/src/peer.c @@ -729,8 +729,6 @@ fastd_peer_t* fastd_peer_add(fastd_peer_config_t *peer_conf) { if (peer_conf) { peer->config = peer_conf; - VECTOR_ALLOC(peer->remotes, 0); - fastd_remote_config_t *remote_config; for (remote_config = peer_conf->remotes; remote_config; remote_config = remote_config->next) { fastd_remote_t remote = {.config = remote_config}; diff --git a/src/peer_hashtable.c b/src/peer_hashtable.c index b835ea9..321b0d5 100644 --- a/src/peer_hashtable.c +++ b/src/peer_hashtable.c @@ -44,11 +44,7 @@ void fastd_peer_hashtable_init(void) { fastd_random_bytes(&ctx.peer_addr_ht_seed, sizeof(ctx.peer_addr_ht_seed), false); - ctx.peer_addr_ht = fastd_new_array(PEER_ADDR_HT_SIZE, __typeof__(*ctx.peer_addr_ht)); - - size_t i; - for (i = 0; i < PEER_ADDR_HT_SIZE; i++) - VECTOR_ALLOC(ctx.peer_addr_ht[i], 0); + ctx.peer_addr_ht = fastd_new0_array(PEER_ADDR_HT_SIZE, __typeof__(*ctx.peer_addr_ht)); } /** Frees the resources used by the hashtable */ diff --git a/src/poll.c b/src/poll.c index a0b41ba..3f94379 100644 --- a/src/poll.c +++ b/src/poll.c @@ -180,7 +180,7 @@ void fastd_poll_handle(void) { #else void fastd_poll_init(void) { - VECTOR_ALLOC(ctx.pollfds, 2 + ctx.n_socks); + VECTOR_RESIZE(ctx.pollfds, 2 + ctx.n_socks); VECTOR_INDEX(ctx.pollfds, 0) = (struct pollfd) { .fd = -1, diff --git a/src/shell.c b/src/shell.c index 87aa320..19dc6e5 100644 --- a/src/shell.c +++ b/src/shell.c @@ -54,10 +54,7 @@ struct fastd_shell_env { /** Allocated a new shell environment */ fastd_shell_env_t * fastd_shell_env_alloc(void) { - fastd_shell_env_t *env = fastd_new(fastd_shell_env_t); - VECTOR_ALLOC(env->entries, 0); - - return env; + return fastd_new0(fastd_shell_env_t); } /** Sets a variable in a shell environment */ diff --git a/src/vector.c b/src/vector.c index 04dd934..054470e 100644 --- a/src/vector.c +++ b/src/vector.c @@ -40,22 +40,6 @@ #define MIN_VECTOR_ALLOC 4 -/** - Allocates a new vector - - Internal function, use VECTOR_ALLOC() instead. -*/ -void _fastd_vector_alloc(fastd_vector_desc_t *desc, void **data, size_t n, size_t elemsize) { - desc->allocated = MIN_VECTOR_ALLOC; - - while (desc->allocated < n*3/2) - desc->allocated <<= 1; - - desc->length = n; - - *data = fastd_alloc(desc->allocated * elemsize); -} - /** Resizes a vector @@ -68,6 +52,11 @@ void _fastd_vector_resize(fastd_vector_desc_t *desc, void **data, size_t n, size size_t alloc = desc->allocated; + if (!alloc) { + alloc = MIN_VECTOR_ALLOC; + n = n*3/2; + } + while (alloc < n) alloc <<= 1; while (alloc > n*3 && alloc > MIN_VECTOR_ALLOC) diff --git a/src/vector.h b/src/vector.h index 0571d4d..8b60f4a 100644 --- a/src/vector.h +++ b/src/vector.h @@ -55,22 +55,11 @@ typedef struct fastd_vector_desc { -void _fastd_vector_alloc(fastd_vector_desc_t *desc, void **data, size_t n, size_t elemsize); void _fastd_vector_resize(fastd_vector_desc_t *desc, void **data, size_t n, size_t elemsize); void _fastd_vector_insert(fastd_vector_desc_t *desc, void **data, void *element, size_t pos, size_t elemsize); void _fastd_vector_delete(fastd_vector_desc_t *desc, void **data, size_t pos, size_t elemsize); -/** - Allocates resources for the vector \e a, starting with \e n elements - - \hideinitializer -*/ -#define VECTOR_ALLOC(v, n) ({ \ - __typeof__(v) *_v = &(v); \ - _fastd_vector_alloc(&_v->desc, (void**)&_v->data, (n), sizeof(*_v->data)); \ - }) - /** Resizes the vector \e a to \e n elements -- cgit v1.2.3