File: grefstring.c

package info (click to toggle)
glib2.0 2.86.1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 67,012 kB
  • sloc: ansic: 544,596; python: 9,702; sh: 1,612; xml: 1,482; perl: 1,222; cpp: 535; makefile: 321; javascript: 11
file content (337 lines) | stat: -rw-r--r-- 9,660 bytes parent folder | download | duplicates (4)
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
/* grefstring.c: Reference counted strings
 *
 * Copyright 2018  Emmanuele Bassi
 *
 * 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/>.
 */

#include "config.h"

#include "grefstring.h"

#include "ghash.h"
#include "gmessages.h"
#include "gtestutils.h"
#include "gthread.h"

#include <string.h>

/* A global table of refcounted strings; the hash table does not own
 * the strings, just a pointer to them. Strings are interned as long
 * as they are alive; once their reference count drops to zero, they
 * are removed from the table
 */
G_LOCK_DEFINE_STATIC (interned_ref_strings);
static GHashTable *interned_ref_strings;

#if G_GNUC_CHECK_VERSION(4,8) || defined(__clang__)
# define _attribute_aligned(n) __attribute__((aligned(n)))
#elif defined(_MSC_VER)
# define _attribute_aligned(n) __declspec(align(n))
#else
# define _attribute_aligned(n)
#endif

typedef struct {
  /* Length of the string without NUL-terminator */
  size_t len;
  /* Atomic reference count placed here to reduce struct padding */
  int ref_count;
  /* TRUE if interned, FALSE otherwise; immutable after construction */
  guint8 interned;

  /* First character of the actual string
   * Make sure it is at least 2 * sizeof (size_t) aligned to allow for SIMD
   * optimizations in operations on the string.
   * Because MSVC sucks we need to handle both cases explicitly. */
#if GLIB_SIZEOF_SIZE_T == 4
  _attribute_aligned (8) char s[];
#elif GLIB_SIZEOF_SIZE_T == 8
  _attribute_aligned (16) char s[];
#else
#error "Only 32 bit and 64 bit size_t supported currently"
#endif
} GRefStringImpl;

/* Assert that the start of the actual string is at least 2 * alignof (size_t) aligned */
G_STATIC_ASSERT (offsetof (GRefStringImpl, s) % (2 * G_ALIGNOF (size_t)) == 0);

/* Gets a pointer to the GRefStringImpl from its string pointer */
#define G_REF_STRING_IMPL_FROM_STR(str) ((GRefStringImpl *) ((guint8 *) str - offsetof (GRefStringImpl, s)))
/* Gets a pointer to the string pointer from the GRefStringImpl */
#define G_REF_STRING_IMPL_TO_STR(str) (str->s)

/**
 * g_ref_string_new:
 * @str: (not nullable): a NUL-terminated string
 *
 * Creates a new reference counted string and copies the contents of @str
 * into it.
 *
 * Returns: (transfer full) (not nullable): the newly created reference counted string
 *
 * Since: 2.58
 */
char *
g_ref_string_new (const char *str)
{
  GRefStringImpl *impl;
  gsize len;

  g_return_val_if_fail (str != NULL, NULL);

  len = strlen (str);

  if (sizeof (char) * len > G_MAXSIZE - sizeof (GRefStringImpl) - 1)
    g_error ("GRefString allocation would overflow");

  impl = g_malloc (sizeof (GRefStringImpl) + sizeof (char) * len + 1);
  impl->len = len;
  impl->ref_count = 1;
  impl->interned = FALSE;
  memcpy (G_REF_STRING_IMPL_TO_STR (impl), str, len + 1);

  return G_REF_STRING_IMPL_TO_STR (impl);
}

/**
 * g_ref_string_new_len:
 * @str: (not nullable): a string
 * @len: length of @str to use, or -1 if @str is nul-terminated
 *
 * Creates a new reference counted string and copies the contents of @str
 * into it, up to @len bytes.
 *
 * Since this function does not stop at nul bytes, it is the caller's
 * responsibility to ensure that @str has at least @len addressable bytes.
 *
 * Returns: (transfer full) (not nullable): the newly created reference counted string
 *
 * Since: 2.58
 */
char *
g_ref_string_new_len (const char *str, gssize len)
{
  GRefStringImpl *impl;

  g_return_val_if_fail (str != NULL, NULL);

  if (len < 0)
    return g_ref_string_new (str);

  /* allocate then copy as str[len] may not be readable */
  if (sizeof (char) * len > G_MAXSIZE - sizeof (GRefStringImpl) - 1)
    g_error ("GRefString allocation would overflow");

  impl = g_malloc (sizeof (GRefStringImpl) + sizeof (char) * len + 1);
  impl->len = len;
  impl->ref_count = 1;
  impl->interned = FALSE;
  memcpy (G_REF_STRING_IMPL_TO_STR (impl), str, len);
  G_REF_STRING_IMPL_TO_STR (impl)[len] = '\0';

  return G_REF_STRING_IMPL_TO_STR (impl);
}

/* interned_str_equal: variant of g_str_equal() that compares
 * pointers as well as contents; this avoids running strcmp()
 * on arbitrarily long strings, as it's more likely to have
 * g_ref_string_new_intern() being called on the same refcounted
 * string instance, than on a different string with the same
 * contents
 */
static gboolean
interned_str_equal (gconstpointer v1,
                    gconstpointer v2)
{
  const char *str1 = v1;
  const char *str2 = v2;

  if (v1 == v2)
    return TRUE;

  return strcmp (str1, str2) == 0;
}

/**
 * g_ref_string_new_intern:
 * @str: (not nullable): a NUL-terminated string
 *
 * Creates a new reference counted string and copies the content of @str
 * into it.
 *
 * If you call this function multiple times with the same @str, or with
 * the same contents of @str, it will return a new reference, instead of
 * creating a new string.
 *
 * Returns: (transfer full) (not nullable): the newly created reference
 *   counted string, or a new reference to an existing string
 *
 * Since: 2.58
 */
char *
g_ref_string_new_intern (const char *str)
{
  char *res;

  g_return_val_if_fail (str != NULL, NULL);

  G_LOCK (interned_ref_strings);

  if (G_UNLIKELY (interned_ref_strings == NULL))
    interned_ref_strings = g_hash_table_new (g_str_hash, interned_str_equal);

  res = g_hash_table_lookup (interned_ref_strings, str);
  if (res != NULL)
    {
      GRefStringImpl *impl = G_REF_STRING_IMPL_FROM_STR (res);
      g_atomic_int_inc (&impl->ref_count);
      G_UNLOCK (interned_ref_strings);
      return res;
    }

  res = g_ref_string_new (str);
  G_REF_STRING_IMPL_FROM_STR (res)->interned = TRUE;
  g_hash_table_add (interned_ref_strings, res);
  G_UNLOCK (interned_ref_strings);

  return res;
}

/**
 * g_ref_string_acquire:
 * @str: a reference counted string
 *
 * Acquires a reference on a string.
 *
 * Returns: the given string, with its reference count increased
 *
 * Since: 2.58
 */
char *
g_ref_string_acquire (char *str)
{
  g_return_val_if_fail (str != NULL, NULL);

  g_atomic_int_inc (&G_REF_STRING_IMPL_FROM_STR (str)->ref_count);

  return str;
}

/**
 * g_ref_string_release:
 * @str: a reference counted string
 *
 * Releases a reference on a string; if it was the last reference, the
 * resources allocated by the string are freed as well.
 *
 * Since: 2.58
 */
void
g_ref_string_release (char *str)
{
  GRefStringImpl *impl;
  int old_ref_count;

  g_return_if_fail (str != NULL);

  impl = G_REF_STRING_IMPL_FROM_STR (str);

  /* Non-interned strings are easy so let's get that out of the way here first */
  if (!impl->interned)
    {
      if (g_atomic_int_dec_and_test (&impl->ref_count))
        g_free (impl);
      return;
    }

  old_ref_count = g_atomic_int_get (&impl->ref_count);
  g_assert (old_ref_count >= 1);
retry:
  /* Fast path: multiple references, we can just try decrementing and be done with it */
  if (old_ref_count > 1)
    {
      /* If the reference count stayed the same we're done, otherwise retry */
      if (!g_atomic_int_compare_and_exchange_full (&impl->ref_count, old_ref_count, old_ref_count - 1, &old_ref_count))
        goto retry;

      return;
    }

  /* This is the last reference *currently* and would potentially free the string.
   * To avoid races between freeing it and returning it from g_ref_string_new_intern()
   * we must take the lock here before decrementing the reference count!
   */
  G_LOCK (interned_ref_strings);
  /* If the string was not given out again in the meantime we're done */
  if (g_atomic_int_dec_and_test (&impl->ref_count))
    {
      gboolean removed G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;

      removed = g_hash_table_remove (interned_ref_strings, str);
      g_assert (removed);

      if (g_hash_table_size (interned_ref_strings) == 0)
        g_clear_pointer (&interned_ref_strings, g_hash_table_destroy);

      g_free (impl);
    }
  G_UNLOCK (interned_ref_strings);
}

/**
 * g_ref_string_length:
 * @str: a reference counted string
 *
 * Retrieves the length of @str.
 *
 * Returns: the length of the given string, in bytes
 *
 * Since: 2.58
 */
gsize
g_ref_string_length (char *str)
{
  g_return_val_if_fail (str != NULL, 0);

  return G_REF_STRING_IMPL_FROM_STR (str)->len;
}

/**
 * g_ref_string_equal:
 * @str1: a reference counted string
 * @str2: a reference counted string
 *
 * Compares two ref-counted strings for byte-by-byte equality.
 *
 * It can be passed to [func@GLib.HashTable.new] as the key equality function,
 * and behaves exactly the same as [func@GLib.str_equal] (or `strcmp()`), but
 * can return slightly faster as it can check the string lengths before checking
 * all the bytes.
 *
 * Returns: `TRUE` if the strings are equal, otherwise `FALSE`
 *
 * Since: 2.84
 */
gboolean
g_ref_string_equal (const char *str1,
                    const char *str2)
{
  if (G_REF_STRING_IMPL_FROM_STR ((char *) str1)->len != G_REF_STRING_IMPL_FROM_STR ((char *) str2)->len)
    return FALSE;

  return strcmp (str1, str2) == 0;
}