pidgin/pidgin

simple: Add explicit destroy functions for structs.

2019-10-25, Elliott Sales de Andrade
b5d8e7f5b5ce
Parents 432bd1f3748f
Children 3f61b0cd94de
simple: Add explicit destroy functions for structs.

This enables simplifying some operations that would iterate twice or
more times over the lists.
--- a/libpurple/protocols/simple/simple.c Fri Oct 25 07:54:39 2019 +0000
+++ b/libpurple/protocols/simple/simple.c Fri Oct 25 05:11:46 2019 -0400
@@ -201,9 +201,9 @@
return watcher;
}
-static void watcher_remove(struct simple_account_data *sip, const gchar *name) {
- struct simple_watcher *watcher = watcher_find(sip, name);
- sip->watcher = g_slist_remove(sip->watcher, watcher);
+static void
+watcher_destroy(struct simple_watcher *watcher)
+{
g_free(watcher->name);
g_free(watcher->dialog.callid);
g_free(watcher->dialog.ourtag);
@@ -218,22 +218,22 @@
return ret;
}
-static void connection_remove(struct simple_account_data *sip, int fd) {
- struct sip_connection *conn = connection_find(sip, fd);
- sip->openconns = g_slist_remove(sip->openconns, conn);
- if(conn->inputhandler) purple_input_remove(conn->inputhandler);
- g_free(conn->inbuf);
+static void
+connection_destroy(struct sip_connection *conn)
+{
+ if (conn->inputhandler) {
+ purple_input_remove(conn->inputhandler);
+ }
+ g_clear_pointer(&conn->inbuf, g_free);
g_free(conn);
}
-static void connection_free_all(struct simple_account_data *sip) {
- struct sip_connection *ret = NULL;
- GSList *entry = sip->openconns;
- while(entry) {
- ret = entry->data;
- connection_remove(sip, ret->fd);
- entry = sip->openconns;
- }
+static void
+connection_remove(struct simple_account_data *sip, int fd)
+{
+ struct sip_connection *conn = connection_find(sip, fd);
+ sip->openconns = g_slist_remove(sip->openconns, conn);
+ connection_destroy(conn);
}
static void simple_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group, const char *message)
@@ -654,9 +654,12 @@
g_string_free(outstr, TRUE);
}
-static void transactions_remove(struct simple_account_data *sip, struct transaction *trans) {
- if(trans->msg) sipmsg_free(trans->msg);
- sip->transactions = g_slist_remove(sip->transactions, trans);
+static void
+transactions_destroy(struct transaction *trans)
+{
+ if (trans->msg) {
+ sipmsg_free(trans->msg);
+ }
g_free(trans);
}
@@ -1056,12 +1059,13 @@
/* remove a timed out suscriber */
tmp = sip->watcher;
while(tmp) {
+ GSList *next = tmp->next;
struct simple_watcher *watcher = tmp->data;
if(watcher->expire < curtime) {
- watcher_remove(sip, watcher->name);
- tmp = sip->watcher;
+ sip->watcher = g_slist_delete_link(sip->watcher, tmp);
+ watcher_destroy(watcher);
}
- if(tmp) tmp = tmp->next;
+ tmp = next;
}
return TRUE;
@@ -1652,7 +1656,9 @@
/* call the callback to process response*/
(trans->callback)(sip, msg, trans);
}
- transactions_remove(sip, trans);
+ sip->transactions =
+ g_slist_remove(sip->transactions, trans);
+ transactions_destroy(trans);
}
}
found = TRUE;
@@ -2086,7 +2092,7 @@
do_register_exp(sip, 0);
}
- connection_free_all(sip);
+ g_slist_free_full(sip->openconns, (GDestroyNotify)connection_destroy);
if (sip->listenpa)
purple_input_remove(sip->listenpa);
@@ -2124,8 +2130,7 @@
g_free(sip->status);
g_hash_table_destroy(sip->buddies);
g_free(sip->regcallid);
- while (sip->transactions)
- transactions_remove(sip, sip->transactions->data);
+ g_slist_free_full(sip->transactions, (GDestroyNotify)transactions_destroy);
g_free(sip->publish_etag);
if (sip->txbuf)
g_object_unref(G_OBJECT(sip->txbuf));
--- a/libpurple/protocols/simple/sipmsg.c Fri Oct 25 07:54:39 2019 +0000
+++ b/libpurple/protocols/simple/sipmsg.c Fri Oct 25 05:11:46 2019 -0400
@@ -177,15 +177,16 @@
msg->headers = g_slist_append(msg->headers, element);
}
+static void
+sipmsg_free_header(struct siphdrelement *elem)
+{
+ g_free(elem->name);
+ g_free(elem->value);
+ g_free(elem);
+}
+
void sipmsg_free(struct sipmsg *msg) {
- struct siphdrelement *elem;
- while(msg->headers) {
- elem = msg->headers->data;
- msg->headers = g_slist_remove(msg->headers,elem);
- g_free(elem->name);
- g_free(elem->value);
- g_free(elem);
- }
+ g_slist_free_full(msg->headers, (GDestroyNotify)sipmsg_free_header);
g_free(msg->method);
g_free(msg->target);
g_free(msg->body);
@@ -197,9 +198,7 @@
if(tmp) {
struct siphdrelement *elem = tmp->data;
msg->headers = g_slist_delete_link(msg->headers, tmp);
- g_free(elem->name);
- g_free(elem->value);
- g_free(elem);
+ sipmsg_free_header(elem);
}
}