1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998 Tim Janik
5 * gquark.c: Functions for dealing with quarks and interned strings
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
24 * file for a list of people on the GLib Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GLib at ftp://ftp.gtk.org/pub/gtk/.
40 #include "gstrfuncs.h"
42 #include "gtestutils.h"
43 #include "glib_trace.h"
45 #define QUARK_BLOCK_SIZE 2048
46 #define QUARK_STRING_BLOCK_SIZE (4096 - sizeof (gsize))
48 static inline GQuark quark_new (gchar *string);
50 G_LOCK_DEFINE_STATIC (quark_global);
51 static GHashTable *quark_ht = NULL;
52 static gchar **quarks = NULL;
53 static gint quark_seq_id = 0;
54 static gchar *quark_block = NULL;
55 static gint quark_block_offset = 0;
60 * @short_description: a 2-way association between a string and a
61 * unique integer identifier
63 * Quarks are associations between strings and integer identifiers.
64 * Given either the string or the #GQuark identifier it is possible to
67 * Quarks are used for both <link
68 * linkend="glib-Datasets">Datasets</link> and <link
69 * linkend="glib-Keyed-Data-Lists">Keyed Data Lists</link>.
71 * To create a new quark from a string, use g_quark_from_string() or
72 * g_quark_from_static_string().
74 * To find the string corresponding to a given #GQuark, use
75 * g_quark_to_string().
77 * To find the #GQuark corresponding to a given string, use
78 * g_quark_try_string().
80 * Another use for the string pool maintained for the quark functions
81 * is string interning, using g_intern_string() or
82 * g_intern_static_string(). An interned string is a canonical
83 * representation for a string. One important advantage of interned
84 * strings is that they can be compared for equality by a simple
85 * pointer comparison, rather than using strcmp().
91 * A GQuark is a non-zero integer which uniquely identifies a
92 * particular string. A GQuark value of zero is associated to %NULL.
97 * @string: (allow-none): a string.
98 * @Returns: the #GQuark associated with the string, or 0 if @string is
99 * %NULL or there is no #GQuark associated with it.
101 * Gets the #GQuark associated with the given string, or 0 if string is
102 * %NULL or it has no associated #GQuark.
104 * If you want the GQuark to be created if it doesn't already exist,
105 * use g_quark_from_string() or g_quark_from_static_string().
108 g_quark_try_string (const gchar *string)
115 G_LOCK (quark_global);
117 quark = GPOINTER_TO_UINT (g_hash_table_lookup (quark_ht, string));
118 G_UNLOCK (quark_global);
123 /* HOLDS: quark_global_lock */
125 quark_strdup (const gchar *string)
130 len = strlen (string) + 1;
132 /* For strings longer than half the block size, fall back
133 to strdup so that we fill our blocks at least 50%. */
134 if (len > QUARK_STRING_BLOCK_SIZE / 2)
135 return g_strdup (string);
137 if (quark_block == NULL ||
138 QUARK_STRING_BLOCK_SIZE - quark_block_offset < len)
140 quark_block = g_malloc (QUARK_STRING_BLOCK_SIZE);
141 quark_block_offset = 0;
144 copy = quark_block + quark_block_offset;
145 memcpy (copy, string, len);
146 quark_block_offset += len;
151 /* HOLDS: quark_global_lock */
153 quark_from_string (const gchar *string,
159 quark = GPOINTER_TO_UINT (g_hash_table_lookup (quark_ht, string));
163 quark = quark_new (duplicate ? quark_strdup (string) : (gchar *)string);
164 TRACE(GLIB_QUARK_NEW(string, quark));
171 * g_quark_from_string:
172 * @string: (allow-none): a string.
174 * Gets the #GQuark identifying the given string. If the string does
175 * not currently have an associated #GQuark, a new #GQuark is created,
176 * using a copy of the string.
178 * Returns: the #GQuark identifying the string, or 0 if @string is
182 g_quark_from_string (const gchar *string)
189 G_LOCK (quark_global);
190 quark = quark_from_string (string, TRUE);
191 G_UNLOCK (quark_global);
197 * g_quark_from_static_string:
198 * @string: (allow-none): a string.
200 * Gets the #GQuark identifying the given (static) string. If the
201 * string does not currently have an associated #GQuark, a new #GQuark
202 * is created, linked to the given string.
204 * Note that this function is identical to g_quark_from_string() except
205 * that if a new #GQuark is created the string itself is used rather
206 * than a copy. This saves memory, but can only be used if the string
207 * will <emphasis>always</emphasis> exist. It can be used with
208 * statically allocated strings in the main program, but not with
209 * statically allocated memory in dynamically loaded modules, if you
210 * expect to ever unload the module again (e.g. do not use this
211 * function in GTK+ theme engines).
213 * Returns: the #GQuark identifying the string, or 0 if @string is
217 g_quark_from_static_string (const gchar *string)
224 G_LOCK (quark_global);
225 quark = quark_from_string (string, FALSE);
226 G_UNLOCK (quark_global);
235 * Gets the string associated with the given #GQuark.
237 * Returns: the string associated with the #GQuark
240 g_quark_to_string (GQuark quark)
242 gchar* result = NULL;
246 seq_id = g_atomic_int_get (&quark_seq_id);
247 strings = g_atomic_pointer_get (&quarks);
250 result = strings[quark];
255 /* HOLDS: g_quark_global_lock */
257 quark_new (gchar *string)
262 if (quark_seq_id % QUARK_BLOCK_SIZE == 0)
264 quarks_new = g_new (gchar*, quark_seq_id + QUARK_BLOCK_SIZE);
265 if (quark_seq_id != 0)
266 memcpy (quarks_new, quarks, sizeof (char *) * quark_seq_id);
267 memset (quarks_new + quark_seq_id, 0, sizeof (char *) * QUARK_BLOCK_SIZE);
268 /* This leaks the old quarks array. Its unfortunate, but it allows
269 * us to do lockless lookup of the arrays, and there shouldn't be that
270 * many quarks in an app
272 g_atomic_pointer_set (&quarks, quarks_new);
276 g_assert (quark_seq_id == 0);
277 quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
278 quarks[quark_seq_id] = NULL;
279 g_atomic_int_inc (&quark_seq_id);
282 quark = quark_seq_id;
283 g_atomic_pointer_set (&quarks[quark], string);
284 g_hash_table_insert (quark_ht, string, GUINT_TO_POINTER (quark));
285 g_atomic_int_inc (&quark_seq_id);
292 * @string: (allow-none): a string
294 * Returns a canonical representation for @string. Interned strings
295 * can be compared for equality by comparing the pointers, instead of
298 * Returns: a canonical representation for the string
303 g_intern_string (const gchar *string)
311 G_LOCK (quark_global);
312 quark = quark_from_string (string, TRUE);
313 result = quarks[quark];
314 G_UNLOCK (quark_global);
320 * g_intern_static_string:
321 * @string: (allow-none): a static string
323 * Returns a canonical representation for @string. Interned strings
324 * can be compared for equality by comparing the pointers, instead of
325 * using strcmp(). g_intern_static_string() does not copy the string,
326 * therefore @string must not be freed or modified.
328 * Returns: a canonical representation for the string
333 g_intern_static_string (const gchar *string)
341 G_LOCK (quark_global);
342 quark = quark_from_string (string, FALSE);
343 result = quarks[quark];
344 G_UNLOCK (quark_global);