1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* Copyright (C) 2004 Colin Walters <walters@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <config.h>
#include <glib.h>
#include <string.h>
#include "rb-util.h"
#include "rb-cut-and-paste-code.h"
#include "rb-refstring.h"
GHashTable *rb_refstrings;
GMutex *rb_refstrings_mutex;
struct RBRefString
{
gint refcount;
gpointer folded;
gpointer sortkey;
char value[1];
};
static void
rb_refstring_free (RBRefString *refstr)
{
refstr->refcount = 0xdeadbeef;
g_free (refstr->folded);
refstr->folded = NULL;
g_free (refstr->sortkey);
refstr->sortkey = NULL;
g_free (refstr);
}
/**
* rb_refstring_system_init:
*
* Sets up the refstring system. Called on startup.
*/
void
rb_refstring_system_init ()
{
rb_refstrings_mutex = g_mutex_new ();
rb_refstrings = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, (GDestroyNotify) rb_refstring_free);
}
/**
* rb_refstring_new:
* @init: string to intern
*
* Returns an #RBRefString for the specified string.
* If one already exists, its reference count is incremented and it is
* returned. Otherwise, a new #RBRefString is created and returned.
*
* Return value: #RBRefString for @init
*/
RBRefString *
rb_refstring_new (const char *init)
{
RBRefString *ret;
g_mutex_lock (rb_refstrings_mutex);
ret = g_hash_table_lookup (rb_refstrings, init);
if (ret) {
rb_refstring_ref (ret);
g_mutex_unlock (rb_refstrings_mutex);
return ret;
}
ret = g_malloc (sizeof (RBRefString) + strlen (init));
g_strlcpy (ret->value, init, strlen (init) + 1);
g_atomic_int_set (&ret->refcount, 1);
ret->folded = NULL;
ret->sortkey = NULL;
g_hash_table_insert (rb_refstrings, ret->value, ret);
g_mutex_unlock (rb_refstrings_mutex);
return ret;
}
/**
* rb_refstring_find:
* @init: string to find
*
* Returns an existing #RBRefString for @init if one exists,
* otherwise returns NULL. If an existing refstring is found,
* its reference count is increased.
*
* Return value: existing #RBRefString, or NULL
*/
RBRefString *
rb_refstring_find (const char *init)
{
RBRefString *ret;
g_mutex_lock (rb_refstrings_mutex);
ret = g_hash_table_lookup (rb_refstrings, init);
if (ret)
rb_refstring_ref (ret);
g_mutex_unlock (rb_refstrings_mutex);
return ret;
}
/**
* rb_refstring_unref:
* @val: #RBRefString to unref
*
* Drops a reference to an #RBRefString. If this is the last
* reference, the string will be freed.
*/
void
rb_refstring_unref (RBRefString *val)
{
if (val == NULL)
return;
g_return_if_fail (g_atomic_int_get (&val->refcount) > 0);
if (g_atomic_int_dec_and_test (&val->refcount)) {
g_mutex_lock (rb_refstrings_mutex);
/* ensure it's still not referenced, as something may have called
* rb_refstring_new since we decremented the count */
if (g_atomic_int_get (&val->refcount) == 0)
g_hash_table_remove (rb_refstrings, val->value);
g_mutex_unlock (rb_refstrings_mutex);
}
}
/**
* rb_refstring_system_shutdown:
*
* Frees all data associated with the refstring system.
* Only called on shutdown.
*/
void
rb_refstring_system_shutdown (void)
{
g_hash_table_destroy (rb_refstrings);
g_mutex_free (rb_refstrings_mutex);
}
/**
* rb_refstring_ref:
* @val: a #RBRefString to reference
*
* Increases the reference count for an existing #RBRefString.
* The refstring is returned for convenience.
*
* Return value: the same refstring
*/
RBRefString *
rb_refstring_ref (RBRefString *val)
{
if (val == NULL)
return NULL;
g_return_val_if_fail (g_atomic_int_get (&val->refcount) > 0, NULL);
g_atomic_int_inc (&val->refcount);
return val;
}
/**
* rb_refstring_get:
* @val: an #RBRefString
*
* Returns the actual string for a #RBRefString.
*
* Return value: underlying string, must not be freed
*/
const char *
rb_refstring_get (const RBRefString *val)
{
return val ? val->value : NULL;
}
/*
* The next two functions will compute the values if they haven't
* been already done. Using g_atomic_* is much more efficient than
* using mutexes (since mutexes may require kernel calls) and these
* get called often.
*/
/**
* rb_refstring_get_folded:
* @val: an #RBRefString
*
* Returns the case-folded version of the string underlying @val.
* The case-folded string is cached inside the #RBRefString for
* speed. See @rb_search_fold for information on case-folding
* strings.
*
* Return value: case-folded string, must not be freed
*/
const char *
rb_refstring_get_folded (RBRefString *val)
{
gpointer *ptr;
const char *string;
if (val == NULL)
return NULL;
ptr = &val->folded;
string = (const char*)g_atomic_pointer_get (ptr);
if (string == NULL) {
char *newstring;
newstring = rb_search_fold (rb_refstring_get (val));
if (g_atomic_pointer_compare_and_exchange (ptr, NULL, newstring)) {
string = newstring;
} else {
g_free (newstring);
string = (const char *)g_atomic_pointer_get (ptr);
g_assert (string);
}
}
return string;
}
/**
* rb_refstring_get_sort_key:
* @val: an #RBRefString
*
* Returns the sort key version of the string underlying @val.
* The sort key string is cached inside the #RBRefString for speed.
* Sort key strings are not generally human readable, so don't display
* them anywhere. See @g_utf8_collate_key_for_filename for information
* on sort keys.
*
* Return value: sort key string, must not be freed.
*/
const char *
rb_refstring_get_sort_key (RBRefString *val)
{
gpointer *ptr;
const char *string;
if (val == NULL)
return NULL;
ptr = &val->sortkey;
string = (const char *)g_atomic_pointer_get (ptr);
if (string == NULL) {
char *newstring;
char *s;
s = g_utf8_casefold (val->value, -1);
newstring = g_utf8_collate_key_for_filename (s, -1);
g_free (s);
if (g_atomic_pointer_compare_and_exchange (ptr, NULL, newstring)) {
string = newstring;
} else {
g_free (newstring);
string = (const char*)g_atomic_pointer_get (ptr);
g_assert (string);
}
}
return string;
}
/**
* rb_refstring_hash:
* @p: an #RBRefString
*
* Hash function suitable for use with @GHashTable.
*
* Return value: hash value for the string underlying @p
*/
guint
rb_refstring_hash (gconstpointer p)
{
const RBRefString *ref = p;
return g_str_hash (rb_refstring_get (ref));
}
/**
* rb_refstring_equal:
* @ap: an #RBRefString
* @bp: another #RBRefstring
*
* Key equality function suitable for use with #GHashTable.
* Equality checks for #RBRefString are just pointer comparisons,
* since there can only be one refstring for a given string.
*
* Return value: %TRUE if the strings are the same
*/
gboolean
rb_refstring_equal (gconstpointer ap, gconstpointer bp)
{
return (ap == bp);
}
GType
rb_refstring_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
type = g_boxed_type_register_static ("RBRefString",
(GBoxedCopyFunc)rb_refstring_ref,
(GBoxedFreeFunc)rb_refstring_unref);
}
return type;
}
|