[glib] Make GMatchInfo refcounted



commit 00afe3fed340cb87b638aaafa01a2829cb60efba
Author: Christian Persch <chpe gnome org>
Date:   Thu Jun 23 18:27:29 2011 +0200

    Make GMatchInfo refcounted

 docs/reference/glib/glib-sections.txt |    2 +
 glib/glib.symbols                     |    2 +
 glib/gregex.c                         |   49 +++++++++++++++++++++++++++++---
 glib/gregex.h                         |    2 +
 4 files changed, 50 insertions(+), 5 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index cc28d68..7eb4309 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -996,6 +996,8 @@ g_regex_check_replacement
 GMatchInfo
 g_match_info_get_regex
 g_match_info_get_string
+g_match_info_ref
+g_match_info_unref
 g_match_info_free
 g_match_info_matches
 g_match_info_next
diff --git a/glib/glib.symbols b/glib/glib.symbols
index bea2784..9d34c55 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -1365,6 +1365,8 @@ g_regex_replace_eval
 g_regex_check_replacement
 g_match_info_get_regex
 g_match_info_get_string
+g_match_info_ref
+g_match_info_unref
 g_match_info_free
 g_match_info_next
 g_match_info_matches
diff --git a/glib/gregex.c b/glib/gregex.c
index 7be508c..e5d1bb2 100644
--- a/glib/gregex.c
+++ b/glib/gregex.c
@@ -135,6 +135,7 @@
 
 struct _GMatchInfo
 {
+  volatile gint ref_count;      /* the ref count */
   GRegex *regex;                /* the regex */
   GRegexMatchFlags match_opts;  /* options used at match time on the regex */
   gint matches;                 /* number of matching sub patterns */
@@ -450,6 +451,7 @@ match_info_new (const GRegex *regex,
     string_len = strlen (string);
 
   match_info = g_new0 (GMatchInfo, 1);
+  match_info->ref_count = 1;
   match_info->regex = g_regex_ref ((GRegex *)regex);
   match_info->string = string;
   match_info->string_len = string_len;
@@ -520,17 +522,36 @@ g_match_info_get_string (const GMatchInfo *match_info)
 }
 
 /**
- * g_match_info_free:
+ * g_match_info_ref:
  * @match_info: a #GMatchInfo
  *
- * Frees all the memory associated with the #GMatchInfo structure.
+ * Increases reference count of @match_ifno by 1.
  *
- * Since: 2.14
+ * Returns: @match_info
+ *
+ * Since: 2.30
+ */
+GMatchInfo       *
+g_match_info_ref (GMatchInfo *match_info)
+{
+  g_return_val_if_fail (match_info != NULL, NULL);
+  g_atomic_int_inc (&match_info->ref_count);
+  return match_info;
+}
+
+/**
+ * g_match_info_unref:
+ * @match_info: a #GMatchInfo
+ *
+ * Decreases reference count of @match_info by 1. When reference count drops
+ * to zero, it frees all the memory associated with the match_info structure.
+ *
+ * Since: 2.30
  */
 void
-g_match_info_free (GMatchInfo *match_info)
+g_match_info_unref (GMatchInfo *match_info)
 {
-  if (match_info)
+  if (g_atomic_int_dec_and_test (&match_info->ref_count))
     {
       g_regex_unref (match_info->regex);
       g_free (match_info->offsets);
@@ -540,6 +561,24 @@ g_match_info_free (GMatchInfo *match_info)
 }
 
 /**
+ * g_match_info_free:
+ * @match_info: (allow-none): a #GMatchInfo, or %NULL
+ *
+ * If @match_info is not %NULL, calls g_match_info_unref(); otherwise does
+ * nothing.
+ *
+ * Since: 2.14
+ */
+void
+g_match_info_free (GMatchInfo *match_info)
+{
+  if (match_info == NULL)
+    return;
+
+  g_match_info_unref (match_info);
+}
+
+/**
  * g_match_info_next:
  * @match_info: a #GMatchInfo structure
  * @error: location to store the error occuring, or %NULL to ignore errors
diff --git a/glib/gregex.h b/glib/gregex.h
index ce1b44a..c3fb753 100644
--- a/glib/gregex.h
+++ b/glib/gregex.h
@@ -443,6 +443,8 @@ gboolean	  g_regex_check_replacement	(const gchar         *replacement,
 GRegex		 *g_match_info_get_regex	(const GMatchInfo    *match_info);
 const gchar      *g_match_info_get_string       (const GMatchInfo    *match_info);
 
+GMatchInfo       *g_match_info_ref              (GMatchInfo          *match_info);
+void              g_match_info_unref            (GMatchInfo          *match_info);
 void		  g_match_info_free		(GMatchInfo          *match_info);
 gboolean	  g_match_info_next		(GMatchInfo          *match_info,
 						 GError             **error);



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