From e56c3887074962c1cbd0a2b209b8ef91f67f3054 Mon Sep 17 00:00:00 2001 From: Shane Bryldt Date: Sat, 10 Dec 2016 07:36:57 +0000 Subject: [PATCH] FS-9775: Adjusted to proper destructor pattern, cleaned up unneccessary zeroing in allocators, and reduced preallocs to use memset --- libs/libks/src/dht/ks_dht-int.h | 4 +- libs/libks/src/dht/ks_dht.c | 85 +++++++------------------ libs/libks/src/dht/ks_dht.h | 6 +- libs/libks/src/dht/ks_dht_endpoint.c | 11 +++- libs/libks/src/dht/ks_dht_message.c | 27 +++----- libs/libks/src/dht/ks_dht_storageitem.c | 21 +++--- libs/libks/src/dht/ks_dht_transaction.c | 25 +++----- libs/libks/test/testdht2.c | 2 +- 8 files changed, 67 insertions(+), 114 deletions(-) diff --git a/libs/libks/src/dht/ks_dht-int.h b/libs/libks/src/dht/ks_dht-int.h index d9ec6a8fb2..9c5b0ea691 100644 --- a/libs/libks/src/dht/ks_dht-int.h +++ b/libs/libks/src/dht/ks_dht-int.h @@ -57,7 +57,7 @@ KS_DECLARE(ks_status_t) ks_dht_process_response_get(ks_dht_t *dht, ks_dht_messag */ KS_DECLARE(ks_status_t) ks_dht_endpoint_alloc(ks_dht_endpoint_t **endpoint, ks_pool_t *pool); KS_DECLARE(ks_status_t) ks_dht_endpoint_prealloc(ks_dht_endpoint_t *endpoint, ks_pool_t *pool); -KS_DECLARE(ks_status_t) ks_dht_endpoint_free(ks_dht_endpoint_t *endpoint); +KS_DECLARE(ks_status_t) ks_dht_endpoint_free(ks_dht_endpoint_t **endpoint); KS_DECLARE(ks_status_t) ks_dht_endpoint_init(ks_dht_endpoint_t *endpoint, const ks_dht_nodeid_t *nodeid, @@ -70,7 +70,7 @@ KS_DECLARE(ks_status_t) ks_dht_endpoint_deinit(ks_dht_endpoint_t *endpoint); */ KS_DECLARE(ks_status_t) ks_dht_storageitem_alloc(ks_dht_storageitem_t **item, ks_pool_t *pool); KS_DECLARE(ks_status_t) ks_dht_storageitem_prealloc(ks_dht_storageitem_t *item, ks_pool_t *pool); -KS_DECLARE(ks_status_t) ks_dht_storageitem_free(ks_dht_storageitem_t *item); +KS_DECLARE(ks_status_t) ks_dht_storageitem_free(ks_dht_storageitem_t **item); KS_DECLARE(ks_status_t) ks_dht_storageitem_init(ks_dht_storageitem_t *item, struct bencode *v); KS_DECLARE(ks_status_t) ks_dht_storageitem_deinit(ks_dht_storageitem_t *item); diff --git a/libs/libks/src/dht/ks_dht.c b/libs/libks/src/dht/ks_dht.c index 6484a297cf..b4b25a0edd 100644 --- a/libs/libks/src/dht/ks_dht.c +++ b/libs/libks/src/dht/ks_dht.c @@ -18,29 +18,6 @@ KS_DECLARE(ks_status_t) ks_dht_alloc(ks_dht_t **dht, ks_pool_t *pool) d->pool = pool; d->pool_alloc = pool_alloc; - d->autoroute = KS_FALSE; - d->autoroute_port = 0; - d->registry_type = NULL; - d->registry_query = NULL; - d->registry_error = NULL; - d->bind_ipv4 = KS_FALSE; - d->bind_ipv6 = KS_FALSE; - d->endpoints = NULL; - d->endpoints_size = 0; - d->endpoints_hash = NULL; - d->endpoints_poll = NULL; - d->send_q = NULL; - d->send_q_unsent = NULL; - d->recv_buffer_length = 0; - d->transactionid_next = 0; - d->transactions_hash = NULL; - d->rt_ipv4 = NULL; - d->rt_ipv6 = NULL; - d->token_secret_current = 0; - d->token_secret_previous = 0; - d->token_secret_expiration = 0; - d->storage_hash = NULL; - return KS_STATUS_SUCCESS; } @@ -52,49 +29,36 @@ KS_DECLARE(ks_status_t) ks_dht_prealloc(ks_dht_t *dht, ks_pool_t *pool) ks_assert(dht); ks_assert(pool); + memset(dht, 0, sizeof(ks_dht_t)); + dht->pool = pool; dht->pool_alloc = KS_FALSE; - dht->autoroute = KS_FALSE; - dht->autoroute_port = 0; - dht->registry_type = NULL; - dht->registry_query = NULL; - dht->registry_error = NULL; - dht->bind_ipv4 = KS_FALSE; - dht->bind_ipv6 = KS_FALSE; - dht->endpoints = NULL; - dht->endpoints_size = 0; - dht->endpoints_hash = NULL; - dht->endpoints_poll = NULL; - dht->send_q = NULL; - dht->send_q_unsent = NULL; - dht->recv_buffer_length = 0; - dht->transactionid_next = 0; - dht->transactions_hash = NULL; - dht->rt_ipv4 = NULL; - dht->rt_ipv6 = NULL; - dht->token_secret_current = 0; - dht->token_secret_previous = 0; - dht->token_secret_expiration = 0; - dht->storage_hash = NULL; - return KS_STATUS_SUCCESS; } /** * */ -KS_DECLARE(ks_status_t) ks_dht_free(ks_dht_t *dht) +KS_DECLARE(ks_status_t) ks_dht_free(ks_dht_t **dht) { - ks_pool_t *pool = dht->pool; - ks_bool_t pool_alloc = dht->pool_alloc; + ks_pool_t *pool; + ks_bool_t pool_alloc; + + ks_assert(dht); + ks_assert(*dht); + + pool = (*dht)->pool; + pool_alloc = (*dht)->pool_alloc; - ks_dht_deinit(dht); - ks_pool_free(pool, dht); + ks_dht_deinit(*dht); + ks_pool_free(pool, *dht); if (pool_alloc) { ks_pool_close(&pool); } + *dht = NULL; + return KS_STATUS_SUCCESS; } @@ -184,20 +148,19 @@ KS_DECLARE(ks_status_t) ks_dht_deinit(ks_dht_t *dht) ks_dht_message_t *msg; while (ks_q_pop_timeout(dht->send_q, (void **)&msg, 1) == KS_STATUS_SUCCESS && msg) { ks_dht_message_deinit(msg); - ks_dht_message_free(msg); + ks_dht_message_free(&msg); } ks_q_destroy(&dht->send_q); dht->send_q = NULL; } if (dht->send_q_unsent) { ks_dht_message_deinit(dht->send_q_unsent); - ks_dht_message_free(dht->send_q_unsent); - dht->send_q_unsent = NULL; + ks_dht_message_free(&dht->send_q_unsent); } for (int32_t i = 0; i < dht->endpoints_size; ++i) { ks_dht_endpoint_t *ep = dht->endpoints[i]; ks_dht_endpoint_deinit(ep); - ks_dht_endpoint_free(ep); + ks_dht_endpoint_free(&ep); } dht->endpoints_size = 0; if (dht->endpoints) { @@ -360,7 +323,7 @@ KS_DECLARE(ks_status_t) ks_dht_bind(ks_dht_t *dht, const ks_dht_nodeid_t *nodeid } if (ks_dht_endpoint_init(ep, nodeid, addr, sock) != KS_STATUS_SUCCESS) { - ks_dht_endpoint_free(ep); + ks_dht_endpoint_free(&ep); ks_socket_close(&sock); return KS_STATUS_FAIL; } @@ -701,7 +664,7 @@ KS_DECLARE(void) ks_dht_idle_send(ks_dht_t *dht) dht->send_q_unsent = message; } else if (ret == KS_STATUS_SUCCESS) { ks_dht_message_deinit(message); - ks_dht_message_free(message); + ks_dht_message_free(&message); } } } @@ -778,7 +741,7 @@ KS_DECLARE(ks_status_t) ks_dht_send_error(ks_dht_t *dht, done: if (ret != KS_STATUS_SUCCESS && error) { ks_dht_message_deinit(error); - ks_dht_message_free(error); + ks_dht_message_free(&error); } return ret; } @@ -844,11 +807,11 @@ KS_DECLARE(ks_status_t) ks_dht_setup_query(ks_dht_t *dht, if (ret != KS_STATUS_SUCCESS) { if (trans) { ks_dht_transaction_deinit(trans); - ks_dht_transaction_free(trans); + ks_dht_transaction_free(&trans); } if (msg) { ks_dht_message_deinit(msg); - ks_dht_message_free(msg); + ks_dht_message_free(&msg); } *message = NULL; } @@ -899,7 +862,7 @@ KS_DECLARE(ks_status_t) ks_dht_setup_response(ks_dht_t *dht, done: if (ret != KS_STATUS_SUCCESS && msg) { ks_dht_message_deinit(msg); - ks_dht_message_free(msg); + ks_dht_message_free(&msg); *message = NULL; } return ret; diff --git a/libs/libks/src/dht/ks_dht.h b/libs/libks/src/dht/ks_dht.h index 977e410dde..b2df07ed3a 100644 --- a/libs/libks/src/dht/ks_dht.h +++ b/libs/libks/src/dht/ks_dht.h @@ -173,7 +173,7 @@ struct ks_dht_s { */ KS_DECLARE(ks_status_t) ks_dht_alloc(ks_dht_t **dht, ks_pool_t *pool); KS_DECLARE(ks_status_t) ks_dht_prealloc(ks_dht_t *dht, ks_pool_t *pool); -KS_DECLARE(ks_status_t) ks_dht_free(ks_dht_t *dht); +KS_DECLARE(ks_status_t) ks_dht_free(ks_dht_t **dht); KS_DECLARE(ks_status_t) ks_dht_init(ks_dht_t *dht); @@ -193,7 +193,7 @@ KS_DECLARE(ks_status_t) ks_dht_register_query(ks_dht_t *dht, const char *value, */ KS_DECLARE(ks_status_t) ks_dht_message_alloc(ks_dht_message_t **message, ks_pool_t *pool); KS_DECLARE(ks_status_t) ks_dht_message_prealloc(ks_dht_message_t *message, ks_pool_t *pool); -KS_DECLARE(ks_status_t) ks_dht_message_free(ks_dht_message_t *message); +KS_DECLARE(ks_status_t) ks_dht_message_free(ks_dht_message_t **message); KS_DECLARE(ks_status_t) ks_dht_message_init(ks_dht_message_t *message, ks_dht_endpoint_t *ep, ks_sockaddr_t *raddr, ks_bool_t alloc_data); KS_DECLARE(ks_status_t) ks_dht_message_deinit(ks_dht_message_t *message); @@ -222,7 +222,7 @@ KS_DECLARE(ks_status_t) ks_dht_message_error(ks_dht_message_t *message, */ KS_DECLARE(ks_status_t) ks_dht_transaction_alloc(ks_dht_transaction_t **transaction, ks_pool_t *pool); KS_DECLARE(ks_status_t) ks_dht_transaction_prealloc(ks_dht_transaction_t *transaction, ks_pool_t *pool); -KS_DECLARE(ks_status_t) ks_dht_transaction_free(ks_dht_transaction_t *transaction); +KS_DECLARE(ks_status_t) ks_dht_transaction_free(ks_dht_transaction_t **transaction); KS_DECLARE(ks_status_t) ks_dht_transaction_init(ks_dht_transaction_t *transaction, ks_sockaddr_t *raddr, diff --git a/libs/libks/src/dht/ks_dht_endpoint.c b/libs/libks/src/dht/ks_dht_endpoint.c index eb84cb7ead..e31a29a6a0 100644 --- a/libs/libks/src/dht/ks_dht_endpoint.c +++ b/libs/libks/src/dht/ks_dht_endpoint.c @@ -27,6 +27,8 @@ KS_DECLARE(ks_status_t) ks_dht_endpoint_prealloc(ks_dht_endpoint_t *endpoint, ks ks_assert(endpoint); ks_assert(pool); + memset(endpoint, 0, sizeof(ks_dht_endpoint_t)); + endpoint->pool = pool; endpoint->sock = KS_SOCK_INVALID; @@ -36,12 +38,15 @@ KS_DECLARE(ks_status_t) ks_dht_endpoint_prealloc(ks_dht_endpoint_t *endpoint, ks /** * */ -KS_DECLARE(ks_status_t) ks_dht_endpoint_free(ks_dht_endpoint_t *endpoint) +KS_DECLARE(ks_status_t) ks_dht_endpoint_free(ks_dht_endpoint_t **endpoint) { ks_assert(endpoint); + ks_assert(*endpoint); - ks_dht_endpoint_deinit(endpoint); - ks_pool_free(endpoint->pool, endpoint); + ks_dht_endpoint_deinit(*endpoint); + ks_pool_free((*endpoint)->pool, *endpoint); + + *endpoint = NULL; return KS_STATUS_SUCCESS; } diff --git a/libs/libks/src/dht/ks_dht_message.c b/libs/libks/src/dht/ks_dht_message.c index 51a8a170de..122983ca7d 100644 --- a/libs/libks/src/dht/ks_dht_message.c +++ b/libs/libks/src/dht/ks_dht_message.c @@ -10,15 +10,9 @@ KS_DECLARE(ks_status_t) ks_dht_message_alloc(ks_dht_message_t **message, ks_pool ks_assert(message); ks_assert(pool); - + *message = msg = ks_pool_alloc(pool, sizeof(ks_dht_message_t)); msg->pool = pool; - msg->endpoint = NULL; - msg->raddr = (const ks_sockaddr_t){ 0 }; - msg->args = NULL; - msg->type[0] = '\0'; - msg->transactionid_length = 0; - msg->data = NULL; return KS_STATUS_SUCCESS; } @@ -30,14 +24,10 @@ KS_DECLARE(ks_status_t) ks_dht_message_prealloc(ks_dht_message_t *message, ks_po { ks_assert(message); ks_assert(pool); - + + memset(message, 0, sizeof(ks_dht_message_t)); + message->pool = pool; - message->endpoint = NULL; - message->raddr = (const ks_sockaddr_t){ 0 }; - message->args = NULL; - message->type[0] = '\0'; - message->transactionid_length = 0; - message->data = NULL; return KS_STATUS_SUCCESS; } @@ -45,12 +35,15 @@ KS_DECLARE(ks_status_t) ks_dht_message_prealloc(ks_dht_message_t *message, ks_po /** * */ -KS_DECLARE(ks_status_t) ks_dht_message_free(ks_dht_message_t *message) +KS_DECLARE(ks_status_t) ks_dht_message_free(ks_dht_message_t **message) { ks_assert(message); + ks_assert(*message); - ks_dht_message_deinit(message); - ks_pool_free(message->pool, message); + ks_dht_message_deinit(*message); + ks_pool_free((*message)->pool, *message); + + *message = NULL; return KS_STATUS_SUCCESS; } diff --git a/libs/libks/src/dht/ks_dht_storageitem.c b/libs/libks/src/dht/ks_dht_storageitem.c index 8672b77b42..e785528463 100644 --- a/libs/libks/src/dht/ks_dht_storageitem.c +++ b/libs/libks/src/dht/ks_dht_storageitem.c @@ -11,13 +11,9 @@ KS_DECLARE(ks_status_t) ks_dht_storageitem_alloc(ks_dht_storageitem_t **item, ks ks_assert(item); ks_assert(pool); - + *item = si = ks_pool_alloc(pool, sizeof(ks_dht_storageitem_t)); si->pool = pool; - si->v = NULL; - si->mutable = KS_FALSE; - si->salt_length = 0; - si->seq = 0; return KS_STATUS_SUCCESS; } @@ -30,11 +26,9 @@ KS_DECLARE(ks_status_t) ks_dht_storageitem_prealloc(ks_dht_storageitem_t *item, ks_assert(item); ks_assert(pool); + memset(item, 0, sizeof(ks_dht_storageitem_t)); + item->pool = pool; - item->v = NULL; - item->mutable = KS_FALSE; - item->salt_length = 0; - item->seq = 0; return KS_STATUS_SUCCESS; } @@ -42,12 +36,15 @@ KS_DECLARE(ks_status_t) ks_dht_storageitem_prealloc(ks_dht_storageitem_t *item, /** * */ -KS_DECLARE(ks_status_t) ks_dht_storageitem_free(ks_dht_storageitem_t *item) +KS_DECLARE(ks_status_t) ks_dht_storageitem_free(ks_dht_storageitem_t **item) { ks_assert(item); + ks_assert(*item); - ks_dht_storageitem_deinit(item); - ks_pool_free(item->pool, item); + ks_dht_storageitem_deinit(*item); + ks_pool_free((*item)->pool, *item); + + *item = NULL; return KS_STATUS_SUCCESS; } diff --git a/libs/libks/src/dht/ks_dht_transaction.c b/libs/libks/src/dht/ks_dht_transaction.c index a01579d7b7..604cb0353a 100644 --- a/libs/libks/src/dht/ks_dht_transaction.c +++ b/libs/libks/src/dht/ks_dht_transaction.c @@ -10,14 +10,9 @@ KS_DECLARE(ks_status_t) ks_dht_transaction_alloc(ks_dht_transaction_t **transact ks_assert(transaction); ks_assert(pool); - + *transaction = tran = ks_pool_alloc(pool, sizeof(ks_dht_transaction_t)); tran->pool = pool; - tran->raddr = (const ks_sockaddr_t){ 0 }; - tran->transactionid = 0; - tran->callback = NULL; - tran->expiration = 0; - tran->finished = KS_FALSE; return KS_STATUS_SUCCESS; } @@ -29,13 +24,10 @@ KS_DECLARE(ks_status_t) ks_dht_transaction_prealloc(ks_dht_transaction_t *transa { ks_assert(transaction); ks_assert(pool); - + + memset(transaction, 0, sizeof(ks_dht_transaction_t)); + transaction->pool = pool; - transaction->raddr = (const ks_sockaddr_t){ 0 }; - transaction->transactionid = 0; - transaction->callback = NULL; - transaction->expiration = 0; - transaction->finished = KS_FALSE; return KS_STATUS_SUCCESS; } @@ -43,12 +35,15 @@ KS_DECLARE(ks_status_t) ks_dht_transaction_prealloc(ks_dht_transaction_t *transa /** * */ -KS_DECLARE(ks_status_t) ks_dht_transaction_free(ks_dht_transaction_t *transaction) +KS_DECLARE(ks_status_t) ks_dht_transaction_free(ks_dht_transaction_t **transaction) { ks_assert(transaction); + ks_assert(*transaction); - ks_dht_transaction_deinit(transaction); - ks_pool_free(transaction->pool, transaction); + ks_dht_transaction_deinit(*transaction); + ks_pool_free((*transaction)->pool, *transaction); + + *transaction = NULL; return KS_STATUS_SUCCESS; } diff --git a/libs/libks/test/testdht2.c b/libs/libks/test/testdht2.c index 961a871361..618f034488 100644 --- a/libs/libks/test/testdht2.c +++ b/libs/libks/test/testdht2.c @@ -135,7 +135,7 @@ int main() { err = ks_dht_deinit(dht1); ok(err == KS_STATUS_SUCCESS); - err = ks_dht_free(dht1); + err = ks_dht_free(&dht1); ok(err == KS_STATUS_SUCCESS); err = ks_shutdown();