GValue: Add interned string support

This adds support to be able to explicitely stored interned strings into
G_TYPE_STRING GValue.

This is useful for cases where the user:
* *knows* the string to be stored in the GValue is canonical
* Wants to know whther the string stored is canonical

This allows:
* zero-cost GValue copy (the content is guaranteed to be unique and exist
  throughout the process life)
* zero-cost string equality checks (if both string GValue are interned, you just
  need to check the pointers for equality or not, instead of doing a strcmp).

Fixes #2109
This commit is contained in:
Edward Hervey
2020-05-15 07:38:30 +02:00
committed by Edward Hervey
parent c964749de6
commit 1a95ce84ed
5 changed files with 99 additions and 0 deletions

View File

@@ -1062,6 +1062,9 @@ g_value_set_string (GValue *value,
* Set the contents of a %G_TYPE_STRING #GValue to @v_string.
* The string is assumed to be static, and is thus not duplicated
* when setting the #GValue.
*
* If the the string is a canonical string, using g_value_set_interned_string()
* is more appropriate.
*/
void
g_value_set_static_string (GValue *value,
@@ -1075,6 +1078,29 @@ g_value_set_static_string (GValue *value,
value->data[0].v_pointer = (gchar*) v_string;
}
/**
* g_value_set_interned_string:
* @value: a valid #GValue of type %G_TYPE_STRING
* @v_string: (nullable): static string to be set
*
* Set the contents of a %G_TYPE_STRING #GValue to @v_string. The string is
* assumed to be static and interned (canonical, for example from
* g_intern_string()), and is thus not duplicated when setting the #GValue.
*
* Since: 2.66
*/
void
g_value_set_interned_string (GValue *value,
const gchar *v_string)
{
g_return_if_fail (G_VALUE_HOLDS_STRING (value));
if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
g_free (value->data[0].v_pointer);
value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS | G_VALUE_INTERNED_STRING;
value->data[0].v_pointer = (gchar *) v_string;
}
/**
* g_value_set_string_take_ownership:
* @value: a valid #GValue of type %G_TYPE_STRING