[strongSwan] Remove all elements from a hashtable_t
tobias at strongswan.org
Sat Jun 5 10:12:31 CEST 2010
>> Does the hashtable_t+enumerator_t support this mode of operation ?
> I think so. The hashtable is based on many linked_lists, and the
> remove function from list can handle this.
> @Tobias: Please correct me if I'm wrong.
That wouldn't work. For the linked list you also can't just call
remove, but have to call remove_at, otherwise the enumerator's internal
state would get invalid. I've added such a function to the hash table,
see the attached patch (I hope it compiles, did this on a Windows machine).
>> Or, is there a better way of doing this ?
> Our list has handy destroy_offset/destroy_function helpers for cleanup
> purposes, but the hashtable is currently missing these functions. So:
We could add such functions to the hash table too, but we would probably
need two parameters, in order to free the keys and the values (since you
could use the same object as key and as value one of these would be
Alternatively, we could also add a new constructor, which would take two
new function parameters to free the keys and the values. If they were
set, destroy/remove/remove_at would automatically free the memory of the
key and value. We could then also easily add a remove_all method to
clear the whole hash table.
>> If all else fails, it may be better to free all the memory that the
>> elements point to and to then destroy the hashtable_t and create a
>> new one.
> Might be a little more efficient for large tables (?), but shouldn't
> really matter.
It probably depends on how often you have to clear the whole hash table,
how large it gets and how fast it grows. If it grows fast to about the
same significant size, it might be better to remove all elements to
avoid the hash table getting resized all the time (you can avoid this if
you set the initial capacity to an appropriate value).
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
More information about the Users