Blame glib/gquark.c

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