aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorReto Buerki <reet@codelabs.ch>2012-09-07 15:44:37 +0200
committerTobias Brunner <tobias@strongswan.org>2013-03-19 15:23:47 +0100
commit8f1dfb3d9ea7f3170f62e1bafa17992fcff4fb29 (patch)
treeeb8531ae33778bf32a0dfe0cca3b70cf7da6e59a /src
parent39727696904fafbda04fbc02ccd058b12ec2a99a (diff)
downloadstrongswan-8f1dfb3d9ea7f3170f62e1bafa17992fcff4fb29.tar.bz2
strongswan-8f1dfb3d9ea7f3170f62e1bafa17992fcff4fb29.tar.xz
chunk_map: Store key, value pair in entry_t struct
To make the chunk map more robust it now stores a clone of the data chunk given on insertion. The entry struct is needed to properly free the allocated chunk after use.
Diffstat (limited to 'src')
-rw-r--r--src/charon-tkm/src/tkm/tkm_chunk_map.c60
1 files changed, 44 insertions, 16 deletions
diff --git a/src/charon-tkm/src/tkm/tkm_chunk_map.c b/src/charon-tkm/src/tkm/tkm_chunk_map.c
index 2a1a944de..03ff22836 100644
--- a/src/charon-tkm/src/tkm/tkm_chunk_map.c
+++ b/src/charon-tkm/src/tkm/tkm_chunk_map.c
@@ -45,44 +45,73 @@ struct private_tkm_chunk_map_t {
};
+/**
+ * Entry for hashtables
+ */
+typedef struct {
+ /** Key chunk */
+ chunk_t key;
+ /** Entry value */
+ uint64_t value;
+} entry_t;
+
+/**
+ * Destroy a hashtable entry
+ */
+static void entry_destroy(entry_t *this)
+{
+ chunk_free(&this->key);
+ free(this);
+}
+
METHOD(tkm_chunk_map_t, insert, void,
private_tkm_chunk_map_t * const this, const chunk_t * const data,
const uint64_t id)
{
- uint64_t *value = malloc_thing(uint64_t);
- *value = id;
+ entry_t *entry;
+ INIT(entry,
+ .key = chunk_clone(*data),
+ .value = id
+ );
this->lock->write_lock(this->lock);
- value = this->mappings->put(this->mappings, (void*)data, value);
+ entry = this->mappings->put(this->mappings, (void*)&entry->key, entry);
this->lock->unlock(this->lock);
- if (value)
+ if (entry)
{
- free(value);
+ entry_destroy(entry);
}
}
METHOD(tkm_chunk_map_t, get_id, uint64_t,
private_tkm_chunk_map_t * const this, chunk_t *data)
{
- uint64_t *value;
+ entry_t *entry;
this->lock->read_lock(this->lock);
- value = this->mappings->get(this->mappings, data);
+ entry = this->mappings->get(this->mappings, data);
this->lock->unlock(this->lock);
- return value == NULL ? 0 : *value;
+ if (!entry)
+ {
+ return 0;
+ }
+
+ return entry->value;
}
METHOD(tkm_chunk_map_t, remove_, bool,
private_tkm_chunk_map_t * const this, chunk_t *data)
{
+ entry_t *entry;
+
this->lock->write_lock(this->lock);
- uint64_t *value = this->mappings->remove(this->mappings, data);
+ entry = this->mappings->remove(this->mappings, data);
this->lock->unlock(this->lock);
- if (value)
+ if (entry)
{
- free(value);
+ entry_destroy(entry);
return TRUE;
}
else
@@ -94,21 +123,20 @@ METHOD(tkm_chunk_map_t, remove_, bool,
METHOD(tkm_chunk_map_t, destroy, void,
private_tkm_chunk_map_t *this)
{
- uint64_t *value;
+ entry_t *entry;
enumerator_t *enumerator;
this->lock->write_lock(this->lock);
enumerator = this->mappings->create_enumerator(this->mappings);
- while (enumerator->enumerate(enumerator, NULL, &value))
+ while (enumerator->enumerate(enumerator, NULL, &entry))
{
- this->mappings->remove_at(this->mappings, enumerator);
- free(value);
+ entry_destroy(entry);
}
enumerator->destroy(enumerator);
this->lock->unlock(this->lock);
- this->lock->destroy(this->lock);
this->mappings->destroy(this->mappings);
+ this->lock->destroy(this->lock);
free(this);
}