FS-9775: Adjusted to proper destructor pattern, cleaned up unneccessary zeroing in allocators, and reduced preallocs to use memset

This commit is contained in:
Shane Bryldt 2016-12-10 07:36:57 +00:00 committed by Mike Jerris
parent fdd7e7ec1c
commit e56c388707
8 changed files with 67 additions and 114 deletions

View File

@ -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_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_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, KS_DECLARE(ks_status_t) ks_dht_endpoint_init(ks_dht_endpoint_t *endpoint,
const ks_dht_nodeid_t *nodeid, 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_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_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_init(ks_dht_storageitem_t *item, struct bencode *v);
KS_DECLARE(ks_status_t) ks_dht_storageitem_deinit(ks_dht_storageitem_t *item); KS_DECLARE(ks_status_t) ks_dht_storageitem_deinit(ks_dht_storageitem_t *item);

View File

@ -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 = pool;
d->pool_alloc = pool_alloc; 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; 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(dht);
ks_assert(pool); ks_assert(pool);
memset(dht, 0, sizeof(ks_dht_t));
dht->pool = pool; dht->pool = pool;
dht->pool_alloc = KS_FALSE; 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; 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_pool_t *pool;
ks_bool_t pool_alloc = dht->pool_alloc; ks_bool_t pool_alloc;
ks_assert(dht);
ks_assert(*dht);
pool = (*dht)->pool;
pool_alloc = (*dht)->pool_alloc;
ks_dht_deinit(dht); ks_dht_deinit(*dht);
ks_pool_free(pool, dht); ks_pool_free(pool, *dht);
if (pool_alloc) { if (pool_alloc) {
ks_pool_close(&pool); ks_pool_close(&pool);
} }
*dht = NULL;
return KS_STATUS_SUCCESS; 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; ks_dht_message_t *msg;
while (ks_q_pop_timeout(dht->send_q, (void **)&msg, 1) == KS_STATUS_SUCCESS && msg) { while (ks_q_pop_timeout(dht->send_q, (void **)&msg, 1) == KS_STATUS_SUCCESS && msg) {
ks_dht_message_deinit(msg); ks_dht_message_deinit(msg);
ks_dht_message_free(msg); ks_dht_message_free(&msg);
} }
ks_q_destroy(&dht->send_q); ks_q_destroy(&dht->send_q);
dht->send_q = NULL; dht->send_q = NULL;
} }
if (dht->send_q_unsent) { if (dht->send_q_unsent) {
ks_dht_message_deinit(dht->send_q_unsent); ks_dht_message_deinit(dht->send_q_unsent);
ks_dht_message_free(dht->send_q_unsent); ks_dht_message_free(&dht->send_q_unsent);
dht->send_q_unsent = NULL;
} }
for (int32_t i = 0; i < dht->endpoints_size; ++i) { for (int32_t i = 0; i < dht->endpoints_size; ++i) {
ks_dht_endpoint_t *ep = dht->endpoints[i]; ks_dht_endpoint_t *ep = dht->endpoints[i];
ks_dht_endpoint_deinit(ep); ks_dht_endpoint_deinit(ep);
ks_dht_endpoint_free(ep); ks_dht_endpoint_free(&ep);
} }
dht->endpoints_size = 0; dht->endpoints_size = 0;
if (dht->endpoints) { 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) { 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); ks_socket_close(&sock);
return KS_STATUS_FAIL; return KS_STATUS_FAIL;
} }
@ -701,7 +664,7 @@ KS_DECLARE(void) ks_dht_idle_send(ks_dht_t *dht)
dht->send_q_unsent = message; dht->send_q_unsent = message;
} else if (ret == KS_STATUS_SUCCESS) { } else if (ret == KS_STATUS_SUCCESS) {
ks_dht_message_deinit(message); 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: done:
if (ret != KS_STATUS_SUCCESS && error) { if (ret != KS_STATUS_SUCCESS && error) {
ks_dht_message_deinit(error); ks_dht_message_deinit(error);
ks_dht_message_free(error); ks_dht_message_free(&error);
} }
return ret; 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 (ret != KS_STATUS_SUCCESS) {
if (trans) { if (trans) {
ks_dht_transaction_deinit(trans); ks_dht_transaction_deinit(trans);
ks_dht_transaction_free(trans); ks_dht_transaction_free(&trans);
} }
if (msg) { if (msg) {
ks_dht_message_deinit(msg); ks_dht_message_deinit(msg);
ks_dht_message_free(msg); ks_dht_message_free(&msg);
} }
*message = NULL; *message = NULL;
} }
@ -899,7 +862,7 @@ KS_DECLARE(ks_status_t) ks_dht_setup_response(ks_dht_t *dht,
done: done:
if (ret != KS_STATUS_SUCCESS && msg) { if (ret != KS_STATUS_SUCCESS && msg) {
ks_dht_message_deinit(msg); ks_dht_message_deinit(msg);
ks_dht_message_free(msg); ks_dht_message_free(&msg);
*message = NULL; *message = NULL;
} }
return ret; return ret;

View File

@ -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_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_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); 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_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_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_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); 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_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_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_DECLARE(ks_status_t) ks_dht_transaction_init(ks_dht_transaction_t *transaction,
ks_sockaddr_t *raddr, ks_sockaddr_t *raddr,

View File

@ -27,6 +27,8 @@ KS_DECLARE(ks_status_t) ks_dht_endpoint_prealloc(ks_dht_endpoint_t *endpoint, ks
ks_assert(endpoint); ks_assert(endpoint);
ks_assert(pool); ks_assert(pool);
memset(endpoint, 0, sizeof(ks_dht_endpoint_t));
endpoint->pool = pool; endpoint->pool = pool;
endpoint->sock = KS_SOCK_INVALID; 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_assert(*endpoint);
ks_dht_endpoint_deinit(endpoint); ks_dht_endpoint_deinit(*endpoint);
ks_pool_free(endpoint->pool, endpoint); ks_pool_free((*endpoint)->pool, *endpoint);
*endpoint = NULL;
return KS_STATUS_SUCCESS; return KS_STATUS_SUCCESS;
} }

View File

@ -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(message);
ks_assert(pool); ks_assert(pool);
*message = msg = ks_pool_alloc(pool, sizeof(ks_dht_message_t)); *message = msg = ks_pool_alloc(pool, sizeof(ks_dht_message_t));
msg->pool = pool; 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; 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(message);
ks_assert(pool); ks_assert(pool);
memset(message, 0, sizeof(ks_dht_message_t));
message->pool = pool; 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; 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_assert(*message);
ks_dht_message_deinit(message); ks_dht_message_deinit(*message);
ks_pool_free(message->pool, message); ks_pool_free((*message)->pool, *message);
*message = NULL;
return KS_STATUS_SUCCESS; return KS_STATUS_SUCCESS;
} }

View File

@ -11,13 +11,9 @@ KS_DECLARE(ks_status_t) ks_dht_storageitem_alloc(ks_dht_storageitem_t **item, ks
ks_assert(item); ks_assert(item);
ks_assert(pool); ks_assert(pool);
*item = si = ks_pool_alloc(pool, sizeof(ks_dht_storageitem_t)); *item = si = ks_pool_alloc(pool, sizeof(ks_dht_storageitem_t));
si->pool = pool; si->pool = pool;
si->v = NULL;
si->mutable = KS_FALSE;
si->salt_length = 0;
si->seq = 0;
return KS_STATUS_SUCCESS; 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(item);
ks_assert(pool); ks_assert(pool);
memset(item, 0, sizeof(ks_dht_storageitem_t));
item->pool = pool; item->pool = pool;
item->v = NULL;
item->mutable = KS_FALSE;
item->salt_length = 0;
item->seq = 0;
return KS_STATUS_SUCCESS; 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_assert(*item);
ks_dht_storageitem_deinit(item); ks_dht_storageitem_deinit(*item);
ks_pool_free(item->pool, item); ks_pool_free((*item)->pool, *item);
*item = NULL;
return KS_STATUS_SUCCESS; return KS_STATUS_SUCCESS;
} }

View File

@ -10,14 +10,9 @@ KS_DECLARE(ks_status_t) ks_dht_transaction_alloc(ks_dht_transaction_t **transact
ks_assert(transaction); ks_assert(transaction);
ks_assert(pool); ks_assert(pool);
*transaction = tran = ks_pool_alloc(pool, sizeof(ks_dht_transaction_t)); *transaction = tran = ks_pool_alloc(pool, sizeof(ks_dht_transaction_t));
tran->pool = pool; 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; 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(transaction);
ks_assert(pool); ks_assert(pool);
memset(transaction, 0, sizeof(ks_dht_transaction_t));
transaction->pool = pool; 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; 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_assert(*transaction);
ks_dht_transaction_deinit(transaction); ks_dht_transaction_deinit(*transaction);
ks_pool_free(transaction->pool, transaction); ks_pool_free((*transaction)->pool, *transaction);
*transaction = NULL;
return KS_STATUS_SUCCESS; return KS_STATUS_SUCCESS;
} }

View File

@ -135,7 +135,7 @@ int main() {
err = ks_dht_deinit(dht1); err = ks_dht_deinit(dht1);
ok(err == KS_STATUS_SUCCESS); ok(err == KS_STATUS_SUCCESS);
err = ks_dht_free(dht1); err = ks_dht_free(&dht1);
ok(err == KS_STATUS_SUCCESS); ok(err == KS_STATUS_SUCCESS);
err = ks_shutdown(); err = ks_shutdown();