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 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
24 * file for a list of people on the GLib Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 * MT safe ; except for g_data*_foreach()
41 #include "gdatasetprivate.h"
44 #include "gstrfuncs.h"
45 #include "gtestutils.h"
47 #include "glib_trace.h"
52 * @short_description: associate groups of data elements with
53 * particular memory locations
55 * Datasets associate groups of data elements with particular memory
56 * locations. These are useful if you need to associate data with a
57 * structure returned from an external library. Since you cannot modify
58 * the structure, you use its location in memory as the key into a
59 * dataset, where you can associate any number of data elements with it.
61 * There are two forms of most of the dataset functions. The first form
62 * uses strings to identify the data elements associated with a
63 * location. The second form uses #GQuark identifiers, which are
64 * created with a call to g_quark_from_string() or
65 * g_quark_from_static_string(). The second form is quicker, since it
66 * does not require looking up the string in the hash table of #GQuark
69 * There is no function to create a dataset. It is automatically
70 * created as soon as you add elements to it.
72 * To add data elements to a dataset use g_dataset_id_set_data(),
73 * g_dataset_id_set_data_full(), g_dataset_set_data() and
74 * g_dataset_set_data_full().
76 * To get data elements from a dataset use g_dataset_id_get_data() and
77 * g_dataset_get_data().
79 * To iterate over all data elements in a dataset use
80 * g_dataset_foreach() (not thread-safe).
82 * To remove data elements from a dataset use
83 * g_dataset_id_remove_data() and g_dataset_remove_data().
85 * To destroy a dataset, use g_dataset_destroy().
90 * @title: Keyed Data Lists
91 * @short_description: lists of data elements which are accessible by a
92 * string or GQuark identifier
94 * Keyed data lists provide lists of arbitrary data elements which can
95 * be accessed either with a string or with a #GQuark corresponding to
98 * The #GQuark methods are quicker, since the strings have to be
99 * converted to #GQuarks anyway.
101 * Data lists are used for associating arbitrary data with #GObjects,
102 * using g_object_set_data() and related functions.
104 * To create a datalist, use g_datalist_init().
106 * To add data elements to a datalist use g_datalist_id_set_data(),
107 * g_datalist_id_set_data_full(), g_datalist_set_data() and
108 * g_datalist_set_data_full().
110 * To get data elements from a datalist use g_datalist_id_get_data()
111 * and g_datalist_get_data().
113 * To iterate over all data elements in a datalist use
114 * g_datalist_foreach() (not thread-safe).
116 * To remove data elements from a datalist use
117 * g_datalist_id_remove_data() and g_datalist_remove_data().
119 * To remove all data elements from a datalist, use g_datalist_clear().
125 * The #GData struct is an opaque data structure to represent a <link
126 * linkend="glib-Keyed-Data-Lists">Keyed Data List</link>. It should
127 * only be accessed via the following functions.
132 * @data: the data element.
134 * Specifies the type of function which is called when a data element
135 * is destroyed. It is passed the pointer to the data element and
136 * should free any memory and resources allocated for it.
139 /* --- defines --- */
140 #define G_QUARK_BLOCK_SIZE (2048)
142 #define G_DATALIST_FLAGS_MASK_INTERNAL 0x7
144 /* datalist pointer accesses have to be carried out atomically */
145 #define G_DATALIST_GET_POINTER(datalist) \
146 ((GData*) ((gsize) g_atomic_pointer_get (datalist) & ~(gsize) G_DATALIST_FLAGS_MASK_INTERNAL))
148 #define G_DATALIST_SET_POINTER(datalist, pointer) G_STMT_START { \
149 gpointer _oldv, _newv; \
151 _oldv = g_atomic_pointer_get (datalist); \
152 _newv = (gpointer) (((gsize) _oldv & G_DATALIST_FLAGS_MASK_INTERNAL) | (gsize) pointer); \
153 } while (!g_atomic_pointer_compare_and_exchange ((void**) datalist, _oldv, _newv)); \
156 /* --- structures --- */
160 GDestroyNotify destroy;
163 typedef struct _GDataset GDataset;
166 guint32 len; /* Number of elements */
167 guint32 alloc; /* Number of allocated elements */
168 GDataElt data[1]; /* Flexible array */
173 gconstpointer location;
178 /* --- prototypes --- */
179 static inline GDataset* g_dataset_lookup (gconstpointer dataset_location);
180 static inline void g_datalist_clear_i (GData **datalist);
181 static void g_dataset_destroy_internal (GDataset *dataset);
182 static inline gpointer g_data_set_internal (GData **datalist,
185 GDestroyNotify destroy_func,
187 static void g_data_initialize (void);
190 * Each standalone GDataList is protected by a bitlock in the datalist pointer,
191 * which protects that modification of the non-flags part of the datalist pointer
192 * and the contents of the datalist.
194 * For GDataSet we have a global lock g_dataset_global that protects
195 * the global dataset hash and cache, and additionally it protects the
196 * datalist such that we can avoid to use the bit lock in a few places
200 /* --- variables --- */
201 G_LOCK_DEFINE_STATIC (g_dataset_global);
202 static GHashTable *g_dataset_location_ht = NULL;
203 static GDataset *g_dataset_cached = NULL; /* should this be
206 /* --- functions --- */
208 #define DATALIST_LOCK_BIT 2
211 g_datalist_lock (GData **datalist)
213 g_pointer_bit_lock ((void **)datalist, DATALIST_LOCK_BIT);
217 g_datalist_unlock (GData **datalist)
219 g_pointer_bit_unlock ((void **)datalist, DATALIST_LOCK_BIT);
222 /* Called with the datalist lock held, or the dataset global
223 * lock for dataset lists
226 g_datalist_clear_i (GData **datalist)
231 data = G_DATALIST_GET_POINTER (datalist);
232 G_DATALIST_SET_POINTER (datalist, NULL);
236 G_UNLOCK (g_dataset_global);
237 for (i = 0; i < data->len; i++)
239 if (data->data[i].data && data->data[i].destroy)
240 data->data[i].destroy (data->data[i].data);
242 G_LOCK (g_dataset_global);
251 * @datalist: a datalist.
253 * Frees all the data elements of the datalist.
254 * The data elements' destroy functions are called
255 * if they have been set.
258 g_datalist_clear (GData **datalist)
263 g_return_if_fail (datalist != NULL);
265 g_datalist_lock (datalist);
267 data = G_DATALIST_GET_POINTER (datalist);
268 G_DATALIST_SET_POINTER (datalist, NULL);
270 g_datalist_unlock (datalist);
274 for (i = 0; i < data->len; i++)
276 if (data->data[i].data && data->data[i].destroy)
277 data->data[i].destroy (data->data[i].data);
284 /* HOLDS: g_dataset_global_lock */
285 static inline GDataset*
286 g_dataset_lookup (gconstpointer dataset_location)
288 register GDataset *dataset;
290 if (g_dataset_cached && g_dataset_cached->location == dataset_location)
291 return g_dataset_cached;
293 dataset = g_hash_table_lookup (g_dataset_location_ht, dataset_location);
295 g_dataset_cached = dataset;
300 /* HOLDS: g_dataset_global_lock */
302 g_dataset_destroy_internal (GDataset *dataset)
304 register gconstpointer dataset_location;
306 dataset_location = dataset->location;
309 if (G_DATALIST_GET_POINTER(&dataset->datalist) == NULL)
311 if (dataset == g_dataset_cached)
312 g_dataset_cached = NULL;
313 g_hash_table_remove (g_dataset_location_ht, dataset_location);
314 g_slice_free (GDataset, dataset);
318 g_datalist_clear_i (&dataset->datalist);
319 dataset = g_dataset_lookup (dataset_location);
325 * @dataset_location: the location identifying the dataset.
327 * Destroys the dataset, freeing all memory allocated, and calling any
328 * destroy functions set for data elements.
331 g_dataset_destroy (gconstpointer dataset_location)
333 g_return_if_fail (dataset_location != NULL);
335 G_LOCK (g_dataset_global);
336 if (g_dataset_location_ht)
338 register GDataset *dataset;
340 dataset = g_dataset_lookup (dataset_location);
342 g_dataset_destroy_internal (dataset);
344 G_UNLOCK (g_dataset_global);
347 /* HOLDS: g_dataset_global_lock if dataset != null */
348 static inline gpointer
349 g_data_set_internal (GData **datalist,
352 GDestroyNotify new_destroy_func,
356 GDataElt old, *data, *data_last, *data_end;
358 g_datalist_lock (datalist);
360 d = G_DATALIST_GET_POINTER (datalist);
362 if (new_data == NULL) /* remove */
367 data_last = data + d->len - 1;
368 while (data <= data_last)
370 if (data->key == key_id)
373 if (data != data_last)
377 /* We don't bother to shrink, but if all data are now gone
378 * we at least free the memory
382 G_DATALIST_SET_POINTER (datalist, NULL);
384 /* datalist may be situated in dataset, so must not be
385 * unlocked after we free it
387 g_datalist_unlock (datalist);
389 /* the dataset destruction *must* be done
390 * prior to invocation of the data destroy function
393 g_dataset_destroy_internal (dataset);
397 g_datalist_unlock (datalist);
400 /* We found and removed an old value
401 * the GData struct *must* already be unlinked
402 * when invoking the destroy function.
403 * we use (new_data==NULL && new_destroy_func!=NULL) as
404 * a special hint combination to "steal"
405 * data without destroy notification
407 if (old.destroy && !new_destroy_func)
410 G_UNLOCK (g_dataset_global);
411 old.destroy (old.data);
413 G_LOCK (g_dataset_global);
429 data_end = data + d->len;
430 while (data < data_end)
432 if (data->key == key_id)
436 data->data = new_data;
437 data->destroy = new_destroy_func;
438 g_datalist_unlock (datalist);
443 data->data = new_data;
444 data->destroy = new_destroy_func;
446 g_datalist_unlock (datalist);
448 /* We found and replaced an old value
449 * the GData struct *must* already be unlinked
450 * when invoking the destroy function.
453 G_UNLOCK (g_dataset_global);
454 old.destroy (old.data);
456 G_LOCK (g_dataset_global);
464 /* The key was not found, insert it */
468 d = g_malloc (sizeof (GData));
472 else if (d->len == d->alloc)
474 d->alloc = d->alloc * 2;
475 d = g_realloc (d, sizeof (GData) + (d->alloc - 1) * sizeof (GDataElt));
478 G_DATALIST_SET_POINTER (datalist, d);
480 d->data[d->len].key = key_id;
481 d->data[d->len].data = new_data;
482 d->data[d->len].destroy = new_destroy_func;
486 g_datalist_unlock (datalist);
493 * g_dataset_id_set_data_full:
494 * @dataset_location: the location identifying the dataset.
495 * @key_id: the #GQuark id to identify the data element.
496 * @data: the data element.
497 * @destroy_func: the function to call when the data element is
498 * removed. This function will be called with the data
499 * element and can be used to free any memory allocated
502 * Sets the data element associated with the given #GQuark id, and also
503 * the function to call when the data element is destroyed. Any
504 * previous data with the same key is removed, and its destroy function
508 * g_dataset_set_data_full:
509 * @l: the location identifying the dataset.
510 * @k: the string to identify the data element.
511 * @d: the data element.
512 * @f: the function to call when the data element is removed. This
513 * function will be called with the data element and can be used to
514 * free any memory allocated for it.
516 * Sets the data corresponding to the given string identifier, and the
517 * function to call when the data element is destroyed.
520 * g_dataset_id_set_data:
521 * @l: the location identifying the dataset.
522 * @k: the #GQuark id to identify the data element.
523 * @d: the data element.
525 * Sets the data element associated with the given #GQuark id. Any
526 * previous data with the same key is removed, and its destroy function
530 * g_dataset_set_data:
531 * @l: the location identifying the dataset.
532 * @k: the string to identify the data element.
533 * @d: the data element.
535 * Sets the data corresponding to the given string identifier.
538 * g_dataset_id_remove_data:
539 * @l: the location identifying the dataset.
540 * @k: the #GQuark id identifying the data element.
542 * Removes a data element from a dataset. The data element's destroy
543 * function is called if it has been set.
546 * g_dataset_remove_data:
547 * @l: the location identifying the dataset.
548 * @k: the string identifying the data element.
550 * Removes a data element corresponding to a string. Its destroy
551 * function is called if it has been set.
554 g_dataset_id_set_data_full (gconstpointer dataset_location,
557 GDestroyNotify destroy_func)
559 register GDataset *dataset;
561 g_return_if_fail (dataset_location != NULL);
563 g_return_if_fail (destroy_func == NULL);
567 g_return_if_fail (key_id > 0);
572 G_LOCK (g_dataset_global);
573 if (!g_dataset_location_ht)
574 g_data_initialize ();
576 dataset = g_dataset_lookup (dataset_location);
579 dataset = g_slice_new (GDataset);
580 dataset->location = dataset_location;
581 g_datalist_init (&dataset->datalist);
582 g_hash_table_insert (g_dataset_location_ht,
583 (gpointer) dataset->location,
587 g_data_set_internal (&dataset->datalist, key_id, data, destroy_func, dataset);
588 G_UNLOCK (g_dataset_global);
592 * g_datalist_id_set_data_full:
593 * @datalist: a datalist.
594 * @key_id: the #GQuark to identify the data element.
595 * @data: (allow-none): the data element or %NULL to remove any previous element
596 * corresponding to @key_id.
597 * @destroy_func: the function to call when the data element is
598 * removed. This function will be called with the data
599 * element and can be used to free any memory allocated
600 * for it. If @data is %NULL, then @destroy_func must
603 * Sets the data corresponding to the given #GQuark id, and the
604 * function to be called when the element is removed from the datalist.
605 * Any previous data with the same key is removed, and its destroy
606 * function is called.
609 * g_datalist_set_data_full:
611 * @k: the string to identify the data element.
612 * @d: (allow-none): the data element, or %NULL to remove any previous element
613 * corresponding to @k.
614 * @f: the function to call when the data element is removed. This
615 * function will be called with the data element and can be used to
616 * free any memory allocated for it. If @d is %NULL, then @f must
619 * Sets the data element corresponding to the given string identifier,
620 * and the function to be called when the data element is removed.
623 * g_datalist_id_set_data:
625 * @q: the #GQuark to identify the data element.
626 * @d: (allow-none): the data element, or %NULL to remove any previous element
627 * corresponding to @q.
629 * Sets the data corresponding to the given #GQuark id. Any previous
630 * data with the same key is removed, and its destroy function is
634 * g_datalist_set_data:
636 * @k: the string to identify the data element.
637 * @d: (allow-none): the data element, or %NULL to remove any previous element
638 * corresponding to @k.
640 * Sets the data element corresponding to the given string identifier.
643 * g_datalist_id_remove_data:
645 * @q: the #GQuark identifying the data element.
647 * Removes an element, using its #GQuark identifier.
650 * g_datalist_remove_data:
652 * @k: the string identifying the data element.
654 * Removes an element using its string identifier. The data element's
655 * destroy function is called if it has been set.
658 g_datalist_id_set_data_full (GData **datalist,
661 GDestroyNotify destroy_func)
663 g_return_if_fail (datalist != NULL);
665 g_return_if_fail (destroy_func == NULL);
669 g_return_if_fail (key_id > 0);
674 g_data_set_internal (datalist, key_id, data, destroy_func, NULL);
678 * g_dataset_id_remove_no_notify:
679 * @dataset_location: the location identifying the dataset.
680 * @key_id: the #GQuark ID identifying the data element.
682 * Removes an element, without calling its destroy notification
685 * Returns: the data previously stored at @key_id, or %NULL if none.
688 * g_dataset_remove_no_notify:
689 * @l: the location identifying the dataset.
690 * @k: the string identifying the data element.
692 * Removes an element, without calling its destroy notifier.
695 g_dataset_id_remove_no_notify (gconstpointer dataset_location,
698 gpointer ret_data = NULL;
700 g_return_val_if_fail (dataset_location != NULL, NULL);
702 G_LOCK (g_dataset_global);
703 if (key_id && g_dataset_location_ht)
707 dataset = g_dataset_lookup (dataset_location);
709 ret_data = g_data_set_internal (&dataset->datalist, key_id, NULL, (GDestroyNotify) 42, dataset);
711 G_UNLOCK (g_dataset_global);
717 * g_datalist_id_remove_no_notify:
718 * @datalist: a datalist.
719 * @key_id: the #GQuark identifying a data element.
721 * Removes an element, without calling its destroy notification
724 * Returns: the data previously stored at @key_id, or %NULL if none.
727 * g_datalist_remove_no_notify:
729 * @k: the string identifying the data element.
731 * Removes an element, without calling its destroy notifier.
734 g_datalist_id_remove_no_notify (GData **datalist,
737 gpointer ret_data = NULL;
739 g_return_val_if_fail (datalist != NULL, NULL);
742 ret_data = g_data_set_internal (datalist, key_id, NULL, (GDestroyNotify) 42, NULL);
748 * g_dataset_id_get_data:
749 * @dataset_location: the location identifying the dataset.
750 * @key_id: the #GQuark id to identify the data element.
752 * Gets the data element corresponding to a #GQuark.
754 * Returns: the data element corresponding to the #GQuark, or %NULL if
758 * g_dataset_get_data:
759 * @l: the location identifying the dataset.
760 * @k: the string identifying the data element.
762 * Gets the data element corresponding to a string.
764 * Returns: the data element corresponding to the string, or %NULL if
768 g_dataset_id_get_data (gconstpointer dataset_location,
771 gpointer retval = NULL;
773 g_return_val_if_fail (dataset_location != NULL, NULL);
775 G_LOCK (g_dataset_global);
776 if (key_id && g_dataset_location_ht)
780 dataset = g_dataset_lookup (dataset_location);
782 retval = g_datalist_id_get_data (&dataset->datalist, key_id);
784 G_UNLOCK (g_dataset_global);
790 * g_datalist_id_get_data:
791 * @datalist: a datalist.
792 * @key_id: the #GQuark identifying a data element.
794 * Retrieves the data element corresponding to @key_id.
796 * Returns: the data element, or %NULL if it is not found.
799 g_datalist_id_get_data (GData **datalist,
802 return g_datalist_id_dup_data (datalist, key_id, NULL, NULL);
807 * @data: the data to duplicate
808 * @user_data: user data that was specified in g_datalist_id_dup_data()
810 * The type of functions that are used to 'duplicate' an object.
811 * What this means depends on the context, it could just be
812 * incrementing the reference count, if @data is a ref-counted
815 * Returns: a duplicate of data
819 * g_datalist_id_dup_data:
820 * @datalist: location of a datalist
821 * @key_id: the #GQuark identifying a data element
822 * @dup_func: (allow-none): function to duplicate the old value
823 * @user_data: (allow-none): passed as user_data to @dup_func
825 * This is a variant of g_datalist_id_get_data() which
826 * returns a 'duplicate' of the value. @dup_func defines the
827 * meaning of 'duplicate' in this context, it could e.g.
828 * take a reference on a ref-counted object.
830 * If the @key_id is not set in the datalist then @dup_func
831 * will be called with a %NULL argument.
833 * Note that @dup_func is called while the datalist is locked, so it
834 * is not allowed to read or modify the datalist.
836 * This function can be useful to avoid races when multiple
837 * threads are using the same datalist and the same key.
839 * Returns: the result of calling @dup_func on the value
840 * associated with @key_id in @datalist, or %NULL if not set.
841 * If @dup_func is %NULL, the value is returned unmodified.
846 g_datalist_id_dup_data (GData **datalist,
848 GDuplicateFunc dup_func,
852 gpointer retval = NULL;
854 GDataElt *data, *data_end;
856 g_return_val_if_fail (datalist != NULL, NULL);
857 g_return_val_if_fail (key_id != 0, NULL);
859 g_datalist_lock (datalist);
861 d = G_DATALIST_GET_POINTER (datalist);
865 data_end = data + d->len - 1;
866 while (data <= data_end)
868 if (data->key == key_id)
878 retval = dup_func (val, user_data);
882 g_datalist_unlock (datalist);
888 * g_datalist_id_replace_data:
889 * @datalist: location of a datalist
890 * @key_id: the #GQuark identifying a data element
891 * @oldval: (allow-none): the old value to compare against
892 * @newval: (allow-none): the new value to replace it with
893 * @destroy: (allow-none): destroy notify for the new value
894 * @old_destroy: (allow-none): destroy notify for the existing value
896 * Compares the member that is associated with @key_id in
897 * @datalist to @oldval, and if they are the same, replace
898 * @oldval with @newval.
900 * This is like a typical atomic compare-and-exchange
901 * operation, for a member of @datalist.
903 * If the previous value was replaced then ownership of the
904 * old value (@oldval) is passed to the caller, including
905 * the registred destroy notify for it (passed out in @old_destroy).
906 * Its up to the caller to free this as he wishes, which may
907 * or may not include using @old_destroy as sometimes replacement
908 * should not destroy the object in the normal way.
910 * Return: %TRUE if the existing value for @key_id was replaced
911 * by @newval, %FALSE otherwise.
916 g_datalist_id_replace_data (GData **datalist,
920 GDestroyNotify destroy,
921 GDestroyNotify *old_destroy)
925 GDataElt *data, *data_end;
927 g_return_val_if_fail (datalist != NULL, FALSE);
928 g_return_val_if_fail (key_id != 0, FALSE);
933 g_datalist_lock (datalist);
935 d = G_DATALIST_GET_POINTER (datalist);
939 data_end = data + d->len - 1;
940 while (data <= data_end)
942 if (data->key == key_id)
948 *old_destroy = data->destroy;
952 data->destroy = destroy;
956 if (data != data_end)
960 /* We don't bother to shrink, but if all data are now gone
961 * we at least free the memory
965 G_DATALIST_SET_POINTER (datalist, NULL);
976 if (val == NULL && oldval == NULL && newval != NULL)
984 d = g_malloc (sizeof (GData));
988 else if (d->len == d->alloc)
990 d->alloc = d->alloc * 2;
991 d = g_realloc (d, sizeof (GData) + (d->alloc - 1) * sizeof (GDataElt));
994 G_DATALIST_SET_POINTER (datalist, d);
996 d->data[d->len].key = key_id;
997 d->data[d->len].data = newval;
998 d->data[d->len].destroy = destroy;
1002 g_datalist_unlock (datalist);
1004 return val == oldval;
1008 * g_datalist_get_data:
1009 * @datalist: a datalist.
1010 * @key: the string identifying a data element.
1012 * Gets a data element, using its string identifier. This is slower than
1013 * g_datalist_id_get_data() because it compares strings.
1015 * Returns: the data element, or %NULL if it is not found.
1018 g_datalist_get_data (GData **datalist,
1021 gpointer res = NULL;
1023 GDataElt *data, *data_end;
1025 g_return_val_if_fail (datalist != NULL, NULL);
1027 g_datalist_lock (datalist);
1029 d = G_DATALIST_GET_POINTER (datalist);
1033 data_end = data + d->len;
1034 while (data < data_end)
1036 if (strcmp (g_quark_to_string (data->key), key) == 0)
1045 g_datalist_unlock (datalist);
1052 * @key_id: the #GQuark id to identifying the data element.
1053 * @data: the data element.
1054 * @user_data: user data passed to g_dataset_foreach().
1056 * Specifies the type of function passed to g_dataset_foreach(). It is
1057 * called with each #GQuark id and associated data element, together
1058 * with the @user_data parameter supplied to g_dataset_foreach().
1062 * g_dataset_foreach:
1063 * @dataset_location: the location identifying the dataset.
1064 * @func: the function to call for each data element.
1065 * @user_data: user data to pass to the function.
1067 * Calls the given function for each data element which is associated
1068 * with the given location. Note that this function is NOT thread-safe.
1069 * So unless @datalist can be protected from any modifications during
1070 * invocation of this function, it should not be called.
1073 g_dataset_foreach (gconstpointer dataset_location,
1074 GDataForeachFunc func,
1077 register GDataset *dataset;
1079 g_return_if_fail (dataset_location != NULL);
1080 g_return_if_fail (func != NULL);
1082 G_LOCK (g_dataset_global);
1083 if (g_dataset_location_ht)
1085 dataset = g_dataset_lookup (dataset_location);
1086 G_UNLOCK (g_dataset_global);
1088 g_datalist_foreach (&dataset->datalist, func, user_data);
1092 G_UNLOCK (g_dataset_global);
1097 * g_datalist_foreach:
1098 * @datalist: a datalist.
1099 * @func: the function to call for each data element.
1100 * @user_data: user data to pass to the function.
1102 * Calls the given function for each data element of the datalist. The
1103 * function is called with each data element's #GQuark id and data,
1104 * together with the given @user_data parameter. Note that this
1105 * function is NOT thread-safe. So unless @datalist can be protected
1106 * from any modifications during invocation of this function, it should
1110 g_datalist_foreach (GData **datalist,
1111 GDataForeachFunc func,
1118 g_return_if_fail (datalist != NULL);
1119 g_return_if_fail (func != NULL);
1121 d = G_DATALIST_GET_POINTER (datalist);
1125 /* We make a copy of the keys so that we can handle it changing
1128 keys = g_new (GQuark, len);
1129 for (i = 0; i < len; i++)
1130 keys[i] = d->data[i].key;
1132 for (i = 0; i < len; i++)
1134 /* A previous callback might have removed a later item, so always check that
1135 it still exists before calling */
1136 d = G_DATALIST_GET_POINTER (datalist);
1140 for (j = 0; j < d->len; j++)
1142 if (d->data[j].key == keys[i]) {
1143 func (d->data[i].key, d->data[i].data, user_data);
1153 * @datalist: a pointer to a pointer to a datalist.
1155 * Resets the datalist to %NULL. It does not free any memory or call
1156 * any destroy functions.
1159 g_datalist_init (GData **datalist)
1161 g_return_if_fail (datalist != NULL);
1163 g_atomic_pointer_set (datalist, NULL);
1167 * g_datalist_set_flags:
1168 * @datalist: pointer to the location that holds a list
1169 * @flags: the flags to turn on. The values of the flags are
1170 * restricted by %G_DATALIST_FLAGS_MASK (currently
1171 * 3; giving two possible boolean flags).
1172 * A value for @flags that doesn't fit within the mask is
1175 * Turns on flag values for a data list. This function is used
1176 * to keep a small number of boolean flags in an object with
1177 * a data list without using any additional space. It is
1178 * not generally useful except in circumstances where space
1179 * is very tight. (It is used in the base #GObject type, for
1185 g_datalist_set_flags (GData **datalist,
1188 g_return_if_fail (datalist != NULL);
1189 g_return_if_fail ((flags & ~G_DATALIST_FLAGS_MASK) == 0);
1191 g_atomic_pointer_or (datalist, (gsize)flags);
1195 * g_datalist_unset_flags:
1196 * @datalist: pointer to the location that holds a list
1197 * @flags: the flags to turn off. The values of the flags are
1198 * restricted by %G_DATALIST_FLAGS_MASK (currently
1199 * 3: giving two possible boolean flags).
1200 * A value for @flags that doesn't fit within the mask is
1203 * Turns off flag values for a data list. See g_datalist_unset_flags()
1208 g_datalist_unset_flags (GData **datalist,
1211 g_return_if_fail (datalist != NULL);
1212 g_return_if_fail ((flags & ~G_DATALIST_FLAGS_MASK) == 0);
1214 g_atomic_pointer_and (datalist, ~(gsize)flags);
1218 * g_datalist_get_flags:
1219 * @datalist: pointer to the location that holds a list
1221 * Gets flags values packed in together with the datalist.
1222 * See g_datalist_set_flags().
1224 * Return value: the flags of the datalist
1229 g_datalist_get_flags (GData **datalist)
1231 g_return_val_if_fail (datalist != NULL, 0);
1233 return G_DATALIST_GET_FLAGS (datalist); /* atomic macro */
1236 /* HOLDS: g_dataset_global_lock */
1238 g_data_initialize (void)
1240 g_return_if_fail (g_dataset_location_ht == NULL);
1242 g_dataset_location_ht = g_hash_table_new (g_direct_hash, NULL);
1243 g_dataset_cached = NULL;