guint i;
res = g_hash_table_lookup (hash->tbl, key);
+
if (res != NULL) {
+ rspamd_min_heap_remove_elt (hash->heap, &res->helt);
g_hash_table_remove (hash->tbl, key);
}
else {
for (i = 0; i < MIN (hash->maxsize, expire_aggressive_count); i ++) {
res = (rspamd_lru_element_t *)rspamd_min_heap_pop (hash->heap);
- g_hash_table_remove (hash->tbl, res->key);
+
+ if (res) {
+ g_hash_table_remove (hash->tbl, res->key);
+ }
+ else {
+ break;
+ }
}
}
}
elt = g_ptr_array_index (heap->ar, 0);
last = g_ptr_array_index (heap->ar, heap->ar->len - 1);
- /* Now replace elt with the last element and sink it if needed */
- heap_swap (heap, elt, last);
- g_ptr_array_remove_index_fast (heap->ar, heap->ar->len - 1);
- rspamd_min_heap_sink (heap, last);
+
+ if (elt != last) {
+ /* Now replace elt with the last element and sink it if needed */
+ heap_swap (heap, elt, last);
+ g_ptr_array_remove_index_fast (heap->ar, heap->ar->len - 1);
+ rspamd_min_heap_sink (heap, last);
+ }
+ else {
+ g_ptr_array_remove_index_fast (heap->ar, heap->ar->len - 1);
+ }
+
return elt;
}