2 * arch-tag: Implementation of reference-counted string
4 * Copyright (C) 2004 Colin Walters <walters@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "rb-cut-and-paste-code.h"
28 #include "rb-refstring.h"
30 GHashTable
*rb_refstrings
;
31 GMutex
*rb_refstrings_mutex
;
42 rb_refstring_free (RBRefString
*refstr
)
44 refstr
->refcount
= 0xdeadbeef;
45 g_free (refstr
->folded
);
46 refstr
->folded
= NULL
;
47 g_free (refstr
->sortkey
);
48 refstr
->sortkey
= NULL
;
53 rb_refstring_system_init ()
55 rb_refstrings_mutex
= g_mutex_new ();
57 rb_refstrings
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
58 NULL
, (GDestroyNotify
) rb_refstring_free
);
62 rb_refstring_new (const char *init
)
66 g_mutex_lock (rb_refstrings_mutex
);
67 ret
= g_hash_table_lookup (rb_refstrings
, init
);
70 rb_refstring_ref (ret
);
71 g_mutex_unlock (rb_refstrings_mutex
);
75 ret
= g_malloc (sizeof (RBRefString
) + strlen (init
));
77 strcpy (ret
->value
, init
);
82 g_hash_table_insert (rb_refstrings
, ret
->value
, ret
);
83 g_mutex_unlock (rb_refstrings_mutex
);
88 rb_refstring_find (const char *init
)
92 g_mutex_lock (rb_refstrings_mutex
);
93 ret
= g_hash_table_lookup (rb_refstrings
, init
);
96 rb_refstring_ref (ret
);
98 g_mutex_unlock (rb_refstrings_mutex
);
103 rb_refstring_unref (RBRefString
*val
)
108 g_return_if_fail (val
->refcount
> 0);
110 if (g_atomic_int_dec_and_test (&val
->refcount
)) {
111 g_mutex_lock (rb_refstrings_mutex
);
112 /* ensure it's still not referenced, as something may have called
113 * rb_refstring_new since we decremented the count */
114 if (g_atomic_int_get (&val
->refcount
) == 0)
115 g_hash_table_remove (rb_refstrings
, val
->value
);
116 g_mutex_unlock (rb_refstrings_mutex
);
121 rb_refstring_system_shutdown (void)
123 g_hash_table_destroy (rb_refstrings
);
124 g_mutex_free (rb_refstrings_mutex
);
128 rb_refstring_ref (RBRefString
*val
)
133 g_return_val_if_fail (val
->refcount
> 0, NULL
);
135 g_atomic_int_inc (&val
->refcount
);
140 rb_refstring_get (const RBRefString
*val
)
142 return val
? val
->value
: NULL
;
146 * The next two functions will compute the values if they haven't
147 * been already done. Using g_atomic_* is much more efficient than
148 * using mutexes (since mutexes may require kernel calls) and these
153 rb_refstring_get_folded (RBRefString
*val
)
162 string
= (const char*)g_atomic_pointer_get (ptr
);
163 if (string
== NULL
) {
166 newstring
= rb_search_fold (rb_refstring_get (val
));
167 if (g_atomic_pointer_compare_and_exchange (ptr
, NULL
, newstring
)) {
171 string
= (const char *)g_atomic_pointer_get (ptr
);
180 rb_refstring_get_sort_key (RBRefString
*val
)
189 string
= (const char *)g_atomic_pointer_get (ptr
);
190 if (string
== NULL
) {
194 s
= rb_refstring_get_folded (val
);
195 newstring
= rb_utf8_collate_key_for_filename (s
, -1);
197 if (g_atomic_pointer_compare_and_exchange (ptr
, NULL
, newstring
)) {
201 string
= (const char*)g_atomic_pointer_get (ptr
);
210 rb_refstring_hash (gconstpointer p
)
212 const RBRefString
*ref
= p
;
213 return g_str_hash (rb_refstring_get (ref
));
217 rb_refstring_equal (gconstpointer ap
, gconstpointer bp
)