diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index cc28d687c..7eb430975 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 bea278433..9d34c555c 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 7be508c54..e5d1bb265 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); @@ -539,6 +560,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 diff --git a/glib/gregex.h b/glib/gregex.h index ce1b44a97..c3fb753ab 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);