[polari/wip/fmuellner/lib-cleanup: 3/4] lib: Move to autofree/autoptr



commit 48fa1c7dd32124eaaa5f4d18a4ecf00660f63151
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Dec 8 22:16:58 2017 +0100

    lib: Move to autofree/autoptr
    
    Modern compilers allow to clean up variables automatically when they go
    out of scope; we don't really care about other compilers, so simplify
    the code by stop doing manual memory management of local variables.

 src/lib/polari-room.c           | 48 +++++++++++++----------------------------
 src/lib/polari-tp-autocleanup.h | 27 +++++++++++++++++++++++
 src/lib/polari-util.c           |  8 +++----
 src/meson.build                 |  6 +++++-
 4 files changed, 50 insertions(+), 39 deletions(-)
---
diff --git a/src/lib/polari-room.c b/src/lib/polari-room.c
index ab458c1..2181faa 100644
--- a/src/lib/polari-room.c
+++ b/src/lib/polari-room.c
@@ -20,6 +20,7 @@
 
 #include "polari-room.h"
 #include "polari-util.h"
+#include "polari-tp-autocleanup.h"
 
 typedef struct _PolariRoomPrivate PolariRoomPrivate;
 
@@ -106,7 +107,8 @@ polari_create_room_id (TpAccount    *account,
                        const char   *name,
                        TpHandleType  type)
 {
-  char *id, *folded_name;
+  g_autofree char *folded_name = NULL;
+  char *id;
 
   g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
   g_return_val_if_fail (name != NULL, NULL);
@@ -116,18 +118,15 @@ polari_create_room_id (TpAccount    *account,
                         tp_proxy_get_object_path (TP_PROXY (account)),
                         type, folded_name);
 
-  g_free (folded_name);
   return id;
 }
 
 #ifdef HAVE_STRCASESTR
 #  define FOLDFUNC(text) ((char *)(text))
 #  define MATCHFUNC(haystick,needle) strcasestr (haystick, needle)
-#  define FREEFUNC(text)
 #else
 #  define FOLDFUNC(text) g_utf8_casefold (text, -1)
 #  define MATCHFUNC(haystick,needle) strstr (haystick, needle)
-#  define FREEFUNC(text) g_free(text)
 #endif
 
 static gboolean
@@ -135,7 +134,8 @@ match_self_nick (PolariRoom *room,
                  const char *text)
 {
   PolariRoomPrivate *priv = room->priv;
-  char *folded_text, *match;
+  g_autofree char *folded_text = NULL;
+  char *match;
   gboolean result = FALSE;
   int len;
 
@@ -160,8 +160,6 @@ match_self_nick (PolariRoom *room,
       match = MATCHFUNC (match + len, priv->self_nick);
     }
 
-  FREEFUNC (folded_text);
-
   return result;
 }
 
@@ -248,7 +246,7 @@ on_identify_message_sent (GObject      *source,
                           gpointer      user_data)
 {
   TpTextChannel *channel = TP_TEXT_CHANNEL (source);
-  GTask *task = user_data;
+  g_autoptr(GTask) task = user_data;
   PolariRoom *room = g_task_get_source_object (task);
   GError *error = NULL;
 
@@ -257,12 +255,10 @@ on_identify_message_sent (GObject      *source,
       room->priv->ignore_identify = FALSE;
 
       g_task_return_error (task, error);
-      g_object_unref (task);
       return;
     }
 
   g_task_return_boolean (task, TRUE);
-  g_object_unref (task);
 }
 
 /**
@@ -279,9 +275,9 @@ polari_room_send_identify_message_async (PolariRoom          *room,
                                          gpointer             user_data)
 {
   PolariRoomPrivate *priv;
-  TpMessage *message;
-  GTask *task;
-  char *text;
+  g_autoptr(TpMessage) message = NULL;
+  g_autoptr(GTask) task = NULL;;
+  g_autofree char *text = NULL;
 
   g_return_if_fail (POLARI_IS_ROOM (room));
   g_return_if_fail (command != NULL && password != NULL);
@@ -294,7 +290,6 @@ polari_room_send_identify_message_async (PolariRoom          *room,
     {
       g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_CONNECTED,
                                "The room is disconnected.");
-      g_object_unref (task);
       return;
     }
 
@@ -309,10 +304,7 @@ polari_room_send_identify_message_async (PolariRoom          *room,
   message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, text);
 
   tp_text_channel_send_message_async (TP_TEXT_CHANNEL (priv->channel), message,
-                                      0, on_identify_message_sent, task);
-
-  g_object_unref (message);
-  g_free (text);
+                                      0, on_identify_message_sent, g_steal_pointer (&task));
 }
 
 gboolean
@@ -405,7 +397,7 @@ on_group_contacts_changed (TpChannel  *channel,
 {
   TpChannelGroupChangeReason reason;
   const char *raw_message;
-  char *message = NULL;
+  g_autofree char *message = NULL;
   guint i;
 
   reason = tp_asv_get_uint32 (details, "change-reason", NULL);
@@ -464,7 +456,6 @@ on_group_contacts_changed (TpChannel  *channel,
     }
 
   g_signal_emit (user_data, signals[MEMBERS_CHANGED], 0);
-  g_free (message);
 }
 
 static void
@@ -476,7 +467,7 @@ on_message_sent (TpTextChannel      *channel,
 {
   PolariRoom *room = user_data;
   PolariRoomPrivate *priv = room->priv;
-  char *command, *username, *password, *text;
+  g_autofree char *command = NULL, *username = NULL, *password = NULL, *text = NULL;
 
   if (priv->type != TP_HANDLE_TYPE_CONTACT)
     return;
@@ -489,13 +480,7 @@ on_message_sent (TpTextChannel      *channel,
         g_signal_emit (room, signals[IDENTIFY_SENT], 0, command, username, password);
 
       priv->ignore_identify = FALSE;
-
-      g_free (command);
-      g_free (username);
-      g_free (password);
     }
-
-  g_free (text);
 }
 
 static void
@@ -514,7 +499,7 @@ update_subject (PolariRoom *room,
 {
   PolariRoomPrivate *priv = room->priv;
   const char *raw_subject;
-  char *subject = NULL;
+  g_autofree char *subject = NULL;
 
   raw_subject = tp_asv_get_string (properties, "Subject");
 
@@ -523,13 +508,10 @@ update_subject (PolariRoom *room,
 
   subject = strip_color_codes (raw_subject);
   if (g_strcmp0 (priv->topic, subject) == 0)
-    {
-      g_free (subject);
-      return;
-    }
+    return;
 
   g_free (priv->topic);
-  priv->topic = subject;
+  priv->topic = g_steal_pointer (&subject);
 
   g_object_notify_by_pspec (G_OBJECT (room), props[PROP_TOPIC]);
 }
diff --git a/src/lib/polari-tp-autocleanup.h b/src/lib/polari-tp-autocleanup.h
new file mode 100644
index 0000000..274d5bd
--- /dev/null
+++ b/src/lib/polari-tp-autocleanup.h
@@ -0,0 +1,27 @@
+/* polari-tp-autocleanup.h
+ *
+ * Copyright © 2017 Florian Müllner <fmuellner gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (TpMessage, g_object_unref)
+
+G_END_DECLS
diff --git a/src/lib/polari-util.c b/src/lib/polari-util.c
index c8d2dd0..2c21c1f 100644
--- a/src/lib/polari-util.c
+++ b/src/lib/polari-util.c
@@ -61,8 +61,9 @@ polari_util_match_identify_message (const char  *message,
                                     char       **password)
 {
   static GRegex *identify_message_regex = NULL;
-  GMatchInfo *match;
-  char *text, *stripped_text;
+  g_autoptr(GMatchInfo) match = NULL;
+  g_autofree char *text = NULL;
+  char *stripped_text;
   gboolean matched;
 
   text = g_strdup (message);
@@ -84,8 +85,5 @@ polari_util_match_identify_message (const char  *message,
         *password = g_match_info_fetch (match, 3);
     }
 
-  g_match_info_free (match);
-  g_free (text);
-
   return matched;
 }
diff --git a/src/meson.build b/src/meson.build
index a030113..9a1b188 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -91,11 +91,15 @@ libsources = [
   'lib/polari-util.h'
 ]
 
+lib_nongir_sources = [
+  'lib/polari-tp-autocleanup.h',
+]
+
 libargs = [
   '-DG_LOG_USE_STRUCTURED',
   '-DG_LOG_DOMAIN="Polari"'
 ]
-libpolari = shared_library('polari-1.0', libsources,
+libpolari = shared_library('polari-1.0', libsources + lib_nongir_sources,
   dependencies: [gio, gtk3, telepathy_glib],
   c_args: libargs,
   install: true,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]