Coverage Report

Created: 2026-05-23 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/irssi/subprojects/glib-2.74.7/glib/ghash.c
Line
Count
Source
1
/* GLIB - Library of useful routines for C programming
2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/*
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22
 * file for a list of people on the GLib Team.  See the ChangeLog
23
 * files for a list of changes.  These files are distributed with
24
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25
 */
26
27
/*
28
 * MT safe
29
 */
30
31
#include "config.h"
32
33
#include <string.h>  /* memset */
34
35
#include "ghash.h"
36
#include "gmacros.h"
37
#include "glib-private.h"
38
#include "gstrfuncs.h"
39
#include "gatomic.h"
40
#include "gtestutils.h"
41
#include "gslice.h"
42
#include "grefcount.h"
43
#include "gvalgrind.h"
44
45
/* The following #pragma is here so we can do this...
46
 *
47
 *   #ifndef USE_SMALL_ARRAYS
48
 *     is_big = TRUE;
49
 *   #endif
50
 *     return is_big ? *(((gpointer *) a) + index) : GUINT_TO_POINTER (*(((guint *) a) + index));
51
 *
52
 * ...instead of this...
53
 *
54
 *   #ifndef USE_SMALL_ARRAYS
55
 *     return *(((gpointer *) a) + index);
56
 *   #else
57
 *     return is_big ? *(((gpointer *) a) + index) : GUINT_TO_POINTER (*(((guint *) a) + index));
58
 *   #endif
59
 *
60
 * ...and still compile successfully when -Werror=duplicated-branches is passed. */
61
62
#if defined(__GNUC__) && __GNUC__ > 6
63
#pragma GCC diagnostic ignored "-Wduplicated-branches"
64
#endif
65
66
/**
67
 * SECTION:hash_tables
68
 * @title: Hash Tables
69
 * @short_description: associations between keys and values so that
70
 *     given a key the value can be found quickly
71
 *
72
 * A #GHashTable provides associations between keys and values which is
73
 * optimized so that given a key, the associated value can be found,
74
 * inserted or removed in amortized O(1). All operations going through
75
 * each element take O(n) time (list all keys/values, table resize, etc.).
76
 *
77
 * Note that neither keys nor values are copied when inserted into the
78
 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
79
 * This means that the use of static strings is OK, but temporary
80
 * strings (i.e. those created in buffers and those returned by GTK
81
 * widgets) should be copied with g_strdup() before being inserted.
82
 *
83
 * If keys or values are dynamically allocated, you must be careful to
84
 * ensure that they are freed when they are removed from the
85
 * #GHashTable, and also when they are overwritten by new insertions
86
 * into the #GHashTable. It is also not advisable to mix static strings
87
 * and dynamically-allocated strings in a #GHashTable, because it then
88
 * becomes difficult to determine whether the string should be freed.
89
 *
90
 * To create a #GHashTable, use g_hash_table_new().
91
 *
92
 * To insert a key and value into a #GHashTable, use
93
 * g_hash_table_insert().
94
 *
95
 * To look up a value corresponding to a given key, use
96
 * g_hash_table_lookup() and g_hash_table_lookup_extended().
97
 *
98
 * g_hash_table_lookup_extended() can also be used to simply
99
 * check if a key is present in the hash table.
100
 *
101
 * To remove a key and value, use g_hash_table_remove().
102
 *
103
 * To call a function for each key and value pair use
104
 * g_hash_table_foreach() or use an iterator to iterate over the
105
 * key/value pairs in the hash table, see #GHashTableIter. The iteration order
106
 * of a hash table is not defined, and you must not rely on iterating over
107
 * keys/values in the same order as they were inserted.
108
 *
109
 * To destroy a #GHashTable use g_hash_table_destroy().
110
 *
111
 * A common use-case for hash tables is to store information about a
112
 * set of keys, without associating any particular value with each
113
 * key. GHashTable optimizes one way of doing so: If you store only
114
 * key-value pairs where key == value, then GHashTable does not
115
 * allocate memory to store the values, which can be a considerable
116
 * space saving, if your set is large. The functions
117
 * g_hash_table_add() and g_hash_table_contains() are designed to be
118
 * used when using #GHashTable this way.
119
 *
120
 * #GHashTable is not designed to be statically initialised with keys and
121
 * values known at compile time. To build a static hash table, use a tool such
122
 * as [gperf](https://www.gnu.org/software/gperf/).
123
 */
124
125
/**
126
 * GHashTable:
127
 *
128
 * The #GHashTable struct is an opaque data structure to represent a
129
 * [Hash Table][glib-Hash-Tables]. It should only be accessed via the
130
 * following functions.
131
 */
132
133
/**
134
 * GHashFunc:
135
 * @key: a key
136
 *
137
 * Specifies the type of the hash function which is passed to
138
 * g_hash_table_new() when a #GHashTable is created.
139
 *
140
 * The function is passed a key and should return a #guint hash value.
141
 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
142
 * hash functions which can be used when the key is a #gpointer, #gint*,
143
 * and #gchar* respectively.
144
 *
145
 * g_direct_hash() is also the appropriate hash function for keys
146
 * of the form `GINT_TO_POINTER (n)` (or similar macros).
147
 *
148
 * A good hash functions should produce
149
 * hash values that are evenly distributed over a fairly large range.
150
 * The modulus is taken with the hash table size (a prime number) to
151
 * find the 'bucket' to place each key into. The function should also
152
 * be very fast, since it is called for each key lookup.
153
 *
154
 * Note that the hash functions provided by GLib have these qualities,
155
 * but are not particularly robust against manufactured keys that
156
 * cause hash collisions. Therefore, you should consider choosing
157
 * a more secure hash function when using a GHashTable with keys
158
 * that originate in untrusted data (such as HTTP requests).
159
 * Using g_str_hash() in that situation might make your application
160
 * vulnerable to
161
 * [Algorithmic Complexity Attacks](https://lwn.net/Articles/474912/).
162
 *
163
 * The key to choosing a good hash is unpredictability.  Even
164
 * cryptographic hashes are very easy to find collisions for when the
165
 * remainder is taken modulo a somewhat predictable prime number.  There
166
 * must be an element of randomness that an attacker is unable to guess.
167
 *
168
 * Returns: the hash value corresponding to the key
169
 */
170
171
/**
172
 * GHFunc:
173
 * @key: a key
174
 * @value: the value corresponding to the key
175
 * @user_data: user data passed to g_hash_table_foreach()
176
 *
177
 * Specifies the type of the function passed to g_hash_table_foreach().
178
 * It is called with each key/value pair, together with the @user_data
179
 * parameter which is passed to g_hash_table_foreach().
180
 */
181
182
/**
183
 * GHRFunc:
184
 * @key: a key
185
 * @value: the value associated with the key
186
 * @user_data: user data passed to g_hash_table_remove()
187
 *
188
 * Specifies the type of the function passed to
189
 * g_hash_table_foreach_remove(). It is called with each key/value
190
 * pair, together with the @user_data parameter passed to
191
 * g_hash_table_foreach_remove(). It should return %TRUE if the
192
 * key/value pair should be removed from the #GHashTable.
193
 *
194
 * Returns: %TRUE if the key/value pair should be removed from the
195
 *     #GHashTable
196
 */
197
198
/**
199
 * GEqualFunc:
200
 * @a: a value
201
 * @b: a value to compare with
202
 *
203
 * Specifies the type of a function used to test two values for
204
 * equality. The function should return %TRUE if both values are equal
205
 * and %FALSE otherwise.
206
 *
207
 * Returns: %TRUE if @a = @b; %FALSE otherwise
208
 */
209
210
/**
211
 * GHashTableIter:
212
 *
213
 * A GHashTableIter structure represents an iterator that can be used
214
 * to iterate over the elements of a #GHashTable. GHashTableIter
215
 * structures are typically allocated on the stack and then initialized
216
 * with g_hash_table_iter_init().
217
 *
218
 * The iteration order of a #GHashTableIter over the keys/values in a hash
219
 * table is not defined.
220
 */
221
222
/**
223
 * g_hash_table_freeze:
224
 * @hash_table: a #GHashTable
225
 *
226
 * This function is deprecated and will be removed in the next major
227
 * release of GLib. It does nothing.
228
 */
229
230
/**
231
 * g_hash_table_thaw:
232
 * @hash_table: a #GHashTable
233
 *
234
 * This function is deprecated and will be removed in the next major
235
 * release of GLib. It does nothing.
236
 */
237
238
2.38M
#define HASH_TABLE_MIN_SHIFT 3  /* 1 << 3 == 8 buckets */
239
240
2.04G
#define UNUSED_HASH_VALUE 0
241
208M
#define TOMBSTONE_HASH_VALUE 1
242
2.04G
#define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
243
415M
#define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
244
1.83G
#define HASH_IS_REAL(h_) ((h_) >= 2)
245
246
/* If int is smaller than void * on our arch, we start out with
247
 * int-sized keys and values and resize to pointer-sized entries as
248
 * needed. This saves a good amount of memory when the HT is being
249
 * used with e.g. GUINT_TO_POINTER(). */
250
251
756k
#define BIG_ENTRY_SIZE (SIZEOF_VOID_P)
252
1.09M
#define SMALL_ENTRY_SIZE (SIZEOF_INT)
253
254
#if SMALL_ENTRY_SIZE < BIG_ENTRY_SIZE
255
# define USE_SMALL_ARRAYS
256
#endif
257
258
struct _GHashTable
259
{
260
  gsize            size;
261
  gint             mod;
262
  guint            mask;
263
  gint             nnodes;
264
  gint             noccupied;  /* nnodes + tombstones */
265
266
  guint            have_big_keys : 1;
267
  guint            have_big_values : 1;
268
269
  gpointer         keys;
270
  guint           *hashes;
271
  gpointer         values;
272
273
  GHashFunc        hash_func;
274
  GEqualFunc       key_equal_func;
275
  gatomicrefcount  ref_count;
276
#ifndef G_DISABLE_ASSERT
277
  /*
278
   * Tracks the structure of the hash table, not its contents: is only
279
   * incremented when a node is added or removed (is not incremented
280
   * when the key or data of a node is modified).
281
   */
282
  int              version;
283
#endif
284
  GDestroyNotify   key_destroy_func;
285
  GDestroyNotify   value_destroy_func;
286
};
287
288
typedef struct
289
{
290
  GHashTable  *hash_table;
291
  gpointer     dummy1;
292
  gpointer     dummy2;
293
  gint         position;
294
  gboolean     dummy3;
295
  gint         version;
296
} RealIter;
297
298
G_STATIC_ASSERT (sizeof (GHashTableIter) == sizeof (RealIter));
299
G_STATIC_ASSERT (G_ALIGNOF (GHashTableIter) >= G_ALIGNOF (RealIter));
300
301
/* Each table size has an associated prime modulo (the first prime
302
 * lower than the table size) used to find the initial bucket. Probing
303
 * then works modulo 2^n. The prime modulo is necessary to get a
304
 * good distribution with poor hash functions.
305
 */
306
static const gint prime_mod [] =
307
{
308
  1,          /* For 1 << 0 */
309
  2,
310
  3,
311
  7,
312
  13,
313
  31,
314
  61,
315
  127,
316
  251,
317
  509,
318
  1021,
319
  2039,
320
  4093,
321
  8191,
322
  16381,
323
  32749,
324
  65521,      /* For 1 << 16 */
325
  131071,
326
  262139,
327
  524287,
328
  1048573,
329
  2097143,
330
  4194301,
331
  8388593,
332
  16777213,
333
  33554393,
334
  67108859,
335
  134217689,
336
  268435399,
337
  536870909,
338
  1073741789,
339
  2147483647  /* For 1 << 31 */
340
};
341
342
static void
343
g_hash_table_set_shift (GHashTable *hash_table, gint shift)
344
532k
{
345
532k
  hash_table->size = 1 << shift;
346
532k
  hash_table->mod  = prime_mod [shift];
347
348
  /* hash_table->size is always a power of two, so we can calculate the mask
349
   * by simply subtracting 1 from it. The leading assertion ensures that
350
   * we're really dealing with a power of two. */
351
352
532k
  g_assert ((hash_table->size & (hash_table->size - 1)) == 0);
353
532k
  hash_table->mask = hash_table->size - 1;
354
532k
}
355
356
static gint
357
g_hash_table_find_closest_shift (gint n)
358
28.9k
{
359
28.9k
  gint i;
360
361
148k
  for (i = 0; n; i++)
362
119k
    n >>= 1;
363
364
28.9k
  return i;
365
28.9k
}
366
367
static void
368
g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
369
28.9k
{
370
28.9k
  gint shift;
371
372
28.9k
  shift = g_hash_table_find_closest_shift (size);
373
28.9k
  shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
374
375
28.9k
  g_hash_table_set_shift (hash_table, shift);
376
28.9k
}
377
378
static inline gpointer
379
g_hash_table_realloc_key_or_value_array (gpointer a, guint size, G_GNUC_UNUSED gboolean is_big)
380
560k
{
381
560k
#ifdef USE_SMALL_ARRAYS
382
560k
  return g_realloc (a, size * (is_big ? BIG_ENTRY_SIZE : SMALL_ENTRY_SIZE));
383
#else
384
  return g_renew (gpointer, a, size);
385
#endif
386
560k
}
387
388
static inline gpointer
389
g_hash_table_fetch_key_or_value (gpointer a, guint index, gboolean is_big)
390
3.71G
{
391
#ifndef USE_SMALL_ARRAYS
392
  is_big = TRUE;
393
#endif
394
3.71G
  return is_big ? *(((gpointer *) a) + index) : GUINT_TO_POINTER (*(((guint *) a) + index));
395
3.71G
}
396
397
static inline void
398
g_hash_table_assign_key_or_value (gpointer a, guint index, gboolean is_big, gpointer v)
399
3.42M
{
400
#ifndef USE_SMALL_ARRAYS
401
  is_big = TRUE;
402
#endif
403
3.42M
  if (is_big)
404
3.09M
    *(((gpointer *) a) + index) = v;
405
336k
  else
406
336k
    *(((guint *) a) + index) = GPOINTER_TO_UINT (v);
407
3.42M
}
408
409
static inline gpointer
410
g_hash_table_evict_key_or_value (gpointer a, guint index, gboolean is_big, gpointer v)
411
1.34M
{
412
#ifndef USE_SMALL_ARRAYS
413
  is_big = TRUE;
414
#endif
415
1.34M
  if (is_big)
416
1.31M
    {
417
1.31M
      gpointer r = *(((gpointer *) a) + index);
418
1.31M
      *(((gpointer *) a) + index) = v;
419
1.31M
      return r;
420
1.31M
    }
421
34.7k
  else
422
34.7k
    {
423
34.7k
      gpointer r = GUINT_TO_POINTER (*(((guint *) a) + index));
424
34.7k
      *(((guint *) a) + index) = GPOINTER_TO_UINT (v);
425
34.7k
      return r;
426
34.7k
    }
427
1.34M
}
428
429
static inline guint
430
g_hash_table_hash_to_index (GHashTable *hash_table, guint hash)
431
1.83G
{
432
  /* Multiply the hash by a small prime before applying the modulo. This
433
   * prevents the table from becoming densely packed, even with a poor hash
434
   * function. A densely packed table would have poor performance on
435
   * workloads with many failed lookups or a high degree of churn. */
436
1.83G
  return (hash * 11) % hash_table->mod;
437
1.83G
}
438
439
/*
440
 * g_hash_table_lookup_node:
441
 * @hash_table: our #GHashTable
442
 * @key: the key to look up against
443
 * @hash_return: key hash return location
444
 *
445
 * Performs a lookup in the hash table, preserving extra information
446
 * usually needed for insertion.
447
 *
448
 * This function first computes the hash value of the key using the
449
 * user's hash function.
450
 *
451
 * If an entry in the table matching @key is found then this function
452
 * returns the index of that entry in the table, and if not, the
453
 * index of an unused node (empty or tombstone) where the key can be
454
 * inserted.
455
 *
456
 * The computed hash value is returned in the variable pointed to
457
 * by @hash_return. This is to save insertions from having to compute
458
 * the hash record again for the new record.
459
 *
460
 * Returns: index of the described node
461
 */
462
static inline guint
463
g_hash_table_lookup_node (GHashTable    *hash_table,
464
                          gconstpointer  key,
465
                          guint         *hash_return)
466
1.83G
{
467
1.83G
  guint node_index;
468
1.83G
  guint node_hash;
469
1.83G
  guint hash_value;
470
1.83G
  guint first_tombstone = 0;
471
1.83G
  gboolean have_tombstone = FALSE;
472
1.83G
  guint step = 0;
473
474
1.83G
  hash_value = hash_table->hash_func (key);
475
1.83G
  if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
476
192M
    hash_value = 2;
477
478
1.83G
  *hash_return = hash_value;
479
480
1.83G
  node_index = g_hash_table_hash_to_index (hash_table, hash_value);
481
1.83G
  node_hash = hash_table->hashes[node_index];
482
483
2.04G
  while (!HASH_IS_UNUSED (node_hash))
484
1.65G
    {
485
      /* We first check if our full hash values
486
       * are equal so we can avoid calling the full-blown
487
       * key equality function in most cases.
488
       */
489
1.65G
      if (node_hash == hash_value)
490
1.44G
        {
491
1.44G
          gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
492
493
1.44G
          if (hash_table->key_equal_func)
494
1.42G
            {
495
1.42G
              if (hash_table->key_equal_func (node_key, key))
496
1.42G
                return node_index;
497
1.42G
            }
498
22.1M
          else if (node_key == key)
499
22.1M
            {
500
22.1M
              return node_index;
501
22.1M
            }
502
1.44G
        }
503
207M
      else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
504
1.47M
        {
505
1.47M
          first_tombstone = node_index;
506
1.47M
          have_tombstone = TRUE;
507
1.47M
        }
508
509
207M
      step++;
510
207M
      node_index += step;
511
207M
      node_index &= hash_table->mask;
512
207M
      node_hash = hash_table->hashes[node_index];
513
207M
    }
514
515
388M
  if (have_tombstone)
516
1.47M
    return first_tombstone;
517
518
386M
  return node_index;
519
388M
}
520
521
/*
522
 * g_hash_table_remove_node:
523
 * @hash_table: our #GHashTable
524
 * @node: pointer to node to remove
525
 * @notify: %TRUE if the destroy notify handlers are to be called
526
 *
527
 * Removes a node from the hash table and updates the node count.
528
 * The node is replaced by a tombstone. No table resize is performed.
529
 *
530
 * If @notify is %TRUE then the destroy notify functions are called
531
 * for the key and value of the hash node.
532
 */
533
static void
534
g_hash_table_remove_node (GHashTable   *hash_table,
535
                          gint          i,
536
                          gboolean      notify)
537
333k
{
538
333k
  gpointer key;
539
333k
  gpointer value;
540
541
333k
  key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
542
333k
  value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
543
544
  /* Erect tombstone */
545
333k
  hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
546
547
  /* Be GC friendly */
548
333k
  g_hash_table_assign_key_or_value (hash_table->keys, i, hash_table->have_big_keys, NULL);
549
333k
  g_hash_table_assign_key_or_value (hash_table->values, i, hash_table->have_big_values, NULL);
550
551
333k
  hash_table->nnodes--;
552
553
333k
  if (notify && hash_table->key_destroy_func)
554
5
    hash_table->key_destroy_func (key);
555
556
333k
  if (notify && hash_table->value_destroy_func)
557
5
    hash_table->value_destroy_func (value);
558
559
333k
}
560
561
/*
562
 * g_hash_table_setup_storage:
563
 * @hash_table: our #GHashTable
564
 *
565
 * Initialise the hash table size, mask, mod, and arrays.
566
 */
567
static void
568
g_hash_table_setup_storage (GHashTable *hash_table)
569
503k
{
570
503k
  gboolean small = FALSE;
571
572
  /* We want to use small arrays only if:
573
   *   - we are running on a system where that makes sense (64 bit); and
574
   *   - we are not running under valgrind.
575
   */
576
577
503k
#ifdef USE_SMALL_ARRAYS
578
503k
  small = TRUE;
579
580
503k
# ifdef ENABLE_VALGRIND
581
503k
  if (RUNNING_ON_VALGRIND)
582
0
    small = FALSE;
583
503k
# endif
584
503k
#endif
585
586
503k
  g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
587
588
503k
  hash_table->have_big_keys = !small;
589
503k
  hash_table->have_big_values = !small;
590
591
503k
  hash_table->keys   = g_hash_table_realloc_key_or_value_array (NULL, hash_table->size, hash_table->have_big_keys);
592
503k
  hash_table->values = hash_table->keys;
593
503k
  hash_table->hashes = g_new0 (guint, hash_table->size);
594
503k
}
595
596
/*
597
 * g_hash_table_remove_all_nodes:
598
 * @hash_table: our #GHashTable
599
 * @notify: %TRUE if the destroy notify handlers are to be called
600
 *
601
 * Removes all nodes from the table.
602
 *
603
 * If @notify is %TRUE then the destroy notify functions are called
604
 * for the key and value of the hash node.
605
 *
606
 * Since this may be a precursor to freeing the table entirely, we'd
607
 * ideally perform no resize, and we can indeed avoid that in some
608
 * cases.  However: in the case that we'll be making callbacks to user
609
 * code (via destroy notifies) we need to consider that the user code
610
 * might call back into the table again.  In this case, we setup a new
611
 * set of arrays so that any callers will see an empty (but valid)
612
 * table.
613
 */
614
static void
615
g_hash_table_remove_all_nodes (GHashTable *hash_table,
616
                               gboolean    notify,
617
                               gboolean    destruction)
618
1.93M
{
619
1.93M
  int i;
620
1.93M
  gpointer key;
621
1.93M
  gpointer value;
622
1.93M
  gint old_size;
623
1.93M
  gpointer *old_keys;
624
1.93M
  gpointer *old_values;
625
1.93M
  guint    *old_hashes;
626
1.93M
  gboolean  old_have_big_keys;
627
1.93M
  gboolean  old_have_big_values;
628
629
  /* If the hash table is already empty, there is nothing to be done. */
630
1.93M
  if (hash_table->nnodes == 0)
631
1.87M
    return;
632
633
57.6k
  hash_table->nnodes = 0;
634
57.6k
  hash_table->noccupied = 0;
635
636
  /* Easy case: no callbacks, so we just zero out the arrays */
637
57.6k
  if (!notify ||
638
57.6k
      (hash_table->key_destroy_func == NULL &&
639
54.1k
       hash_table->value_destroy_func == NULL))
640
54.1k
    {
641
54.1k
      if (!destruction)
642
54.1k
        {
643
54.1k
          memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
644
645
54.1k
#ifdef USE_SMALL_ARRAYS
646
54.1k
          memset (hash_table->keys, 0, hash_table->size * (hash_table->have_big_keys ? BIG_ENTRY_SIZE : SMALL_ENTRY_SIZE));
647
54.1k
          memset (hash_table->values, 0, hash_table->size * (hash_table->have_big_values ? BIG_ENTRY_SIZE : SMALL_ENTRY_SIZE));
648
#else
649
          memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
650
          memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
651
#endif
652
54.1k
        }
653
654
54.1k
      return;
655
54.1k
    }
656
657
  /* Hard case: we need to do user callbacks.  There are two
658
   * possibilities here:
659
   *
660
   *   1) there are no outstanding references on the table and therefore
661
   *   nobody should be calling into it again (destroying == true)
662
   *
663
   *   2) there are outstanding references, and there may be future
664
   *   calls into the table, either after we return, or from the destroy
665
   *   notifies that we're about to do (destroying == false)
666
   *
667
   * We handle both cases by taking the current state of the table into
668
   * local variables and replacing it with something else: in the "no
669
   * outstanding references" cases we replace it with a bunch of
670
   * null/zero values so that any access to the table will fail.  In the
671
   * "may receive future calls" case, we reinitialise the struct to
672
   * appear like a newly-created empty table.
673
   *
674
   * In both cases, we take over the references for the current state,
675
   * freeing them below.
676
   */
677
3.51k
  old_size = hash_table->size;
678
3.51k
  old_have_big_keys = hash_table->have_big_keys;
679
3.51k
  old_have_big_values = hash_table->have_big_values;
680
3.51k
  old_keys   = g_steal_pointer (&hash_table->keys);
681
3.51k
  old_values = g_steal_pointer (&hash_table->values);
682
3.51k
  old_hashes = g_steal_pointer (&hash_table->hashes);
683
684
3.51k
  if (!destruction)
685
    /* Any accesses will see an empty table */
686
3.51k
    g_hash_table_setup_storage (hash_table);
687
0
  else
688
    /* Will cause a quick crash on any attempted access */
689
0
    hash_table->size = hash_table->mod = hash_table->mask = 0;
690
691
  /* Now do the actual destroy notifies */
692
36.1k
  for (i = 0; i < old_size; i++)
693
32.6k
    {
694
32.6k
      if (HASH_IS_REAL (old_hashes[i]))
695
7.81k
        {
696
7.81k
          key = g_hash_table_fetch_key_or_value (old_keys, i, old_have_big_keys);
697
7.81k
          value = g_hash_table_fetch_key_or_value (old_values, i, old_have_big_values);
698
699
7.81k
          old_hashes[i] = UNUSED_HASH_VALUE;
700
701
7.81k
          g_hash_table_assign_key_or_value (old_keys, i, old_have_big_keys, NULL);
702
7.81k
          g_hash_table_assign_key_or_value (old_values, i, old_have_big_values, NULL);
703
704
7.81k
          if (hash_table->key_destroy_func != NULL)
705
7.81k
            hash_table->key_destroy_func (key);
706
707
7.81k
          if (hash_table->value_destroy_func != NULL)
708
7.81k
            hash_table->value_destroy_func (value);
709
7.81k
        }
710
32.6k
    }
711
712
  /* Destroy old storage space. */
713
3.51k
  if (old_keys != old_values)
714
3.51k
    g_free (old_values);
715
716
3.51k
  g_free (old_keys);
717
3.51k
  g_free (old_hashes);
718
3.51k
}
719
720
static void
721
realloc_arrays (GHashTable *hash_table, gboolean is_a_set)
722
28.8k
{
723
28.8k
  hash_table->hashes = g_renew (guint, hash_table->hashes, hash_table->size);
724
28.8k
  hash_table->keys = g_hash_table_realloc_key_or_value_array (hash_table->keys, hash_table->size, hash_table->have_big_keys);
725
726
28.8k
  if (is_a_set)
727
194
    hash_table->values = hash_table->keys;
728
28.6k
  else
729
28.6k
    hash_table->values = g_hash_table_realloc_key_or_value_array (hash_table->values, hash_table->size, hash_table->have_big_values);
730
28.8k
}
731
732
/* When resizing the table in place, we use a temporary bit array to keep
733
 * track of which entries have been assigned a proper location in the new
734
 * table layout.
735
 *
736
 * Each bit corresponds to a bucket. A bit is set if an entry was assigned
737
 * its corresponding location during the resize and thus should not be
738
 * evicted. The array starts out cleared to zero. */
739
740
static inline gboolean
741
get_status_bit (const guint32 *bitmap, guint index)
742
1.70M
{
743
1.70M
  return (bitmap[index / 32] >> (index % 32)) & 1;
744
1.70M
}
745
746
static inline void
747
set_status_bit (guint32 *bitmap, guint index)
748
674k
{
749
674k
  bitmap[index / 32] |= 1U << (index % 32);
750
674k
}
751
752
/* By calling dedicated resize functions for sets and maps, we avoid 2x
753
 * test-and-branch per key in the inner loop. This yields a small
754
 * performance improvement at the cost of a bit of macro gunk. */
755
756
#define DEFINE_RESIZE_FUNC(fname) \
757
28.9k
static void fname (GHashTable *hash_table, guint old_size, guint32 *reallocated_buckets_bitmap) \
758
28.9k
{                                                                       \
759
28.9k
  guint i;                                                              \
760
28.9k
                                                                        \
761
1.44M
  for (i = 0; i < old_size; i++)                                        \
762
1.41M
    {                                                                   \
763
1.41M
      guint node_hash = hash_table->hashes[i];                          \
764
1.41M
      gpointer key, value G_GNUC_UNUSED;                                \
765
1.41M
                                                                        \
766
1.41M
      if (!HASH_IS_REAL (node_hash))                                    \
767
1.41M
        {                                                               \
768
726k
          /* Clear tombstones */                                        \
769
726k
          hash_table->hashes[i] = UNUSED_HASH_VALUE;                    \
770
726k
          continue;                                                     \
771
726k
        }                                                               \
772
1.41M
                                                                        \
773
1.41M
      /* Skip entries relocated through eviction */                     \
774
1.41M
      if (get_status_bit (reallocated_buckets_bitmap, i))               \
775
685k
        continue;                                                       \
776
685k
                                                                        \
777
685k
      hash_table->hashes[i] = UNUSED_HASH_VALUE;                        \
778
449k
      EVICT_KEYVAL (hash_table, i, NULL, NULL, key, value);             \
779
449k
                                                                        \
780
449k
      for (;;)                                                          \
781
674k
        {                                                               \
782
674k
          guint hash_val;                                               \
783
674k
          guint replaced_hash;                                          \
784
674k
          guint step = 0;                                               \
785
674k
                                                                        \
786
674k
          hash_val = g_hash_table_hash_to_index (hash_table, node_hash); \
787
674k
                                                                        \
788
1.01M
          while (get_status_bit (reallocated_buckets_bitmap, hash_val)) \
789
674k
            {                                                           \
790
344k
              step++;                                                   \
791
344k
              hash_val += step;                                         \
792
344k
              hash_val &= hash_table->mask;                             \
793
344k
            }                                                           \
794
674k
                                                                        \
795
674k
          set_status_bit (reallocated_buckets_bitmap, hash_val);        \
796
674k
                                                                        \
797
674k
          replaced_hash = hash_table->hashes[hash_val];                 \
798
674k
          hash_table->hashes[hash_val] = node_hash;                     \
799
674k
          if (!HASH_IS_REAL (replaced_hash))                            \
800
674k
            {                                                           \
801
449k
              ASSIGN_KEYVAL (hash_table, hash_val, key, value);         \
802
449k
              break;                                                    \
803
449k
            }                                                           \
804
674k
                                                                        \
805
674k
          node_hash = replaced_hash;                                    \
806
225k
          EVICT_KEYVAL (hash_table, hash_val, key, value, key, value);  \
807
225k
        }                                                               \
808
449k
    }                                                                   \
809
28.9k
}
ghash.c:resize_set
Line
Count
Source
757
194
static void fname (GHashTable *hash_table, guint old_size, guint32 *reallocated_buckets_bitmap) \
758
194
{                                                                       \
759
194
  guint i;                                                              \
760
194
                                                                        \
761
2.54k
  for (i = 0; i < old_size; i++)                                        \
762
2.35k
    {                                                                   \
763
2.35k
      guint node_hash = hash_table->hashes[i];                          \
764
2.35k
      gpointer key, value G_GNUC_UNUSED;                                \
765
2.35k
                                                                        \
766
2.35k
      if (!HASH_IS_REAL (node_hash))                                    \
767
2.35k
        {                                                               \
768
1.27k
          /* Clear tombstones */                                        \
769
1.27k
          hash_table->hashes[i] = UNUSED_HASH_VALUE;                    \
770
1.27k
          continue;                                                     \
771
1.27k
        }                                                               \
772
2.35k
                                                                        \
773
2.35k
      /* Skip entries relocated through eviction */                     \
774
2.35k
      if (get_status_bit (reallocated_buckets_bitmap, i))               \
775
1.07k
        continue;                                                       \
776
1.07k
                                                                        \
777
1.07k
      hash_table->hashes[i] = UNUSED_HASH_VALUE;                        \
778
708
      EVICT_KEYVAL (hash_table, i, NULL, NULL, key, value);             \
779
708
                                                                        \
780
708
      for (;;)                                                          \
781
1.07k
        {                                                               \
782
1.07k
          guint hash_val;                                               \
783
1.07k
          guint replaced_hash;                                          \
784
1.07k
          guint step = 0;                                               \
785
1.07k
                                                                        \
786
1.07k
          hash_val = g_hash_table_hash_to_index (hash_table, node_hash); \
787
1.07k
                                                                        \
788
1.61k
          while (get_status_bit (reallocated_buckets_bitmap, hash_val)) \
789
1.07k
            {                                                           \
790
531
              step++;                                                   \
791
531
              hash_val += step;                                         \
792
531
              hash_val &= hash_table->mask;                             \
793
531
            }                                                           \
794
1.07k
                                                                        \
795
1.07k
          set_status_bit (reallocated_buckets_bitmap, hash_val);        \
796
1.07k
                                                                        \
797
1.07k
          replaced_hash = hash_table->hashes[hash_val];                 \
798
1.07k
          hash_table->hashes[hash_val] = node_hash;                     \
799
1.07k
          if (!HASH_IS_REAL (replaced_hash))                            \
800
1.07k
            {                                                           \
801
708
              ASSIGN_KEYVAL (hash_table, hash_val, key, value);         \
802
708
              break;                                                    \
803
708
            }                                                           \
804
1.07k
                                                                        \
805
1.07k
          node_hash = replaced_hash;                                    \
806
371
          EVICT_KEYVAL (hash_table, hash_val, key, value, key, value);  \
807
371
        }                                                               \
808
708
    }                                                                   \
809
194
}
ghash.c:resize_map
Line
Count
Source
757
28.7k
static void fname (GHashTable *hash_table, guint old_size, guint32 *reallocated_buckets_bitmap) \
758
28.7k
{                                                                       \
759
28.7k
  guint i;                                                              \
760
28.7k
                                                                        \
761
1.43M
  for (i = 0; i < old_size; i++)                                        \
762
1.40M
    {                                                                   \
763
1.40M
      guint node_hash = hash_table->hashes[i];                          \
764
1.40M
      gpointer key, value G_GNUC_UNUSED;                                \
765
1.40M
                                                                        \
766
1.40M
      if (!HASH_IS_REAL (node_hash))                                    \
767
1.40M
        {                                                               \
768
724k
          /* Clear tombstones */                                        \
769
724k
          hash_table->hashes[i] = UNUSED_HASH_VALUE;                    \
770
724k
          continue;                                                     \
771
724k
        }                                                               \
772
1.40M
                                                                        \
773
1.40M
      /* Skip entries relocated through eviction */                     \
774
1.40M
      if (get_status_bit (reallocated_buckets_bitmap, i))               \
775
684k
        continue;                                                       \
776
684k
                                                                        \
777
684k
      hash_table->hashes[i] = UNUSED_HASH_VALUE;                        \
778
448k
      EVICT_KEYVAL (hash_table, i, NULL, NULL, key, value);             \
779
448k
                                                                        \
780
448k
      for (;;)                                                          \
781
673k
        {                                                               \
782
673k
          guint hash_val;                                               \
783
673k
          guint replaced_hash;                                          \
784
673k
          guint step = 0;                                               \
785
673k
                                                                        \
786
673k
          hash_val = g_hash_table_hash_to_index (hash_table, node_hash); \
787
673k
                                                                        \
788
1.01M
          while (get_status_bit (reallocated_buckets_bitmap, hash_val)) \
789
673k
            {                                                           \
790
344k
              step++;                                                   \
791
344k
              hash_val += step;                                         \
792
344k
              hash_val &= hash_table->mask;                             \
793
344k
            }                                                           \
794
673k
                                                                        \
795
673k
          set_status_bit (reallocated_buckets_bitmap, hash_val);        \
796
673k
                                                                        \
797
673k
          replaced_hash = hash_table->hashes[hash_val];                 \
798
673k
          hash_table->hashes[hash_val] = node_hash;                     \
799
673k
          if (!HASH_IS_REAL (replaced_hash))                            \
800
673k
            {                                                           \
801
448k
              ASSIGN_KEYVAL (hash_table, hash_val, key, value);         \
802
448k
              break;                                                    \
803
448k
            }                                                           \
804
673k
                                                                        \
805
673k
          node_hash = replaced_hash;                                    \
806
225k
          EVICT_KEYVAL (hash_table, hash_val, key, value, key, value);  \
807
225k
        }                                                               \
808
448k
    }                                                                   \
809
28.7k
}
810
811
448k
#define ASSIGN_KEYVAL(ht, index, key, value) G_STMT_START{ \
812
448k
    g_hash_table_assign_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
813
448k
    g_hash_table_assign_key_or_value ((ht)->values, (index), (ht)->have_big_values, (value)); \
814
448k
  }G_STMT_END
815
816
673k
#define EVICT_KEYVAL(ht, index, key, value, outkey, outvalue) G_STMT_START{ \
817
673k
    (outkey) = g_hash_table_evict_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
818
673k
    (outvalue) = g_hash_table_evict_key_or_value ((ht)->values, (index), (ht)->have_big_values, (value)); \
819
673k
  }G_STMT_END
820
821
DEFINE_RESIZE_FUNC (resize_map)
822
823
#undef ASSIGN_KEYVAL
824
#undef EVICT_KEYVAL
825
826
708
#define ASSIGN_KEYVAL(ht, index, key, value) G_STMT_START{ \
827
708
    g_hash_table_assign_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
828
708
  }G_STMT_END
829
830
1.07k
#define EVICT_KEYVAL(ht, index, key, value, outkey, outvalue) G_STMT_START{ \
831
1.07k
    (outkey) = g_hash_table_evict_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
832
1.07k
  }G_STMT_END
833
834
DEFINE_RESIZE_FUNC (resize_set)
835
836
#undef ASSIGN_KEYVAL
837
#undef EVICT_KEYVAL
838
839
/*
840
 * g_hash_table_resize:
841
 * @hash_table: our #GHashTable
842
 *
843
 * Resizes the hash table to the optimal size based on the number of
844
 * nodes currently held. If you call this function then a resize will
845
 * occur, even if one does not need to occur.
846
 * Use g_hash_table_maybe_resize() instead.
847
 *
848
 * This function may "resize" the hash table to its current size, with
849
 * the side effect of cleaning up tombstones and otherwise optimizing
850
 * the probe sequences.
851
 */
852
static void
853
g_hash_table_resize (GHashTable *hash_table)
854
28.9k
{
855
28.9k
  guint32 *reallocated_buckets_bitmap;
856
28.9k
  gsize old_size;
857
28.9k
  gboolean is_a_set;
858
859
28.9k
  old_size = hash_table->size;
860
28.9k
  is_a_set = hash_table->keys == hash_table->values;
861
862
  /* The outer checks in g_hash_table_maybe_resize() will only consider
863
   * cleanup/resize when the load factor goes below .25 (1/4, ignoring
864
   * tombstones) or above .9375 (15/16, including tombstones).
865
   *
866
   * Once this happens, tombstones will always be cleaned out. If our
867
   * load sans tombstones is greater than .75 (1/1.333, see below), we'll
868
   * take this opportunity to grow the table too.
869
   *
870
   * Immediately after growing, the load factor will be in the range
871
   * .375 .. .469. After shrinking, it will be exactly .5. */
872
873
28.9k
  g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 1.333);
874
875
28.9k
  if (hash_table->size > old_size)
876
22.1k
    {
877
22.1k
      realloc_arrays (hash_table, is_a_set);
878
22.1k
      memset (&hash_table->hashes[old_size], 0, (hash_table->size - old_size) * sizeof (guint));
879
880
22.1k
      reallocated_buckets_bitmap = g_new0 (guint32, (hash_table->size + 31) / 32);
881
22.1k
    }
882
6.74k
  else
883
6.74k
    {
884
6.74k
      reallocated_buckets_bitmap = g_new0 (guint32, (old_size + 31) / 32);
885
6.74k
    }
886
887
28.9k
  if (is_a_set)
888
194
    resize_set (hash_table, old_size, reallocated_buckets_bitmap);
889
28.7k
  else
890
28.7k
    resize_map (hash_table, old_size, reallocated_buckets_bitmap);
891
892
28.9k
  g_free (reallocated_buckets_bitmap);
893
894
28.9k
  if (hash_table->size < old_size)
895
6.68k
    realloc_arrays (hash_table, is_a_set);
896
897
28.9k
  hash_table->noccupied = hash_table->nnodes;
898
28.9k
}
899
900
/*
901
 * g_hash_table_maybe_resize:
902
 * @hash_table: our #GHashTable
903
 *
904
 * Resizes the hash table, if needed.
905
 *
906
 * Essentially, calls g_hash_table_resize() if the table has strayed
907
 * too far from its ideal size for its number of nodes.
908
 */
909
static inline void
910
g_hash_table_maybe_resize (GHashTable *hash_table)
911
2.57M
{
912
2.57M
  gint noccupied = hash_table->noccupied;
913
2.57M
  gint size = hash_table->size;
914
915
2.57M
  if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
916
2.57M
      (size <= noccupied + (noccupied / 16)))
917
28.9k
    g_hash_table_resize (hash_table);
918
2.57M
}
919
920
#ifdef USE_SMALL_ARRAYS
921
922
static inline gboolean
923
entry_is_big (gpointer v)
924
590k
{
925
590k
  return (((guintptr) v) >> ((BIG_ENTRY_SIZE - SMALL_ENTRY_SIZE) * 8)) != 0;
926
590k
}
927
928
static inline gboolean
929
g_hash_table_maybe_make_big_keys_or_values (gpointer *a_p, gpointer v, gint ht_size)
930
590k
{
931
590k
  if (entry_is_big (v))
932
425k
    {
933
425k
      guint *a = (guint *) *a_p;
934
425k
      gpointer *a_new;
935
425k
      gint i;
936
937
425k
      a_new = g_new (gpointer, ht_size);
938
939
3.83M
      for (i = 0; i < ht_size; i++)
940
3.40M
        {
941
3.40M
          a_new[i] = GUINT_TO_POINTER (a[i]);
942
3.40M
        }
943
944
425k
      g_free (a);
945
425k
      *a_p = a_new;
946
425k
      return TRUE;
947
425k
    }
948
949
164k
  return FALSE;
950
590k
}
951
952
#endif
953
954
static inline void
955
g_hash_table_ensure_keyval_fits (GHashTable *hash_table, gpointer key, gpointer value)
956
924k
{
957
924k
  gboolean is_a_set = (hash_table->keys == hash_table->values);
958
959
924k
#ifdef USE_SMALL_ARRAYS
960
961
  /* Convert from set to map? */
962
924k
  if (is_a_set)
963
215k
    {
964
215k
      if (hash_table->have_big_keys)
965
1.15k
        {
966
1.15k
          if (key != value)
967
0
            hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
968
          /* Keys and values are both big now, so no need for further checks */
969
1.15k
          return;
970
1.15k
        }
971
214k
      else
972
214k
        {
973
214k
          if (key != value)
974
211k
            {
975
211k
              hash_table->values = g_memdup2 (hash_table->keys, sizeof (guint) * hash_table->size);
976
211k
              is_a_set = FALSE;
977
211k
            }
978
214k
        }
979
215k
    }
980
981
  /* Make keys big? */
982
923k
  if (!hash_table->have_big_keys)
983
366k
    {
984
366k
      hash_table->have_big_keys = g_hash_table_maybe_make_big_keys_or_values (&hash_table->keys, key, hash_table->size);
985
986
366k
      if (is_a_set)
987
2.56k
        {
988
2.56k
          hash_table->values = hash_table->keys;
989
2.56k
          hash_table->have_big_values = hash_table->have_big_keys;
990
2.56k
        }
991
366k
    }
992
993
  /* Make values big? */
994
923k
  if (!is_a_set && !hash_table->have_big_values)
995
223k
    {
996
223k
      hash_table->have_big_values = g_hash_table_maybe_make_big_keys_or_values (&hash_table->values, value, hash_table->size);
997
223k
    }
998
999
#else
1000
1001
  /* Just split if necessary */
1002
  if (is_a_set && key != value)
1003
    hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
1004
1005
#endif
1006
923k
}
1007
1008
/**
1009
 * g_hash_table_new:
1010
 * @hash_func: a function to create a hash value from a key
1011
 * @key_equal_func: a function to check two keys for equality
1012
 *
1013
 * Creates a new #GHashTable with a reference count of 1.
1014
 *
1015
 * Hash values returned by @hash_func are used to determine where keys
1016
 * are stored within the #GHashTable data structure. The g_direct_hash(),
1017
 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
1018
 * functions are provided for some common types of keys.
1019
 * If @hash_func is %NULL, g_direct_hash() is used.
1020
 *
1021
 * @key_equal_func is used when looking up keys in the #GHashTable.
1022
 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
1023
 * and g_str_equal() functions are provided for the most common types
1024
 * of keys. If @key_equal_func is %NULL, keys are compared directly in
1025
 * a similar fashion to g_direct_equal(), but without the overhead of
1026
 * a function call. @key_equal_func is called with the key from the hash table
1027
 * as its first parameter, and the user-provided key to check against as
1028
 * its second.
1029
 *
1030
 * Returns: a new #GHashTable
1031
 */
1032
GHashTable *
1033
g_hash_table_new (GHashFunc  hash_func,
1034
                  GEqualFunc key_equal_func)
1035
425k
{
1036
425k
  return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
1037
425k
}
1038
1039
1040
/**
1041
 * g_hash_table_new_full:
1042
 * @hash_func: a function to create a hash value from a key
1043
 * @key_equal_func: a function to check two keys for equality
1044
 * @key_destroy_func: (nullable): a function to free the memory allocated for the key
1045
 *     used when removing the entry from the #GHashTable, or %NULL
1046
 *     if you don't want to supply such a function.
1047
 * @value_destroy_func: (nullable): a function to free the memory allocated for the
1048
 *     value used when removing the entry from the #GHashTable, or %NULL
1049
 *     if you don't want to supply such a function.
1050
 *
1051
 * Creates a new #GHashTable like g_hash_table_new() with a reference
1052
 * count of 1 and allows to specify functions to free the memory
1053
 * allocated for the key and value that get called when removing the
1054
 * entry from the #GHashTable.
1055
 *
1056
 * Since version 2.42 it is permissible for destroy notify functions to
1057
 * recursively remove further items from the hash table. This is only
1058
 * permissible if the application still holds a reference to the hash table.
1059
 * This means that you may need to ensure that the hash table is empty by
1060
 * calling g_hash_table_remove_all() before releasing the last reference using
1061
 * g_hash_table_unref().
1062
 *
1063
 * Returns: a new #GHashTable
1064
 */
1065
GHashTable *
1066
g_hash_table_new_full (GHashFunc      hash_func,
1067
                       GEqualFunc     key_equal_func,
1068
                       GDestroyNotify key_destroy_func,
1069
                       GDestroyNotify value_destroy_func)
1070
499k
{
1071
499k
  GHashTable *hash_table;
1072
1073
499k
  hash_table = g_slice_new (GHashTable);
1074
499k
  g_atomic_ref_count_init (&hash_table->ref_count);
1075
499k
  hash_table->nnodes             = 0;
1076
499k
  hash_table->noccupied          = 0;
1077
499k
  hash_table->hash_func          = hash_func ? hash_func : g_direct_hash;
1078
499k
  hash_table->key_equal_func     = key_equal_func;
1079
499k
#ifndef G_DISABLE_ASSERT
1080
499k
  hash_table->version            = 0;
1081
499k
#endif
1082
499k
  hash_table->key_destroy_func   = key_destroy_func;
1083
499k
  hash_table->value_destroy_func = value_destroy_func;
1084
1085
499k
  g_hash_table_setup_storage (hash_table);
1086
1087
499k
  return hash_table;
1088
499k
}
1089
1090
/**
1091
 * g_hash_table_new_similar:
1092
 * @other_hash_table: (not nullable) (transfer none): Another #GHashTable
1093
 *
1094
 * Creates a new #GHashTable like g_hash_table_new_full() with a reference
1095
 * count of 1.
1096
 *
1097
 * It inherits the hash function, the key equal function, the key destroy function,
1098
 * as well as the value destroy function, from @other_hash_table.
1099
 *
1100
 * The returned hash table will be empty; it will not contain the keys
1101
 * or values from @other_hash_table.
1102
 *
1103
 * Returns: (transfer full) (not nullable): a new #GHashTable
1104
 * Since: 2.72
1105
 */
1106
GHashTable *
1107
g_hash_table_new_similar (GHashTable *other_hash_table)
1108
0
{
1109
0
  g_return_val_if_fail (other_hash_table, NULL);
1110
1111
0
  return g_hash_table_new_full (other_hash_table->hash_func,
1112
0
                                other_hash_table->key_equal_func,
1113
0
                                other_hash_table->key_destroy_func,
1114
0
                                other_hash_table->value_destroy_func);
1115
0
}
1116
1117
/**
1118
 * g_hash_table_iter_init:
1119
 * @iter: an uninitialized #GHashTableIter
1120
 * @hash_table: a #GHashTable
1121
 *
1122
 * Initializes a key/value pair iterator and associates it with
1123
 * @hash_table. Modifying the hash table after calling this function
1124
 * invalidates the returned iterator.
1125
 *
1126
 * The iteration order of a #GHashTableIter over the keys/values in a hash
1127
 * table is not defined.
1128
 *
1129
 * |[<!-- language="C" -->
1130
 * GHashTableIter iter;
1131
 * gpointer key, value;
1132
 *
1133
 * g_hash_table_iter_init (&iter, hash_table);
1134
 * while (g_hash_table_iter_next (&iter, &key, &value))
1135
 *   {
1136
 *     // do something with key and value
1137
 *   }
1138
 * ]|
1139
 *
1140
 * Since: 2.16
1141
 */
1142
void
1143
g_hash_table_iter_init (GHashTableIter *iter,
1144
                        GHashTable     *hash_table)
1145
0
{
1146
0
  RealIter *ri = (RealIter *) iter;
1147
1148
0
  g_return_if_fail (iter != NULL);
1149
0
  g_return_if_fail (hash_table != NULL);
1150
1151
0
  ri->hash_table = hash_table;
1152
0
  ri->position = -1;
1153
0
#ifndef G_DISABLE_ASSERT
1154
0
  ri->version = hash_table->version;
1155
0
#endif
1156
0
}
1157
1158
/**
1159
 * g_hash_table_iter_next:
1160
 * @iter: an initialized #GHashTableIter
1161
 * @key: (out) (optional): a location to store the key
1162
 * @value: (out) (optional) (nullable): a location to store the value
1163
 *
1164
 * Advances @iter and retrieves the key and/or value that are now
1165
 * pointed to as a result of this advancement. If %FALSE is returned,
1166
 * @key and @value are not set, and the iterator becomes invalid.
1167
 *
1168
 * Returns: %FALSE if the end of the #GHashTable has been reached.
1169
 *
1170
 * Since: 2.16
1171
 */
1172
gboolean
1173
g_hash_table_iter_next (GHashTableIter *iter,
1174
                        gpointer       *key,
1175
                        gpointer       *value)
1176
0
{
1177
0
  RealIter *ri = (RealIter *) iter;
1178
0
  gint position;
1179
1180
0
  g_return_val_if_fail (iter != NULL, FALSE);
1181
0
#ifndef G_DISABLE_ASSERT
1182
0
  g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
1183
0
#endif
1184
0
  g_return_val_if_fail (ri->position < (gssize) ri->hash_table->size, FALSE);
1185
1186
0
  position = ri->position;
1187
1188
0
  do
1189
0
    {
1190
0
      position++;
1191
0
      if (position >= (gssize) ri->hash_table->size)
1192
0
        {
1193
0
          ri->position = position;
1194
0
          return FALSE;
1195
0
        }
1196
0
    }
1197
0
  while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
1198
1199
0
  if (key != NULL)
1200
0
    *key = g_hash_table_fetch_key_or_value (ri->hash_table->keys, position, ri->hash_table->have_big_keys);
1201
0
  if (value != NULL)
1202
0
    *value = g_hash_table_fetch_key_or_value (ri->hash_table->values, position, ri->hash_table->have_big_values);
1203
1204
0
  ri->position = position;
1205
0
  return TRUE;
1206
0
}
1207
1208
/**
1209
 * g_hash_table_iter_get_hash_table:
1210
 * @iter: an initialized #GHashTableIter
1211
 *
1212
 * Returns the #GHashTable associated with @iter.
1213
 *
1214
 * Returns: the #GHashTable associated with @iter.
1215
 *
1216
 * Since: 2.16
1217
 */
1218
GHashTable *
1219
g_hash_table_iter_get_hash_table (GHashTableIter *iter)
1220
0
{
1221
0
  g_return_val_if_fail (iter != NULL, NULL);
1222
1223
0
  return ((RealIter *) iter)->hash_table;
1224
0
}
1225
1226
static void
1227
iter_remove_or_steal (RealIter *ri, gboolean notify)
1228
0
{
1229
0
  g_return_if_fail (ri != NULL);
1230
0
#ifndef G_DISABLE_ASSERT
1231
0
  g_return_if_fail (ri->version == ri->hash_table->version);
1232
0
#endif
1233
0
  g_return_if_fail (ri->position >= 0);
1234
0
  g_return_if_fail ((gsize) ri->position < ri->hash_table->size);
1235
1236
0
  g_hash_table_remove_node (ri->hash_table, ri->position, notify);
1237
1238
0
#ifndef G_DISABLE_ASSERT
1239
0
  ri->version++;
1240
0
  ri->hash_table->version++;
1241
0
#endif
1242
0
}
1243
1244
/**
1245
 * g_hash_table_iter_remove:
1246
 * @iter: an initialized #GHashTableIter
1247
 *
1248
 * Removes the key/value pair currently pointed to by the iterator
1249
 * from its associated #GHashTable. Can only be called after
1250
 * g_hash_table_iter_next() returned %TRUE, and cannot be called
1251
 * more than once for the same key/value pair.
1252
 *
1253
 * If the #GHashTable was created using g_hash_table_new_full(),
1254
 * the key and value are freed using the supplied destroy functions,
1255
 * otherwise you have to make sure that any dynamically allocated
1256
 * values are freed yourself.
1257
 *
1258
 * It is safe to continue iterating the #GHashTable afterward:
1259
 * |[<!-- language="C" -->
1260
 * while (g_hash_table_iter_next (&iter, &key, &value))
1261
 *   {
1262
 *     if (condition)
1263
 *       g_hash_table_iter_remove (&iter);
1264
 *   }
1265
 * ]|
1266
 *
1267
 * Since: 2.16
1268
 */
1269
void
1270
g_hash_table_iter_remove (GHashTableIter *iter)
1271
0
{
1272
0
  iter_remove_or_steal ((RealIter *) iter, TRUE);
1273
0
}
1274
1275
/*
1276
 * g_hash_table_insert_node:
1277
 * @hash_table: our #GHashTable
1278
 * @node_index: pointer to node to insert/replace
1279
 * @key_hash: key hash
1280
 * @key: (nullable): key to replace with, or %NULL
1281
 * @value: value to replace with
1282
 * @keep_new_key: whether to replace the key in the node with @key
1283
 * @reusing_key: whether @key was taken out of the existing node
1284
 *
1285
 * Inserts a value at @node_index in the hash table and updates it.
1286
 *
1287
 * If @key has been taken out of the existing node (ie it is not
1288
 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
1289
 * should be %TRUE.
1290
 *
1291
 * Returns: %TRUE if the key did not exist yet
1292
 */
1293
static gboolean
1294
g_hash_table_insert_node (GHashTable *hash_table,
1295
                          guint       node_index,
1296
                          guint       key_hash,
1297
                          gpointer    new_key,
1298
                          gpointer    new_value,
1299
                          gboolean    keep_new_key,
1300
                          gboolean    reusing_key)
1301
924k
{
1302
924k
  gboolean already_exists;
1303
924k
  guint old_hash;
1304
924k
  gpointer key_to_free = NULL;
1305
924k
  gpointer key_to_keep = NULL;
1306
924k
  gpointer value_to_free = NULL;
1307
1308
924k
  old_hash = hash_table->hashes[node_index];
1309
924k
  already_exists = HASH_IS_REAL (old_hash);
1310
1311
  /* Proceed in three steps.  First, deal with the key because it is the
1312
   * most complicated.  Then consider if we need to split the table in
1313
   * two (because writing the value will result in the set invariant
1314
   * becoming broken).  Then deal with the value.
1315
   *
1316
   * There are three cases for the key:
1317
   *
1318
   *  - entry already exists in table, reusing key:
1319
   *    free the just-passed-in new_key and use the existing value
1320
   *
1321
   *  - entry already exists in table, not reusing key:
1322
   *    free the entry in the table, use the new key
1323
   *
1324
   *  - entry not already in table:
1325
   *    use the new key, free nothing
1326
   *
1327
   * We update the hash at the same time...
1328
   */
1329
924k
  if (already_exists)
1330
1.54k
    {
1331
      /* Note: we must record the old value before writing the new key
1332
       * because we might change the value in the event that the two
1333
       * arrays are shared.
1334
       */
1335
1.54k
      value_to_free = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1336
1337
1.54k
      if (keep_new_key)
1338
1.15k
        {
1339
1.15k
          key_to_free = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1340
1.15k
          key_to_keep = new_key;
1341
1.15k
        }
1342
390
      else
1343
390
        {
1344
390
          key_to_free = new_key;
1345
390
          key_to_keep = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1346
390
        }
1347
1.54k
    }
1348
923k
  else
1349
923k
    {
1350
923k
      hash_table->hashes[node_index] = key_hash;
1351
923k
      key_to_keep = new_key;
1352
923k
    }
1353
1354
  /* Resize key/value arrays and split table as necessary */
1355
924k
  g_hash_table_ensure_keyval_fits (hash_table, key_to_keep, new_value);
1356
924k
  g_hash_table_assign_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys, key_to_keep);
1357
1358
  /* Step 3: Actually do the write */
1359
924k
  g_hash_table_assign_key_or_value (hash_table->values, node_index, hash_table->have_big_values, new_value);
1360
1361
  /* Now, the bookkeeping... */
1362
924k
  if (!already_exists)
1363
923k
    {
1364
923k
      hash_table->nnodes++;
1365
1366
923k
      if (HASH_IS_UNUSED (old_hash))
1367
753k
        {
1368
          /* We replaced an empty node, and not a tombstone */
1369
753k
          hash_table->noccupied++;
1370
753k
          g_hash_table_maybe_resize (hash_table);
1371
753k
        }
1372
1373
923k
#ifndef G_DISABLE_ASSERT
1374
923k
      hash_table->version++;
1375
923k
#endif
1376
923k
    }
1377
1378
924k
  if (already_exists)
1379
1.54k
    {
1380
1.54k
      if (hash_table->key_destroy_func && !reusing_key)
1381
1.15k
        (* hash_table->key_destroy_func) (key_to_free);
1382
1.54k
      if (hash_table->value_destroy_func)
1383
1.15k
        (* hash_table->value_destroy_func) (value_to_free);
1384
1.54k
    }
1385
1386
924k
  return !already_exists;
1387
924k
}
1388
1389
/**
1390
 * g_hash_table_iter_replace:
1391
 * @iter: an initialized #GHashTableIter
1392
 * @value: the value to replace with
1393
 *
1394
 * Replaces the value currently pointed to by the iterator
1395
 * from its associated #GHashTable. Can only be called after
1396
 * g_hash_table_iter_next() returned %TRUE.
1397
 *
1398
 * If you supplied a @value_destroy_func when creating the
1399
 * #GHashTable, the old value is freed using that function.
1400
 *
1401
 * Since: 2.30
1402
 */
1403
void
1404
g_hash_table_iter_replace (GHashTableIter *iter,
1405
                           gpointer        value)
1406
0
{
1407
0
  RealIter *ri;
1408
0
  guint node_hash;
1409
0
  gpointer key;
1410
1411
0
  ri = (RealIter *) iter;
1412
1413
0
  g_return_if_fail (ri != NULL);
1414
0
#ifndef G_DISABLE_ASSERT
1415
0
  g_return_if_fail (ri->version == ri->hash_table->version);
1416
0
#endif
1417
0
  g_return_if_fail (ri->position >= 0);
1418
0
  g_return_if_fail ((gsize) ri->position < ri->hash_table->size);
1419
1420
0
  node_hash = ri->hash_table->hashes[ri->position];
1421
1422
0
  key = g_hash_table_fetch_key_or_value (ri->hash_table->keys, ri->position, ri->hash_table->have_big_keys);
1423
1424
0
  g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
1425
1426
0
#ifndef G_DISABLE_ASSERT
1427
0
  ri->version++;
1428
0
  ri->hash_table->version++;
1429
0
#endif
1430
0
}
1431
1432
/**
1433
 * g_hash_table_iter_steal:
1434
 * @iter: an initialized #GHashTableIter
1435
 *
1436
 * Removes the key/value pair currently pointed to by the
1437
 * iterator from its associated #GHashTable, without calling
1438
 * the key and value destroy functions. Can only be called
1439
 * after g_hash_table_iter_next() returned %TRUE, and cannot
1440
 * be called more than once for the same key/value pair.
1441
 *
1442
 * Since: 2.16
1443
 */
1444
void
1445
g_hash_table_iter_steal (GHashTableIter *iter)
1446
0
{
1447
0
  iter_remove_or_steal ((RealIter *) iter, FALSE);
1448
0
}
1449
1450
1451
/**
1452
 * g_hash_table_ref:
1453
 * @hash_table: a valid #GHashTable
1454
 *
1455
 * Atomically increments the reference count of @hash_table by one.
1456
 * This function is MT-safe and may be called from any thread.
1457
 *
1458
 * Returns: the passed in #GHashTable
1459
 *
1460
 * Since: 2.10
1461
 */
1462
GHashTable *
1463
g_hash_table_ref (GHashTable *hash_table)
1464
0
{
1465
0
  g_return_val_if_fail (hash_table != NULL, NULL);
1466
1467
0
  g_atomic_ref_count_inc (&hash_table->ref_count);
1468
1469
0
  return hash_table;
1470
0
}
1471
1472
/**
1473
 * g_hash_table_unref:
1474
 * @hash_table: a valid #GHashTable
1475
 *
1476
 * Atomically decrements the reference count of @hash_table by one.
1477
 * If the reference count drops to 0, all keys and values will be
1478
 * destroyed, and all memory allocated by the hash table is released.
1479
 * This function is MT-safe and may be called from any thread.
1480
 *
1481
 * Since: 2.10
1482
 */
1483
void
1484
g_hash_table_unref (GHashTable *hash_table)
1485
465k
{
1486
465k
  g_return_if_fail (hash_table != NULL);
1487
1488
465k
  if (g_atomic_ref_count_dec (&hash_table->ref_count))
1489
465k
    {
1490
465k
      g_hash_table_remove_all_nodes (hash_table, TRUE, TRUE);
1491
465k
      if (hash_table->keys != hash_table->values)
1492
208k
        g_free (hash_table->values);
1493
465k
      g_free (hash_table->keys);
1494
465k
      g_free (hash_table->hashes);
1495
465k
      g_slice_free (GHashTable, hash_table);
1496
465k
    }
1497
465k
}
1498
1499
/**
1500
 * g_hash_table_destroy:
1501
 * @hash_table: a #GHashTable
1502
 *
1503
 * Destroys all keys and values in the #GHashTable and decrements its
1504
 * reference count by 1. If keys and/or values are dynamically allocated,
1505
 * you should either free them first or create the #GHashTable with destroy
1506
 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1507
 * functions you supplied will be called on all keys and values during the
1508
 * destruction phase.
1509
 */
1510
void
1511
g_hash_table_destroy (GHashTable *hash_table)
1512
465k
{
1513
465k
  g_return_if_fail (hash_table != NULL);
1514
1515
465k
  g_hash_table_remove_all (hash_table);
1516
465k
  g_hash_table_unref (hash_table);
1517
465k
}
1518
1519
/**
1520
 * g_hash_table_lookup:
1521
 * @hash_table: a #GHashTable
1522
 * @key: the key to look up
1523
 *
1524
 * Looks up a key in a #GHashTable. Note that this function cannot
1525
 * distinguish between a key that is not present and one which is present
1526
 * and has the value %NULL. If you need this distinction, use
1527
 * g_hash_table_lookup_extended().
1528
 *
1529
 * Returns: (nullable): the associated value, or %NULL if the key is not found
1530
 */
1531
gpointer
1532
g_hash_table_lookup (GHashTable    *hash_table,
1533
                     gconstpointer  key)
1534
1.01G
{
1535
1.01G
  guint node_index;
1536
1.01G
  guint node_hash;
1537
1538
1.01G
  g_return_val_if_fail (hash_table != NULL, NULL);
1539
1540
1.01G
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1541
1542
1.01G
  return HASH_IS_REAL (hash_table->hashes[node_index])
1543
1.01G
    ? g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values)
1544
1.01G
    : NULL;
1545
1.01G
}
1546
1547
/**
1548
 * g_hash_table_lookup_extended:
1549
 * @hash_table: a #GHashTable
1550
 * @lookup_key: the key to look up
1551
 * @orig_key: (out) (optional): return location for the original key
1552
 * @value: (out) (optional) (nullable): return location for the value associated
1553
 * with the key
1554
 *
1555
 * Looks up a key in the #GHashTable, returning the original key and the
1556
 * associated value and a #gboolean which is %TRUE if the key was found. This
1557
 * is useful if you need to free the memory allocated for the original key,
1558
 * for example before calling g_hash_table_remove().
1559
 *
1560
 * You can actually pass %NULL for @lookup_key to test
1561
 * whether the %NULL key exists, provided the hash and equal functions
1562
 * of @hash_table are %NULL-safe.
1563
 *
1564
 * Returns: %TRUE if the key was found in the #GHashTable
1565
 */
1566
gboolean
1567
g_hash_table_lookup_extended (GHashTable    *hash_table,
1568
                              gconstpointer  lookup_key,
1569
                              gpointer      *orig_key,
1570
                              gpointer      *value)
1571
816M
{
1572
816M
  guint node_index;
1573
816M
  guint node_hash;
1574
1575
816M
  g_return_val_if_fail (hash_table != NULL, FALSE);
1576
1577
816M
  node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1578
1579
816M
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1580
51.9k
    {
1581
51.9k
      if (orig_key != NULL)
1582
27.6k
        *orig_key = NULL;
1583
51.9k
      if (value != NULL)
1584
27.6k
        *value = NULL;
1585
1586
51.9k
      return FALSE;
1587
51.9k
    }
1588
1589
816M
  if (orig_key)
1590
816M
    *orig_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1591
1592
816M
  if (value)
1593
816M
    *value = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1594
1595
816M
  return TRUE;
1596
816M
}
1597
1598
/*
1599
 * g_hash_table_insert_internal:
1600
 * @hash_table: our #GHashTable
1601
 * @key: the key to insert
1602
 * @value: the value to insert
1603
 * @keep_new_key: if %TRUE and this key already exists in the table
1604
 *   then call the destroy notify function on the old key.  If %FALSE
1605
 *   then call the destroy notify function on the new key.
1606
 *
1607
 * Implements the common logic for the g_hash_table_insert() and
1608
 * g_hash_table_replace() functions.
1609
 *
1610
 * Do a lookup of @key. If it is found, replace it with the new
1611
 * @value (and perhaps the new @key). If it is not found, create
1612
 * a new node.
1613
 *
1614
 * Returns: %TRUE if the key did not exist yet
1615
 */
1616
static gboolean
1617
g_hash_table_insert_internal (GHashTable *hash_table,
1618
                              gpointer    key,
1619
                              gpointer    value,
1620
                              gboolean    keep_new_key)
1621
924k
{
1622
924k
  guint key_hash;
1623
924k
  guint node_index;
1624
1625
924k
  g_return_val_if_fail (hash_table != NULL, FALSE);
1626
1627
924k
  node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1628
1629
924k
  return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1630
924k
}
1631
1632
/**
1633
 * g_hash_table_insert:
1634
 * @hash_table: a #GHashTable
1635
 * @key: a key to insert
1636
 * @value: the value to associate with the key
1637
 *
1638
 * Inserts a new key and value into a #GHashTable.
1639
 *
1640
 * If the key already exists in the #GHashTable its current
1641
 * value is replaced with the new value. If you supplied a
1642
 * @value_destroy_func when creating the #GHashTable, the old
1643
 * value is freed using that function. If you supplied a
1644
 * @key_destroy_func when creating the #GHashTable, the passed
1645
 * key is freed using that function.
1646
 *
1647
 * Starting from GLib 2.40, this function returns a boolean value to
1648
 * indicate whether the newly added value was already in the hash table
1649
 * or not.
1650
 *
1651
 * Returns: %TRUE if the key did not exist yet
1652
 */
1653
gboolean
1654
g_hash_table_insert (GHashTable *hash_table,
1655
                     gpointer    key,
1656
                     gpointer    value)
1657
912k
{
1658
912k
  return g_hash_table_insert_internal (hash_table, key, value, FALSE);
1659
912k
}
1660
1661
/**
1662
 * g_hash_table_replace:
1663
 * @hash_table: a #GHashTable
1664
 * @key: a key to insert
1665
 * @value: the value to associate with the key
1666
 *
1667
 * Inserts a new key and value into a #GHashTable similar to
1668
 * g_hash_table_insert(). The difference is that if the key
1669
 * already exists in the #GHashTable, it gets replaced by the
1670
 * new key. If you supplied a @value_destroy_func when creating
1671
 * the #GHashTable, the old value is freed using that function.
1672
 * If you supplied a @key_destroy_func when creating the
1673
 * #GHashTable, the old key is freed using that function.
1674
 *
1675
 * Starting from GLib 2.40, this function returns a boolean value to
1676
 * indicate whether the newly added value was already in the hash table
1677
 * or not.
1678
 *
1679
 * Returns: %TRUE if the key did not exist yet
1680
 */
1681
gboolean
1682
g_hash_table_replace (GHashTable *hash_table,
1683
                      gpointer    key,
1684
                      gpointer    value)
1685
8.97k
{
1686
8.97k
  return g_hash_table_insert_internal (hash_table, key, value, TRUE);
1687
8.97k
}
1688
1689
/**
1690
 * g_hash_table_add:
1691
 * @hash_table: a #GHashTable
1692
 * @key: (transfer full): a key to insert
1693
 *
1694
 * This is a convenience function for using a #GHashTable as a set.  It
1695
 * is equivalent to calling g_hash_table_replace() with @key as both the
1696
 * key and the value.
1697
 *
1698
 * In particular, this means that if @key already exists in the hash table, then
1699
 * the old copy of @key in the hash table is freed and @key replaces it in the
1700
 * table.
1701
 *
1702
 * When a hash table only ever contains keys that have themselves as the
1703
 * corresponding value it is able to be stored more efficiently.  See
1704
 * the discussion in the section description.
1705
 *
1706
 * Starting from GLib 2.40, this function returns a boolean value to
1707
 * indicate whether the newly added value was already in the hash table
1708
 * or not.
1709
 *
1710
 * Returns: %TRUE if the key did not exist yet
1711
 *
1712
 * Since: 2.32
1713
 */
1714
gboolean
1715
g_hash_table_add (GHashTable *hash_table,
1716
                  gpointer    key)
1717
3.71k
{
1718
3.71k
  return g_hash_table_insert_internal (hash_table, key, key, TRUE);
1719
3.71k
}
1720
1721
/**
1722
 * g_hash_table_contains:
1723
 * @hash_table: a #GHashTable
1724
 * @key: a key to check
1725
 *
1726
 * Checks if @key is in @hash_table.
1727
 *
1728
 * Returns: %TRUE if @key is in @hash_table, %FALSE otherwise.
1729
 *
1730
 * Since: 2.32
1731
 **/
1732
gboolean
1733
g_hash_table_contains (GHashTable    *hash_table,
1734
                       gconstpointer  key)
1735
5.46k
{
1736
5.46k
  guint node_index;
1737
5.46k
  guint node_hash;
1738
1739
5.46k
  g_return_val_if_fail (hash_table != NULL, FALSE);
1740
1741
5.46k
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1742
1743
5.46k
  return HASH_IS_REAL (hash_table->hashes[node_index]);
1744
5.46k
}
1745
1746
/*
1747
 * g_hash_table_remove_internal:
1748
 * @hash_table: our #GHashTable
1749
 * @key: the key to remove
1750
 * @notify: %TRUE if the destroy notify handlers are to be called
1751
 * Returns: %TRUE if a node was found and removed, else %FALSE
1752
 *
1753
 * Implements the common logic for the g_hash_table_remove() and
1754
 * g_hash_table_steal() functions.
1755
 *
1756
 * Do a lookup of @key and remove it if it is found, calling the
1757
 * destroy notify handlers only if @notify is %TRUE.
1758
 */
1759
static gboolean
1760
g_hash_table_remove_internal (GHashTable    *hash_table,
1761
                              gconstpointer  key,
1762
                              gboolean       notify)
1763
587k
{
1764
587k
  guint node_index;
1765
587k
  guint node_hash;
1766
1767
587k
  g_return_val_if_fail (hash_table != NULL, FALSE);
1768
1769
587k
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1770
1771
587k
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1772
253k
    return FALSE;
1773
1774
333k
  g_hash_table_remove_node (hash_table, node_index, notify);
1775
333k
  g_hash_table_maybe_resize (hash_table);
1776
1777
333k
#ifndef G_DISABLE_ASSERT
1778
333k
  hash_table->version++;
1779
333k
#endif
1780
1781
333k
  return TRUE;
1782
587k
}
1783
1784
/**
1785
 * g_hash_table_remove:
1786
 * @hash_table: a #GHashTable
1787
 * @key: the key to remove
1788
 *
1789
 * Removes a key and its associated value from a #GHashTable.
1790
 *
1791
 * If the #GHashTable was created using g_hash_table_new_full(), the
1792
 * key and value are freed using the supplied destroy functions, otherwise
1793
 * you have to make sure that any dynamically allocated values are freed
1794
 * yourself.
1795
 *
1796
 * Returns: %TRUE if the key was found and removed from the #GHashTable
1797
 */
1798
gboolean
1799
g_hash_table_remove (GHashTable    *hash_table,
1800
                     gconstpointer  key)
1801
587k
{
1802
587k
  return g_hash_table_remove_internal (hash_table, key, TRUE);
1803
587k
}
1804
1805
/**
1806
 * g_hash_table_steal:
1807
 * @hash_table: a #GHashTable
1808
 * @key: the key to remove
1809
 *
1810
 * Removes a key and its associated value from a #GHashTable without
1811
 * calling the key and value destroy functions.
1812
 *
1813
 * Returns: %TRUE if the key was found and removed from the #GHashTable
1814
 */
1815
gboolean
1816
g_hash_table_steal (GHashTable    *hash_table,
1817
                    gconstpointer  key)
1818
0
{
1819
0
  return g_hash_table_remove_internal (hash_table, key, FALSE);
1820
0
}
1821
1822
/**
1823
 * g_hash_table_steal_extended:
1824
 * @hash_table: a #GHashTable
1825
 * @lookup_key: the key to look up
1826
 * @stolen_key: (out) (optional) (transfer full): return location for the
1827
 *    original key
1828
 * @stolen_value: (out) (optional) (nullable) (transfer full): return location
1829
 *    for the value associated with the key
1830
 *
1831
 * Looks up a key in the #GHashTable, stealing the original key and the
1832
 * associated value and returning %TRUE if the key was found. If the key was
1833
 * not found, %FALSE is returned.
1834
 *
1835
 * If found, the stolen key and value are removed from the hash table without
1836
 * calling the key and value destroy functions, and ownership is transferred to
1837
 * the caller of this method; as with g_hash_table_steal().
1838
 *
1839
 * You can pass %NULL for @lookup_key, provided the hash and equal functions
1840
 * of @hash_table are %NULL-safe.
1841
 *
1842
 * The dictionary implementation optimizes for having all values identical to
1843
 * their keys, for example by using g_hash_table_add(). When stealing both the
1844
 * key and the value from such a dictionary, the value will be %NULL.
1845
 *
1846
 * Returns: %TRUE if the key was found in the #GHashTable
1847
 * Since: 2.58
1848
 */
1849
gboolean
1850
g_hash_table_steal_extended (GHashTable    *hash_table,
1851
                             gconstpointer  lookup_key,
1852
                             gpointer      *stolen_key,
1853
                             gpointer      *stolen_value)
1854
0
{
1855
0
  guint node_index;
1856
0
  guint node_hash;
1857
1858
0
  g_return_val_if_fail (hash_table != NULL, FALSE);
1859
1860
0
  node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1861
1862
0
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1863
0
    {
1864
0
      if (stolen_key != NULL)
1865
0
        *stolen_key = NULL;
1866
0
      if (stolen_value != NULL)
1867
0
        *stolen_value = NULL;
1868
0
      return FALSE;
1869
0
    }
1870
1871
0
  if (stolen_key != NULL)
1872
0
  {
1873
0
    *stolen_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1874
0
    g_hash_table_assign_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys, NULL);
1875
0
  }
1876
1877
0
  if (stolen_value != NULL)
1878
0
  {
1879
0
    *stolen_value = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1880
0
    g_hash_table_assign_key_or_value (hash_table->values, node_index, hash_table->have_big_values, NULL);
1881
0
  }
1882
1883
0
  g_hash_table_remove_node (hash_table, node_index, FALSE);
1884
0
  g_hash_table_maybe_resize (hash_table);
1885
1886
0
#ifndef G_DISABLE_ASSERT
1887
0
  hash_table->version++;
1888
0
#endif
1889
1890
0
  return TRUE;
1891
0
}
1892
1893
/**
1894
 * g_hash_table_remove_all:
1895
 * @hash_table: a #GHashTable
1896
 *
1897
 * Removes all keys and their associated values from a #GHashTable.
1898
 *
1899
 * If the #GHashTable was created using g_hash_table_new_full(),
1900
 * the keys and values are freed using the supplied destroy functions,
1901
 * otherwise you have to make sure that any dynamically allocated
1902
 * values are freed yourself.
1903
 *
1904
 * Since: 2.12
1905
 */
1906
void
1907
g_hash_table_remove_all (GHashTable *hash_table)
1908
1.46M
{
1909
1.46M
  g_return_if_fail (hash_table != NULL);
1910
1911
1.46M
#ifndef G_DISABLE_ASSERT
1912
1.46M
  if (hash_table->nnodes != 0)
1913
57.6k
    hash_table->version++;
1914
1.46M
#endif
1915
1916
1.46M
  g_hash_table_remove_all_nodes (hash_table, TRUE, FALSE);
1917
1.46M
  g_hash_table_maybe_resize (hash_table);
1918
1.46M
}
1919
1920
/**
1921
 * g_hash_table_steal_all:
1922
 * @hash_table: a #GHashTable
1923
 *
1924
 * Removes all keys and their associated values from a #GHashTable
1925
 * without calling the key and value destroy functions.
1926
 *
1927
 * Since: 2.12
1928
 */
1929
void
1930
g_hash_table_steal_all (GHashTable *hash_table)
1931
0
{
1932
0
  g_return_if_fail (hash_table != NULL);
1933
1934
0
#ifndef G_DISABLE_ASSERT
1935
0
  if (hash_table->nnodes != 0)
1936
0
    hash_table->version++;
1937
0
#endif
1938
1939
0
  g_hash_table_remove_all_nodes (hash_table, FALSE, FALSE);
1940
0
  g_hash_table_maybe_resize (hash_table);
1941
0
}
1942
1943
/*
1944
 * g_hash_table_foreach_remove_or_steal:
1945
 * @hash_table: a #GHashTable
1946
 * @func: the user's callback function
1947
 * @user_data: data for @func
1948
 * @notify: %TRUE if the destroy notify handlers are to be called
1949
 *
1950
 * Implements the common logic for g_hash_table_foreach_remove()
1951
 * and g_hash_table_foreach_steal().
1952
 *
1953
 * Iterates over every node in the table, calling @func with the key
1954
 * and value of the node (and @user_data). If @func returns %TRUE the
1955
 * node is removed from the table.
1956
 *
1957
 * If @notify is true then the destroy notify handlers will be called
1958
 * for each removed node.
1959
 */
1960
static guint
1961
g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1962
                                      GHRFunc     func,
1963
                                      gpointer    user_data,
1964
                                      gboolean    notify)
1965
22.8k
{
1966
22.8k
  guint deleted = 0;
1967
22.8k
  gsize i;
1968
22.8k
#ifndef G_DISABLE_ASSERT
1969
22.8k
  gint version = hash_table->version;
1970
22.8k
#endif
1971
1972
206k
  for (i = 0; i < hash_table->size; i++)
1973
183k
    {
1974
183k
      guint node_hash = hash_table->hashes[i];
1975
183k
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
1976
183k
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
1977
1978
183k
      if (HASH_IS_REAL (node_hash) &&
1979
0
          (* func) (node_key, node_value, user_data))
1980
0
        {
1981
0
          g_hash_table_remove_node (hash_table, i, notify);
1982
0
          deleted++;
1983
0
        }
1984
1985
183k
#ifndef G_DISABLE_ASSERT
1986
183k
      g_return_val_if_fail (version == hash_table->version, 0);
1987
183k
#endif
1988
183k
    }
1989
1990
22.8k
  g_hash_table_maybe_resize (hash_table);
1991
1992
22.8k
#ifndef G_DISABLE_ASSERT
1993
22.8k
  if (deleted > 0)
1994
0
    hash_table->version++;
1995
22.8k
#endif
1996
1997
22.8k
  return deleted;
1998
22.8k
}
1999
2000
/**
2001
 * g_hash_table_foreach_remove:
2002
 * @hash_table: a #GHashTable
2003
 * @func: the function to call for each key/value pair
2004
 * @user_data: user data to pass to the function
2005
 *
2006
 * Calls the given function for each key/value pair in the
2007
 * #GHashTable. If the function returns %TRUE, then the key/value
2008
 * pair is removed from the #GHashTable. If you supplied key or
2009
 * value destroy functions when creating the #GHashTable, they are
2010
 * used to free the memory allocated for the removed keys and values.
2011
 *
2012
 * See #GHashTableIter for an alternative way to loop over the
2013
 * key/value pairs in the hash table.
2014
 *
2015
 * Returns: the number of key/value pairs removed
2016
 */
2017
guint
2018
g_hash_table_foreach_remove (GHashTable *hash_table,
2019
                             GHRFunc     func,
2020
                             gpointer    user_data)
2021
22.8k
{
2022
22.8k
  g_return_val_if_fail (hash_table != NULL, 0);
2023
22.8k
  g_return_val_if_fail (func != NULL, 0);
2024
2025
22.8k
  return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
2026
22.8k
}
2027
2028
/**
2029
 * g_hash_table_foreach_steal:
2030
 * @hash_table: a #GHashTable
2031
 * @func: the function to call for each key/value pair
2032
 * @user_data: user data to pass to the function
2033
 *
2034
 * Calls the given function for each key/value pair in the
2035
 * #GHashTable. If the function returns %TRUE, then the key/value
2036
 * pair is removed from the #GHashTable, but no key or value
2037
 * destroy functions are called.
2038
 *
2039
 * See #GHashTableIter for an alternative way to loop over the
2040
 * key/value pairs in the hash table.
2041
 *
2042
 * Returns: the number of key/value pairs removed.
2043
 */
2044
guint
2045
g_hash_table_foreach_steal (GHashTable *hash_table,
2046
                            GHRFunc     func,
2047
                            gpointer    user_data)
2048
0
{
2049
0
  g_return_val_if_fail (hash_table != NULL, 0);
2050
0
  g_return_val_if_fail (func != NULL, 0);
2051
2052
0
  return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
2053
0
}
2054
2055
/**
2056
 * g_hash_table_foreach:
2057
 * @hash_table: a #GHashTable
2058
 * @func: the function to call for each key/value pair
2059
 * @user_data: user data to pass to the function
2060
 *
2061
 * Calls the given function for each of the key/value pairs in the
2062
 * #GHashTable.  The function is passed the key and value of each
2063
 * pair, and the given @user_data parameter.  The hash table may not
2064
 * be modified while iterating over it (you can't add/remove
2065
 * items). To remove all items matching a predicate, use
2066
 * g_hash_table_foreach_remove().
2067
 *
2068
 * The order in which g_hash_table_foreach() iterates over the keys/values in
2069
 * the hash table is not defined.
2070
 *
2071
 * See g_hash_table_find() for performance caveats for linear
2072
 * order searches in contrast to g_hash_table_lookup().
2073
 */
2074
void
2075
g_hash_table_foreach (GHashTable *hash_table,
2076
                      GHFunc      func,
2077
                      gpointer    user_data)
2078
213k
{
2079
213k
  gsize i;
2080
213k
#ifndef G_DISABLE_ASSERT
2081
213k
  gint version;
2082
213k
#endif
2083
2084
213k
  g_return_if_fail (hash_table != NULL);
2085
213k
  g_return_if_fail (func != NULL);
2086
2087
213k
#ifndef G_DISABLE_ASSERT
2088
213k
  version = hash_table->version;
2089
213k
#endif
2090
2091
3.17M
  for (i = 0; i < hash_table->size; i++)
2092
2.96M
    {
2093
2.96M
      guint node_hash = hash_table->hashes[i];
2094
2.96M
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2095
2.96M
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
2096
2097
2.96M
      if (HASH_IS_REAL (node_hash))
2098
1.00M
        (* func) (node_key, node_value, user_data);
2099
2100
2.96M
#ifndef G_DISABLE_ASSERT
2101
2.96M
      g_return_if_fail (version == hash_table->version);
2102
2.96M
#endif
2103
2.96M
    }
2104
213k
}
2105
2106
/**
2107
 * g_hash_table_find:
2108
 * @hash_table: a #GHashTable
2109
 * @predicate: function to test the key/value pairs for a certain property
2110
 * @user_data: user data to pass to the function
2111
 *
2112
 * Calls the given function for key/value pairs in the #GHashTable
2113
 * until @predicate returns %TRUE. The function is passed the key
2114
 * and value of each pair, and the given @user_data parameter. The
2115
 * hash table may not be modified while iterating over it (you can't
2116
 * add/remove items).
2117
 *
2118
 * Note, that hash tables are really only optimized for forward
2119
 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
2120
 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
2121
 * once per every entry in a hash table) should probably be reworked
2122
 * to use additional or different data structures for reverse lookups
2123
 * (keep in mind that an O(n) find/foreach operation issued for all n
2124
 * values in a hash table ends up needing O(n*n) operations).
2125
 *
2126
 * Returns: (nullable): The value of the first key/value pair is returned,
2127
 *     for which @predicate evaluates to %TRUE. If no pair with the
2128
 *     requested property is found, %NULL is returned.
2129
 *
2130
 * Since: 2.4
2131
 */
2132
gpointer
2133
g_hash_table_find (GHashTable *hash_table,
2134
                   GHRFunc     predicate,
2135
                   gpointer    user_data)
2136
0
{
2137
0
  gsize i;
2138
0
#ifndef G_DISABLE_ASSERT
2139
0
  gint version;
2140
0
#endif
2141
0
  gboolean match;
2142
2143
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2144
0
  g_return_val_if_fail (predicate != NULL, NULL);
2145
2146
0
#ifndef G_DISABLE_ASSERT
2147
0
  version = hash_table->version;
2148
0
#endif
2149
2150
0
  match = FALSE;
2151
2152
0
  for (i = 0; i < hash_table->size; i++)
2153
0
    {
2154
0
      guint node_hash = hash_table->hashes[i];
2155
0
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2156
0
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
2157
2158
0
      if (HASH_IS_REAL (node_hash))
2159
0
        match = predicate (node_key, node_value, user_data);
2160
2161
0
#ifndef G_DISABLE_ASSERT
2162
0
      g_return_val_if_fail (version == hash_table->version, NULL);
2163
0
#endif
2164
2165
0
      if (match)
2166
0
        return node_value;
2167
0
    }
2168
2169
0
  return NULL;
2170
0
}
2171
2172
/**
2173
 * g_hash_table_size:
2174
 * @hash_table: a #GHashTable
2175
 *
2176
 * Returns the number of elements contained in the #GHashTable.
2177
 *
2178
 * Returns: the number of key/value pairs in the #GHashTable.
2179
 */
2180
guint
2181
g_hash_table_size (GHashTable *hash_table)
2182
52.8k
{
2183
52.8k
  g_return_val_if_fail (hash_table != NULL, 0);
2184
2185
52.8k
  return hash_table->nnodes;
2186
52.8k
}
2187
2188
/**
2189
 * g_hash_table_get_keys:
2190
 * @hash_table: a #GHashTable
2191
 *
2192
 * Retrieves every key inside @hash_table. The returned data is valid
2193
 * until changes to the hash release those keys.
2194
 *
2195
 * This iterates over every entry in the hash table to build its return value.
2196
 * To iterate over the entries in a #GHashTable more efficiently, use a
2197
 * #GHashTableIter.
2198
 *
2199
 * Returns: (transfer container): a #GList containing all the keys
2200
 *     inside the hash table. The content of the list is owned by the
2201
 *     hash table and should not be modified or freed. Use g_list_free()
2202
 *     when done using the list.
2203
 *
2204
 * Since: 2.14
2205
 */
2206
GList *
2207
g_hash_table_get_keys (GHashTable *hash_table)
2208
0
{
2209
0
  gsize i;
2210
0
  GList *retval;
2211
2212
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2213
2214
0
  retval = NULL;
2215
0
  for (i = 0; i < hash_table->size; i++)
2216
0
    {
2217
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2218
0
        retval = g_list_prepend (retval, g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys));
2219
0
    }
2220
2221
0
  return retval;
2222
0
}
2223
2224
/**
2225
 * g_hash_table_get_keys_as_array:
2226
 * @hash_table: a #GHashTable
2227
 * @length: (out) (optional): the length of the returned array
2228
 *
2229
 * Retrieves every key inside @hash_table, as an array.
2230
 *
2231
 * The returned array is %NULL-terminated but may contain %NULL as a
2232
 * key.  Use @length to determine the true length if it's possible that
2233
 * %NULL was used as the value for a key.
2234
 *
2235
 * Note: in the common case of a string-keyed #GHashTable, the return
2236
 * value of this function can be conveniently cast to (const gchar **).
2237
 *
2238
 * This iterates over every entry in the hash table to build its return value.
2239
 * To iterate over the entries in a #GHashTable more efficiently, use a
2240
 * #GHashTableIter.
2241
 *
2242
 * You should always free the return result with g_free().  In the
2243
 * above-mentioned case of a string-keyed hash table, it may be
2244
 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
2245
 * first to transfer ownership of the keys.
2246
 *
2247
 * Returns: (array length=length) (transfer container): a
2248
 *   %NULL-terminated array containing each key from the table.
2249
 *
2250
 * Since: 2.40
2251
 **/
2252
gpointer *
2253
g_hash_table_get_keys_as_array (GHashTable *hash_table,
2254
                                guint      *length)
2255
0
{
2256
0
  gpointer *result;
2257
0
  gsize i, j = 0;
2258
2259
0
  result = g_new (gpointer, hash_table->nnodes + 1);
2260
0
  for (i = 0; i < hash_table->size; i++)
2261
0
    {
2262
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2263
0
        result[j++] = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2264
0
    }
2265
0
  g_assert_cmpint (j, ==, hash_table->nnodes);
2266
0
  result[j] = NULL;
2267
2268
0
  if (length)
2269
0
    *length = j;
2270
2271
0
  return result;
2272
0
}
2273
2274
/**
2275
 * g_hash_table_get_values:
2276
 * @hash_table: a #GHashTable
2277
 *
2278
 * Retrieves every value inside @hash_table. The returned data
2279
 * is valid until @hash_table is modified.
2280
 *
2281
 * This iterates over every entry in the hash table to build its return value.
2282
 * To iterate over the entries in a #GHashTable more efficiently, use a
2283
 * #GHashTableIter.
2284
 *
2285
 * Returns: (transfer container): a #GList containing all the values
2286
 *     inside the hash table. The content of the list is owned by the
2287
 *     hash table and should not be modified or freed. Use g_list_free()
2288
 *     when done using the list.
2289
 *
2290
 * Since: 2.14
2291
 */
2292
GList *
2293
g_hash_table_get_values (GHashTable *hash_table)
2294
0
{
2295
0
  gsize i;
2296
0
  GList *retval;
2297
2298
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2299
2300
0
  retval = NULL;
2301
0
  for (i = 0; i < hash_table->size; i++)
2302
0
    {
2303
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2304
0
        retval = g_list_prepend (retval, g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values));
2305
0
    }
2306
2307
0
  return retval;
2308
0
}
2309
2310
/* Hash functions.
2311
 */
2312
2313
/**
2314
 * g_str_equal:
2315
 * @v1: (not nullable): a key
2316
 * @v2: (not nullable): a key to compare with @v1
2317
 *
2318
 * Compares two strings for byte-by-byte equality and returns %TRUE
2319
 * if they are equal. It can be passed to g_hash_table_new() as the
2320
 * @key_equal_func parameter, when using non-%NULL strings as keys in a
2321
 * #GHashTable.
2322
 *
2323
 * This function is typically used for hash table comparisons, but can be used
2324
 * for general purpose comparisons of non-%NULL strings. For a %NULL-safe string
2325
 * comparison function, see g_strcmp0().
2326
 *
2327
 * Returns: %TRUE if the two keys match
2328
 */
2329
gboolean
2330
(g_str_equal) (gconstpointer v1,
2331
               gconstpointer v2)
2332
998M
{
2333
998M
  const gchar *string1 = v1;
2334
998M
  const gchar *string2 = v2;
2335
2336
998M
  return strcmp (string1, string2) == 0;
2337
998M
}
2338
2339
/**
2340
 * g_str_hash:
2341
 * @v: (not nullable): a string key
2342
 *
2343
 * Converts a string to a hash value.
2344
 *
2345
 * This function implements the widely used "djb" hash apparently
2346
 * posted by Daniel Bernstein to comp.lang.c some time ago.  The 32
2347
 * bit unsigned hash value starts at 5381 and for each byte 'c' in
2348
 * the string, is updated: `hash = hash * 33 + c`. This function
2349
 * uses the signed value of each byte.
2350
 *
2351
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2352
 * when using non-%NULL strings as keys in a #GHashTable.
2353
 *
2354
 * Note that this function may not be a perfect fit for all use cases.
2355
 * For example, it produces some hash collisions with strings as short
2356
 * as 2.
2357
 *
2358
 * Returns: a hash value corresponding to the key
2359
 */
2360
guint
2361
g_str_hash (gconstpointer v)
2362
1.00G
{
2363
1.00G
  const signed char *p;
2364
1.00G
  guint32 h = 5381;
2365
2366
13.2G
  for (p = v; *p != '\0'; p++)
2367
12.2G
    h = (h << 5) + h + *p;
2368
2369
1.00G
  return h;
2370
1.00G
}
2371
2372
/**
2373
 * g_direct_hash:
2374
 * @v: (nullable): a #gpointer key
2375
 *
2376
 * Converts a gpointer to a hash value.
2377
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2378
 * when using opaque pointers compared by pointer value as keys in a
2379
 * #GHashTable.
2380
 *
2381
 * This hash function is also appropriate for keys that are integers
2382
 * stored in pointers, such as `GINT_TO_POINTER (n)`.
2383
 *
2384
 * Returns: a hash value corresponding to the key.
2385
 */
2386
guint
2387
g_direct_hash (gconstpointer v)
2388
816M
{
2389
816M
  return GPOINTER_TO_UINT (v);
2390
816M
}
2391
2392
/**
2393
 * g_direct_equal:
2394
 * @v1: (nullable): a key
2395
 * @v2: (nullable): a key to compare with @v1
2396
 *
2397
 * Compares two #gpointer arguments and returns %TRUE if they are equal.
2398
 * It can be passed to g_hash_table_new() as the @key_equal_func
2399
 * parameter, when using opaque pointers compared by pointer value as
2400
 * keys in a #GHashTable.
2401
 *
2402
 * This equality function is also appropriate for keys that are integers
2403
 * stored in pointers, such as `GINT_TO_POINTER (n)`.
2404
 *
2405
 * Returns: %TRUE if the two keys match.
2406
 */
2407
gboolean
2408
g_direct_equal (gconstpointer v1,
2409
                gconstpointer v2)
2410
416M
{
2411
416M
  return v1 == v2;
2412
416M
}
2413
2414
/**
2415
 * g_int_equal:
2416
 * @v1: (not nullable): a pointer to a #gint key
2417
 * @v2: (not nullable): a pointer to a #gint key to compare with @v1
2418
 *
2419
 * Compares the two #gint values being pointed to and returns
2420
 * %TRUE if they are equal.
2421
 * It can be passed to g_hash_table_new() as the @key_equal_func
2422
 * parameter, when using non-%NULL pointers to integers as keys in a
2423
 * #GHashTable.
2424
 *
2425
 * Note that this function acts on pointers to #gint, not on #gint
2426
 * directly: if your hash table's keys are of the form
2427
 * `GINT_TO_POINTER (n)`, use g_direct_equal() instead.
2428
 *
2429
 * Returns: %TRUE if the two keys match.
2430
 */
2431
gboolean
2432
g_int_equal (gconstpointer v1,
2433
             gconstpointer v2)
2434
0
{
2435
0
  return *((const gint*) v1) == *((const gint*) v2);
2436
0
}
2437
2438
/**
2439
 * g_int_hash:
2440
 * @v: (not nullable): a pointer to a #gint key
2441
 *
2442
 * Converts a pointer to a #gint to a hash value.
2443
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2444
 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
2445
 *
2446
 * Note that this function acts on pointers to #gint, not on #gint
2447
 * directly: if your hash table's keys are of the form
2448
 * `GINT_TO_POINTER (n)`, use g_direct_hash() instead.
2449
 *
2450
 * Returns: a hash value corresponding to the key.
2451
 */
2452
guint
2453
g_int_hash (gconstpointer v)
2454
0
{
2455
0
  return *(const gint*) v;
2456
0
}
2457
2458
/**
2459
 * g_int64_equal:
2460
 * @v1: (not nullable): a pointer to a #gint64 key
2461
 * @v2: (not nullable): a pointer to a #gint64 key to compare with @v1
2462
 *
2463
 * Compares the two #gint64 values being pointed to and returns
2464
 * %TRUE if they are equal.
2465
 * It can be passed to g_hash_table_new() as the @key_equal_func
2466
 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
2467
 * #GHashTable.
2468
 *
2469
 * Returns: %TRUE if the two keys match.
2470
 *
2471
 * Since: 2.22
2472
 */
2473
gboolean
2474
g_int64_equal (gconstpointer v1,
2475
               gconstpointer v2)
2476
0
{
2477
0
  return *((const gint64*) v1) == *((const gint64*) v2);
2478
0
}
2479
2480
/**
2481
 * g_int64_hash:
2482
 * @v: (not nullable): a pointer to a #gint64 key
2483
 *
2484
 * Converts a pointer to a #gint64 to a hash value.
2485
 *
2486
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2487
 * when using non-%NULL pointers to 64-bit integer values as keys in a
2488
 * #GHashTable.
2489
 *
2490
 * Returns: a hash value corresponding to the key.
2491
 *
2492
 * Since: 2.22
2493
 */
2494
guint
2495
g_int64_hash (gconstpointer v)
2496
0
{
2497
0
  return (guint) *(const gint64*) v;
2498
0
}
2499
2500
/**
2501
 * g_double_equal:
2502
 * @v1: (not nullable): a pointer to a #gdouble key
2503
 * @v2: (not nullable): a pointer to a #gdouble key to compare with @v1
2504
 *
2505
 * Compares the two #gdouble values being pointed to and returns
2506
 * %TRUE if they are equal.
2507
 * It can be passed to g_hash_table_new() as the @key_equal_func
2508
 * parameter, when using non-%NULL pointers to doubles as keys in a
2509
 * #GHashTable.
2510
 *
2511
 * Returns: %TRUE if the two keys match.
2512
 *
2513
 * Since: 2.22
2514
 */
2515
gboolean
2516
g_double_equal (gconstpointer v1,
2517
                gconstpointer v2)
2518
0
{
2519
0
  return *((const gdouble*) v1) == *((const gdouble*) v2);
2520
0
}
2521
2522
/**
2523
 * g_double_hash:
2524
 * @v: (not nullable): a pointer to a #gdouble key
2525
 *
2526
 * Converts a pointer to a #gdouble to a hash value.
2527
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2528
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2529
 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
2530
 *
2531
 * Returns: a hash value corresponding to the key.
2532
 *
2533
 * Since: 2.22
2534
 */
2535
guint
2536
g_double_hash (gconstpointer v)
2537
0
{
2538
0
  return (guint) *(const gdouble*) v;
2539
0
}