aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libmm-glib/mm-common-helpers.h23
-rw-r--r--plugins/altair/mm-modem-helpers-altair-lte.c27
2 files changed, 29 insertions, 21 deletions
diff --git a/libmm-glib/mm-common-helpers.h b/libmm-glib/mm-common-helpers.h
index 6f8b68ac..fce9ff25 100644
--- a/libmm-glib/mm-common-helpers.h
+++ b/libmm-glib/mm-common-helpers.h
@@ -174,4 +174,27 @@ gboolean mm_utils_ishexstr (const gchar *hex);
gboolean mm_utils_check_for_single_value (guint32 value);
+#if GLIB_CHECK_VERSION(2, 44, 0)
+#define mm_autoptr g_autoptr
+#else
+
+/* Re-implement for those glib that don't have it */
+#define _MM_AUTOPTR_FUNC_NAME(TypeName) mm_autoptr_cleanup_##TypeName
+#define _MM_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr
+#define _MM_CLEANUP(func) __attribute__((cleanup(func)))
+
+#define _MM_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, cleanup) \
+ typedef TypeName *_MM_AUTOPTR_TYPENAME(TypeName); \
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
+ static G_GNUC_UNUSED inline void _MM_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) \
+ { if (_ptr && *_ptr) (cleanup) (*_ptr); } \
+ G_GNUC_END_IGNORE_DEPRECATIONS
+
+#define mm_autoptr(TypeName) _MM_CLEANUP(_MM_AUTOPTR_FUNC_NAME(TypeName)) _MM_AUTOPTR_TYPENAME(TypeName)
+
+_MM_DEFINE_AUTOPTR_CLEANUP_FUNCS(GRegex, g_regex_unref)
+_MM_DEFINE_AUTOPTR_CLEANUP_FUNCS(GMatchInfo, g_match_info_unref)
+
+#endif
+
#endif /* MM_COMMON_HELPERS_H */
diff --git a/plugins/altair/mm-modem-helpers-altair-lte.c b/plugins/altair/mm-modem-helpers-altair-lte.c
index 080e719a..bb247d75 100644
--- a/plugins/altair/mm-modem-helpers-altair-lte.c
+++ b/plugins/altair/mm-modem-helpers-altair-lte.c
@@ -71,8 +71,8 @@ gchar *
mm_altair_parse_ceer_response (const gchar *response,
GError **error)
{
- GRegex *r;
- GMatchInfo *match_info = NULL;
+ g_autoptr(GRegex) r = NULL;
+ g_autoptr(GMatchInfo) match_info = NULL;
gchar *ceer_response = NULL;
@@ -93,8 +93,6 @@ mm_altair_parse_ceer_response (const gchar *response,
if (!g_regex_match (r, response, 0, &match_info)) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Could not parse +CEER response");
- g_match_info_free (match_info);
- g_regex_unref (r);
return NULL;
}
@@ -104,8 +102,6 @@ mm_altair_parse_ceer_response (const gchar *response,
ceer_response = g_strdup ("");
}
- g_match_info_free (match_info);
- g_regex_unref (r);
return ceer_response;
}
@@ -115,15 +111,13 @@ mm_altair_parse_ceer_response (const gchar *response,
guint
mm_altair_parse_cid (const gchar *response, GError **error)
{
- GRegex *regex;
- GMatchInfo *match_info;
+ mm_autoptr(GRegex) regex = NULL;
+ mm_autoptr(GMatchInfo) match_info = NULL;
guint cid = -1;
regex = g_regex_new ("\\%CGINFO:\\s*(\\d+)", G_REGEX_RAW, 0, NULL);
g_assert (regex);
if (!g_regex_match_full (regex, response, strlen (response), 0, 0, &match_info, error)) {
- g_match_info_free (match_info);
- g_regex_unref (regex);
return -1;
}
@@ -133,8 +127,6 @@ mm_altair_parse_cid (const gchar *response, GError **error)
MM_CORE_ERROR_FAILED,
"Failed to parse %%CGINFO=\"cid\",1 response");
- g_match_info_free (match_info);
- g_regex_unref (regex);
return cid;
}
@@ -144,8 +136,8 @@ mm_altair_parse_cid (const gchar *response, GError **error)
MMPco *
mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
{
- GRegex *regex;
- GMatchInfo *match_info;
+ g_autoptr(GRegex) regex = NULL;
+ g_autoptr(GMatchInfo) match_info = NULL;
MMPco *pco = NULL;
gint num_matches;
@@ -163,8 +155,6 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
0, NULL);
g_assert (regex);
if (!g_regex_match_full (regex, pco_info, strlen (pco_info), 0, 0, &match_info, error)) {
- g_match_info_free (match_info);
- g_regex_unref (regex);
return NULL;
}
@@ -175,8 +165,6 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
MM_CORE_ERROR_FAILED,
"Failed to parse substrings, number of matches: %d",
num_matches);
- g_match_info_free (match_info);
- g_regex_unref (regex);
return NULL;
}
@@ -288,8 +276,5 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
break;
}
- g_match_info_free (match_info);
- g_regex_unref (regex);
-
return pco;
}