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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
* Copyright (C) 1998 Tim Janik
*
* gquark.c: Functions for dealing with quarks and interned strings
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
/*
* MT safe
*/
#include "config.h"
#include <string.h>
#include "gslice.h"
#include "ghash.h"
#include "gquark.h"
#include "gstrfuncs.h"
#include "gthread.h"
#include "gtestutils.h"
#include "glib_trace.h"
#include "glib-init.h"
#include "glib-private.h"
#define QUARK_BLOCK_SIZE 2048
#define QUARK_STRING_BLOCK_SIZE (4096 - sizeof (gsize))
static inline GQuark quark_new (gchar *string);
G_LOCK_DEFINE_STATIC (quark_global);
static GHashTable *quark_ht = NULL;
static gchar **quarks = NULL;
static gint quark_seq_id = 0;
static gchar *quark_block = NULL;
static gint quark_block_offset = 0;
void
g_quark_init (void)
{
g_assert (quark_seq_id == 0);
quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
quarks = g_new (gchar*, QUARK_BLOCK_SIZE);
quarks[0] = NULL;
quark_seq_id = 1;
}
/**
* GQuark:
*
* A GQuark is a non-zero integer which uniquely identifies a
* particular string.
*
* A GQuark value of zero is associated to `NULL`.
*
* Given either the string or the `GQuark` identifier it is possible to
* retrieve the other.
*
* Quarks are used for both
* [datasets and keyed data lists](datalist-and-dataset.html).
*
* To create a new quark from a string, use [func@GLib.quark_from_string]
* or [func@GLib.quark_from_static_string].
*
* To find the string corresponding to a given `GQuark`, use
* [func@GLib.quark_to_string].
*
* To find the `GQuark` corresponding to a given string, use
* [func@GLib.quark_try_string].
*
* Another use for the string pool maintained for the quark functions
* is string interning, using [func@GLib.intern_string] or
* [func@GLib.intern_static_string]. An interned string is a canonical
* representation for a string. One important advantage of interned
* strings is that they can be compared for equality by a simple
* pointer comparison, rather than using `strcmp()`.
*/
/**
* G_DEFINE_QUARK:
* @QN: the name to return a #GQuark for
* @q_n: prefix for the function name
*
* A convenience macro which defines a function returning the
* #GQuark for the name @QN. The function will be named
* @q_n_quark().
*
* Note that the quark name will be stringified automatically
* in the macro, so you shouldn't use double quotes.
*
* Since: 2.34
*/
/**
* g_quark_try_string:
* @string: (nullable): a string
*
* Gets the #GQuark associated with the given string, or 0 if string is
* %NULL or it has no associated #GQuark.
*
* If you want the GQuark to be created if it doesn't already exist,
* use g_quark_from_string() or g_quark_from_static_string().
*
* This function must not be used before library constructors have finished
* running.
*
* Returns: the #GQuark associated with the string, or 0 if @string is
* %NULL or there is no #GQuark associated with it
*/
GQuark
g_quark_try_string (const gchar *string)
{
GQuark quark = 0;
if (string == NULL)
return 0;
G_LOCK (quark_global);
quark = GPOINTER_TO_UINT (g_hash_table_lookup (quark_ht, string));
G_UNLOCK (quark_global);
return quark;
}
/* HOLDS: quark_global_lock */
static char *
quark_strdup (const gchar *string)
{
gchar *copy;
gsize len;
len = strlen (string) + 1;
/* For strings longer than half the block size, fall back
to strdup so that we fill our blocks at least 50%. */
if (len > QUARK_STRING_BLOCK_SIZE / 2)
return g_strdup (string);
if (quark_block == NULL ||
QUARK_STRING_BLOCK_SIZE - quark_block_offset < len)
{
quark_block = g_malloc (QUARK_STRING_BLOCK_SIZE);
quark_block_offset = 0;
}
copy = quark_block + quark_block_offset;
memcpy (copy, string, len);
quark_block_offset += len;
return copy;
}
/* HOLDS: quark_global_lock */
static inline GQuark
quark_from_string (const gchar *string,
gboolean duplicate)
{
GQuark quark = 0;
quark = GPOINTER_TO_UINT (g_hash_table_lookup (quark_ht, string));
if (!quark)
{
quark = quark_new (duplicate ? quark_strdup (string) : (gchar *)string);
TRACE(GLIB_QUARK_NEW(string, quark));
}
return quark;
}
static inline GQuark
quark_from_string_locked (const gchar *string,
gboolean duplicate)
{
GQuark quark = 0;
if (!string)
return 0;
G_LOCK (quark_global);
quark = quark_from_string (string, duplicate);
G_UNLOCK (quark_global);
return quark;
}
/**
* g_quark_from_string:
* @string: (nullable): a string
*
* Gets the #GQuark identifying the given string. If the string does
* not currently have an associated #GQuark, a new #GQuark is created,
* using a copy of the string.
*
* This function must not be used before library constructors have finished
* running. In particular, this means it cannot be used to initialize global
* variables in C++.
*
* Returns: the #GQuark identifying the string, or 0 if @string is %NULL
*/
GQuark
g_quark_from_string (const gchar *string)
{
return quark_from_string_locked (string, TRUE);
}
/**
* g_quark_from_static_string:
* @string: (nullable): a string
*
* Gets the #GQuark identifying the given (static) string. If the
* string does not currently have an associated #GQuark, a new #GQuark
* is created, linked to the given string.
*
* Note that this function is identical to g_quark_from_string() except
* that if a new #GQuark is created the string itself is used rather
* than a copy. This saves memory, but can only be used if the string
* will continue to exist until the program terminates. It can be used
* with statically allocated strings in the main program, but not with
* statically allocated memory in dynamically loaded modules, if you
* expect to ever unload the module again (e.g. do not use this
* function in GTK theme engines).
*
* This function must not be used before library constructors have finished
* running. In particular, this means it cannot be used to initialize global
* variables in C++.
*
* Returns: the #GQuark identifying the string, or 0 if @string is %NULL
*/
GQuark
g_quark_from_static_string (const gchar *string)
{
return quark_from_string_locked (string, FALSE);
}
/**
* g_quark_to_string:
* @quark: a #GQuark.
*
* Gets the string associated with the given #GQuark.
*
* Returns: the string associated with the #GQuark
*/
const gchar *
g_quark_to_string (GQuark quark)
{
gchar* result = NULL;
gchar **strings;
guint seq_id;
seq_id = (guint) g_atomic_int_get (&quark_seq_id);
strings = g_atomic_pointer_get (&quarks);
if (quark < seq_id)
result = strings[quark];
return result;
}
/* HOLDS: g_quark_global_lock */
static inline GQuark
quark_new (gchar *string)
{
GQuark quark;
gchar **quarks_new;
if (quark_seq_id % QUARK_BLOCK_SIZE == 0)
{
quarks_new = g_new (gchar*, quark_seq_id + QUARK_BLOCK_SIZE);
if (quark_seq_id != 0)
memcpy (quarks_new, quarks, sizeof (char *) * quark_seq_id);
memset (quarks_new + quark_seq_id, 0, sizeof (char *) * QUARK_BLOCK_SIZE);
/* This leaks the old quarks array. Its unfortunate, but it allows
* us to do lockless lookup of the arrays, and there shouldn't be that
* many quarks in an app
*/
g_ignore_leak (g_atomic_pointer_get (&quarks));
g_atomic_pointer_set (&quarks, quarks_new);
}
quark = quark_seq_id;
g_atomic_pointer_set (&quarks[quark], string);
g_hash_table_insert (quark_ht, string, GUINT_TO_POINTER (quark));
g_atomic_int_inc (&quark_seq_id);
return quark;
}
static inline const gchar *
quark_intern_string_locked (const gchar *string,
gboolean duplicate)
{
const gchar *result;
GQuark quark;
if (!string)
return NULL;
G_LOCK (quark_global);
quark = quark_from_string (string, duplicate);
result = quarks[quark];
G_UNLOCK (quark_global);
return result;
}
/**
* g_intern_string:
* @string: (nullable): a string
*
* Returns a canonical representation for @string. Interned strings
* can be compared for equality by comparing the pointers, instead of
* using strcmp().
*
* This function must not be used before library constructors have finished
* running. In particular, this means it cannot be used to initialize global
* variables in C++.
*
* Returns: a canonical representation for the string
*
* Since: 2.10
*/
const gchar *
g_intern_string (const gchar *string)
{
return quark_intern_string_locked (string, TRUE);
}
/**
* g_intern_static_string:
* @string: (nullable): a static string
*
* Returns a canonical representation for @string. Interned strings
* can be compared for equality by comparing the pointers, instead of
* using strcmp(). g_intern_static_string() does not copy the string,
* therefore @string must not be freed or modified.
*
* This function must not be used before library constructors have finished
* running. In particular, this means it cannot be used to initialize global
* variables in C++.
*
* Returns: a canonical representation for the string
*
* Since: 2.10
*/
const gchar *
g_intern_static_string (const gchar *string)
{
return quark_intern_string_locked (string, FALSE);
}
|