1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gdataset.c: Generic dataset mechanism, similar to GtkObject data.
5 * Copyright (C) 1998 Tim Janik
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 * MT safe ; except for g_data*_foreach()
42 #include "gdatasetprivate.h"
43 #include "gutilsprivate.h"
46 #include "gstrfuncs.h"
47 #include "gtestutils.h"
49 #include "glib_trace.h"
55 * An opaque data structure that represents a keyed data list.
57 * See also: [Keyed data lists](datalist-and-dataset.html).
62 * @data: the data element.
64 * Specifies the type of function which is called when a data element
65 * is destroyed. It is passed the pointer to the data element and
66 * should free any memory and resources allocated for it.
69 #define G_DATALIST_FLAGS_MASK_INTERNAL 0x7
71 #define G_DATALIST_CLEAN_POINTER(ptr) \
72 ((GData *) ((gpointer) (((guintptr) (ptr)) & ~((guintptr) G_DATALIST_FLAGS_MASK_INTERNAL))))
74 /* datalist pointer accesses have to be carried out atomically */
75 #define G_DATALIST_GET_POINTER(datalist) \
76 G_DATALIST_CLEAN_POINTER (g_atomic_pointer_get (datalist))
78 #define G_DATALIST_SET_POINTER(datalist, pointer) G_STMT_START { \
79 gpointer _oldv = g_atomic_pointer_get (datalist); \
82 _newv = (gpointer) (((guintptr) _oldv & ((guintptr) G_DATALIST_FLAGS_MASK_INTERNAL)) | (guintptr) pointer); \
83 } while (!g_atomic_pointer_compare_and_exchange_full ((void**) datalist, _oldv, \
87 /* --- structures --- */
91 GDestroyNotify destroy;
94 typedef struct _GDataset GDataset;
97 guint32 len; /* Number of elements */
98 guint32 alloc; /* Number of allocated elements */
99 GDataElt data[1]; /* Flexible array */
104 gconstpointer location;
109 /* --- prototypes --- */
110 static inline GDataset* g_dataset_lookup (gconstpointer dataset_location);
111 static void g_dataset_destroy_internal (GDataset *dataset);
112 static inline gpointer g_data_set_internal (GData **datalist,
115 GDestroyNotify destroy_func,
117 static void g_data_initialize (void);
120 * Each standalone GDataList is protected by a bitlock in the datalist pointer,
121 * which protects that modification of the non-flags part of the datalist pointer
122 * and the contents of the datalist.
124 * For GDataSet we have a global lock g_dataset_global that protects
125 * the global dataset hash and cache, and additionally it protects the
126 * datalist such that we can avoid to use the bit lock in a few places
130 /* --- variables --- */
131 G_LOCK_DEFINE_STATIC (g_dataset_global);
132 static GHashTable *g_dataset_location_ht = NULL;
133 static GDataset *g_dataset_cached = NULL; /* should this be
136 /* --- functions --- */
138 #define DATALIST_LOCK_BIT 2
140 G_ALWAYS_INLINE static inline GData *
141 g_datalist_lock_and_get (GData **datalist)
145 g_pointer_bit_lock_and_get ((void **) datalist, DATALIST_LOCK_BIT, &ptr);
146 return G_DATALIST_CLEAN_POINTER (ptr);
150 g_datalist_unlock (GData **datalist)
152 g_pointer_bit_unlock ((void **)datalist, DATALIST_LOCK_BIT);
156 g_datalist_unlock_and_set (GData **datalist, gpointer ptr)
158 g_pointer_bit_unlock_and_set ((void **) datalist, DATALIST_LOCK_BIT, ptr, G_DATALIST_FLAGS_MASK_INTERNAL);
162 datalist_append (GData **data, GQuark key_id, gpointer new_data, GDestroyNotify destroy_func)
164 gboolean reallocated;
170 d = g_malloc (G_STRUCT_OFFSET (GData, data) + 2u * sizeof (GDataElt));
176 else if (d->len == d->alloc)
178 d->alloc = d->alloc * 2u;
180 /* d->alloc is always a power of two. It thus overflows the first time
181 * when going to (G_MAXUINT32+1), or when requesting 2^31+1 elements.
183 * This is not handled, and we just crash. That's because we track the GData
184 * in a linear list, which horribly degrades long before we add 2 billion entries.
185 * Don't ever try to do that. */
186 g_assert (d->alloc > d->len);
188 d = g_realloc (d, G_STRUCT_OFFSET (GData, data) + d->alloc * sizeof (GDataElt));
195 d->data[d->len] = (GDataElt){
198 .destroy = destroy_func,
206 datalist_remove (GData *data, guint32 idx)
209 g_assert (idx < data->len);
212 /* g_data_remove_internal() relies on the fact, that this function removes
213 * the entry similar to g_array_remove_index_fast(). That is, the entries up
214 * to @idx are left unchanged, and the last entry is moved to position @idx.
219 if (idx != data->len)
220 data->data[idx] = data->data[data->len];
224 datalist_shrink (GData **data, GData **d_to_free)
232 alloc_by_4 = d->alloc / 4u;
234 if (G_LIKELY (d->len > alloc_by_4))
242 /* The list became empty. We drop the allocated memory altogether. */
244 /* The caller will free the buffer after releasing the lock, to minimize
245 * the time we hold the lock. Transfer it out. */
251 /* If the buffer is filled not more than 25%. Shrink to double the current length. */
256 /* d->alloc is a power of two. Usually, we remove one element at a
257 * time, then we will just reach reach a quarter of that.
259 * However, with g_datalist_id_remove_multiple(), len can be smaller
260 * at once. In that case, find first the next power of two. */
261 v = g_nearest_pow (v);
266 g_assert (v > d->len);
267 g_assert (v <= d->alloc / 2u);
271 d = g_realloc (d, G_STRUCT_OFFSET (GData, data) + (v * sizeof (GDataElt)));
278 datalist_find (GData *data, GQuark key_id, guint32 *out_idx)
284 for (i = 0; i < data->len; i++)
286 GDataElt *data_elt = &data->data[i];
288 if (data_elt->key == key_id)
297 *out_idx = G_MAXUINT32;
302 * g_datalist_clear: (skip)
303 * @datalist: a datalist.
305 * Frees all the data elements of the datalist.
306 * The data elements' destroy functions are called
307 * if they have been set.
310 g_datalist_clear (GData **datalist)
315 g_return_if_fail (datalist != NULL);
317 data = g_datalist_lock_and_get (datalist);
321 g_datalist_unlock (datalist);
325 g_datalist_unlock_and_set (datalist, NULL);
327 for (i = 0; i < data->len; i++)
329 if (data->data[i].data && data->data[i].destroy)
330 data->data[i].destroy (data->data[i].data);
336 /* HOLDS: g_dataset_global_lock */
337 static inline GDataset*
338 g_dataset_lookup (gconstpointer dataset_location)
342 if (g_dataset_cached && g_dataset_cached->location == dataset_location)
343 return g_dataset_cached;
345 dataset = g_hash_table_lookup (g_dataset_location_ht, dataset_location);
347 g_dataset_cached = dataset;
352 /* HOLDS: g_dataset_global_lock */
354 g_dataset_destroy_internal (GDataset *dataset)
356 gconstpointer dataset_location;
358 dataset_location = dataset->location;
364 data = G_DATALIST_GET_POINTER (&dataset->datalist);
368 if (dataset == g_dataset_cached)
369 g_dataset_cached = NULL;
370 g_hash_table_remove (g_dataset_location_ht, dataset_location);
371 g_slice_free (GDataset, dataset);
375 G_DATALIST_SET_POINTER (&dataset->datalist, NULL);
377 G_UNLOCK (g_dataset_global);
379 for (i = 0; i < data->len; i++)
381 if (data->data[i].data && data->data[i].destroy)
382 data->data[i].destroy (data->data[i].data);
386 G_LOCK (g_dataset_global);
387 dataset = g_dataset_lookup (dataset_location);
393 * @dataset_location: (not nullable): the location identifying the dataset.
395 * Destroys the dataset, freeing all memory allocated, and calling any
396 * destroy functions set for data elements.
399 g_dataset_destroy (gconstpointer dataset_location)
401 g_return_if_fail (dataset_location != NULL);
403 G_LOCK (g_dataset_global);
404 if (g_dataset_location_ht)
408 dataset = g_dataset_lookup (dataset_location);
410 g_dataset_destroy_internal (dataset);
412 G_UNLOCK (g_dataset_global);
415 /* HOLDS: g_dataset_global_lock if dataset != null */
416 static inline gpointer
417 g_data_set_internal (GData **datalist,
420 GDestroyNotify new_destroy_func,
428 d = g_datalist_lock_and_get (datalist);
430 data = datalist_find (d, key_id, &idx);
432 if (new_data == NULL) /* remove */
440 datalist_remove (d, idx);
441 if (datalist_shrink (&d, &d_to_free))
443 g_datalist_unlock_and_set (datalist, d);
445 /* the dataset destruction *must* be done
446 * prior to invocation of the data destroy function
449 g_dataset_destroy_internal (dataset);
455 g_datalist_unlock (datalist);
457 /* We found and removed an old value
458 * the GData struct *must* already be unlinked
459 * when invoking the destroy function.
460 * we use (new_data==NULL && new_destroy_func!=NULL) as
461 * a special hint combination to "steal"
462 * data without destroy notification
464 if (old.destroy && !new_destroy_func)
467 G_UNLOCK (g_dataset_global);
468 old.destroy (old.data);
470 G_LOCK (g_dataset_global);
483 data->data = new_data;
484 data->destroy = new_destroy_func;
485 g_datalist_unlock (datalist);
490 data->data = new_data;
491 data->destroy = new_destroy_func;
493 g_datalist_unlock (datalist);
495 /* We found and replaced an old value
496 * the GData struct *must* already be unlinked
497 * when invoking the destroy function.
500 G_UNLOCK (g_dataset_global);
501 old.destroy (old.data);
503 G_LOCK (g_dataset_global);
508 /* The key was not found, insert it */
509 if (datalist_append (&d, key_id, new_data, new_destroy_func))
514 g_datalist_unlock_and_set (datalist, new_d);
516 g_datalist_unlock (datalist);
523 g_data_remove_internal (GData **datalist,
529 GDataElt *old_to_free = NULL;
535 d = g_datalist_lock_and_get (datalist);
539 g_datalist_unlock (datalist);
543 /* Allocate an array of GDataElt to hold copies of the elements
544 * that are removed from the datalist. Allow enough space for all
545 * the keys; if a key is not found, the corresponding element of
546 * old is not populated, so we initialize them all to NULL to
549 * At most allocate 400 bytes on the stack. Especially since we call
550 * out to external code, we don't know how much stack we can use. */
551 if (n_keys <= 400u / sizeof (GDataElt))
552 old = g_newa0 (GDataElt, n_keys);
555 old_to_free = g_new0 (GDataElt, n_keys);
561 while (i_data < d->len && found_keys < n_keys)
563 GDataElt *data = &d->data[i_data];
564 gboolean remove = FALSE;
566 for (i_keys = 0; i_keys < n_keys; i_keys++)
568 if (data->key == keys[i_keys])
570 /* We must invoke the destroy notifications in the order of @keys.
571 * Hence, build up the list @old at index @i_keys. */
585 datalist_remove (d, i_data);
588 if (found_keys > 0 && datalist_shrink (&d, &d_to_free))
590 g_datalist_unlock_and_set (datalist, d);
595 g_datalist_unlock (datalist);
599 for (i_keys = 0; i_keys < n_keys; i_keys++)
601 if (old[i_keys].destroy)
602 old[i_keys].destroy (old[i_keys].data);
606 if (G_UNLIKELY (old_to_free))
607 g_free (old_to_free);
611 * g_dataset_id_set_data_full: (skip)
612 * @dataset_location: (not nullable): the location identifying the dataset.
613 * @key_id: the #GQuark id to identify the data element.
614 * @data: the data element.
615 * @destroy_func: the function to call when the data element is
616 * removed. This function will be called with the data
617 * element and can be used to free any memory allocated
620 * Sets the data element associated with the given #GQuark id, and also
621 * the function to call when the data element is destroyed. Any
622 * previous data with the same key is removed, and its destroy function
626 * g_dataset_set_data_full: (skip)
627 * @l: the location identifying the dataset.
628 * @k: the string to identify the data element.
629 * @d: the data element.
630 * @f: the function to call when the data element is removed. This
631 * function will be called with the data element and can be used to
632 * free any memory allocated for it.
634 * Sets the data corresponding to the given string identifier, and the
635 * function to call when the data element is destroyed.
638 * g_dataset_id_set_data:
639 * @l: the location identifying the dataset.
640 * @k: the #GQuark id to identify the data element.
641 * @d: the data element.
643 * Sets the data element associated with the given #GQuark id. Any
644 * previous data with the same key is removed, and its destroy function
648 * g_dataset_set_data:
649 * @l: the location identifying the dataset.
650 * @k: the string to identify the data element.
651 * @d: the data element.
653 * Sets the data corresponding to the given string identifier.
656 * g_dataset_id_remove_data:
657 * @l: the location identifying the dataset.
658 * @k: the #GQuark id identifying the data element.
660 * Removes a data element from a dataset. The data element's destroy
661 * function is called if it has been set.
664 * g_dataset_remove_data:
665 * @l: the location identifying the dataset.
666 * @k: the string identifying the data element.
668 * Removes a data element corresponding to a string. Its destroy
669 * function is called if it has been set.
672 g_dataset_id_set_data_full (gconstpointer dataset_location,
675 GDestroyNotify destroy_func)
679 g_return_if_fail (dataset_location != NULL);
681 g_return_if_fail (destroy_func == NULL);
685 g_return_if_fail (key_id > 0);
690 G_LOCK (g_dataset_global);
691 if (!g_dataset_location_ht)
692 g_data_initialize ();
694 dataset = g_dataset_lookup (dataset_location);
697 dataset = g_slice_new (GDataset);
698 dataset->location = dataset_location;
699 g_datalist_init (&dataset->datalist);
700 g_hash_table_insert (g_dataset_location_ht,
701 (gpointer) dataset->location,
705 g_data_set_internal (&dataset->datalist, key_id, data, destroy_func, dataset);
706 G_UNLOCK (g_dataset_global);
710 * g_datalist_id_set_data_full: (skip)
711 * @datalist: a datalist.
712 * @key_id: the #GQuark to identify the data element.
713 * @data: (nullable): the data element or %NULL to remove any previous element
714 * corresponding to @key_id.
715 * @destroy_func: (nullable): the function to call when the data element is
716 * removed. This function will be called with the data
717 * element and can be used to free any memory allocated
718 * for it. If @data is %NULL, then @destroy_func must
721 * Sets the data corresponding to the given #GQuark id, and the
722 * function to be called when the element is removed from the datalist.
723 * Any previous data with the same key is removed, and its destroy
724 * function is called.
727 * g_datalist_set_data_full: (skip)
729 * @k: the string to identify the data element.
730 * @d: (nullable): the data element, or %NULL to remove any previous element
731 * corresponding to @k.
732 * @f: (nullable): the function to call when the data element is removed.
733 * This function will be called with the data element and can be used to
734 * free any memory allocated for it. If @d is %NULL, then @f must
737 * Sets the data element corresponding to the given string identifier,
738 * and the function to be called when the data element is removed.
741 * g_datalist_id_set_data:
743 * @q: the #GQuark to identify the data element.
744 * @d: (nullable): the data element, or %NULL to remove any previous element
745 * corresponding to @q.
747 * Sets the data corresponding to the given #GQuark id. Any previous
748 * data with the same key is removed, and its destroy function is
752 * g_datalist_set_data:
754 * @k: the string to identify the data element.
755 * @d: (nullable): the data element, or %NULL to remove any previous element
756 * corresponding to @k.
758 * Sets the data element corresponding to the given string identifier.
761 * g_datalist_id_remove_data:
763 * @q: the #GQuark identifying the data element.
765 * Removes an element, using its #GQuark identifier.
768 * g_datalist_remove_data:
770 * @k: the string identifying the data element.
772 * Removes an element using its string identifier. The data element's
773 * destroy function is called if it has been set.
776 g_datalist_id_set_data_full (GData **datalist,
779 GDestroyNotify destroy_func)
781 g_return_if_fail (datalist != NULL);
783 g_return_if_fail (destroy_func == NULL);
787 g_return_if_fail (key_id > 0);
792 g_data_set_internal (datalist, key_id, data, destroy_func, NULL);
796 * g_datalist_id_remove_multiple:
797 * @datalist: a datalist
798 * @keys: (array length=n_keys): keys to remove
799 * @n_keys: length of @keys.
801 * Removes multiple keys from a datalist.
803 * This is more efficient than calling g_datalist_id_remove_data()
804 * multiple times in a row.
806 * Before 2.80, @n_keys had to be not larger than 16. Now it can be larger, but
807 * note that GData does a linear search, so an excessive number of keys will
813 g_datalist_id_remove_multiple (GData **datalist,
817 g_data_remove_internal (datalist, keys, n_keys);
821 * g_dataset_id_remove_no_notify: (skip)
822 * @dataset_location: (not nullable): the location identifying the dataset.
823 * @key_id: the #GQuark ID identifying the data element.
825 * Removes an element, without calling its destroy notification
828 * Returns: (nullable): the data previously stored at @key_id,
832 * g_dataset_remove_no_notify: (skip)
833 * @l: the location identifying the dataset.
834 * @k: the string identifying the data element.
836 * Removes an element, without calling its destroy notifier.
839 g_dataset_id_remove_no_notify (gconstpointer dataset_location,
842 gpointer ret_data = NULL;
844 g_return_val_if_fail (dataset_location != NULL, NULL);
846 G_LOCK (g_dataset_global);
847 if (key_id && g_dataset_location_ht)
851 dataset = g_dataset_lookup (dataset_location);
853 ret_data = g_data_set_internal (&dataset->datalist, key_id, NULL, (GDestroyNotify) 42, dataset);
855 G_UNLOCK (g_dataset_global);
861 * g_datalist_id_remove_no_notify: (skip)
862 * @datalist: a datalist.
863 * @key_id: the #GQuark identifying a data element.
865 * Removes an element, without calling its destroy notification
868 * Returns: (nullable): the data previously stored at @key_id,
872 * g_datalist_remove_no_notify: (skip)
874 * @k: the string identifying the data element.
876 * Removes an element, without calling its destroy notifier.
879 g_datalist_id_remove_no_notify (GData **datalist,
882 gpointer ret_data = NULL;
884 g_return_val_if_fail (datalist != NULL, NULL);
887 ret_data = g_data_set_internal (datalist, key_id, NULL, (GDestroyNotify) 42, NULL);
893 * g_datalist_id_update_atomic:
894 * @datalist: the data list
895 * @key_id: the key to add.
896 * @callback: (scope call): callback to update (set, remove, steal, update) the
898 * @user_data: the user data for @callback.
900 * Will call @callback while holding the lock on @datalist. Be careful to not
901 * end up calling into another data-list function, because the lock is not
902 * reentrant and deadlock will happen.
904 * The callback receives the current data and destroy function. If @key_id is
905 * currently not in @datalist, they will be %NULL. The callback can update
906 * those pointers, and @datalist will be updated with the result. Note that if
907 * callback modifies a received data, then it MUST steal it and take ownership
908 * on it. Possibly by freeing it with the provided destroy function.
910 * The point is to atomically access the entry, while holding a lock
911 * of @datalist. Without this, the user would have to hold their own mutex
912 * while handling @key_id entry.
914 * The return value of @callback is not used, except it becomes the return
915 * value of the function. This is an alternative to returning a result via
918 * Returns: the value returned by @callback.
923 g_datalist_id_update_atomic (GData **datalist,
925 GDataListUpdateAtomicFunc callback,
932 GDestroyNotify new_destroy;
934 gboolean to_unlock = TRUE;
936 d = g_datalist_lock_and_get (datalist);
938 data = datalist_find (d, key_id, &idx);
942 new_data = data->data;
943 new_destroy = data->destroy;
951 result = callback (key_id, &new_data, &new_destroy, user_data);
953 if (data && !new_data)
957 /* Remove. The callback indicates to drop the entry.
959 * The old data->data was stolen by callback(). */
960 datalist_remove (d, idx);
961 if (datalist_shrink (&d, &d_to_free))
963 g_datalist_unlock_and_set (datalist, d);
971 /* Update. The callback may have provided new pointers to an existing
974 * The old data was stolen by callback(). We only update the pointers and
976 data->data = new_data;
977 data->destroy = new_destroy;
979 else if (!data && !new_data)
981 /* Absent. No change. The entry didn't exist and still does not. */
985 /* Add. Add a new entry that didn't exist previously. */
986 if (datalist_append (&d, key_id, new_data, new_destroy))
988 g_datalist_unlock_and_set (datalist, d);
994 g_datalist_unlock (datalist);
1000 * g_dataset_id_get_data:
1001 * @dataset_location: (not nullable): the location identifying the dataset.
1002 * @key_id: the #GQuark id to identify the data element.
1004 * Gets the data element corresponding to a #GQuark.
1006 * Returns: (transfer none) (nullable): the data element corresponding to
1007 * the #GQuark, or %NULL if it is not found.
1010 * g_dataset_get_data:
1011 * @l: the location identifying the dataset.
1012 * @k: the string identifying the data element.
1014 * Gets the data element corresponding to a string.
1016 * Returns: (transfer none) (nullable): the data element corresponding to
1017 * the string, or %NULL if it is not found.
1020 g_dataset_id_get_data (gconstpointer dataset_location,
1023 gpointer retval = NULL;
1025 g_return_val_if_fail (dataset_location != NULL, NULL);
1027 G_LOCK (g_dataset_global);
1028 if (key_id && g_dataset_location_ht)
1032 dataset = g_dataset_lookup (dataset_location);
1034 retval = g_datalist_id_get_data (&dataset->datalist, key_id);
1036 G_UNLOCK (g_dataset_global);
1042 * g_datalist_id_get_data:
1043 * @datalist: a datalist.
1044 * @key_id: the #GQuark identifying a data element.
1046 * Retrieves the data element corresponding to @key_id.
1048 * Returns: (transfer none) (nullable): the data element, or %NULL if
1052 g_datalist_id_get_data (GData **datalist,
1055 return g_datalist_id_dup_data (datalist, key_id, NULL, NULL);
1060 * @data: the data to duplicate
1061 * @user_data: (closure): user data that was specified in
1062 * g_datalist_id_dup_data()
1064 * The type of functions that are used to 'duplicate' an object.
1065 * What this means depends on the context, it could just be
1066 * incrementing the reference count, if @data is a ref-counted
1069 * Returns: a duplicate of data
1073 * g_datalist_id_dup_data: (skip)
1074 * @datalist: location of a datalist
1075 * @key_id: the #GQuark identifying a data element
1076 * @dup_func: (scope call) (closure user_data) (nullable): function to
1077 * duplicate the old value
1078 * @user_data: passed as user_data to @dup_func
1080 * This is a variant of g_datalist_id_get_data() which
1081 * returns a 'duplicate' of the value. @dup_func defines the
1082 * meaning of 'duplicate' in this context, it could e.g.
1083 * take a reference on a ref-counted object.
1085 * If the @key_id is not set in the datalist then @dup_func
1086 * will be called with a %NULL argument.
1088 * Note that @dup_func is called while the datalist is locked, so it
1089 * is not allowed to read or modify the datalist.
1091 * This function can be useful to avoid races when multiple
1092 * threads are using the same datalist and the same key.
1094 * Returns: (nullable): the result of calling @dup_func on the value
1095 * associated with @key_id in @datalist, or %NULL if not set.
1096 * If @dup_func is %NULL, the value is returned unmodified.
1101 g_datalist_id_dup_data (GData **datalist,
1103 GDuplicateFunc dup_func,
1106 gpointer val = NULL;
1107 gpointer retval = NULL;
1111 d = g_datalist_lock_and_get (datalist);
1113 data = datalist_find (d, key_id, NULL);
1118 retval = dup_func (val, user_data);
1122 g_datalist_unlock (datalist);
1128 * g_datalist_id_replace_data: (skip)
1129 * @datalist: location of a datalist
1130 * @key_id: the #GQuark identifying a data element
1131 * @oldval: (nullable): the old value to compare against
1132 * @newval: (nullable): the new value to replace it with
1133 * @destroy: (nullable): destroy notify for the new value
1134 * @old_destroy: (out) (optional): destroy notify for the existing value
1136 * Compares the member that is associated with @key_id in
1137 * @datalist to @oldval, and if they are the same, replace
1138 * @oldval with @newval.
1140 * This is like a typical atomic compare-and-exchange
1141 * operation, for a member of @datalist.
1143 * If the previous value was replaced then ownership of the
1144 * old value (@oldval) is passed to the caller, including
1145 * the registered destroy notify for it (passed out in @old_destroy).
1146 * Its up to the caller to free this as they wish, which may
1147 * or may not include using @old_destroy as sometimes replacement
1148 * should not destroy the object in the normal way.
1150 * Returns: %TRUE if the existing value for @key_id was replaced
1151 * by @newval, %FALSE otherwise.
1156 g_datalist_id_replace_data (GData **datalist,
1160 GDestroyNotify destroy,
1161 GDestroyNotify *old_destroy)
1163 gpointer val = NULL;
1166 GData *d_to_free = NULL;
1167 gboolean set_d = FALSE;
1170 g_return_val_if_fail (datalist != NULL, FALSE);
1171 g_return_val_if_fail (key_id != 0, FALSE);
1174 *old_destroy = NULL;
1176 d = g_datalist_lock_and_get (datalist);
1178 data = datalist_find (d, key_id, &idx);
1185 *old_destroy = data->destroy;
1188 data->data = newval;
1189 data->destroy = destroy;
1193 datalist_remove (d, idx);
1194 if (datalist_shrink (&d, &d_to_free))
1200 if (val == NULL && oldval == NULL && newval != NULL)
1202 if (datalist_append (&d, key_id, newval, destroy))
1209 g_datalist_unlock_and_set (datalist, d);
1211 g_datalist_unlock (datalist);
1216 return val == oldval;
1220 * g_datalist_get_data:
1221 * @datalist: a datalist.
1222 * @key: the string identifying a data element.
1224 * Gets a data element, using its string identifier. This is slower than
1225 * g_datalist_id_get_data() because it compares strings.
1227 * Returns: (transfer none) (nullable): the data element, or %NULL if it
1231 g_datalist_get_data (GData **datalist,
1234 gpointer res = NULL;
1236 GDataElt *data, *data_end;
1238 g_return_val_if_fail (datalist != NULL, NULL);
1240 d = g_datalist_lock_and_get (datalist);
1244 data_end = data + d->len;
1245 while (data < data_end)
1247 /* Here we intentionally compare by strings, instead of calling
1248 * g_quark_try_string() first.
1250 * See commit 1cceda49b60b ('Make g_datalist_get_data not look up the
1253 if (g_strcmp0 (g_quark_to_string (data->key), key) == 0)
1262 g_datalist_unlock (datalist);
1269 * @key_id: the #GQuark id to identifying the data element.
1270 * @data: the data element.
1271 * @user_data: (closure): user data passed to g_dataset_foreach().
1273 * Specifies the type of function passed to g_dataset_foreach(). It is
1274 * called with each #GQuark id and associated data element, together
1275 * with the @user_data parameter supplied to g_dataset_foreach().
1279 * g_dataset_foreach:
1280 * @dataset_location: (not nullable): the location identifying the dataset.
1281 * @func: (scope call) (closure user_data): the function to call for each data element.
1282 * @user_data: user data to pass to the function.
1284 * Calls the given function for each data element which is associated
1285 * with the given location. Note that this function is NOT thread-safe.
1286 * So unless @dataset_location can be protected from any modifications
1287 * during invocation of this function, it should not be called.
1289 * @func can make changes to the dataset, but the iteration will not
1290 * reflect changes made during the g_dataset_foreach() call, other
1291 * than skipping over elements that are removed.
1294 g_dataset_foreach (gconstpointer dataset_location,
1295 GDataForeachFunc func,
1300 g_return_if_fail (dataset_location != NULL);
1301 g_return_if_fail (func != NULL);
1303 G_LOCK (g_dataset_global);
1304 if (g_dataset_location_ht)
1306 dataset = g_dataset_lookup (dataset_location);
1307 G_UNLOCK (g_dataset_global);
1309 g_datalist_foreach (&dataset->datalist, func, user_data);
1313 G_UNLOCK (g_dataset_global);
1318 * g_datalist_foreach:
1319 * @datalist: a datalist.
1320 * @func: (scope call) (closure user_data): the function to call for each data element.
1321 * @user_data: user data to pass to the function.
1323 * Calls the given function for each data element of the datalist. The
1324 * function is called with each data element's #GQuark id and data,
1325 * together with the given @user_data parameter. Note that this
1326 * function is NOT thread-safe. So unless @datalist can be protected
1327 * from any modifications during invocation of this function, it should
1330 * @func can make changes to @datalist, but the iteration will not
1331 * reflect changes made during the g_datalist_foreach() call, other
1332 * than skipping over elements that are removed.
1335 g_datalist_foreach (GData **datalist,
1336 GDataForeachFunc func,
1343 g_return_if_fail (datalist != NULL);
1344 g_return_if_fail (func != NULL);
1346 d = G_DATALIST_GET_POINTER (datalist);
1350 /* We make a copy of the keys so that we can handle it changing
1353 keys = g_new (GQuark, len);
1354 for (i = 0; i < len; i++)
1355 keys[i] = d->data[i].key;
1357 for (i = 0; i < len; i++)
1359 /* A previous callback might have removed a later item, so always check that
1360 it still exists before calling */
1361 d = G_DATALIST_GET_POINTER (datalist);
1365 for (j = 0; j < d->len; j++)
1367 if (d->data[j].key == keys[i]) {
1368 func (d->data[i].key, d->data[i].data, user_data);
1377 * g_datalist_init: (skip)
1378 * @datalist: a pointer to a pointer to a datalist.
1380 * Resets the datalist to %NULL. It does not free any memory or call
1381 * any destroy functions.
1384 g_datalist_init (GData **datalist)
1386 g_return_if_fail (datalist != NULL);
1388 g_atomic_pointer_set (datalist, NULL);
1392 * g_datalist_set_flags:
1393 * @datalist: pointer to the location that holds a list
1394 * @flags: the flags to turn on. The values of the flags are
1395 * restricted by %G_DATALIST_FLAGS_MASK (currently
1396 * 3; giving two possible boolean flags).
1397 * A value for @flags that doesn't fit within the mask is
1400 * Turns on flag values for a data list. This function is used
1401 * to keep a small number of boolean flags in an object with
1402 * a data list without using any additional space. It is
1403 * not generally useful except in circumstances where space
1404 * is very tight. (It is used in the base #GObject type, for
1410 g_datalist_set_flags (GData **datalist,
1413 g_return_if_fail (datalist != NULL);
1414 g_return_if_fail ((flags & ~G_DATALIST_FLAGS_MASK) == 0);
1416 g_atomic_pointer_or (datalist, (gsize)flags);
1420 * g_datalist_unset_flags:
1421 * @datalist: pointer to the location that holds a list
1422 * @flags: the flags to turn off. The values of the flags are
1423 * restricted by %G_DATALIST_FLAGS_MASK (currently
1424 * 3: giving two possible boolean flags).
1425 * A value for @flags that doesn't fit within the mask is
1428 * Turns off flag values for a data list. See g_datalist_unset_flags()
1433 g_datalist_unset_flags (GData **datalist,
1436 g_return_if_fail (datalist != NULL);
1437 g_return_if_fail ((flags & ~G_DATALIST_FLAGS_MASK) == 0);
1439 g_atomic_pointer_and (datalist, ~(gsize)flags);
1443 * g_datalist_get_flags:
1444 * @datalist: pointer to the location that holds a list
1446 * Gets flags values packed in together with the datalist.
1447 * See g_datalist_set_flags().
1449 * Returns: the flags of the datalist
1454 g_datalist_get_flags (GData **datalist)
1456 g_return_val_if_fail (datalist != NULL, 0);
1458 return G_DATALIST_GET_FLAGS (datalist); /* atomic macro */
1461 /* HOLDS: g_dataset_global_lock */
1463 g_data_initialize (void)
1465 g_return_if_fail (g_dataset_location_ht == NULL);
1467 g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL);
1468 g_dataset_cached = NULL;