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