Coverage Report

Created: 2025-07-17 06:56

/src/rauc/subprojects/glib-2.76.5/glib/ghash.c
Line
Count
Source (jump to first uncovered line)
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
156k
#define HASH_TABLE_MIN_SHIFT 3  /* 1 << 3 == 8 buckets */
239
240
1.65M
#define UNUSED_HASH_VALUE 0
241
544k
#define TOMBSTONE_HASH_VALUE 1
242
1.45M
#define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
243
966k
#define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
244
1.23M
#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
85.1k
#define BIG_ENTRY_SIZE (SIZEOF_VOID_P)
252
107k
#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
  guint            nnodes;
264
  guint            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
60.9k
{
345
60.9k
  hash_table->size = 1 << shift;
346
60.9k
  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
60.9k
  g_assert ((hash_table->size & (hash_table->size - 1)) == 0);
353
60.9k
  hash_table->mask = hash_table->size - 1;
354
60.9k
}
355
356
static gint
357
g_hash_table_find_closest_shift (gint n)
358
7.32k
{
359
7.32k
  gint i;
360
361
34.4k
  for (i = 0; n; i++)
362
27.1k
    n >>= 1;
363
364
7.32k
  return i;
365
7.32k
}
366
367
static void
368
g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
369
7.32k
{
370
7.32k
  gint shift;
371
372
7.32k
  shift = g_hash_table_find_closest_shift (size);
373
7.32k
  shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
374
375
7.32k
  g_hash_table_set_shift (hash_table, shift);
376
7.32k
}
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
68.2k
{
381
68.2k
#ifdef USE_SMALL_ARRAYS
382
68.2k
  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
68.2k
}
387
388
static inline gpointer
389
g_hash_table_fetch_key_or_value (gpointer a, guint index, gboolean is_big)
390
1.12M
{
391
#ifndef USE_SMALL_ARRAYS
392
  is_big = TRUE;
393
#endif
394
1.12M
  return is_big ? *(((gpointer *) a) + index) : GUINT_TO_POINTER (*(((guint *) a) + index));
395
1.12M
}
396
397
static inline void
398
g_hash_table_assign_key_or_value (gpointer a, guint index, gboolean is_big, gpointer v)
399
783k
{
400
#ifndef USE_SMALL_ARRAYS
401
  is_big = TRUE;
402
#endif
403
783k
  if (is_big)
404
783k
    *(((gpointer *) a) + index) = v;
405
658
  else
406
658
    *(((guint *) a) + index) = GPOINTER_TO_UINT (v);
407
783k
}
408
409
static inline gpointer
410
g_hash_table_evict_key_or_value (gpointer a, guint index, gboolean is_big, gpointer v)
411
217k
{
412
#ifndef USE_SMALL_ARRAYS
413
  is_big = TRUE;
414
#endif
415
217k
  if (is_big)
416
217k
    {
417
217k
      gpointer r = *(((gpointer *) a) + index);
418
217k
      *(((gpointer *) a) + index) = v;
419
217k
      return r;
420
217k
    }
421
397
  else
422
397
    {
423
397
      gpointer r = GUINT_TO_POINTER (*(((guint *) a) + index));
424
397
      *(((guint *) a) + index) = GPOINTER_TO_UINT (v);
425
397
      return r;
426
397
    }
427
217k
}
428
429
static inline guint
430
g_hash_table_hash_to_index (GHashTable *hash_table, guint hash)
431
980k
{
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
980k
  return (hash * 11) % hash_table->mod;
437
980k
}
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
871k
{
467
871k
  guint node_index;
468
871k
  guint node_hash;
469
871k
  guint hash_value;
470
871k
  guint first_tombstone = 0;
471
871k
  gboolean have_tombstone = FALSE;
472
871k
  guint step = 0;
473
474
871k
  hash_value = hash_table->hash_func (key);
475
871k
  if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
476
2.75k
    hash_value = 2;
477
478
871k
  *hash_return = hash_value;
479
480
871k
  node_index = g_hash_table_hash_to_index (hash_table, hash_value);
481
871k
  node_hash = hash_table->hashes[node_index];
482
483
1.36M
  while (!HASH_IS_UNUSED (node_hash))
484
934k
    {
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
934k
      if (node_hash == hash_value)
490
450k
        {
491
450k
          gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
492
493
450k
          if (hash_table->key_equal_func)
494
450k
            {
495
450k
              if (hash_table->key_equal_func (node_key, key))
496
444k
                return node_index;
497
450k
            }
498
10
          else if (node_key == key)
499
10
            {
500
10
              return node_index;
501
10
            }
502
450k
        }
503
483k
      else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
504
44.1k
        {
505
44.1k
          first_tombstone = node_index;
506
44.1k
          have_tombstone = TRUE;
507
44.1k
        }
508
509
489k
      step++;
510
489k
      node_index += step;
511
489k
      node_index &= hash_table->mask;
512
489k
      node_hash = hash_table->hashes[node_index];
513
489k
    }
514
515
427k
  if (have_tombstone)
516
10.7k
    return first_tombstone;
517
518
416k
  return node_index;
519
427k
}
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
61.6k
{
538
61.6k
  gpointer key;
539
61.6k
  gpointer value;
540
541
61.6k
  key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
542
61.6k
  value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
543
544
  /* Erect tombstone */
545
61.6k
  hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
546
547
  /* Be GC friendly */
548
61.6k
  g_hash_table_assign_key_or_value (hash_table->keys, i, hash_table->have_big_keys, NULL);
549
61.6k
  g_hash_table_assign_key_or_value (hash_table->values, i, hash_table->have_big_values, NULL);
550
551
61.6k
  g_assert (hash_table->nnodes > 0);
552
61.6k
  hash_table->nnodes--;
553
554
61.6k
  if (notify && hash_table->key_destroy_func)
555
0
    hash_table->key_destroy_func (key);
556
557
61.6k
  if (notify && hash_table->value_destroy_func)
558
0
    hash_table->value_destroy_func (value);
559
560
61.6k
}
561
562
/*
563
 * g_hash_table_setup_storage:
564
 * @hash_table: our #GHashTable
565
 *
566
 * Initialise the hash table size, mask, mod, and arrays.
567
 */
568
static void
569
g_hash_table_setup_storage (GHashTable *hash_table)
570
53.5k
{
571
53.5k
  gboolean small = FALSE;
572
573
  /* We want to use small arrays only if:
574
   *   - we are running on a system where that makes sense (64 bit); and
575
   *   - we are not running under valgrind.
576
   */
577
578
53.5k
#ifdef USE_SMALL_ARRAYS
579
53.5k
  small = TRUE;
580
581
53.5k
# ifdef ENABLE_VALGRIND
582
53.5k
  if (RUNNING_ON_VALGRIND)
583
0
    small = FALSE;
584
53.5k
# endif
585
53.5k
#endif
586
587
53.5k
  g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
588
589
53.5k
  hash_table->have_big_keys = !small;
590
53.5k
  hash_table->have_big_values = !small;
591
592
53.5k
  hash_table->keys   = g_hash_table_realloc_key_or_value_array (NULL, hash_table->size, hash_table->have_big_keys);
593
53.5k
  hash_table->values = hash_table->keys;
594
53.5k
  hash_table->hashes = g_new0 (guint, hash_table->size);
595
53.5k
}
596
597
/*
598
 * g_hash_table_remove_all_nodes:
599
 * @hash_table: our #GHashTable
600
 * @notify: %TRUE if the destroy notify handlers are to be called
601
 *
602
 * Removes all nodes from the table.
603
 *
604
 * If @notify is %TRUE then the destroy notify functions are called
605
 * for the key and value of the hash node.
606
 *
607
 * Since this may be a precursor to freeing the table entirely, we'd
608
 * ideally perform no resize, and we can indeed avoid that in some
609
 * cases.  However: in the case that we'll be making callbacks to user
610
 * code (via destroy notifies) we need to consider that the user code
611
 * might call back into the table again.  In this case, we setup a new
612
 * set of arrays so that any callers will see an empty (but valid)
613
 * table.
614
 */
615
static void
616
g_hash_table_remove_all_nodes (GHashTable *hash_table,
617
                               gboolean    notify,
618
                               gboolean    destruction)
619
103k
{
620
103k
  int i;
621
103k
  gpointer key;
622
103k
  gpointer value;
623
103k
  gint old_size;
624
103k
  gpointer *old_keys;
625
103k
  gpointer *old_values;
626
103k
  guint    *old_hashes;
627
103k
  gboolean  old_have_big_keys;
628
103k
  gboolean  old_have_big_values;
629
630
  /* If the hash table is already empty, there is nothing to be done. */
631
103k
  if (hash_table->nnodes == 0)
632
92.9k
    return;
633
634
10.4k
  hash_table->nnodes = 0;
635
10.4k
  hash_table->noccupied = 0;
636
637
  /* Easy case: no callbacks, so we just zero out the arrays */
638
10.4k
  if (!notify ||
639
10.4k
      (hash_table->key_destroy_func == NULL &&
640
10.4k
       hash_table->value_destroy_func == NULL))
641
8.47k
    {
642
8.47k
      if (!destruction)
643
8.47k
        {
644
8.47k
          memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
645
646
8.47k
#ifdef USE_SMALL_ARRAYS
647
8.47k
          memset (hash_table->keys, 0, hash_table->size * (hash_table->have_big_keys ? BIG_ENTRY_SIZE : SMALL_ENTRY_SIZE));
648
8.47k
          memset (hash_table->values, 0, hash_table->size * (hash_table->have_big_values ? BIG_ENTRY_SIZE : SMALL_ENTRY_SIZE));
649
#else
650
          memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
651
          memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
652
#endif
653
8.47k
        }
654
655
8.47k
      return;
656
8.47k
    }
657
658
  /* Hard case: we need to do user callbacks.  There are two
659
   * possibilities here:
660
   *
661
   *   1) there are no outstanding references on the table and therefore
662
   *   nobody should be calling into it again (destroying == true)
663
   *
664
   *   2) there are outstanding references, and there may be future
665
   *   calls into the table, either after we return, or from the destroy
666
   *   notifies that we're about to do (destroying == false)
667
   *
668
   * We handle both cases by taking the current state of the table into
669
   * local variables and replacing it with something else: in the "no
670
   * outstanding references" cases we replace it with a bunch of
671
   * null/zero values so that any access to the table will fail.  In the
672
   * "may receive future calls" case, we reinitialise the struct to
673
   * appear like a newly-created empty table.
674
   *
675
   * In both cases, we take over the references for the current state,
676
   * freeing them below.
677
   */
678
1.93k
  old_size = hash_table->size;
679
1.93k
  old_have_big_keys = hash_table->have_big_keys;
680
1.93k
  old_have_big_values = hash_table->have_big_values;
681
1.93k
  old_keys   = g_steal_pointer (&hash_table->keys);
682
1.93k
  old_values = g_steal_pointer (&hash_table->values);
683
1.93k
  old_hashes = g_steal_pointer (&hash_table->hashes);
684
685
1.93k
  if (!destruction)
686
    /* Any accesses will see an empty table */
687
1.82k
    g_hash_table_setup_storage (hash_table);
688
108
  else
689
    /* Will cause a quick crash on any attempted access */
690
108
    hash_table->size = hash_table->mod = hash_table->mask = 0;
691
692
  /* Now do the actual destroy notifies */
693
27.9k
  for (i = 0; i < old_size; i++)
694
26.0k
    {
695
26.0k
      if (HASH_IS_REAL (old_hashes[i]))
696
10.5k
        {
697
10.5k
          key = g_hash_table_fetch_key_or_value (old_keys, i, old_have_big_keys);
698
10.5k
          value = g_hash_table_fetch_key_or_value (old_values, i, old_have_big_values);
699
700
10.5k
          old_hashes[i] = UNUSED_HASH_VALUE;
701
702
10.5k
          g_hash_table_assign_key_or_value (old_keys, i, old_have_big_keys, NULL);
703
10.5k
          g_hash_table_assign_key_or_value (old_values, i, old_have_big_values, NULL);
704
705
10.5k
          if (hash_table->key_destroy_func != NULL)
706
10.5k
            hash_table->key_destroy_func (key);
707
708
10.5k
          if (hash_table->value_destroy_func != NULL)
709
10.5k
            hash_table->value_destroy_func (value);
710
10.5k
        }
711
26.0k
    }
712
713
  /* Destroy old storage space. */
714
1.93k
  if (old_keys != old_values)
715
1.93k
    g_free (old_values);
716
717
1.93k
  g_free (old_keys);
718
1.93k
  g_free (old_hashes);
719
1.93k
}
720
721
static void
722
realloc_arrays (GHashTable *hash_table, gboolean is_a_set)
723
7.32k
{
724
7.32k
  hash_table->hashes = g_renew (guint, hash_table->hashes, hash_table->size);
725
7.32k
  hash_table->keys = g_hash_table_realloc_key_or_value_array (hash_table->keys, hash_table->size, hash_table->have_big_keys);
726
727
7.32k
  if (is_a_set)
728
0
    hash_table->values = hash_table->keys;
729
7.32k
  else
730
7.32k
    hash_table->values = g_hash_table_realloc_key_or_value_array (hash_table->values, hash_table->size, hash_table->have_big_values);
731
7.32k
}
732
733
/* When resizing the table in place, we use a temporary bit array to keep
734
 * track of which entries have been assigned a proper location in the new
735
 * table layout.
736
 *
737
 * Each bit corresponds to a bucket. A bit is set if an entry was assigned
738
 * its corresponding location during the resize and thus should not be
739
 * evicted. The array starts out cleared to zero. */
740
741
static inline gboolean
742
get_status_bit (const guint32 *bitmap, guint index)
743
264k
{
744
264k
  return (bitmap[index / 32] >> (index % 32)) & 1;
745
264k
}
746
747
static inline void
748
set_status_bit (guint32 *bitmap, guint index)
749
108k
{
750
108k
  bitmap[index / 32] |= 1U << (index % 32);
751
108k
}
752
753
/* By calling dedicated resize functions for sets and maps, we avoid 2x
754
 * test-and-branch per key in the inner loop. This yields a small
755
 * performance improvement at the cost of a bit of macro gunk. */
756
757
#define DEFINE_RESIZE_FUNC(fname) \
758
7.32k
static void fname (GHashTable *hash_table, guint old_size, guint32 *reallocated_buckets_bitmap) \
759
7.32k
{                                                                       \
760
7.32k
  guint i;                                                              \
761
7.32k
                                                                        \
762
230k
  for (i = 0; i < old_size; i++)                                        \
763
223k
    {                                                                   \
764
223k
      guint node_hash = hash_table->hashes[i];                          \
765
223k
      gpointer key, value G_GNUC_UNUSED;                                \
766
223k
                                                                        \
767
223k
      if (!HASH_IS_REAL (node_hash))                                    \
768
223k
        {                                                               \
769
108k
          /* Clear tombstones */                                        \
770
108k
          hash_table->hashes[i] = UNUSED_HASH_VALUE;                    \
771
108k
          continue;                                                     \
772
108k
        }                                                               \
773
223k
                                                                        \
774
223k
      /* Skip entries relocated through eviction */                     \
775
223k
      if (get_status_bit (reallocated_buckets_bitmap, i))               \
776
114k
        continue;                                                       \
777
114k
                                                                        \
778
114k
      hash_table->hashes[i] = UNUSED_HASH_VALUE;                        \
779
81.0k
      EVICT_KEYVAL (hash_table, i, NULL, NULL, key, value);             \
780
81.0k
                                                                        \
781
81.0k
      for (;;)                                                          \
782
108k
        {                                                               \
783
108k
          guint hash_val;                                               \
784
108k
          guint replaced_hash;                                          \
785
108k
          guint step = 0;                                               \
786
108k
                                                                        \
787
108k
          hash_val = g_hash_table_hash_to_index (hash_table, node_hash); \
788
108k
                                                                        \
789
149k
          while (get_status_bit (reallocated_buckets_bitmap, hash_val)) \
790
108k
            {                                                           \
791
40.6k
              step++;                                                   \
792
40.6k
              hash_val += step;                                         \
793
40.6k
              hash_val &= hash_table->mask;                             \
794
40.6k
            }                                                           \
795
108k
                                                                        \
796
108k
          set_status_bit (reallocated_buckets_bitmap, hash_val);        \
797
108k
                                                                        \
798
108k
          replaced_hash = hash_table->hashes[hash_val];                 \
799
108k
          hash_table->hashes[hash_val] = node_hash;                     \
800
108k
          if (!HASH_IS_REAL (replaced_hash))                            \
801
108k
            {                                                           \
802
81.0k
              ASSIGN_KEYVAL (hash_table, hash_val, key, value);         \
803
81.0k
              break;                                                    \
804
81.0k
            }                                                           \
805
108k
                                                                        \
806
108k
          node_hash = replaced_hash;                                    \
807
27.8k
          EVICT_KEYVAL (hash_table, hash_val, key, value, key, value);  \
808
27.8k
        }                                                               \
809
81.0k
    }                                                                   \
810
7.32k
}
Unexecuted instantiation: ghash.c:resize_set
ghash.c:resize_map
Line
Count
Source
758
7.32k
static void fname (GHashTable *hash_table, guint old_size, guint32 *reallocated_buckets_bitmap) \
759
7.32k
{                                                                       \
760
7.32k
  guint i;                                                              \
761
7.32k
                                                                        \
762
230k
  for (i = 0; i < old_size; i++)                                        \
763
223k
    {                                                                   \
764
223k
      guint node_hash = hash_table->hashes[i];                          \
765
223k
      gpointer key, value G_GNUC_UNUSED;                                \
766
223k
                                                                        \
767
223k
      if (!HASH_IS_REAL (node_hash))                                    \
768
223k
        {                                                               \
769
108k
          /* Clear tombstones */                                        \
770
108k
          hash_table->hashes[i] = UNUSED_HASH_VALUE;                    \
771
108k
          continue;                                                     \
772
108k
        }                                                               \
773
223k
                                                                        \
774
223k
      /* Skip entries relocated through eviction */                     \
775
223k
      if (get_status_bit (reallocated_buckets_bitmap, i))               \
776
114k
        continue;                                                       \
777
114k
                                                                        \
778
114k
      hash_table->hashes[i] = UNUSED_HASH_VALUE;                        \
779
81.0k
      EVICT_KEYVAL (hash_table, i, NULL, NULL, key, value);             \
780
81.0k
                                                                        \
781
81.0k
      for (;;)                                                          \
782
108k
        {                                                               \
783
108k
          guint hash_val;                                               \
784
108k
          guint replaced_hash;                                          \
785
108k
          guint step = 0;                                               \
786
108k
                                                                        \
787
108k
          hash_val = g_hash_table_hash_to_index (hash_table, node_hash); \
788
108k
                                                                        \
789
149k
          while (get_status_bit (reallocated_buckets_bitmap, hash_val)) \
790
108k
            {                                                           \
791
40.6k
              step++;                                                   \
792
40.6k
              hash_val += step;                                         \
793
40.6k
              hash_val &= hash_table->mask;                             \
794
40.6k
            }                                                           \
795
108k
                                                                        \
796
108k
          set_status_bit (reallocated_buckets_bitmap, hash_val);        \
797
108k
                                                                        \
798
108k
          replaced_hash = hash_table->hashes[hash_val];                 \
799
108k
          hash_table->hashes[hash_val] = node_hash;                     \
800
108k
          if (!HASH_IS_REAL (replaced_hash))                            \
801
108k
            {                                                           \
802
81.0k
              ASSIGN_KEYVAL (hash_table, hash_val, key, value);         \
803
81.0k
              break;                                                    \
804
81.0k
            }                                                           \
805
108k
                                                                        \
806
108k
          node_hash = replaced_hash;                                    \
807
27.8k
          EVICT_KEYVAL (hash_table, hash_val, key, value, key, value);  \
808
27.8k
        }                                                               \
809
81.0k
    }                                                                   \
810
7.32k
}
811
812
81.0k
#define ASSIGN_KEYVAL(ht, index, key, value) G_STMT_START{ \
813
81.0k
    g_hash_table_assign_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
814
81.0k
    g_hash_table_assign_key_or_value ((ht)->values, (index), (ht)->have_big_values, (value)); \
815
81.0k
  }G_STMT_END
816
817
108k
#define EVICT_KEYVAL(ht, index, key, value, outkey, outvalue) G_STMT_START{ \
818
108k
    (outkey) = g_hash_table_evict_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
819
108k
    (outvalue) = g_hash_table_evict_key_or_value ((ht)->values, (index), (ht)->have_big_values, (value)); \
820
108k
  }G_STMT_END
821
822
DEFINE_RESIZE_FUNC (resize_map)
823
824
#undef ASSIGN_KEYVAL
825
#undef EVICT_KEYVAL
826
827
0
#define ASSIGN_KEYVAL(ht, index, key, value) G_STMT_START{ \
828
0
    g_hash_table_assign_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
829
0
  }G_STMT_END
830
831
0
#define EVICT_KEYVAL(ht, index, key, value, outkey, outvalue) G_STMT_START{ \
832
0
    (outkey) = g_hash_table_evict_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
833
0
  }G_STMT_END
834
835
DEFINE_RESIZE_FUNC (resize_set)
836
837
#undef ASSIGN_KEYVAL
838
#undef EVICT_KEYVAL
839
840
/*
841
 * g_hash_table_resize:
842
 * @hash_table: our #GHashTable
843
 *
844
 * Resizes the hash table to the optimal size based on the number of
845
 * nodes currently held. If you call this function then a resize will
846
 * occur, even if one does not need to occur.
847
 * Use g_hash_table_maybe_resize() instead.
848
 *
849
 * This function may "resize" the hash table to its current size, with
850
 * the side effect of cleaning up tombstones and otherwise optimizing
851
 * the probe sequences.
852
 */
853
static void
854
g_hash_table_resize (GHashTable *hash_table)
855
7.32k
{
856
7.32k
  guint32 *reallocated_buckets_bitmap;
857
7.32k
  gsize old_size;
858
7.32k
  gboolean is_a_set;
859
860
7.32k
  old_size = hash_table->size;
861
7.32k
  is_a_set = hash_table->keys == hash_table->values;
862
863
  /* The outer checks in g_hash_table_maybe_resize() will only consider
864
   * cleanup/resize when the load factor goes below .25 (1/4, ignoring
865
   * tombstones) or above .9375 (15/16, including tombstones).
866
   *
867
   * Once this happens, tombstones will always be cleaned out. If our
868
   * load sans tombstones is greater than .75 (1/1.333, see below), we'll
869
   * take this opportunity to grow the table too.
870
   *
871
   * Immediately after growing, the load factor will be in the range
872
   * .375 .. .469. After shrinking, it will be exactly .5. */
873
874
7.32k
  g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 1.333);
875
876
7.32k
  if (hash_table->size > old_size)
877
4.14k
    {
878
4.14k
      realloc_arrays (hash_table, is_a_set);
879
4.14k
      memset (&hash_table->hashes[old_size], 0, (hash_table->size - old_size) * sizeof (guint));
880
881
4.14k
      reallocated_buckets_bitmap = g_new0 (guint32, (hash_table->size + 31) / 32);
882
4.14k
    }
883
3.17k
  else
884
3.17k
    {
885
3.17k
      reallocated_buckets_bitmap = g_new0 (guint32, (old_size + 31) / 32);
886
3.17k
    }
887
888
7.32k
  if (is_a_set)
889
0
    resize_set (hash_table, old_size, reallocated_buckets_bitmap);
890
7.32k
  else
891
7.32k
    resize_map (hash_table, old_size, reallocated_buckets_bitmap);
892
893
7.32k
  g_free (reallocated_buckets_bitmap);
894
895
7.32k
  if (hash_table->size < old_size)
896
3.17k
    realloc_arrays (hash_table, is_a_set);
897
898
7.32k
  hash_table->noccupied = hash_table->nnodes;
899
7.32k
}
900
901
/*
902
 * g_hash_table_maybe_resize:
903
 * @hash_table: our #GHashTable
904
 *
905
 * Resizes the hash table, if needed.
906
 *
907
 * Essentially, calls g_hash_table_resize() if the table has strayed
908
 * too far from its ideal size for its number of nodes.
909
 */
910
static inline void
911
g_hash_table_maybe_resize (GHashTable *hash_table)
912
209k
{
913
209k
  gsize noccupied = hash_table->noccupied;
914
209k
  gsize size = hash_table->size;
915
916
209k
  if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
917
209k
      (size <= noccupied + (noccupied / 16)))
918
7.32k
    g_hash_table_resize (hash_table);
919
209k
}
920
921
#ifdef USE_SMALL_ARRAYS
922
923
static inline gboolean
924
entry_is_big (gpointer v)
925
53.5k
{
926
53.5k
  return (((guintptr) v) >> ((BIG_ENTRY_SIZE - SMALL_ENTRY_SIZE) * 8)) != 0;
927
53.5k
}
928
929
static inline gboolean
930
g_hash_table_maybe_make_big_keys_or_values (gpointer *a_p, gpointer v, gint ht_size)
931
53.5k
{
932
53.5k
  if (entry_is_big (v))
933
53.1k
    {
934
53.1k
      guint *a = (guint *) *a_p;
935
53.1k
      gpointer *a_new;
936
53.1k
      gint i;
937
938
53.1k
      a_new = g_new (gpointer, ht_size);
939
940
478k
      for (i = 0; i < ht_size; i++)
941
425k
        {
942
425k
          a_new[i] = GUINT_TO_POINTER (a[i]);
943
425k
        }
944
945
53.1k
      g_free (a);
946
53.1k
      *a_p = a_new;
947
53.1k
      return TRUE;
948
53.1k
    }
949
950
370
  return FALSE;
951
53.5k
}
952
953
#endif
954
955
static inline void
956
g_hash_table_ensure_keyval_fits (GHashTable *hash_table, gpointer key, gpointer value)
957
238k
{
958
238k
  gboolean is_a_set = (hash_table->keys == hash_table->values);
959
960
238k
#ifdef USE_SMALL_ARRAYS
961
962
  /* Convert from set to map? */
963
238k
  if (is_a_set)
964
26.6k
    {
965
26.6k
      if (hash_table->have_big_keys)
966
0
        {
967
0
          if (key != value)
968
0
            hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
969
          /* Keys and values are both big now, so no need for further checks */
970
0
          return;
971
0
        }
972
26.6k
      else
973
26.6k
        {
974
26.6k
          if (key != value)
975
26.6k
            {
976
26.6k
              hash_table->values = g_memdup2 (hash_table->keys, sizeof (guint) * hash_table->size);
977
26.6k
              is_a_set = FALSE;
978
26.6k
            }
979
26.6k
        }
980
26.6k
    }
981
982
  /* Make keys big? */
983
238k
  if (!hash_table->have_big_keys)
984
26.6k
    {
985
26.6k
      hash_table->have_big_keys = g_hash_table_maybe_make_big_keys_or_values (&hash_table->keys, key, hash_table->size);
986
987
26.6k
      if (is_a_set)
988
0
        {
989
0
          hash_table->values = hash_table->keys;
990
0
          hash_table->have_big_values = hash_table->have_big_keys;
991
0
        }
992
26.6k
    }
993
994
  /* Make values big? */
995
238k
  if (!is_a_set && !hash_table->have_big_values)
996
26.9k
    {
997
26.9k
      hash_table->have_big_values = g_hash_table_maybe_make_big_keys_or_values (&hash_table->values, value, hash_table->size);
998
26.9k
    }
999
1000
#else
1001
1002
  /* Just split if necessary */
1003
  if (is_a_set && key != value)
1004
    hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
1005
1006
#endif
1007
238k
}
1008
1009
/**
1010
 * g_hash_table_new:
1011
 * @hash_func: a function to create a hash value from a key
1012
 * @key_equal_func: a function to check two keys for equality
1013
 *
1014
 * Creates a new #GHashTable with a reference count of 1.
1015
 *
1016
 * Hash values returned by @hash_func are used to determine where keys
1017
 * are stored within the #GHashTable data structure. The g_direct_hash(),
1018
 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
1019
 * functions are provided for some common types of keys.
1020
 * If @hash_func is %NULL, g_direct_hash() is used.
1021
 *
1022
 * @key_equal_func is used when looking up keys in the #GHashTable.
1023
 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
1024
 * and g_str_equal() functions are provided for the most common types
1025
 * of keys. If @key_equal_func is %NULL, keys are compared directly in
1026
 * a similar fashion to g_direct_equal(), but without the overhead of
1027
 * a function call. @key_equal_func is called with the key from the hash table
1028
 * as its first parameter, and the user-provided key to check against as
1029
 * its second.
1030
 *
1031
 * Returns: a new #GHashTable
1032
 */
1033
GHashTable *
1034
g_hash_table_new (GHashFunc  hash_func,
1035
                  GEqualFunc key_equal_func)
1036
46.0k
{
1037
46.0k
  return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
1038
46.0k
}
1039
1040
1041
/**
1042
 * g_hash_table_new_full:
1043
 * @hash_func: a function to create a hash value from a key
1044
 * @key_equal_func: a function to check two keys for equality
1045
 * @key_destroy_func: (nullable): a function to free the memory allocated for the key
1046
 *     used when removing the entry from the #GHashTable, or %NULL
1047
 *     if you don't want to supply such a function.
1048
 * @value_destroy_func: (nullable): a function to free the memory allocated for the
1049
 *     value used when removing the entry from the #GHashTable, or %NULL
1050
 *     if you don't want to supply such a function.
1051
 *
1052
 * Creates a new #GHashTable like g_hash_table_new() with a reference
1053
 * count of 1 and allows to specify functions to free the memory
1054
 * allocated for the key and value that get called when removing the
1055
 * entry from the #GHashTable.
1056
 *
1057
 * Since version 2.42 it is permissible for destroy notify functions to
1058
 * recursively remove further items from the hash table. This is only
1059
 * permissible if the application still holds a reference to the hash table.
1060
 * This means that you may need to ensure that the hash table is empty by
1061
 * calling g_hash_table_remove_all() before releasing the last reference using
1062
 * g_hash_table_unref().
1063
 *
1064
 * Returns: a new #GHashTable
1065
 */
1066
GHashTable *
1067
g_hash_table_new_full (GHashFunc      hash_func,
1068
                       GEqualFunc     key_equal_func,
1069
                       GDestroyNotify key_destroy_func,
1070
                       GDestroyNotify value_destroy_func)
1071
51.7k
{
1072
51.7k
  GHashTable *hash_table;
1073
1074
51.7k
  hash_table = g_slice_new (GHashTable);
1075
51.7k
  g_atomic_ref_count_init (&hash_table->ref_count);
1076
51.7k
  hash_table->nnodes             = 0;
1077
51.7k
  hash_table->noccupied          = 0;
1078
51.7k
  hash_table->hash_func          = hash_func ? hash_func : g_direct_hash;
1079
51.7k
  hash_table->key_equal_func     = key_equal_func;
1080
51.7k
#ifndef G_DISABLE_ASSERT
1081
51.7k
  hash_table->version            = 0;
1082
51.7k
#endif
1083
51.7k
  hash_table->key_destroy_func   = key_destroy_func;
1084
51.7k
  hash_table->value_destroy_func = value_destroy_func;
1085
1086
51.7k
  g_hash_table_setup_storage (hash_table);
1087
1088
51.7k
  return hash_table;
1089
51.7k
}
1090
1091
/**
1092
 * g_hash_table_new_similar:
1093
 * @other_hash_table: (not nullable) (transfer none): Another #GHashTable
1094
 *
1095
 * Creates a new #GHashTable like g_hash_table_new_full() with a reference
1096
 * count of 1.
1097
 *
1098
 * It inherits the hash function, the key equal function, the key destroy function,
1099
 * as well as the value destroy function, from @other_hash_table.
1100
 *
1101
 * The returned hash table will be empty; it will not contain the keys
1102
 * or values from @other_hash_table.
1103
 *
1104
 * Returns: (transfer full) (not nullable): a new #GHashTable
1105
 * Since: 2.72
1106
 */
1107
GHashTable *
1108
g_hash_table_new_similar (GHashTable *other_hash_table)
1109
0
{
1110
0
  g_return_val_if_fail (other_hash_table, NULL);
1111
1112
0
  return g_hash_table_new_full (other_hash_table->hash_func,
1113
0
                                other_hash_table->key_equal_func,
1114
0
                                other_hash_table->key_destroy_func,
1115
0
                                other_hash_table->value_destroy_func);
1116
0
}
1117
1118
/**
1119
 * g_hash_table_iter_init:
1120
 * @iter: an uninitialized #GHashTableIter
1121
 * @hash_table: a #GHashTable
1122
 *
1123
 * Initializes a key/value pair iterator and associates it with
1124
 * @hash_table. Modifying the hash table after calling this function
1125
 * invalidates the returned iterator.
1126
 *
1127
 * The iteration order of a #GHashTableIter over the keys/values in a hash
1128
 * table is not defined.
1129
 *
1130
 * |[<!-- language="C" -->
1131
 * GHashTableIter iter;
1132
 * gpointer key, value;
1133
 *
1134
 * g_hash_table_iter_init (&iter, hash_table);
1135
 * while (g_hash_table_iter_next (&iter, &key, &value))
1136
 *   {
1137
 *     // do something with key and value
1138
 *   }
1139
 * ]|
1140
 *
1141
 * Since: 2.16
1142
 */
1143
void
1144
g_hash_table_iter_init (GHashTableIter *iter,
1145
                        GHashTable     *hash_table)
1146
0
{
1147
0
  RealIter *ri = (RealIter *) iter;
1148
1149
0
  g_return_if_fail (iter != NULL);
1150
0
  g_return_if_fail (hash_table != NULL);
1151
1152
0
  ri->hash_table = hash_table;
1153
0
  ri->position = -1;
1154
0
#ifndef G_DISABLE_ASSERT
1155
0
  ri->version = hash_table->version;
1156
0
#endif
1157
0
}
1158
1159
/**
1160
 * g_hash_table_iter_next:
1161
 * @iter: an initialized #GHashTableIter
1162
 * @key: (out) (optional): a location to store the key
1163
 * @value: (out) (optional) (nullable): a location to store the value
1164
 *
1165
 * Advances @iter and retrieves the key and/or value that are now
1166
 * pointed to as a result of this advancement. If %FALSE is returned,
1167
 * @key and @value are not set, and the iterator becomes invalid.
1168
 *
1169
 * Returns: %FALSE if the end of the #GHashTable has been reached.
1170
 *
1171
 * Since: 2.16
1172
 */
1173
gboolean
1174
g_hash_table_iter_next (GHashTableIter *iter,
1175
                        gpointer       *key,
1176
                        gpointer       *value)
1177
0
{
1178
0
  RealIter *ri = (RealIter *) iter;
1179
0
  gint position;
1180
1181
0
  g_return_val_if_fail (iter != NULL, FALSE);
1182
0
#ifndef G_DISABLE_ASSERT
1183
0
  g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
1184
0
#endif
1185
0
  g_return_val_if_fail (ri->position < (gssize) ri->hash_table->size, FALSE);
1186
1187
0
  position = ri->position;
1188
1189
0
  do
1190
0
    {
1191
0
      position++;
1192
0
      if (position >= (gssize) ri->hash_table->size)
1193
0
        {
1194
0
          ri->position = position;
1195
0
          return FALSE;
1196
0
        }
1197
0
    }
1198
0
  while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
1199
1200
0
  if (key != NULL)
1201
0
    *key = g_hash_table_fetch_key_or_value (ri->hash_table->keys, position, ri->hash_table->have_big_keys);
1202
0
  if (value != NULL)
1203
0
    *value = g_hash_table_fetch_key_or_value (ri->hash_table->values, position, ri->hash_table->have_big_values);
1204
1205
0
  ri->position = position;
1206
0
  return TRUE;
1207
0
}
1208
1209
/**
1210
 * g_hash_table_iter_get_hash_table:
1211
 * @iter: an initialized #GHashTableIter
1212
 *
1213
 * Returns the #GHashTable associated with @iter.
1214
 *
1215
 * Returns: the #GHashTable associated with @iter.
1216
 *
1217
 * Since: 2.16
1218
 */
1219
GHashTable *
1220
g_hash_table_iter_get_hash_table (GHashTableIter *iter)
1221
0
{
1222
0
  g_return_val_if_fail (iter != NULL, NULL);
1223
1224
0
  return ((RealIter *) iter)->hash_table;
1225
0
}
1226
1227
static void
1228
iter_remove_or_steal (RealIter *ri, gboolean notify)
1229
0
{
1230
0
  g_return_if_fail (ri != NULL);
1231
0
#ifndef G_DISABLE_ASSERT
1232
0
  g_return_if_fail (ri->version == ri->hash_table->version);
1233
0
#endif
1234
0
  g_return_if_fail (ri->position >= 0);
1235
0
  g_return_if_fail ((gsize) ri->position < ri->hash_table->size);
1236
1237
0
  g_hash_table_remove_node (ri->hash_table, ri->position, notify);
1238
1239
0
#ifndef G_DISABLE_ASSERT
1240
0
  ri->version++;
1241
0
  ri->hash_table->version++;
1242
0
#endif
1243
0
}
1244
1245
/**
1246
 * g_hash_table_iter_remove:
1247
 * @iter: an initialized #GHashTableIter
1248
 *
1249
 * Removes the key/value pair currently pointed to by the iterator
1250
 * from its associated #GHashTable. Can only be called after
1251
 * g_hash_table_iter_next() returned %TRUE, and cannot be called
1252
 * more than once for the same key/value pair.
1253
 *
1254
 * If the #GHashTable was created using g_hash_table_new_full(),
1255
 * the key and value are freed using the supplied destroy functions,
1256
 * otherwise you have to make sure that any dynamically allocated
1257
 * values are freed yourself.
1258
 *
1259
 * It is safe to continue iterating the #GHashTable afterward:
1260
 * |[<!-- language="C" -->
1261
 * while (g_hash_table_iter_next (&iter, &key, &value))
1262
 *   {
1263
 *     if (condition)
1264
 *       g_hash_table_iter_remove (&iter);
1265
 *   }
1266
 * ]|
1267
 *
1268
 * Since: 2.16
1269
 */
1270
void
1271
g_hash_table_iter_remove (GHashTableIter *iter)
1272
0
{
1273
0
  iter_remove_or_steal ((RealIter *) iter, TRUE);
1274
0
}
1275
1276
/*
1277
 * g_hash_table_insert_node:
1278
 * @hash_table: our #GHashTable
1279
 * @node_index: pointer to node to insert/replace
1280
 * @key_hash: key hash
1281
 * @key: (nullable): key to replace with, or %NULL
1282
 * @value: value to replace with
1283
 * @keep_new_key: whether to replace the key in the node with @key
1284
 * @reusing_key: whether @key was taken out of the existing node
1285
 *
1286
 * Inserts a value at @node_index in the hash table and updates it.
1287
 *
1288
 * If @key has been taken out of the existing node (ie it is not
1289
 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
1290
 * should be %TRUE.
1291
 *
1292
 * Returns: %TRUE if the key did not exist yet
1293
 */
1294
static gboolean
1295
g_hash_table_insert_node (GHashTable *hash_table,
1296
                          guint       node_index,
1297
                          guint       key_hash,
1298
                          gpointer    new_key,
1299
                          gpointer    new_value,
1300
                          gboolean    keep_new_key,
1301
                          gboolean    reusing_key)
1302
238k
{
1303
238k
  gboolean already_exists;
1304
238k
  guint old_hash;
1305
238k
  gpointer key_to_free = NULL;
1306
238k
  gpointer key_to_keep = NULL;
1307
238k
  gpointer value_to_free = NULL;
1308
1309
238k
  old_hash = hash_table->hashes[node_index];
1310
238k
  already_exists = HASH_IS_REAL (old_hash);
1311
1312
  /* Proceed in three steps.  First, deal with the key because it is the
1313
   * most complicated.  Then consider if we need to split the table in
1314
   * two (because writing the value will result in the set invariant
1315
   * becoming broken).  Then deal with the value.
1316
   *
1317
   * There are three cases for the key:
1318
   *
1319
   *  - entry already exists in table, reusing key:
1320
   *    free the just-passed-in new_key and use the existing value
1321
   *
1322
   *  - entry already exists in table, not reusing key:
1323
   *    free the entry in the table, use the new key
1324
   *
1325
   *  - entry not already in table:
1326
   *    use the new key, free nothing
1327
   *
1328
   * We update the hash at the same time...
1329
   */
1330
238k
  if (already_exists)
1331
142k
    {
1332
      /* Note: we must record the old value before writing the new key
1333
       * because we might change the value in the event that the two
1334
       * arrays are shared.
1335
       */
1336
142k
      value_to_free = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1337
1338
142k
      if (keep_new_key)
1339
142k
        {
1340
142k
          key_to_free = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1341
142k
          key_to_keep = new_key;
1342
142k
        }
1343
0
      else
1344
0
        {
1345
0
          key_to_free = new_key;
1346
0
          key_to_keep = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1347
0
        }
1348
142k
    }
1349
95.9k
  else
1350
95.9k
    {
1351
95.9k
      hash_table->hashes[node_index] = key_hash;
1352
95.9k
      key_to_keep = new_key;
1353
95.9k
    }
1354
1355
  /* Resize key/value arrays and split table as necessary */
1356
238k
  g_hash_table_ensure_keyval_fits (hash_table, key_to_keep, new_value);
1357
238k
  g_hash_table_assign_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys, key_to_keep);
1358
1359
  /* Step 3: Actually do the write */
1360
238k
  g_hash_table_assign_key_or_value (hash_table->values, node_index, hash_table->have_big_values, new_value);
1361
1362
  /* Now, the bookkeeping... */
1363
238k
  if (!already_exists)
1364
95.9k
    {
1365
95.9k
      hash_table->nnodes++;
1366
1367
95.9k
      if (HASH_IS_UNUSED (old_hash))
1368
95.9k
        {
1369
          /* We replaced an empty node, and not a tombstone */
1370
95.9k
          hash_table->noccupied++;
1371
95.9k
          g_hash_table_maybe_resize (hash_table);
1372
95.9k
        }
1373
1374
95.9k
#ifndef G_DISABLE_ASSERT
1375
95.9k
      hash_table->version++;
1376
95.9k
#endif
1377
95.9k
    }
1378
1379
238k
  if (already_exists)
1380
142k
    {
1381
142k
      if (hash_table->key_destroy_func && !reusing_key)
1382
0
        (* hash_table->key_destroy_func) (key_to_free);
1383
142k
      if (hash_table->value_destroy_func)
1384
0
        (* hash_table->value_destroy_func) (value_to_free);
1385
142k
    }
1386
1387
238k
  return !already_exists;
1388
238k
}
1389
1390
/**
1391
 * g_hash_table_iter_replace:
1392
 * @iter: an initialized #GHashTableIter
1393
 * @value: the value to replace with
1394
 *
1395
 * Replaces the value currently pointed to by the iterator
1396
 * from its associated #GHashTable. Can only be called after
1397
 * g_hash_table_iter_next() returned %TRUE.
1398
 *
1399
 * If you supplied a @value_destroy_func when creating the
1400
 * #GHashTable, the old value is freed using that function.
1401
 *
1402
 * Since: 2.30
1403
 */
1404
void
1405
g_hash_table_iter_replace (GHashTableIter *iter,
1406
                           gpointer        value)
1407
0
{
1408
0
  RealIter *ri;
1409
0
  guint node_hash;
1410
0
  gpointer key;
1411
1412
0
  ri = (RealIter *) iter;
1413
1414
0
  g_return_if_fail (ri != NULL);
1415
0
#ifndef G_DISABLE_ASSERT
1416
0
  g_return_if_fail (ri->version == ri->hash_table->version);
1417
0
#endif
1418
0
  g_return_if_fail (ri->position >= 0);
1419
0
  g_return_if_fail ((gsize) ri->position < ri->hash_table->size);
1420
1421
0
  node_hash = ri->hash_table->hashes[ri->position];
1422
1423
0
  key = g_hash_table_fetch_key_or_value (ri->hash_table->keys, ri->position, ri->hash_table->have_big_keys);
1424
1425
0
  g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
1426
1427
0
#ifndef G_DISABLE_ASSERT
1428
0
  ri->version++;
1429
0
  ri->hash_table->version++;
1430
0
#endif
1431
0
}
1432
1433
/**
1434
 * g_hash_table_iter_steal:
1435
 * @iter: an initialized #GHashTableIter
1436
 *
1437
 * Removes the key/value pair currently pointed to by the
1438
 * iterator from its associated #GHashTable, without calling
1439
 * the key and value destroy functions. Can only be called
1440
 * after g_hash_table_iter_next() returned %TRUE, and cannot
1441
 * be called more than once for the same key/value pair.
1442
 *
1443
 * Since: 2.16
1444
 */
1445
void
1446
g_hash_table_iter_steal (GHashTableIter *iter)
1447
0
{
1448
0
  iter_remove_or_steal ((RealIter *) iter, FALSE);
1449
0
}
1450
1451
1452
/**
1453
 * g_hash_table_ref:
1454
 * @hash_table: a valid #GHashTable
1455
 *
1456
 * Atomically increments the reference count of @hash_table by one.
1457
 * This function is MT-safe and may be called from any thread.
1458
 *
1459
 * Returns: the passed in #GHashTable
1460
 *
1461
 * Since: 2.10
1462
 */
1463
GHashTable *
1464
g_hash_table_ref (GHashTable *hash_table)
1465
0
{
1466
0
  g_return_val_if_fail (hash_table != NULL, NULL);
1467
1468
0
  g_atomic_ref_count_inc (&hash_table->ref_count);
1469
1470
0
  return hash_table;
1471
0
}
1472
1473
/**
1474
 * g_hash_table_unref:
1475
 * @hash_table: a valid #GHashTable
1476
 *
1477
 * Atomically decrements the reference count of @hash_table by one.
1478
 * If the reference count drops to 0, all keys and values will be
1479
 * destroyed, and all memory allocated by the hash table is released.
1480
 * This function is MT-safe and may be called from any thread.
1481
 *
1482
 * Since: 2.10
1483
 */
1484
void
1485
g_hash_table_unref (GHashTable *hash_table)
1486
51.7k
{
1487
51.7k
  g_return_if_fail (hash_table != NULL);
1488
1489
51.7k
  if (g_atomic_ref_count_dec (&hash_table->ref_count))
1490
51.7k
    {
1491
51.7k
      g_hash_table_remove_all_nodes (hash_table, TRUE, TRUE);
1492
51.7k
      if (hash_table->keys != hash_table->values)
1493
24.6k
        g_free (hash_table->values);
1494
51.7k
      g_free (hash_table->keys);
1495
51.7k
      g_free (hash_table->hashes);
1496
51.7k
      g_slice_free (GHashTable, hash_table);
1497
51.7k
    }
1498
51.7k
}
1499
1500
/**
1501
 * g_hash_table_destroy:
1502
 * @hash_table: a #GHashTable
1503
 *
1504
 * Destroys all keys and values in the #GHashTable and decrements its
1505
 * reference count by 1. If keys and/or values are dynamically allocated,
1506
 * you should either free them first or create the #GHashTable with destroy
1507
 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1508
 * functions you supplied will be called on all keys and values during the
1509
 * destruction phase.
1510
 */
1511
void
1512
g_hash_table_destroy (GHashTable *hash_table)
1513
51.5k
{
1514
51.5k
  g_return_if_fail (hash_table != NULL);
1515
1516
51.5k
  g_hash_table_remove_all (hash_table);
1517
51.5k
  g_hash_table_unref (hash_table);
1518
51.5k
}
1519
1520
/**
1521
 * g_hash_table_lookup:
1522
 * @hash_table: a #GHashTable
1523
 * @key: the key to look up
1524
 *
1525
 * Looks up a key in a #GHashTable. Note that this function cannot
1526
 * distinguish between a key that is not present and one which is present
1527
 * and has the value %NULL. If you need this distinction, use
1528
 * g_hash_table_lookup_extended().
1529
 *
1530
 * Returns: (nullable): the associated value, or %NULL if the key is not found
1531
 */
1532
gpointer
1533
g_hash_table_lookup (GHashTable    *hash_table,
1534
                     gconstpointer  key)
1535
571k
{
1536
571k
  guint node_index;
1537
571k
  guint node_hash;
1538
1539
571k
  g_return_val_if_fail (hash_table != NULL, NULL);
1540
1541
571k
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1542
1543
571k
  return HASH_IS_REAL (hash_table->hashes[node_index])
1544
571k
    ? g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values)
1545
571k
    : NULL;
1546
571k
}
1547
1548
/**
1549
 * g_hash_table_lookup_extended:
1550
 * @hash_table: a #GHashTable
1551
 * @lookup_key: the key to look up
1552
 * @orig_key: (out) (optional): return location for the original key
1553
 * @value: (out) (optional) (nullable): return location for the value associated
1554
 * with the key
1555
 *
1556
 * Looks up a key in the #GHashTable, returning the original key and the
1557
 * associated value and a #gboolean which is %TRUE if the key was found. This
1558
 * is useful if you need to free the memory allocated for the original key,
1559
 * for example before calling g_hash_table_remove().
1560
 *
1561
 * You can actually pass %NULL for @lookup_key to test
1562
 * whether the %NULL key exists, provided the hash and equal functions
1563
 * of @hash_table are %NULL-safe.
1564
 *
1565
 * Returns: %TRUE if the key was found in the #GHashTable
1566
 */
1567
gboolean
1568
g_hash_table_lookup_extended (GHashTable    *hash_table,
1569
                              gconstpointer  lookup_key,
1570
                              gpointer      *orig_key,
1571
                              gpointer      *value)
1572
0
{
1573
0
  guint node_index;
1574
0
  guint node_hash;
1575
1576
0
  g_return_val_if_fail (hash_table != NULL, FALSE);
1577
1578
0
  node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1579
1580
0
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1581
0
    {
1582
0
      if (orig_key != NULL)
1583
0
        *orig_key = NULL;
1584
0
      if (value != NULL)
1585
0
        *value = NULL;
1586
1587
0
      return FALSE;
1588
0
    }
1589
1590
0
  if (orig_key)
1591
0
    *orig_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1592
1593
0
  if (value)
1594
0
    *value = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1595
1596
0
  return TRUE;
1597
0
}
1598
1599
/*
1600
 * g_hash_table_insert_internal:
1601
 * @hash_table: our #GHashTable
1602
 * @key: the key to insert
1603
 * @value: the value to insert
1604
 * @keep_new_key: if %TRUE and this key already exists in the table
1605
 *   then call the destroy notify function on the old key.  If %FALSE
1606
 *   then call the destroy notify function on the new key.
1607
 *
1608
 * Implements the common logic for the g_hash_table_insert() and
1609
 * g_hash_table_replace() functions.
1610
 *
1611
 * Do a lookup of @key. If it is found, replace it with the new
1612
 * @value (and perhaps the new @key). If it is not found, create
1613
 * a new node.
1614
 *
1615
 * Returns: %TRUE if the key did not exist yet
1616
 */
1617
static gboolean
1618
g_hash_table_insert_internal (GHashTable *hash_table,
1619
                              gpointer    key,
1620
                              gpointer    value,
1621
                              gboolean    keep_new_key)
1622
238k
{
1623
238k
  guint key_hash;
1624
238k
  guint node_index;
1625
1626
238k
  g_return_val_if_fail (hash_table != NULL, FALSE);
1627
1628
238k
  node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1629
1630
238k
  return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1631
238k
}
1632
1633
/**
1634
 * g_hash_table_insert:
1635
 * @hash_table: a #GHashTable
1636
 * @key: a key to insert
1637
 * @value: the value to associate with the key
1638
 *
1639
 * Inserts a new key and value into a #GHashTable.
1640
 *
1641
 * If the key already exists in the #GHashTable its current
1642
 * value is replaced with the new value. If you supplied a
1643
 * @value_destroy_func when creating the #GHashTable, the old
1644
 * value is freed using that function. If you supplied a
1645
 * @key_destroy_func when creating the #GHashTable, the passed
1646
 * key is freed using that function.
1647
 *
1648
 * Starting from GLib 2.40, this function returns a boolean value to
1649
 * indicate whether the newly added value was already in the hash table
1650
 * or not.
1651
 *
1652
 * Returns: %TRUE if the key did not exist yet
1653
 */
1654
gboolean
1655
g_hash_table_insert (GHashTable *hash_table,
1656
                     gpointer    key,
1657
                     gpointer    value)
1658
52.2k
{
1659
52.2k
  return g_hash_table_insert_internal (hash_table, key, value, FALSE);
1660
52.2k
}
1661
1662
/**
1663
 * g_hash_table_replace:
1664
 * @hash_table: a #GHashTable
1665
 * @key: a key to insert
1666
 * @value: the value to associate with the key
1667
 *
1668
 * Inserts a new key and value into a #GHashTable similar to
1669
 * g_hash_table_insert(). The difference is that if the key
1670
 * already exists in the #GHashTable, it gets replaced by the
1671
 * new key. If you supplied a @value_destroy_func when creating
1672
 * the #GHashTable, the old value is freed using that function.
1673
 * If you supplied a @key_destroy_func when creating the
1674
 * #GHashTable, the old key is freed using that function.
1675
 *
1676
 * Starting from GLib 2.40, this function returns a boolean value to
1677
 * indicate whether the newly added value was already in the hash table
1678
 * or not.
1679
 *
1680
 * Returns: %TRUE if the key did not exist yet
1681
 */
1682
gboolean
1683
g_hash_table_replace (GHashTable *hash_table,
1684
                      gpointer    key,
1685
                      gpointer    value)
1686
186k
{
1687
186k
  return g_hash_table_insert_internal (hash_table, key, value, TRUE);
1688
186k
}
1689
1690
/**
1691
 * g_hash_table_add:
1692
 * @hash_table: a #GHashTable
1693
 * @key: (transfer full): a key to insert
1694
 *
1695
 * This is a convenience function for using a #GHashTable as a set.  It
1696
 * is equivalent to calling g_hash_table_replace() with @key as both the
1697
 * key and the value.
1698
 *
1699
 * In particular, this means that if @key already exists in the hash table, then
1700
 * the old copy of @key in the hash table is freed and @key replaces it in the
1701
 * table.
1702
 *
1703
 * When a hash table only ever contains keys that have themselves as the
1704
 * corresponding value it is able to be stored more efficiently.  See
1705
 * the discussion in the section description.
1706
 *
1707
 * Starting from GLib 2.40, this function returns a boolean value to
1708
 * indicate whether the newly added value was already in the hash table
1709
 * or not.
1710
 *
1711
 * Returns: %TRUE if the key did not exist yet
1712
 *
1713
 * Since: 2.32
1714
 */
1715
gboolean
1716
g_hash_table_add (GHashTable *hash_table,
1717
                  gpointer    key)
1718
0
{
1719
0
  return g_hash_table_insert_internal (hash_table, key, key, TRUE);
1720
0
}
1721
1722
/**
1723
 * g_hash_table_contains:
1724
 * @hash_table: a #GHashTable
1725
 * @key: a key to check
1726
 *
1727
 * Checks if @key is in @hash_table.
1728
 *
1729
 * Returns: %TRUE if @key is in @hash_table, %FALSE otherwise.
1730
 *
1731
 * Since: 2.32
1732
 **/
1733
gboolean
1734
g_hash_table_contains (GHashTable    *hash_table,
1735
                       gconstpointer  key)
1736
0
{
1737
0
  guint node_index;
1738
0
  guint node_hash;
1739
1740
0
  g_return_val_if_fail (hash_table != NULL, FALSE);
1741
1742
0
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1743
1744
0
  return HASH_IS_REAL (hash_table->hashes[node_index]);
1745
0
}
1746
1747
/*
1748
 * g_hash_table_remove_internal:
1749
 * @hash_table: our #GHashTable
1750
 * @key: the key to remove
1751
 * @notify: %TRUE if the destroy notify handlers are to be called
1752
 * Returns: %TRUE if a node was found and removed, else %FALSE
1753
 *
1754
 * Implements the common logic for the g_hash_table_remove() and
1755
 * g_hash_table_steal() functions.
1756
 *
1757
 * Do a lookup of @key and remove it if it is found, calling the
1758
 * destroy notify handlers only if @notify is %TRUE.
1759
 */
1760
static gboolean
1761
g_hash_table_remove_internal (GHashTable    *hash_table,
1762
                              gconstpointer  key,
1763
                              gboolean       notify)
1764
61.6k
{
1765
61.6k
  guint node_index;
1766
61.6k
  guint node_hash;
1767
1768
61.6k
  g_return_val_if_fail (hash_table != NULL, FALSE);
1769
1770
61.6k
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1771
1772
61.6k
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1773
2
    return FALSE;
1774
1775
61.6k
  g_hash_table_remove_node (hash_table, node_index, notify);
1776
61.6k
  g_hash_table_maybe_resize (hash_table);
1777
1778
61.6k
#ifndef G_DISABLE_ASSERT
1779
61.6k
  hash_table->version++;
1780
61.6k
#endif
1781
1782
61.6k
  return TRUE;
1783
61.6k
}
1784
1785
/**
1786
 * g_hash_table_remove:
1787
 * @hash_table: a #GHashTable
1788
 * @key: the key to remove
1789
 *
1790
 * Removes a key and its associated value from a #GHashTable.
1791
 *
1792
 * If the #GHashTable was created using g_hash_table_new_full(), the
1793
 * key and value are freed using the supplied destroy functions, otherwise
1794
 * you have to make sure that any dynamically allocated values are freed
1795
 * yourself.
1796
 *
1797
 * Returns: %TRUE if the key was found and removed from the #GHashTable
1798
 */
1799
gboolean
1800
g_hash_table_remove (GHashTable    *hash_table,
1801
                     gconstpointer  key)
1802
61.6k
{
1803
61.6k
  return g_hash_table_remove_internal (hash_table, key, TRUE);
1804
61.6k
}
1805
1806
/**
1807
 * g_hash_table_steal:
1808
 * @hash_table: a #GHashTable
1809
 * @key: the key to remove
1810
 *
1811
 * Removes a key and its associated value from a #GHashTable without
1812
 * calling the key and value destroy functions.
1813
 *
1814
 * Returns: %TRUE if the key was found and removed from the #GHashTable
1815
 */
1816
gboolean
1817
g_hash_table_steal (GHashTable    *hash_table,
1818
                    gconstpointer  key)
1819
0
{
1820
0
  return g_hash_table_remove_internal (hash_table, key, FALSE);
1821
0
}
1822
1823
/**
1824
 * g_hash_table_steal_extended:
1825
 * @hash_table: a #GHashTable
1826
 * @lookup_key: the key to look up
1827
 * @stolen_key: (out) (optional) (transfer full): return location for the
1828
 *    original key
1829
 * @stolen_value: (out) (optional) (nullable) (transfer full): return location
1830
 *    for the value associated with the key
1831
 *
1832
 * Looks up a key in the #GHashTable, stealing the original key and the
1833
 * associated value and returning %TRUE if the key was found. If the key was
1834
 * not found, %FALSE is returned.
1835
 *
1836
 * If found, the stolen key and value are removed from the hash table without
1837
 * calling the key and value destroy functions, and ownership is transferred to
1838
 * the caller of this method, as with g_hash_table_steal(). That is the case
1839
 * regardless whether @stolen_key or @stolen_value output parameters are
1840
 * requested.
1841
 *
1842
 * You can pass %NULL for @lookup_key, provided the hash and equal functions
1843
 * of @hash_table are %NULL-safe.
1844
 *
1845
 * The dictionary implementation optimizes for having all values identical to
1846
 * their keys, for example by using g_hash_table_add(). When stealing both the
1847
 * key and the value from such a dictionary, the value will be %NULL.
1848
 *
1849
 * Returns: %TRUE if the key was found in the #GHashTable
1850
 * Since: 2.58
1851
 */
1852
gboolean
1853
g_hash_table_steal_extended (GHashTable    *hash_table,
1854
                             gconstpointer  lookup_key,
1855
                             gpointer      *stolen_key,
1856
                             gpointer      *stolen_value)
1857
0
{
1858
0
  guint node_index;
1859
0
  guint node_hash;
1860
1861
0
  g_return_val_if_fail (hash_table != NULL, FALSE);
1862
1863
0
  node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1864
1865
0
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1866
0
    {
1867
0
      if (stolen_key != NULL)
1868
0
        *stolen_key = NULL;
1869
0
      if (stolen_value != NULL)
1870
0
        *stolen_value = NULL;
1871
0
      return FALSE;
1872
0
    }
1873
1874
0
  if (stolen_key != NULL)
1875
0
  {
1876
0
    *stolen_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1877
0
    g_hash_table_assign_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys, NULL);
1878
0
  }
1879
1880
0
  if (stolen_value != NULL)
1881
0
  {
1882
0
    *stolen_value = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1883
0
    g_hash_table_assign_key_or_value (hash_table->values, node_index, hash_table->have_big_values, NULL);
1884
0
  }
1885
1886
0
  g_hash_table_remove_node (hash_table, node_index, FALSE);
1887
0
  g_hash_table_maybe_resize (hash_table);
1888
1889
0
#ifndef G_DISABLE_ASSERT
1890
0
  hash_table->version++;
1891
0
#endif
1892
1893
0
  return TRUE;
1894
0
}
1895
1896
/**
1897
 * g_hash_table_remove_all:
1898
 * @hash_table: a #GHashTable
1899
 *
1900
 * Removes all keys and their associated values from a #GHashTable.
1901
 *
1902
 * If the #GHashTable was created using g_hash_table_new_full(),
1903
 * the keys and values are freed using the supplied destroy functions,
1904
 * otherwise you have to make sure that any dynamically allocated
1905
 * values are freed yourself.
1906
 *
1907
 * Since: 2.12
1908
 */
1909
void
1910
g_hash_table_remove_all (GHashTable *hash_table)
1911
51.5k
{
1912
51.5k
  g_return_if_fail (hash_table != NULL);
1913
1914
51.5k
#ifndef G_DISABLE_ASSERT
1915
51.5k
  if (hash_table->nnodes != 0)
1916
10.2k
    hash_table->version++;
1917
51.5k
#endif
1918
1919
51.5k
  g_hash_table_remove_all_nodes (hash_table, TRUE, FALSE);
1920
51.5k
  g_hash_table_maybe_resize (hash_table);
1921
51.5k
}
1922
1923
/**
1924
 * g_hash_table_steal_all:
1925
 * @hash_table: a #GHashTable
1926
 *
1927
 * Removes all keys and their associated values from a #GHashTable
1928
 * without calling the key and value destroy functions.
1929
 *
1930
 * Since: 2.12
1931
 */
1932
void
1933
g_hash_table_steal_all (GHashTable *hash_table)
1934
0
{
1935
0
  g_return_if_fail (hash_table != NULL);
1936
1937
0
#ifndef G_DISABLE_ASSERT
1938
0
  if (hash_table->nnodes != 0)
1939
0
    hash_table->version++;
1940
0
#endif
1941
1942
0
  g_hash_table_remove_all_nodes (hash_table, FALSE, FALSE);
1943
0
  g_hash_table_maybe_resize (hash_table);
1944
0
}
1945
1946
/**
1947
 * g_hash_table_steal_all_keys: (skip)
1948
 * @hash_table: a #GHashTable
1949
 *
1950
 * Removes all keys and their associated values from a #GHashTable
1951
 * without calling the key destroy functions, returning the keys
1952
 * as a #GPtrArray with the free func set to the @hash_table key
1953
 * destroy function.
1954
 *
1955
 * Returns: (transfer container): a #GPtrArray containing each key of
1956
 * the table. Unref with with g_ptr_array_unref() when done.
1957
 *
1958
 * Since: 2.76
1959
 */
1960
GPtrArray *
1961
g_hash_table_steal_all_keys (GHashTable *hash_table)
1962
0
{
1963
0
  GPtrArray *array;
1964
0
  GDestroyNotify key_destroy_func;
1965
1966
0
  g_return_val_if_fail (hash_table != NULL, NULL);
1967
1968
0
  array = g_hash_table_get_keys_as_ptr_array (hash_table);
1969
1970
  /* Ignore the key destroy notify calls during removal, and use it for the
1971
   * array elements instead, but restore it after the hash table has been
1972
   * cleared, so that newly added keys will continue using it.
1973
   */
1974
0
  key_destroy_func = g_steal_pointer (&hash_table->key_destroy_func);
1975
0
  g_ptr_array_set_free_func (array, key_destroy_func);
1976
1977
0
  g_hash_table_remove_all (hash_table);
1978
0
  hash_table->key_destroy_func = g_steal_pointer (&key_destroy_func);
1979
1980
0
  return array;
1981
0
}
1982
1983
/**
1984
 * g_hash_table_steal_all_values: (skip)
1985
 * @hash_table: a #GHashTable
1986
 *
1987
 * Removes all keys and their associated values from a #GHashTable
1988
 * without calling the value destroy functions, returning the values
1989
 * as a #GPtrArray with the free func set to the @hash_table value
1990
 * destroy function.
1991
 *
1992
 * Returns: (transfer container): a #GPtrArray containing each value of
1993
 * the table. Unref with with g_ptr_array_unref() when done.
1994
 *
1995
 * Since: 2.76
1996
 */
1997
GPtrArray *
1998
g_hash_table_steal_all_values (GHashTable *hash_table)
1999
0
{
2000
0
  GPtrArray *array;
2001
0
  GDestroyNotify value_destroy_func;
2002
2003
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2004
2005
0
  array = g_hash_table_get_values_as_ptr_array (hash_table);
2006
2007
  /* Ignore the value destroy notify calls during removal, and use it for the
2008
   * array elements instead, but restore it after the hash table has been
2009
   * cleared, so that newly added values will continue using it.
2010
   */
2011
0
  value_destroy_func = g_steal_pointer (&hash_table->value_destroy_func);
2012
0
  g_ptr_array_set_free_func (array, value_destroy_func);
2013
2014
0
  g_hash_table_remove_all (hash_table);
2015
0
  hash_table->value_destroy_func = g_steal_pointer (&value_destroy_func);
2016
2017
0
  return array;
2018
0
}
2019
2020
/*
2021
 * g_hash_table_foreach_remove_or_steal:
2022
 * @hash_table: a #GHashTable
2023
 * @func: the user's callback function
2024
 * @user_data: data for @func
2025
 * @notify: %TRUE if the destroy notify handlers are to be called
2026
 *
2027
 * Implements the common logic for g_hash_table_foreach_remove()
2028
 * and g_hash_table_foreach_steal().
2029
 *
2030
 * Iterates over every node in the table, calling @func with the key
2031
 * and value of the node (and @user_data). If @func returns %TRUE the
2032
 * node is removed from the table.
2033
 *
2034
 * If @notify is true then the destroy notify handlers will be called
2035
 * for each removed node.
2036
 */
2037
static guint
2038
g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
2039
                                      GHRFunc     func,
2040
                                      gpointer    user_data,
2041
                                      gboolean    notify)
2042
0
{
2043
0
  guint deleted = 0;
2044
0
  gsize i;
2045
0
#ifndef G_DISABLE_ASSERT
2046
0
  gint version = hash_table->version;
2047
0
#endif
2048
2049
0
  for (i = 0; i < hash_table->size; i++)
2050
0
    {
2051
0
      guint node_hash = hash_table->hashes[i];
2052
0
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2053
0
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
2054
2055
0
      if (HASH_IS_REAL (node_hash) &&
2056
0
          (* func) (node_key, node_value, user_data))
2057
0
        {
2058
0
          g_hash_table_remove_node (hash_table, i, notify);
2059
0
          deleted++;
2060
0
        }
2061
2062
0
#ifndef G_DISABLE_ASSERT
2063
0
      g_return_val_if_fail (version == hash_table->version, 0);
2064
0
#endif
2065
0
    }
2066
2067
0
  g_hash_table_maybe_resize (hash_table);
2068
2069
0
#ifndef G_DISABLE_ASSERT
2070
0
  if (deleted > 0)
2071
0
    hash_table->version++;
2072
0
#endif
2073
2074
0
  return deleted;
2075
0
}
2076
2077
/**
2078
 * g_hash_table_foreach_remove:
2079
 * @hash_table: a #GHashTable
2080
 * @func: the function to call for each key/value pair
2081
 * @user_data: user data to pass to the function
2082
 *
2083
 * Calls the given function for each key/value pair in the
2084
 * #GHashTable. If the function returns %TRUE, then the key/value
2085
 * pair is removed from the #GHashTable. If you supplied key or
2086
 * value destroy functions when creating the #GHashTable, they are
2087
 * used to free the memory allocated for the removed keys and values.
2088
 *
2089
 * See #GHashTableIter for an alternative way to loop over the
2090
 * key/value pairs in the hash table.
2091
 *
2092
 * Returns: the number of key/value pairs removed
2093
 */
2094
guint
2095
g_hash_table_foreach_remove (GHashTable *hash_table,
2096
                             GHRFunc     func,
2097
                             gpointer    user_data)
2098
0
{
2099
0
  g_return_val_if_fail (hash_table != NULL, 0);
2100
0
  g_return_val_if_fail (func != NULL, 0);
2101
2102
0
  return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
2103
0
}
2104
2105
/**
2106
 * g_hash_table_foreach_steal:
2107
 * @hash_table: a #GHashTable
2108
 * @func: the function to call for each key/value pair
2109
 * @user_data: user data to pass to the function
2110
 *
2111
 * Calls the given function for each key/value pair in the
2112
 * #GHashTable. If the function returns %TRUE, then the key/value
2113
 * pair is removed from the #GHashTable, but no key or value
2114
 * destroy functions are called.
2115
 *
2116
 * See #GHashTableIter for an alternative way to loop over the
2117
 * key/value pairs in the hash table.
2118
 *
2119
 * Returns: the number of key/value pairs removed.
2120
 */
2121
guint
2122
g_hash_table_foreach_steal (GHashTable *hash_table,
2123
                            GHRFunc     func,
2124
                            gpointer    user_data)
2125
0
{
2126
0
  g_return_val_if_fail (hash_table != NULL, 0);
2127
0
  g_return_val_if_fail (func != NULL, 0);
2128
2129
0
  return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
2130
0
}
2131
2132
/**
2133
 * g_hash_table_foreach:
2134
 * @hash_table: a #GHashTable
2135
 * @func: the function to call for each key/value pair
2136
 * @user_data: user data to pass to the function
2137
 *
2138
 * Calls the given function for each of the key/value pairs in the
2139
 * #GHashTable.  The function is passed the key and value of each
2140
 * pair, and the given @user_data parameter.  The hash table may not
2141
 * be modified while iterating over it (you can't add/remove
2142
 * items). To remove all items matching a predicate, use
2143
 * g_hash_table_foreach_remove().
2144
 *
2145
 * The order in which g_hash_table_foreach() iterates over the keys/values in
2146
 * the hash table is not defined.
2147
 *
2148
 * See g_hash_table_find() for performance caveats for linear
2149
 * order searches in contrast to g_hash_table_lookup().
2150
 */
2151
void
2152
g_hash_table_foreach (GHashTable *hash_table,
2153
                      GHFunc      func,
2154
                      gpointer    user_data)
2155
3
{
2156
3
  gsize i;
2157
3
#ifndef G_DISABLE_ASSERT
2158
3
  gint version;
2159
3
#endif
2160
2161
3
  g_return_if_fail (hash_table != NULL);
2162
3
  g_return_if_fail (func != NULL);
2163
2164
3
#ifndef G_DISABLE_ASSERT
2165
3
  version = hash_table->version;
2166
3
#endif
2167
2168
27
  for (i = 0; i < hash_table->size; i++)
2169
24
    {
2170
24
      guint node_hash = hash_table->hashes[i];
2171
24
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2172
24
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
2173
2174
24
      if (HASH_IS_REAL (node_hash))
2175
0
        (* func) (node_key, node_value, user_data);
2176
2177
24
#ifndef G_DISABLE_ASSERT
2178
24
      g_return_if_fail (version == hash_table->version);
2179
24
#endif
2180
24
    }
2181
3
}
2182
2183
/**
2184
 * g_hash_table_find:
2185
 * @hash_table: a #GHashTable
2186
 * @predicate: function to test the key/value pairs for a certain property
2187
 * @user_data: user data to pass to the function
2188
 *
2189
 * Calls the given function for key/value pairs in the #GHashTable
2190
 * until @predicate returns %TRUE. The function is passed the key
2191
 * and value of each pair, and the given @user_data parameter. The
2192
 * hash table may not be modified while iterating over it (you can't
2193
 * add/remove items).
2194
 *
2195
 * Note, that hash tables are really only optimized for forward
2196
 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
2197
 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
2198
 * once per every entry in a hash table) should probably be reworked
2199
 * to use additional or different data structures for reverse lookups
2200
 * (keep in mind that an O(n) find/foreach operation issued for all n
2201
 * values in a hash table ends up needing O(n*n) operations).
2202
 *
2203
 * Returns: (nullable): The value of the first key/value pair is returned,
2204
 *     for which @predicate evaluates to %TRUE. If no pair with the
2205
 *     requested property is found, %NULL is returned.
2206
 *
2207
 * Since: 2.4
2208
 */
2209
gpointer
2210
g_hash_table_find (GHashTable *hash_table,
2211
                   GHRFunc     predicate,
2212
                   gpointer    user_data)
2213
0
{
2214
0
  gsize i;
2215
0
#ifndef G_DISABLE_ASSERT
2216
0
  gint version;
2217
0
#endif
2218
0
  gboolean match;
2219
2220
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2221
0
  g_return_val_if_fail (predicate != NULL, NULL);
2222
2223
0
#ifndef G_DISABLE_ASSERT
2224
0
  version = hash_table->version;
2225
0
#endif
2226
2227
0
  match = FALSE;
2228
2229
0
  for (i = 0; i < hash_table->size; i++)
2230
0
    {
2231
0
      guint node_hash = hash_table->hashes[i];
2232
0
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2233
0
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
2234
2235
0
      if (HASH_IS_REAL (node_hash))
2236
0
        match = predicate (node_key, node_value, user_data);
2237
2238
0
#ifndef G_DISABLE_ASSERT
2239
0
      g_return_val_if_fail (version == hash_table->version, NULL);
2240
0
#endif
2241
2242
0
      if (match)
2243
0
        return node_value;
2244
0
    }
2245
2246
0
  return NULL;
2247
0
}
2248
2249
/**
2250
 * g_hash_table_size:
2251
 * @hash_table: a #GHashTable
2252
 *
2253
 * Returns the number of elements contained in the #GHashTable.
2254
 *
2255
 * Returns: the number of key/value pairs in the #GHashTable.
2256
 */
2257
guint
2258
g_hash_table_size (GHashTable *hash_table)
2259
0
{
2260
0
  g_return_val_if_fail (hash_table != NULL, 0);
2261
2262
0
  return hash_table->nnodes;
2263
0
}
2264
2265
/**
2266
 * g_hash_table_get_keys:
2267
 * @hash_table: a #GHashTable
2268
 *
2269
 * Retrieves every key inside @hash_table. The returned data is valid
2270
 * until changes to the hash release those keys.
2271
 *
2272
 * This iterates over every entry in the hash table to build its return value.
2273
 * To iterate over the entries in a #GHashTable more efficiently, use a
2274
 * #GHashTableIter.
2275
 *
2276
 * Returns: (transfer container): a #GList containing all the keys
2277
 *     inside the hash table. The content of the list is owned by the
2278
 *     hash table and should not be modified or freed. Use g_list_free()
2279
 *     when done using the list.
2280
 *
2281
 * Since: 2.14
2282
 */
2283
GList *
2284
g_hash_table_get_keys (GHashTable *hash_table)
2285
0
{
2286
0
  gsize i;
2287
0
  GList *retval;
2288
2289
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2290
2291
0
  retval = NULL;
2292
0
  for (i = 0; i < hash_table->size; i++)
2293
0
    {
2294
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2295
0
        retval = g_list_prepend (retval, g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys));
2296
0
    }
2297
2298
0
  return retval;
2299
0
}
2300
2301
/**
2302
 * g_hash_table_get_keys_as_array:
2303
 * @hash_table: a #GHashTable
2304
 * @length: (out) (optional): the length of the returned array
2305
 *
2306
 * Retrieves every key inside @hash_table, as an array.
2307
 *
2308
 * The returned array is %NULL-terminated but may contain %NULL as a
2309
 * key.  Use @length to determine the true length if it's possible that
2310
 * %NULL was used as the value for a key.
2311
 *
2312
 * Note: in the common case of a string-keyed #GHashTable, the return
2313
 * value of this function can be conveniently cast to (const gchar **).
2314
 *
2315
 * This iterates over every entry in the hash table to build its return value.
2316
 * To iterate over the entries in a #GHashTable more efficiently, use a
2317
 * #GHashTableIter.
2318
 *
2319
 * You should always free the return result with g_free().  In the
2320
 * above-mentioned case of a string-keyed hash table, it may be
2321
 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
2322
 * first to transfer ownership of the keys.
2323
 *
2324
 * Returns: (array length=length) (transfer container): a
2325
 *   %NULL-terminated array containing each key from the table.
2326
 *
2327
 * Since: 2.40
2328
 **/
2329
gpointer *
2330
g_hash_table_get_keys_as_array (GHashTable *hash_table,
2331
                                guint      *length)
2332
0
{
2333
0
  gpointer *result;
2334
0
  gsize i, j = 0;
2335
2336
0
  result = g_new (gpointer, hash_table->nnodes + 1);
2337
0
  for (i = 0; i < hash_table->size; i++)
2338
0
    {
2339
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2340
0
        result[j++] = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2341
0
    }
2342
0
  g_assert (j == hash_table->nnodes);
2343
0
  result[j] = NULL;
2344
2345
0
  if (length)
2346
0
    *length = j;
2347
2348
0
  return result;
2349
0
}
2350
2351
/**
2352
 * g_hash_table_get_keys_as_ptr_array: (skip)
2353
 * @hash_table: a #GHashTable
2354
 *
2355
 * Retrieves every key inside @hash_table, as a #GPtrArray.
2356
 * The returned data is valid until changes to the hash release those keys.
2357
 *
2358
 * This iterates over every entry in the hash table to build its return value.
2359
 * To iterate over the entries in a #GHashTable more efficiently, use a
2360
 * #GHashTableIter.
2361
 *
2362
 * You should always unref the returned array with g_ptr_array_unref().
2363
 *
2364
 * Returns: (transfer container): a #GPtrArray containing each key from
2365
 * the table. Unref with with g_ptr_array_unref() when done.
2366
 *
2367
 * Since: 2.76
2368
 **/
2369
GPtrArray *
2370
g_hash_table_get_keys_as_ptr_array (GHashTable *hash_table)
2371
0
{
2372
0
  GPtrArray *array;
2373
2374
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2375
2376
0
  array = g_ptr_array_sized_new (hash_table->size);
2377
0
  for (gsize i = 0; i < hash_table->size; ++i)
2378
0
    {
2379
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2380
0
        {
2381
0
          g_ptr_array_add (array, g_hash_table_fetch_key_or_value (
2382
0
            hash_table->keys, i, hash_table->have_big_keys));
2383
0
        }
2384
0
    }
2385
0
  g_assert (array->len == hash_table->nnodes);
2386
2387
0
  return array;
2388
0
}
2389
2390
/**
2391
 * g_hash_table_get_values:
2392
 * @hash_table: a #GHashTable
2393
 *
2394
 * Retrieves every value inside @hash_table. The returned data
2395
 * is valid until @hash_table is modified.
2396
 *
2397
 * This iterates over every entry in the hash table to build its return value.
2398
 * To iterate over the entries in a #GHashTable more efficiently, use a
2399
 * #GHashTableIter.
2400
 *
2401
 * Returns: (transfer container): a #GList containing all the values
2402
 *     inside the hash table. The content of the list is owned by the
2403
 *     hash table and should not be modified or freed. Use g_list_free()
2404
 *     when done using the list.
2405
 *
2406
 * Since: 2.14
2407
 */
2408
GList *
2409
g_hash_table_get_values (GHashTable *hash_table)
2410
0
{
2411
0
  gsize i;
2412
0
  GList *retval;
2413
2414
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2415
2416
0
  retval = NULL;
2417
0
  for (i = 0; i < hash_table->size; i++)
2418
0
    {
2419
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2420
0
        retval = g_list_prepend (retval, g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values));
2421
0
    }
2422
2423
0
  return retval;
2424
0
}
2425
2426
/**
2427
 * g_hash_table_get_values_as_ptr_array: (skip)
2428
 * @hash_table: a #GHashTable
2429
 *
2430
 * Retrieves every value inside @hash_table, as a #GPtrArray.
2431
 * The returned data is valid until changes to the hash release those values.
2432
 *
2433
 * This iterates over every entry in the hash table to build its return value.
2434
 * To iterate over the entries in a #GHashTable more efficiently, use a
2435
 * #GHashTableIter.
2436
 *
2437
 * You should always unref the returned array with g_ptr_array_unref().
2438
 *
2439
 * Returns: (transfer container): a #GPtrArray containing each value from
2440
 * the table. Unref with with g_ptr_array_unref() when done.
2441
 *
2442
 * Since: 2.76
2443
 **/
2444
GPtrArray *
2445
g_hash_table_get_values_as_ptr_array (GHashTable *hash_table)
2446
0
{
2447
0
  GPtrArray *array;
2448
2449
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2450
2451
0
  array = g_ptr_array_sized_new (hash_table->size);
2452
0
  for (gsize i = 0; i < hash_table->size; ++i)
2453
0
    {
2454
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2455
0
        {
2456
0
          g_ptr_array_add (array, g_hash_table_fetch_key_or_value (
2457
0
            hash_table->values, i, hash_table->have_big_values));
2458
0
        }
2459
0
    }
2460
0
  g_assert (array->len == hash_table->nnodes);
2461
2462
0
  return array;
2463
0
}
2464
2465
/* Hash functions.
2466
 */
2467
2468
/**
2469
 * g_str_equal:
2470
 * @v1: (not nullable): a key
2471
 * @v2: (not nullable): a key to compare with @v1
2472
 *
2473
 * Compares two strings for byte-by-byte equality and returns %TRUE
2474
 * if they are equal. It can be passed to g_hash_table_new() as the
2475
 * @key_equal_func parameter, when using non-%NULL strings as keys in a
2476
 * #GHashTable.
2477
 *
2478
 * This function is typically used for hash table comparisons, but can be used
2479
 * for general purpose comparisons of non-%NULL strings. For a %NULL-safe string
2480
 * comparison function, see g_strcmp0().
2481
 *
2482
 * Returns: %TRUE if the two keys match
2483
 */
2484
gboolean
2485
(g_str_equal) (gconstpointer v1,
2486
               gconstpointer v2)
2487
450k
{
2488
450k
  const gchar *string1 = v1;
2489
450k
  const gchar *string2 = v2;
2490
2491
450k
  return strcmp (string1, string2) == 0;
2492
450k
}
2493
2494
/**
2495
 * g_str_hash:
2496
 * @v: (not nullable): a string key
2497
 *
2498
 * Converts a string to a hash value.
2499
 *
2500
 * This function implements the widely used "djb" hash apparently
2501
 * posted by Daniel Bernstein to comp.lang.c some time ago.  The 32
2502
 * bit unsigned hash value starts at 5381 and for each byte 'c' in
2503
 * the string, is updated: `hash = hash * 33 + c`. This function
2504
 * uses the signed value of each byte.
2505
 *
2506
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2507
 * when using non-%NULL strings as keys in a #GHashTable.
2508
 *
2509
 * Note that this function may not be a perfect fit for all use cases.
2510
 * For example, it produces some hash collisions with strings as short
2511
 * as 2.
2512
 *
2513
 * Returns: a hash value corresponding to the key
2514
 */
2515
guint
2516
g_str_hash (gconstpointer v)
2517
704k
{
2518
704k
  const signed char *p;
2519
704k
  guint32 h = 5381;
2520
2521
28.1M
  for (p = v; *p != '\0'; p++)
2522
27.4M
    h = (h << 5) + h + *p;
2523
2524
704k
  return h;
2525
704k
}
2526
2527
/**
2528
 * g_direct_hash:
2529
 * @v: (nullable): a #gpointer key
2530
 *
2531
 * Converts a gpointer to a hash value.
2532
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2533
 * when using opaque pointers compared by pointer value as keys in a
2534
 * #GHashTable.
2535
 *
2536
 * This hash function is also appropriate for keys that are integers
2537
 * stored in pointers, such as `GINT_TO_POINTER (n)`.
2538
 *
2539
 * Returns: a hash value corresponding to the key.
2540
 */
2541
guint
2542
g_direct_hash (gconstpointer v)
2543
167k
{
2544
167k
  return GPOINTER_TO_UINT (v);
2545
167k
}
2546
2547
/**
2548
 * g_direct_equal:
2549
 * @v1: (nullable): a key
2550
 * @v2: (nullable): a key to compare with @v1
2551
 *
2552
 * Compares two #gpointer arguments and returns %TRUE if they are equal.
2553
 * It can be passed to g_hash_table_new() as the @key_equal_func
2554
 * parameter, when using opaque pointers compared by pointer value as
2555
 * keys in a #GHashTable.
2556
 *
2557
 * This equality function is also appropriate for keys that are integers
2558
 * stored in pointers, such as `GINT_TO_POINTER (n)`.
2559
 *
2560
 * Returns: %TRUE if the two keys match.
2561
 */
2562
gboolean
2563
g_direct_equal (gconstpointer v1,
2564
                gconstpointer v2)
2565
0
{
2566
0
  return v1 == v2;
2567
0
}
2568
2569
/**
2570
 * g_int_equal:
2571
 * @v1: (not nullable): a pointer to a #gint key
2572
 * @v2: (not nullable): a pointer to a #gint key to compare with @v1
2573
 *
2574
 * Compares the two #gint values being pointed to and returns
2575
 * %TRUE if they are equal.
2576
 * It can be passed to g_hash_table_new() as the @key_equal_func
2577
 * parameter, when using non-%NULL pointers to integers as keys in a
2578
 * #GHashTable.
2579
 *
2580
 * Note that this function acts on pointers to #gint, not on #gint
2581
 * directly: if your hash table's keys are of the form
2582
 * `GINT_TO_POINTER (n)`, use g_direct_equal() instead.
2583
 *
2584
 * Returns: %TRUE if the two keys match.
2585
 */
2586
gboolean
2587
g_int_equal (gconstpointer v1,
2588
             gconstpointer v2)
2589
0
{
2590
0
  return *((const gint*) v1) == *((const gint*) v2);
2591
0
}
2592
2593
/**
2594
 * g_int_hash:
2595
 * @v: (not nullable): a pointer to a #gint key
2596
 *
2597
 * Converts a pointer to a #gint to a hash value.
2598
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2599
 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
2600
 *
2601
 * Note that this function acts on pointers to #gint, not on #gint
2602
 * directly: if your hash table's keys are of the form
2603
 * `GINT_TO_POINTER (n)`, use g_direct_hash() instead.
2604
 *
2605
 * Returns: a hash value corresponding to the key.
2606
 */
2607
guint
2608
g_int_hash (gconstpointer v)
2609
0
{
2610
0
  return *(const gint*) v;
2611
0
}
2612
2613
/**
2614
 * g_int64_equal:
2615
 * @v1: (not nullable): a pointer to a #gint64 key
2616
 * @v2: (not nullable): a pointer to a #gint64 key to compare with @v1
2617
 *
2618
 * Compares the two #gint64 values being pointed to and returns
2619
 * %TRUE if they are equal.
2620
 * It can be passed to g_hash_table_new() as the @key_equal_func
2621
 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
2622
 * #GHashTable.
2623
 *
2624
 * Returns: %TRUE if the two keys match.
2625
 *
2626
 * Since: 2.22
2627
 */
2628
gboolean
2629
g_int64_equal (gconstpointer v1,
2630
               gconstpointer v2)
2631
0
{
2632
0
  return *((const gint64*) v1) == *((const gint64*) v2);
2633
0
}
2634
2635
/**
2636
 * g_int64_hash:
2637
 * @v: (not nullable): a pointer to a #gint64 key
2638
 *
2639
 * Converts a pointer to a #gint64 to a hash value.
2640
 *
2641
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2642
 * when using non-%NULL pointers to 64-bit integer values as keys in a
2643
 * #GHashTable.
2644
 *
2645
 * Returns: a hash value corresponding to the key.
2646
 *
2647
 * Since: 2.22
2648
 */
2649
guint
2650
g_int64_hash (gconstpointer v)
2651
0
{
2652
0
  const guint64 *bits = v;
2653
2654
0
  return (guint) ((*bits >> 32) ^ (*bits & 0xffffffffU));
2655
0
}
2656
2657
/**
2658
 * g_double_equal:
2659
 * @v1: (not nullable): a pointer to a #gdouble key
2660
 * @v2: (not nullable): a pointer to a #gdouble key to compare with @v1
2661
 *
2662
 * Compares the two #gdouble values being pointed to and returns
2663
 * %TRUE if they are equal.
2664
 * It can be passed to g_hash_table_new() as the @key_equal_func
2665
 * parameter, when using non-%NULL pointers to doubles as keys in a
2666
 * #GHashTable.
2667
 *
2668
 * Returns: %TRUE if the two keys match.
2669
 *
2670
 * Since: 2.22
2671
 */
2672
gboolean
2673
g_double_equal (gconstpointer v1,
2674
                gconstpointer v2)
2675
0
{
2676
0
  return *((const gdouble*) v1) == *((const gdouble*) v2);
2677
0
}
2678
2679
/**
2680
 * g_double_hash:
2681
 * @v: (not nullable): a pointer to a #gdouble key
2682
 *
2683
 * Converts a pointer to a #gdouble to a hash value.
2684
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2685
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2686
 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
2687
 *
2688
 * Returns: a hash value corresponding to the key.
2689
 *
2690
 * Since: 2.22
2691
 */
2692
guint
2693
g_double_hash (gconstpointer v)
2694
0
{
2695
  /* Same as g_int64_hash() */
2696
0
  const guint64 *bits = v;
2697
2698
0
  return (guint) ((*bits >> 32) ^ (*bits & 0xffffffffU));
2699
0
}