Hello,

while debugging resource leaks in my code I found that I used ohash incorrectly. I came here to ask what is the best way of doing what I need to do.

The task is simple: remove all elements that satisfy some property.

Naively, I wrote:

for (el = ohash_first(map, &it); el; el = ohash_next(map, &it))

    if (condition(el))

        ohash_remove(el->key);


This fails, because the hash map may be resized in the middle of the loop. If that happens, some elements that meet the condition can be moved to a position before the iterator and skipped. It seems to me that in order to do a proper deletion, I should either:

* do 2 passes; collect keys to delete in the first and remove them in the second, or

* add another layer of looping and keep iterating as long as a pass over the map deleted at least one element.

Neither seems to be a good way of doing such a trivial task.

Please note that man is totally unhelpful:

"Those functions are safe to use even while entries are added to/removed from the table, but in such a case they don't guarantee that new entries will be returned. As a special case, they can safely be used to free elements in the table."

In my opinion, the man entry should be clarified.

Regards,

--
Maciej Adamczyk

Reply via email to