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 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
41 #include "gstrfuncs.h"
43 #include "gtestutils.h"
44 #include "glib_trace.h"
45 #include "glib-init.h"
46 #include "glib-private.h"
48 #define QUARK_BLOCK_SIZE 32
49 #define QUARK_STRING_BLOCK_SIZE (4096 - sizeof (gsize))
51 static inline GQuark quark_new (gchar *string);
53 G_LOCK_DEFINE_STATIC (quark_global);
54 static GHashTable *quark_ht = NULL;
55 static gchar **quarks = NULL;
56 static gint quark_seq_id = 0;
57 static gchar *quark_block = NULL;
58 static gint quark_block_offset = 0;
63 g_assert (quark_seq_id == 0);
64 quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
65 quarks = g_new (gchar*, QUARK_BLOCK_SIZE);
73 * A GQuark is a non-zero integer which uniquely identifies a
76 * A GQuark value of zero is associated to `NULL`.
78 * Given either the string or the `GQuark` identifier it is possible to
81 * Quarks are used for both
82 * [datasets and keyed data lists](datalist-and-dataset.html).
84 * To create a new quark from a string, use [func@GLib.quark_from_string]
85 * or [func@GLib.quark_from_static_string].
87 * To find the string corresponding to a given `GQuark`, use
88 * [func@GLib.quark_to_string].
90 * To find the `GQuark` corresponding to a given string, use
91 * [func@GLib.quark_try_string].
93 * Another use for the string pool maintained for the quark functions
94 * is string interning, using [func@GLib.intern_string] or
95 * [func@GLib.intern_static_string]. An interned string is a canonical
96 * representation for a string. One important advantage of interned
97 * strings is that they can be compared for equality by a simple
98 * pointer comparison, rather than using `strcmp()`.
103 * @QN: the name to return a #GQuark for
104 * @q_n: prefix for the function name
106 * A convenience macro which defines a function returning the
107 * #GQuark for the name @QN. The function will be named
110 * Note that the quark name will be stringified automatically
111 * in the macro, so you shouldn't use double quotes.
117 * g_quark_try_string:
118 * @string: (nullable): a string
120 * Gets the #GQuark associated with the given string, or 0 if string is
121 * %NULL or it has no associated #GQuark.
123 * If you want the GQuark to be created if it doesn't already exist,
124 * use g_quark_from_string() or g_quark_from_static_string().
126 * This function must not be used before library constructors have finished
129 * Returns: the #GQuark associated with the string, or 0 if @string is
130 * %NULL or there is no #GQuark associated with it
133 g_quark_try_string (const gchar *string)
140 G_LOCK (quark_global);
141 quark = GPOINTER_TO_UINT (g_hash_table_lookup (quark_ht, string));
142 G_UNLOCK (quark_global);
147 /* HOLDS: quark_global_lock */
149 quark_strdup (const gchar *string)
154 len = strlen (string) + 1;
156 /* For strings longer than half the block size, fall back
157 to strdup so that we fill our blocks at least 50%. */
158 if (len > QUARK_STRING_BLOCK_SIZE / 2)
159 return g_strdup (string);
161 if (quark_block == NULL ||
162 QUARK_STRING_BLOCK_SIZE - quark_block_offset < len)
164 quark_block = g_malloc (QUARK_STRING_BLOCK_SIZE);
165 quark_block_offset = 0;
168 copy = quark_block + quark_block_offset;
169 memcpy (copy, string, len);
170 quark_block_offset += len;
175 /* HOLDS: quark_global_lock */
177 quark_from_string (const gchar *string,
182 quark = GPOINTER_TO_UINT (g_hash_table_lookup (quark_ht, string));
186 quark = quark_new (duplicate ? quark_strdup (string) : (gchar *)string);
187 TRACE(GLIB_QUARK_NEW(string, quark));
194 quark_from_string_locked (const gchar *string,
202 G_LOCK (quark_global);
203 quark = quark_from_string (string, duplicate);
204 G_UNLOCK (quark_global);
210 * g_quark_from_string:
211 * @string: (nullable): a string
213 * Gets the #GQuark identifying the given string. If the string does
214 * not currently have an associated #GQuark, a new #GQuark is created,
215 * using a copy of the string.
217 * This function must not be used before library constructors have finished
218 * running. In particular, this means it cannot be used to initialize global
221 * Returns: the #GQuark identifying the string, or 0 if @string is %NULL
224 g_quark_from_string (const gchar *string)
226 return quark_from_string_locked (string, TRUE);
230 * g_quark_from_static_string:
231 * @string: (nullable): a string
233 * Gets the #GQuark identifying the given (static) string. If the
234 * string does not currently have an associated #GQuark, a new #GQuark
235 * is created, linked to the given string.
237 * Note that this function is identical to g_quark_from_string() except
238 * that if a new #GQuark is created the string itself is used rather
239 * than a copy. This saves memory, but can only be used if the string
240 * will continue to exist until the program terminates. It can be used
241 * with statically allocated strings in the main program, but not with
242 * statically allocated memory in dynamically loaded modules, if you
243 * expect to ever unload the module again (e.g. do not use this
244 * function in GTK theme engines).
246 * This function must not be used before library constructors have finished
247 * running. In particular, this means it cannot be used to initialize global
250 * Returns: the #GQuark identifying the string, or 0 if @string is %NULL
253 g_quark_from_static_string (const gchar *string)
255 return quark_from_string_locked (string, FALSE);
262 * Gets the string associated with the given #GQuark.
264 * Returns: the string associated with the #GQuark
267 g_quark_to_string (GQuark quark)
269 gchar* result = NULL;
273 seq_id = (guint) g_atomic_int_get (&quark_seq_id);
274 strings = g_atomic_pointer_get (&quarks);
277 result = strings[quark];
282 /* HOLDS: g_quark_global_lock */
284 quark_new (gchar *string)
289 if (quark_seq_id % QUARK_BLOCK_SIZE == 0)
291 quarks_new = g_new (gchar*, quark_seq_id + QUARK_BLOCK_SIZE);
292 if (quark_seq_id != 0)
293 memcpy (quarks_new, quarks, sizeof (char *) * quark_seq_id);
294 memset (quarks_new + quark_seq_id, 0, sizeof (char *) * QUARK_BLOCK_SIZE);
295 /* This leaks the old quarks array. Its unfortunate, but it allows
296 * us to do lockless lookup of the arrays, and there shouldn't be that
297 * many quarks in an app
299 g_ignore_leak (g_atomic_pointer_get (&quarks));
300 g_atomic_pointer_set (&quarks, quarks_new);
303 quark = quark_seq_id;
304 g_atomic_pointer_set (&quarks[quark], string);
305 g_hash_table_insert (quark_ht, string, GUINT_TO_POINTER (quark));
306 g_atomic_int_inc (&quark_seq_id);
311 static inline const gchar *
312 quark_intern_string_locked (const gchar *string,
321 G_LOCK (quark_global);
322 quark = quark_from_string (string, duplicate);
323 result = quarks[quark];
324 G_UNLOCK (quark_global);
331 * @string: (nullable): a string
333 * Returns a canonical representation for @string. Interned strings
334 * can be compared for equality by comparing the pointers, instead of
337 * This function must not be used before library constructors have finished
338 * running. In particular, this means it cannot be used to initialize global
341 * Returns: a canonical representation for the string
346 g_intern_string (const gchar *string)
348 return quark_intern_string_locked (string, TRUE);
352 * g_intern_static_string:
353 * @string: (nullable): a static string
355 * Returns a canonical representation for @string. Interned strings
356 * can be compared for equality by comparing the pointers, instead of
357 * using strcmp(). g_intern_static_string() does not copy the string,
358 * therefore @string must not be freed or modified.
360 * This function must not be used before library constructors have finished
361 * running. In particular, this means it cannot be used to initialize global
364 * Returns: a canonical representation for the string
369 g_intern_static_string (const gchar *string)
371 return quark_intern_string_locked (string, FALSE);