Updated Macedonian Translation <arangela@cvs.gnome.org>
[rhythmbox.git] / rhythmdb / rb-refstring.c
blob6f1bd5adf6f2ce87effc00dcbfc8e157244e7cbb
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
29 GHashTable *rb_refstrings;
30 GMutex *rb_refstrings_mutex;
32 #define G_IMPLEMENT_INLINES 1
33 #define __RB_REFSTRING_C__
34 #include "rb-refstring.h"
35 #undef G_IMPLEMENT_INLINES
37 static void
38 rb_refstring_free (RBRefString *refstr)
40 refstr->refcount = 0xdeadbeef;
41 g_free (refstr->folded);
42 g_free (refstr->sortkey);
43 g_free (refstr);
46 void
47 rb_refstring_system_init (void)
49 rb_refstrings_mutex = g_mutex_new ();
51 rb_refstrings = g_hash_table_new_full (g_str_hash, g_str_equal,
52 NULL, (GDestroyNotify) rb_refstring_free);
55 RBRefString *
56 rb_refstring_new_full (const char *init, gboolean compute_sortdata)
58 RBRefString *ret;
60 g_mutex_lock (rb_refstrings_mutex);
61 ret = g_hash_table_lookup (rb_refstrings, init);
62 g_mutex_unlock (rb_refstrings_mutex);
64 if (ret) {
65 rb_refstring_ref (ret);
66 return ret;
69 ret = g_malloc (sizeof (RBRefString) + strlen (init));
71 ret->refcount = 1;
73 if (compute_sortdata) {
74 ret->folded = rb_search_fold (init);
75 ret->sortkey = rb_utf8_collate_key_for_filename (ret->folded, -1);
76 } else {
77 ret->folded = NULL;
78 ret->sortkey = NULL;
81 strcpy (ret->value, init);
83 g_mutex_lock (rb_refstrings_mutex);
84 g_hash_table_insert (rb_refstrings, ret->value, ret);
85 g_mutex_unlock (rb_refstrings_mutex);
86 return ret;
89 void
90 rb_refstring_unref (RBRefString *val)
92 if (!val)
93 return;
95 if (g_atomic_int_dec_and_test (&val->refcount)) {
96 g_mutex_lock (rb_refstrings_mutex);
97 g_hash_table_remove (rb_refstrings, val->value);
98 g_mutex_unlock (rb_refstrings_mutex);
102 void
103 rb_refstring_system_shutdown (void)
105 g_hash_table_destroy (rb_refstrings);
106 g_mutex_free (rb_refstrings_mutex);