-
-/**
- * SECTION: quarks
- * @title: Quarks
- * @short_description: a 2-way association between a string and a
- * unique integer identifier
- *
- * Quarks are associations between strings and integer identifiers.
- * Given either the string or the #GQuark identifier it is possible to
- * retrieve the other.
- *
- * Quarks are used for both <link
- * linkend="glib-datasets">Datasets</link> and <link
- * linkend="glib-keyed-data-lists">Keyed Data Lists</link>.
- *
- * To create a new quark from a string, use g_quark_from_string() or
- * g_quark_from_static_string().
- *
- * To find the string corresponding to a given #GQuark, use
- * g_quark_to_string().
- *
- * To find the #GQuark corresponding to a given string, use
- * g_quark_try_string().
- *
- * Another use for the string pool maintained for the quark functions
- * is string interning, using g_intern_string() or
- * g_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 comparision, rather than using strcmp().
- **/
-
-/**
- * GQuark:
- *
- * A GQuark is a non-zero integer which uniquely identifies a
- * particular string. A GQuark value of zero is associated to %NULL.
- **/
-
-/**
- * g_quark_try_string:
- * @string: a string.
- * @Returns: the #GQuark associated with the string, or 0 if @string is
- * %NULL or there is no #GQuark associated with it.
- *
- * 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().
- **/
-GQuark
-g_quark_try_string (const gchar *string)
-{
- GQuark quark = 0;
- g_return_val_if_fail (string != NULL, 0);
-
- G_LOCK (g_quark_global);
- if (g_quark_ht)
- quark = GPOINTER_TO_UINT (g_hash_table_lookup (g_quark_ht, string));
- G_UNLOCK (g_quark_global);
-
- return quark;
-}
-
-#define QUARK_STRING_BLOCK_SIZE (4096 - sizeof (gsize))
-static char *quark_block = NULL;
-static int quark_block_offset = 0;
-
-/* HOLDS: g_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: g_quark_global_lock */
-static inline GQuark
-g_quark_from_string_internal (const gchar *string,
- gboolean duplicate)
-{
- GQuark quark = 0;
-
- if (g_quark_ht)
- quark = GPOINTER_TO_UINT (g_hash_table_lookup (g_quark_ht, string));
-
- if (!quark)
- {
- quark = g_quark_new (duplicate ? quark_strdup (string) : (gchar *)string);
- TRACE(GLIB_QUARK_NEW(string, quark));
- }
-
- return quark;
-}
-
-/**
- * g_quark_from_string:
- * @string: a string.
- * @Returns: the #GQuark identifying the string, or 0 if @string is
- * %NULL.
- *
- * 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.
- **/
-GQuark
-g_quark_from_string (const gchar *string)
-{
- GQuark quark;
-
- if (!string)
- return 0;
-
- G_LOCK (g_quark_global);
- quark = g_quark_from_string_internal (string, TRUE);
- G_UNLOCK (g_quark_global);
-
- return quark;
-}
-
-/**
- * g_quark_from_static_string:
- * @string: a string.
- * @Returns: the #GQuark identifying the string, or 0 if @string is
- * %NULL.
- *
- * 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 <emphasis>always</emphasis> exist. 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).
- **/
-GQuark
-g_quark_from_static_string (const gchar *string)
-{
- GQuark quark;
-
- if (!string)
- return 0;
-
- G_LOCK (g_quark_global);
- quark = g_quark_from_string_internal (string, FALSE);
- G_UNLOCK (g_quark_global);
-
- return quark;
-}
-
-/**
- * g_quark_to_string:
- * @quark: a #GQuark.
- * @Returns: the string associated with the #GQuark.
- *
- * Gets the string associated with the given #GQuark.
- **/
-G_CONST_RETURN gchar*
-g_quark_to_string (GQuark quark)
-{
- gchar* result = NULL;
-
- G_LOCK (g_quark_global);
- if (quark < g_quark_seq_id)
- result = g_quarks[quark];
- G_UNLOCK (g_quark_global);
-
- return result;
-}
-
-/* HOLDS: g_quark_global_lock */
-static inline GQuark
-g_quark_new (gchar *string)
-{
- GQuark quark;
-
- if (g_quark_seq_id % G_QUARK_BLOCK_SIZE == 0)
- g_quarks = g_renew (gchar*, g_quarks, g_quark_seq_id + G_QUARK_BLOCK_SIZE);
- if (!g_quark_ht)
- {
- g_assert (g_quark_seq_id == 0);
- g_quark_ht = g_hash_table_new (g_str_hash, g_str_equal);
- g_quarks[g_quark_seq_id++] = NULL;
- }
-
- quark = g_quark_seq_id++;
- g_quarks[quark] = string;
- g_hash_table_insert (g_quark_ht, string, GUINT_TO_POINTER (quark));
-
- return quark;
-}
-
-/**
- * g_intern_string:
- * @string: a string
- *
- * Returns a canonical representation for @string. Interned strings can
- * be compared for equality by comparing the pointers, instead of using strcmp().
- *
- * Returns: a canonical representation for the string
- *
- * Since: 2.10
- */
-G_CONST_RETURN gchar*
-g_intern_string (const gchar *string)
-{
- const gchar *result;
- GQuark quark;
-
- if (!string)
- return NULL;
-
- G_LOCK (g_quark_global);
- quark = g_quark_from_string_internal (string, TRUE);
- result = g_quarks[quark];
- G_UNLOCK (g_quark_global);
-
- return result;
-}
-
-/**
- * g_intern_static_string:
- * @string: 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.
- *
- * Returns: a canonical representation for the string
- *
- * Since: 2.10
- */
-G_CONST_RETURN gchar*
-g_intern_static_string (const gchar *string)
-{
- GQuark quark;
- const gchar *result;
-
- if (!string)
- return NULL;
-
- G_LOCK (g_quark_global);
- quark = g_quark_from_string_internal (string, FALSE);
- result = g_quarks[quark];
- G_UNLOCK (g_quark_global);
-
- return result;
-}