From 8f568265bf65f4c7bdc19738f91b4e98ed826394 Mon Sep 17 00:00:00 2001 From: rbuj Date: Tue, 5 Mar 2019 19:23:42 +0100 Subject: eel: replace eel_ref_str with GRefString GNOME/nautilus@b4d200f https://developer.gnome.org/glib/stable/glib-Reference-counted-strings.html --- eel/eel-string.c | 112 ------------------------------------------------------- 1 file changed, 112 deletions(-) (limited to 'eel/eel-string.c') diff --git a/eel/eel-string.c b/eel/eel-string.c index aea801ea..c44706c1 100644 --- a/eel/eel-string.c +++ b/eel/eel-string.c @@ -746,118 +746,6 @@ eel_strdup_printf_with_custom (EelPrintfHandler *handlers, return res; } -/*********** refcounted strings ****************/ - -G_LOCK_DEFINE_STATIC (unique_ref_strs); -static GHashTable *unique_ref_strs = NULL; - -static eel_ref_str -eel_ref_str_new_internal (const char *string, int start_count) -{ - char *res; - volatile gint *count; - gsize len; - - len = strlen (string); - res = g_malloc (sizeof (gint) + len + 1); - count = (volatile gint *)res; - *count = start_count; - res += sizeof(gint); - memcpy (res, string, len + 1); - return res; -} - -eel_ref_str -eel_ref_str_new (const char *string) -{ - if (string == NULL) - { - return NULL; - } - - return eel_ref_str_new_internal (string, 1); -} - -eel_ref_str -eel_ref_str_get_unique (const char *string) -{ - eel_ref_str res; - - if (string == NULL) - { - return NULL; - } - - G_LOCK (unique_ref_strs); - if (unique_ref_strs == NULL) - { - unique_ref_strs = - g_hash_table_new(g_str_hash, g_str_equal); - } - - res = g_hash_table_lookup (unique_ref_strs, string); - if (res != NULL) - { - eel_ref_str_ref (res); - } - else - { - res = eel_ref_str_new_internal (string, 0x80000001); - g_hash_table_insert (unique_ref_strs, res, res); - } - - G_UNLOCK (unique_ref_strs); - - return res; -} - -eel_ref_str -eel_ref_str_ref (eel_ref_str str) -{ - volatile gint *count; - - count = (volatile gint *)((char *)str - sizeof (gint)); - g_atomic_int_add (count, 1); - - return str; -} - -void -eel_ref_str_unref (eel_ref_str str) -{ - volatile gint *count; - gint old_ref; - - if (str == NULL) - return; - - count = (volatile gint *)((char *)str - sizeof (gint)); - -retry_atomic_decrement: - old_ref = g_atomic_int_get (count); - if (old_ref == 1) - { - g_free ((char *)count); - } - else if (old_ref == 0x80000001) - { - G_LOCK (unique_ref_strs); - /* Need to recheck after taking lock to avoid races with _get_unique() */ - if (g_atomic_int_add (count, -1) == 0x80000001) - { - g_hash_table_remove (unique_ref_strs, (char *)str); - g_free ((char *)count); - } - G_UNLOCK (unique_ref_strs); - } - else if (!g_atomic_int_compare_and_exchange (count, - old_ref, old_ref - 1)) - { - goto retry_atomic_decrement; - } -} - - #if !defined (EEL_OMIT_SELF_CHECK) static void -- cgit v1.2.1