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.
This commit is contained in:
committed by
Tobias Brunner
parent
3972769690
commit
8f1dfb3d9e
@@ -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,
|
METHOD(tkm_chunk_map_t, insert, void,
|
||||||
private_tkm_chunk_map_t * const this, const chunk_t * const data,
|
private_tkm_chunk_map_t * const this, const chunk_t * const data,
|
||||||
const uint64_t id)
|
const uint64_t id)
|
||||||
{
|
{
|
||||||
uint64_t *value = malloc_thing(uint64_t);
|
entry_t *entry;
|
||||||
*value = id;
|
INIT(entry,
|
||||||
|
.key = chunk_clone(*data),
|
||||||
|
.value = id
|
||||||
|
);
|
||||||
|
|
||||||
this->lock->write_lock(this->lock);
|
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);
|
this->lock->unlock(this->lock);
|
||||||
|
|
||||||
if (value)
|
if (entry)
|
||||||
{
|
{
|
||||||
free(value);
|
entry_destroy(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD(tkm_chunk_map_t, get_id, uint64_t,
|
METHOD(tkm_chunk_map_t, get_id, uint64_t,
|
||||||
private_tkm_chunk_map_t * const this, chunk_t *data)
|
private_tkm_chunk_map_t * const this, chunk_t *data)
|
||||||
{
|
{
|
||||||
uint64_t *value;
|
entry_t *entry;
|
||||||
this->lock->read_lock(this->lock);
|
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);
|
this->lock->unlock(this->lock);
|
||||||
|
|
||||||
return value == NULL ? 0 : *value;
|
if (!entry)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return entry->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD(tkm_chunk_map_t, remove_, bool,
|
METHOD(tkm_chunk_map_t, remove_, bool,
|
||||||
private_tkm_chunk_map_t * const this, chunk_t *data)
|
private_tkm_chunk_map_t * const this, chunk_t *data)
|
||||||
{
|
{
|
||||||
|
entry_t *entry;
|
||||||
|
|
||||||
this->lock->write_lock(this->lock);
|
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);
|
this->lock->unlock(this->lock);
|
||||||
|
|
||||||
if (value)
|
if (entry)
|
||||||
{
|
{
|
||||||
free(value);
|
entry_destroy(entry);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -94,21 +123,20 @@ METHOD(tkm_chunk_map_t, remove_, bool,
|
|||||||
METHOD(tkm_chunk_map_t, destroy, void,
|
METHOD(tkm_chunk_map_t, destroy, void,
|
||||||
private_tkm_chunk_map_t *this)
|
private_tkm_chunk_map_t *this)
|
||||||
{
|
{
|
||||||
uint64_t *value;
|
entry_t *entry;
|
||||||
enumerator_t *enumerator;
|
enumerator_t *enumerator;
|
||||||
|
|
||||||
this->lock->write_lock(this->lock);
|
this->lock->write_lock(this->lock);
|
||||||
enumerator = this->mappings->create_enumerator(this->mappings);
|
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);
|
entry_destroy(entry);
|
||||||
free(value);
|
|
||||||
}
|
}
|
||||||
enumerator->destroy(enumerator);
|
enumerator->destroy(enumerator);
|
||||||
this->lock->unlock(this->lock);
|
this->lock->unlock(this->lock);
|
||||||
|
|
||||||
this->lock->destroy(this->lock);
|
|
||||||
this->mappings->destroy(this->mappings);
|
this->mappings->destroy(this->mappings);
|
||||||
|
this->lock->destroy(this->lock);
|
||||||
free(this);
|
free(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user