linked list cleanups

added list methods invoke(), destroy_offset(), destroy_function()
simplified list destruction when destroying its items
This commit is contained in:
Martin Willi
2006-10-24 08:46:17 +00:00
parent 5c4cc9a4e3
commit 55bbff11ec
26 changed files with 312 additions and 551 deletions
+1 -8
View File
@@ -333,14 +333,7 @@ static void destroy(private_connection_t *this)
{
if (ref_put(&this->refcount))
{
proposal_t *proposal;
while (this->proposals->remove_last(this->proposals, (void**)&proposal) == SUCCESS)
{
proposal->destroy(proposal);
}
this->proposals->destroy(this->proposals);
this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
this->my_host->destroy(this->my_host);
this->other_host->destroy(this->other_host);
free(this->name);
@@ -215,14 +215,8 @@ static iterator_t* create_iterator(private_local_connection_store_t *this)
*/
static void destroy (private_local_connection_store_t *this)
{
connection_t *connection;
pthread_mutex_lock(&(this->mutex));
while (this->connections->remove_last(this->connections, (void**)&connection) == SUCCESS)
{
connection->destroy(connection);
}
this->connections->destroy(this->connections);
this->connections->destroy_offset(this->connections, offsetof(connection_t, destroy));
pthread_mutex_unlock(&(this->mutex));
free(this);
}
@@ -63,16 +63,8 @@ struct shared_key_t {
*/
static void shared_key_destroy(shared_key_t *this)
{
identification_t *id;
/* destroy peer id list */
while (this->peers->remove_last(this->peers, (void**)&id) == SUCCESS)
{
id->destroy(id);
}
this->peers->destroy(this->peers);
this->peers->destroy_offset(this->peers, offsetof(identification_t, destroy));
chunk_free(&this->secret);
free(this);
}
@@ -1077,48 +1069,11 @@ error:
*/
static void destroy(private_local_credential_store_t *this)
{
x509_t *cert;
crl_t *crl;
rsa_private_key_t *key;
shared_key_t *shared_key;
/* destroy cert list */
while (this->certs->remove_last(this->certs, (void**)&cert) == SUCCESS)
{
cert->destroy(cert);
}
this->certs->destroy(this->certs);
/* destroy ca cert list */
while (this->ca_certs->remove_last(this->ca_certs, (void**)&cert) == SUCCESS)
{
cert->destroy(cert);
}
this->ca_certs->destroy(this->ca_certs);
/* destroy crl list */
pthread_mutex_lock(&(this->crls_mutex));
while (this->crls->remove_last(this->crls, (void**)&crl) == SUCCESS)
{
crl->destroy(crl);
}
this->crls->destroy(this->crls);
pthread_mutex_unlock(&(this->crls_mutex));
/* destroy private key list */
while (this->private_keys->remove_last(this->private_keys, (void**)&key) == SUCCESS)
{
key->destroy(key);
}
this->private_keys->destroy(this->private_keys);
/* destroy shared keys list */
while (this->shared_keys->remove_last(this->shared_keys, (void**)&shared_key) == SUCCESS)
{
shared_key_destroy(shared_key);
}
this->shared_keys->destroy(this->shared_keys);
this->certs->destroy_offset(this->certs, offsetof(x509_t, destroy));
this->ca_certs->destroy_offset(this->ca_certs, offsetof(x509_t, destroy));
this->crls->destroy_offset(this->crls, offsetof(crl_t, destroy));
this->private_keys->destroy_offset(this->private_keys, offsetof(rsa_private_key_t, destroy));
this->shared_keys->destroy_function(this->shared_keys, (void*)shared_key_destroy);
free(this);
}
@@ -1128,7 +1083,7 @@ static void destroy(private_local_credential_store_t *this)
local_credential_store_t * local_credential_store_create(bool strict)
{
private_local_credential_store_t *this = malloc_thing(private_local_credential_store_t);
this->public.credential_store.get_shared_key = (status_t (*) (credential_store_t*,identification_t*,identification_t*,chunk_t*))get_shared_key;
this->public.credential_store.get_rsa_public_key = (rsa_public_key_t*(*)(credential_store_t*,identification_t*))get_rsa_public_key;
this->public.credential_store.get_rsa_private_key = (rsa_private_key_t* (*) (credential_store_t*,rsa_public_key_t*))get_rsa_private_key;
@@ -69,7 +69,6 @@ static bool contains_traffic_selectors(policy_t *policy, bool mine,
{
linked_list_t *selected;
bool contains = FALSE;
traffic_selector_t *to;
if (mine)
{
@@ -83,11 +82,7 @@ static bool contains_traffic_selectors(policy_t *policy, bool mine,
{
contains = TRUE;
}
while (selected->remove_last(selected, (void**)&to) == SUCCESS)
{
to->destroy(to);
}
selected->destroy(selected);
selected->destroy_offset(selected, offsetof(traffic_selector_t, destroy));
return contains;
}
@@ -249,14 +244,8 @@ static iterator_t* create_iterator(private_local_policy_store_t *this)
*/
static void destroy(private_local_policy_store_t *this)
{
policy_t *policy;
pthread_mutex_lock(&(this->mutex));
while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
{
policy->destroy(policy);
}
this->policies->destroy(this->policies);
this->policies->destroy_offset(this->policies, offsetof(policy_t, destroy));
pthread_mutex_unlock(&(this->mutex));
free(this);
}
+3 -22
View File
@@ -432,29 +432,10 @@ static void destroy(private_policy_t *this)
{
if (ref_put(&this->refcount))
{
proposal_t *proposal;
traffic_selector_t *traffic_selector;
/* delete proposals */
while(this->proposals->remove_last(this->proposals, (void**)&proposal) == SUCCESS)
{
proposal->destroy(proposal);
}
this->proposals->destroy(this->proposals);
/* delete traffic selectors */
while(this->my_ts->remove_last(this->my_ts, (void**)&traffic_selector) == SUCCESS)
{
traffic_selector->destroy(traffic_selector);
}
this->my_ts->destroy(this->my_ts);
/* delete traffic selectors */
while(this->other_ts->remove_last(this->other_ts, (void**)&traffic_selector) == SUCCESS)
{
traffic_selector->destroy(traffic_selector);
}
this->other_ts->destroy(this->other_ts);
this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
this->my_ts->destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy));
this->other_ts->destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy));
/* delete certification authorities */
if (this->my_ca)
+5 -20
View File
@@ -392,21 +392,6 @@ static proposal_t *clone_(private_proposal_t *this)
return &clone->public;
}
/**
* Frees all list items and destroys the list
*/
static void free_algo_list(linked_list_t *list)
{
algorithm_t *algo;
while(list->get_count(list) > 0)
{
list->remove_last(list, (void**)&algo);
free(algo);
}
list->destroy(list);
}
static status_t add_string_algo(private_proposal_t *this, chunk_t alg)
{
if (strncmp(alg.ptr, "aes128", alg.len) == 0)
@@ -489,11 +474,11 @@ static status_t add_string_algo(private_proposal_t *this, chunk_t alg)
*/
static void destroy(private_proposal_t *this)
{
free_algo_list(this->encryption_algos);
free_algo_list(this->integrity_algos);
free_algo_list(this->prf_algos);
free_algo_list(this->dh_groups);
free_algo_list(this->esns);
this->encryption_algos->destroy_function(this->encryption_algos, free);
this->integrity_algos->destroy_function(this->integrity_algos, free);
this->prf_algos->destroy_function(this->prf_algos, free);
this->dh_groups->destroy_function(this->dh_groups, free);
this->esns->destroy_function(this->esns, free);
free(this);
}