Make GMatchInfo refcounted
authorChristian Persch <chpe@gnome.org>
Thu, 23 Jun 2011 16:27:29 +0000 (18:27 +0200)
committerChristian Persch <chpe@gnome.org>
Thu, 23 Jun 2011 23:03:46 +0000 (01:03 +0200)
docs/reference/glib/glib-sections.txt
glib/glib.symbols
glib/gregex.c
glib/gregex.h

index cc28d68..7eb4309 100644 (file)
@@ -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
index bea2784..9d34c55 100644 (file)
@@ -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
index 7be508c..e5d1bb2 100644 (file)
 
 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
index ce1b44a..c3fb753 100644 (file)
@@ -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);