hash: avoid allocation when replace value in hash_add()

Memory allocation may fail. We don't need to allocate new entry when we
already have one.
This commit is contained in:
Dmitry Podgorny
2017-06-24 14:14:08 +03:00
parent 5ecb0c5d0c
commit a845c7ec3d
2 changed files with 40 additions and 34 deletions

View File

@@ -87,8 +87,7 @@ void handler_fire_stanza(xmpp_conn_t * const conn,
head_old = head;
_handler_item_remove(&head, item);
if (head != head_old) {
/* TODO implement hash_replace() */
hash_drop(conn->id_handlers, id);
/* replace old value */
hash_add(conn->id_handlers, id, head);
}
xmpp_free(conn->ctx, item->id);
@@ -677,10 +676,11 @@ void handler_system_delete_all(xmpp_conn_t *conn)
freed memory. */
key = hash_iter_next(iter);
if (head != head_old) {
/* TODO implement hash_replace() to reduce number of allocations */
hash_drop(conn->id_handlers, key2);
/* hash_add() replaces value if the key exists */
if (head != NULL)
hash_add(conn->id_handlers, key2, head);
else
hash_drop(conn->id_handlers, key2);
xmpp_free(conn->ctx, key2);
}
}