udapted vi.po
[rhythmbox.git] / rhythmdb / rb-refstring.c
bloba67716c849947991ebb5108889057e34558952b9
1 /*
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.
22 #include <config.h>
24 #include <glib.h>
25 #include <string.h>
26 #include "rb-util.h"
27 #include "rb-cut-and-paste-code.h"
28 #include "rb-refstring.h"
30 GHashTable *rb_refstrings;
31 GMutex *rb_refstrings_mutex;
33 struct RBRefString
35 gint refcount;
36 gpointer folded;
37 gpointer sortkey;
38 char value[1];
41 static void
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;
49 g_free (refstr);
52 void
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);
61 RBRefString *
62 rb_refstring_new (const char *init)
64 RBRefString *ret;
66 g_mutex_lock (rb_refstrings_mutex);
67 ret = g_hash_table_lookup (rb_refstrings, init);
69 if (ret) {
70 rb_refstring_ref (ret);
71 g_mutex_unlock (rb_refstrings_mutex);
72 return ret;
75 ret = g_malloc (sizeof (RBRefString) + strlen (init));
77 strcpy (ret->value, init);
78 ret->refcount = 1;
79 ret->folded = NULL;
80 ret->sortkey = NULL;
82 g_hash_table_insert (rb_refstrings, ret->value, ret);
83 g_mutex_unlock (rb_refstrings_mutex);
84 return ret;
87 RBRefString *
88 rb_refstring_find (const char *init)
90 RBRefString *ret;
92 g_mutex_lock (rb_refstrings_mutex);
93 ret = g_hash_table_lookup (rb_refstrings, init);
95 if (ret)
96 rb_refstring_ref (ret);
98 g_mutex_unlock (rb_refstrings_mutex);
99 return ret;
102 void
103 rb_refstring_unref (RBRefString *val)
105 if (val == NULL)
106 return;
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);
120 void
121 rb_refstring_system_shutdown (void)
123 g_hash_table_destroy (rb_refstrings);
124 g_mutex_free (rb_refstrings_mutex);
127 RBRefString *
128 rb_refstring_ref (RBRefString *val)
130 if (val == NULL)
131 return NULL;
133 g_return_val_if_fail (val->refcount > 0, NULL);
135 g_atomic_int_inc (&val->refcount);
136 return val;
139 const char *
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
149 * get called often.
152 const char *
153 rb_refstring_get_folded (RBRefString *val)
155 gpointer *ptr;
156 const char *string;
158 if (val == NULL)
159 return NULL;
161 ptr = &val->folded;
162 string = (const char*)g_atomic_pointer_get (ptr);
163 if (string == NULL) {
164 char *newstring;
166 newstring = rb_search_fold (rb_refstring_get (val));
167 if (g_atomic_pointer_compare_and_exchange (ptr, NULL, newstring)) {
168 string = newstring;
169 } else {
170 g_free (newstring);
171 string = (const char *)g_atomic_pointer_get (ptr);
172 g_assert (string);
176 return string;
179 const char *
180 rb_refstring_get_sort_key (RBRefString *val)
182 gpointer *ptr;
183 const char *string;
185 if (val == NULL)
186 return NULL;
188 ptr = &val->sortkey;
189 string = (const char *)g_atomic_pointer_get (ptr);
190 if (string == NULL) {
191 char *newstring;
192 const char *s;
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)) {
198 string = newstring;
199 } else {
200 g_free (newstring);
201 string = (const char*)g_atomic_pointer_get (ptr);
202 g_assert (string);
206 return string;
209 guint
210 rb_refstring_hash (gconstpointer p)
212 const RBRefString *ref = p;
213 return g_str_hash (rb_refstring_get (ref));
216 gboolean
217 rb_refstring_equal (gconstpointer ap, gconstpointer bp)
219 return (ap == bp);