1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GHook: Callback maintenance functions
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, see <http://www.gnu.org/licenses/>.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gtestutils.h"
41 * @title: Hook Functions
42 * @short_description: support for manipulating lists of hook functions
44 * The #GHookList, #GHook and their related functions provide support for
45 * lists of hook functions. Functions can be added and removed from the lists,
46 * and the list of hook functions can be invoked.
51 * @seq_id: the next free #GHook id
52 * @hook_size: the size of the #GHookList elements, in bytes
53 * @is_setup: 1 if the #GHookList has been initialized
54 * @hooks: the first #GHook element in the list
56 * @finalize_hook: the function to call to finalize a #GHook element.
57 * The default behaviour is to call the hooks @destroy function
60 * The #GHookList struct represents a list of hook functions.
65 * @hook_list: a #GHookList
66 * @hook: the hook in @hook_list that gets finalized
68 * Defines the type of function to be called when a hook in a
69 * list of hooks gets finalized.
74 * @G_HOOK_FLAG_ACTIVE: set if the hook has not been destroyed
75 * @G_HOOK_FLAG_IN_CALL: set if the hook is currently being run
76 * @G_HOOK_FLAG_MASK: A mask covering all bits reserved for
77 * hook flags; see %G_HOOK_FLAG_USER_SHIFT
79 * Flags used internally in the #GHook implementation.
86 * Gets the flags of a hook.
90 * G_HOOK_FLAG_USER_SHIFT:
92 * The position of the first bit which is not reserved for internal
93 * use be the #GHook implementation, i.e.
94 * `1 << G_HOOK_FLAG_USER_SHIFT` is the first
95 * bit which can be used for application-defined flags.
102 * Casts a pointer to a `GHook*`.
109 * Returns %TRUE if the #GHook is valid, i.e. it is in a #GHookList,
110 * it is active and it has not been destroyed.
112 * Returns: %TRUE if the #GHook is valid
119 * Returns %TRUE if the #GHook is active, which is normally the case
120 * until the #GHook is destroyed.
122 * Returns: %TRUE if the #GHook is active
129 * Returns %TRUE if the #GHook function is currently executing.
131 * Returns: %TRUE if the #GHook function is currently executing
135 * G_HOOK_IS_UNLINKED:
138 * Returns %TRUE if the #GHook is not in a #GHookList.
140 * Returns: %TRUE if the #GHook is not in a #GHookList
145 * @data: data which is passed to func when this hook is invoked
146 * @next: pointer to the next hook in the list
147 * @prev: pointer to the previous hook in the list
148 * @ref_count: the reference count of this hook
149 * @hook_id: the id of this hook, which is unique within its list
150 * @flags: flags which are set for this hook. See #GHookFlagMask for
152 * @func: the function to call when this hook is invoked. The possible
153 * signatures for this function are #GHookFunc and #GHookCheckFunc
154 * @destroy: the default @finalize_hook function of a #GHookList calls
155 * this member of the hook that is being finalized
157 * The #GHook struct represents a single hook function in a #GHookList.
162 * @data: the data field of the #GHook is passed to the hook function here
164 * Defines the type of a hook function that can be invoked
165 * by g_hook_list_invoke().
170 * @data: the data field of the #GHook is passed to the hook function here
172 * Defines the type of a hook function that can be invoked
173 * by g_hook_list_invoke_check().
175 * Returns: %FALSE if the #GHook should be destroyed
178 /* --- functions --- */
180 default_finalize_hook (GHookList *hook_list,
183 GDestroyNotify destroy = hook->destroy;
187 hook->destroy = NULL;
188 destroy (hook->data);
194 * @hook_list: a #GHookList
195 * @hook_size: the size of each element in the #GHookList,
196 * typically `sizeof (GHook)`.
198 * Initializes a #GHookList.
199 * This must be called before the #GHookList is used.
202 g_hook_list_init (GHookList *hook_list,
205 g_return_if_fail (hook_list != NULL);
206 g_return_if_fail (hook_size >= sizeof (GHook));
208 hook_list->seq_id = 1;
209 hook_list->hook_size = hook_size;
210 hook_list->is_setup = TRUE;
211 hook_list->hooks = NULL;
212 hook_list->dummy3 = NULL;
213 hook_list->finalize_hook = default_finalize_hook;
214 hook_list->dummy[0] = NULL;
215 hook_list->dummy[1] = NULL;
220 * @hook_list: a #GHookList
222 * Removes all the #GHook elements from a #GHookList.
225 g_hook_list_clear (GHookList *hook_list)
227 g_return_if_fail (hook_list != NULL);
229 if (hook_list->is_setup)
233 hook_list->is_setup = FALSE;
235 hook = hook_list->hooks;
238 /* destroy hook_list->hook_memchunk */
245 g_hook_ref (hook_list, hook);
246 g_hook_destroy_link (hook_list, hook);
248 g_hook_unref (hook_list, hook);
257 * @hook_list: a #GHookList
259 * Allocates space for a #GHook and initializes it.
261 * Returns: a new #GHook
264 g_hook_alloc (GHookList *hook_list)
268 g_return_val_if_fail (hook_list != NULL, NULL);
269 g_return_val_if_fail (hook_list->is_setup, NULL);
271 hook = g_slice_alloc0 (hook_list->hook_size);
275 hook->flags = G_HOOK_FLAG_ACTIVE;
279 hook->destroy = NULL;
285 * @hook_list: a #GHookList
286 * @hook: the #GHook to free
288 * Calls the #GHookList @finalize_hook function if it exists,
289 * and frees the memory allocated for the #GHook.
292 g_hook_free (GHookList *hook_list,
295 g_return_if_fail (hook_list != NULL);
296 g_return_if_fail (hook_list->is_setup);
297 g_return_if_fail (hook != NULL);
298 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
299 g_return_if_fail (!G_HOOK_IN_CALL (hook));
301 if(hook_list->finalize_hook != NULL)
302 hook_list->finalize_hook (hook_list, hook);
303 g_slice_free1 (hook_list->hook_size, hook);
307 * g_hook_destroy_link:
308 * @hook_list: a #GHookList
309 * @hook: the #GHook to remove
311 * Removes one #GHook from a #GHookList, marking it
312 * inactive and calling g_hook_unref() on it.
315 g_hook_destroy_link (GHookList *hook_list,
318 g_return_if_fail (hook_list != NULL);
319 g_return_if_fail (hook != NULL);
321 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
325 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
331 * @hook_list: a #GHookList
332 * @hook_id: a hook ID
334 * Destroys a #GHook, given its ID.
336 * Returns: %TRUE if the #GHook was found in the #GHookList and destroyed
339 g_hook_destroy (GHookList *hook_list,
344 g_return_val_if_fail (hook_list != NULL, FALSE);
345 g_return_val_if_fail (hook_id > 0, FALSE);
347 hook = g_hook_get (hook_list, hook_id);
350 g_hook_destroy_link (hook_list, hook);
359 * @hook_list: a #GHookList
360 * @hook: the #GHook to unref
362 * Decrements the reference count of a #GHook.
363 * If the reference count falls to 0, the #GHook is removed
364 * from the #GHookList and g_hook_free() is called to free it.
367 g_hook_unref (GHookList *hook_list,
370 g_return_if_fail (hook_list != NULL);
371 g_return_if_fail (hook != NULL);
372 g_return_if_fail (hook->ref_count > 0);
375 if (!hook->ref_count)
377 g_return_if_fail (hook->hook_id == 0);
378 g_return_if_fail (!G_HOOK_IN_CALL (hook));
381 hook->prev->next = hook->next;
383 hook_list->hooks = hook->next;
386 hook->next->prev = hook->prev;
391 if (!hook_list->is_setup)
393 hook_list->is_setup = TRUE;
394 g_hook_free (hook_list, hook);
395 hook_list->is_setup = FALSE;
397 if (!hook_list->hooks)
399 /* destroy hook_list->hook_memchunk */
403 g_hook_free (hook_list, hook);
409 * @hook_list: a #GHookList
410 * @hook: the #GHook to increment the reference count of
412 * Increments the reference count for a #GHook.
414 * Returns: the @hook that was passed in (since 2.6)
417 g_hook_ref (GHookList *hook_list,
420 g_return_val_if_fail (hook_list != NULL, NULL);
421 g_return_val_if_fail (hook != NULL, NULL);
422 g_return_val_if_fail (hook->ref_count > 0, NULL);
431 * @hook_list: a #GHookList
432 * @hook: the #GHook to add to the end of @hook_list
434 * Appends a #GHook onto the end of a #GHookList.
439 * @hook_list: a #GHookList
440 * @hook: the #GHook to add to the start of @hook_list
442 * Prepends a #GHook on the start of a #GHookList.
445 g_hook_prepend (GHookList *hook_list,
448 g_return_if_fail (hook_list != NULL);
450 g_hook_insert_before (hook_list, hook_list->hooks, hook);
454 * g_hook_insert_before:
455 * @hook_list: a #GHookList
456 * @sibling: the #GHook to insert the new #GHook before
457 * @hook: the #GHook to insert
459 * Inserts a #GHook into a #GHookList, before a given #GHook.
462 g_hook_insert_before (GHookList *hook_list,
466 g_return_if_fail (hook_list != NULL);
467 g_return_if_fail (hook_list->is_setup);
468 g_return_if_fail (hook != NULL);
469 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
470 g_return_if_fail (hook->ref_count == 0);
472 hook->hook_id = hook_list->seq_id++;
473 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
479 hook->prev = sibling->prev;
480 hook->prev->next = hook;
481 hook->next = sibling;
482 sibling->prev = hook;
486 hook_list->hooks = hook;
487 hook->next = sibling;
488 sibling->prev = hook;
493 if (hook_list->hooks)
495 sibling = hook_list->hooks;
496 while (sibling->next)
497 sibling = sibling->next;
498 hook->prev = sibling;
499 sibling->next = hook;
502 hook_list->hooks = hook;
507 * g_hook_list_invoke:
508 * @hook_list: a #GHookList
509 * @may_recurse: %TRUE if functions which are already running
510 * (e.g. in another thread) can be called. If set to %FALSE,
513 * Calls all of the #GHook functions in a #GHookList.
516 g_hook_list_invoke (GHookList *hook_list,
517 gboolean may_recurse)
521 g_return_if_fail (hook_list != NULL);
522 g_return_if_fail (hook_list->is_setup);
524 hook = g_hook_first_valid (hook_list, may_recurse);
528 gboolean was_in_call;
530 func = (GHookFunc) hook->func;
532 was_in_call = G_HOOK_IN_CALL (hook);
533 hook->flags |= G_HOOK_FLAG_IN_CALL;
536 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
538 hook = g_hook_next_valid (hook_list, hook, may_recurse);
543 * g_hook_list_invoke_check:
544 * @hook_list: a #GHookList
545 * @may_recurse: %TRUE if functions which are already running
546 * (e.g. in another thread) can be called. If set to %FALSE,
549 * Calls all of the #GHook functions in a #GHookList.
550 * Any function which returns %FALSE is removed from the #GHookList.
553 g_hook_list_invoke_check (GHookList *hook_list,
554 gboolean may_recurse)
558 g_return_if_fail (hook_list != NULL);
559 g_return_if_fail (hook_list->is_setup);
561 hook = g_hook_first_valid (hook_list, may_recurse);
565 gboolean was_in_call;
566 gboolean need_destroy;
568 func = (GHookCheckFunc) hook->func;
570 was_in_call = G_HOOK_IN_CALL (hook);
571 hook->flags |= G_HOOK_FLAG_IN_CALL;
572 need_destroy = !func (hook->data);
574 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
576 g_hook_destroy_link (hook_list, hook);
578 hook = g_hook_next_valid (hook_list, hook, may_recurse);
583 * GHookCheckMarshaller:
585 * @marshal_data: user data
587 * Defines the type of function used by g_hook_list_marshal_check().
589 * Returns: %FALSE if @hook should be destroyed
593 * g_hook_list_marshal_check:
594 * @hook_list: a #GHookList
595 * @may_recurse: %TRUE if hooks which are currently running
596 * (e.g. in another thread) are considered valid. If set to %FALSE,
598 * @marshaller: the function to call for each #GHook
599 * @marshal_data: data to pass to @marshaller
601 * Calls a function on each valid #GHook and destroys it if the
602 * function returns %FALSE.
605 g_hook_list_marshal_check (GHookList *hook_list,
606 gboolean may_recurse,
607 GHookCheckMarshaller marshaller,
612 g_return_if_fail (hook_list != NULL);
613 g_return_if_fail (hook_list->is_setup);
614 g_return_if_fail (marshaller != NULL);
616 hook = g_hook_first_valid (hook_list, may_recurse);
619 gboolean was_in_call;
620 gboolean need_destroy;
622 was_in_call = G_HOOK_IN_CALL (hook);
623 hook->flags |= G_HOOK_FLAG_IN_CALL;
624 need_destroy = !marshaller (hook, data);
626 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
628 g_hook_destroy_link (hook_list, hook);
630 hook = g_hook_next_valid (hook_list, hook, may_recurse);
637 * @marshal_data: user data
639 * Defines the type of function used by g_hook_list_marshal().
643 * g_hook_list_marshal:
644 * @hook_list: a #GHookList
645 * @may_recurse: %TRUE if hooks which are currently running
646 * (e.g. in another thread) are considered valid. If set to %FALSE,
648 * @marshaller: the function to call for each #GHook
649 * @marshal_data: data to pass to @marshaller
651 * Calls a function on each valid #GHook.
654 g_hook_list_marshal (GHookList *hook_list,
655 gboolean may_recurse,
656 GHookMarshaller marshaller,
661 g_return_if_fail (hook_list != NULL);
662 g_return_if_fail (hook_list->is_setup);
663 g_return_if_fail (marshaller != NULL);
665 hook = g_hook_first_valid (hook_list, may_recurse);
668 gboolean was_in_call;
670 was_in_call = G_HOOK_IN_CALL (hook);
671 hook->flags |= G_HOOK_FLAG_IN_CALL;
672 marshaller (hook, data);
674 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
676 hook = g_hook_next_valid (hook_list, hook, may_recurse);
681 * g_hook_first_valid:
682 * @hook_list: a #GHookList
683 * @may_be_in_call: %TRUE if hooks which are currently running
684 * (e.g. in another thread) are considered valid. If set to %FALSE,
687 * Returns the first #GHook in a #GHookList which has not been destroyed.
688 * The reference count for the #GHook is incremented, so you must call
689 * g_hook_unref() to restore it when no longer needed. (Or call
690 * g_hook_next_valid() if you are stepping through the #GHookList.)
692 * Returns: the first valid #GHook, or %NULL if none are valid
695 g_hook_first_valid (GHookList *hook_list,
696 gboolean may_be_in_call)
698 g_return_val_if_fail (hook_list != NULL, NULL);
700 if (hook_list->is_setup)
704 hook = hook_list->hooks;
707 g_hook_ref (hook_list, hook);
708 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
711 return g_hook_next_valid (hook_list, hook, may_be_in_call);
720 * @hook_list: a #GHookList
721 * @hook: the current #GHook
722 * @may_be_in_call: %TRUE if hooks which are currently running
723 * (e.g. in another thread) are considered valid. If set to %FALSE,
726 * Returns the next #GHook in a #GHookList which has not been destroyed.
727 * The reference count for the #GHook is incremented, so you must call
728 * g_hook_unref() to restore it when no longer needed. (Or continue to call
729 * g_hook_next_valid() until %NULL is returned.)
731 * Returns: the next valid #GHook, or %NULL if none are valid
734 g_hook_next_valid (GHookList *hook_list,
736 gboolean may_be_in_call)
740 g_return_val_if_fail (hook_list != NULL, NULL);
748 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
750 g_hook_ref (hook_list, hook);
751 g_hook_unref (hook_list, ohook);
757 g_hook_unref (hook_list, ohook);
764 * @hook_list: a #GHookList
765 * @hook_id: a hook id
767 * Returns the #GHook with the given id, or %NULL if it is not found.
769 * Returns: the #GHook with the given id, or %NULL if it is not found
772 g_hook_get (GHookList *hook_list,
777 g_return_val_if_fail (hook_list != NULL, NULL);
778 g_return_val_if_fail (hook_id > 0, NULL);
780 hook = hook_list->hooks;
783 if (hook->hook_id == hook_id)
794 * @data: user data passed to g_hook_find_func()
796 * Defines the type of the function passed to g_hook_find().
798 * Returns: %TRUE if the required #GHook has been found
803 * @hook_list: a #GHookList
804 * @need_valids: %TRUE if #GHook elements which have been destroyed
806 * @func: the function to call for each #GHook, which should return
807 * %TRUE when the #GHook has been found
808 * @data: the data to pass to @func
810 * Finds a #GHook in a #GHookList using the given function to
813 * Returns: the found #GHook or %NULL if no matching #GHook is found
816 g_hook_find (GHookList *hook_list,
817 gboolean need_valids,
823 g_return_val_if_fail (hook_list != NULL, NULL);
824 g_return_val_if_fail (func != NULL, NULL);
826 hook = hook_list->hooks;
831 /* test only non-destroyed hooks */
838 g_hook_ref (hook_list, hook);
840 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
842 g_hook_unref (hook_list, hook);
848 g_hook_unref (hook_list, hook);
857 * @hook_list: a #GHookList
858 * @need_valids: %TRUE if #GHook elements which have been destroyed
860 * @data: the data to find
862 * Finds a #GHook in a #GHookList with the given data.
864 * Returns: the #GHook with the given @data or %NULL if no matching
868 g_hook_find_data (GHookList *hook_list,
869 gboolean need_valids,
874 g_return_val_if_fail (hook_list != NULL, NULL);
876 hook = hook_list->hooks;
879 /* test only non-destroyed hooks */
880 if (hook->data == data &&
882 (!need_valids || G_HOOK_ACTIVE (hook)))
893 * @hook_list: a #GHookList
894 * @need_valids: %TRUE if #GHook elements which have been destroyed
896 * @func: the function to find
898 * Finds a #GHook in a #GHookList with the given function.
900 * Returns: the #GHook with the given @func or %NULL if no matching
904 g_hook_find_func (GHookList *hook_list,
905 gboolean need_valids,
910 g_return_val_if_fail (hook_list != NULL, NULL);
911 g_return_val_if_fail (func != NULL, NULL);
913 hook = hook_list->hooks;
916 /* test only non-destroyed hooks */
917 if (hook->func == func &&
919 (!need_valids || G_HOOK_ACTIVE (hook)))
929 * g_hook_find_func_data:
930 * @hook_list: a #GHookList
931 * @need_valids: %TRUE if #GHook elements which have been destroyed
933 * @func: the function to find
934 * @data: the data to find
936 * Finds a #GHook in a #GHookList with the given function and data.
938 * Returns: the #GHook with the given @func and @data or %NULL if
939 * no matching #GHook is found
942 g_hook_find_func_data (GHookList *hook_list,
943 gboolean need_valids,
949 g_return_val_if_fail (hook_list != NULL, NULL);
950 g_return_val_if_fail (func != NULL, NULL);
952 hook = hook_list->hooks;
955 /* test only non-destroyed hooks */
956 if (hook->data == data &&
957 hook->func == func &&
959 (!need_valids || G_HOOK_ACTIVE (hook)))
970 * @new_hook: the #GHook being inserted
971 * @sibling: the #GHook to compare with @new_hook
973 * Defines the type of function used to compare #GHook elements in
974 * g_hook_insert_sorted().
976 * Returns: a value <= 0 if @new_hook should be before @sibling
980 * g_hook_insert_sorted:
981 * @hook_list: a #GHookList
982 * @hook: the #GHook to insert
983 * @func: the comparison function used to sort the #GHook elements
985 * Inserts a #GHook into a #GHookList, sorted by the given function.
988 g_hook_insert_sorted (GHookList *hook_list,
990 GHookCompareFunc func)
994 g_return_if_fail (hook_list != NULL);
995 g_return_if_fail (hook_list->is_setup);
996 g_return_if_fail (hook != NULL);
997 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
998 g_return_if_fail (hook->func != NULL);
999 g_return_if_fail (func != NULL);
1001 /* first non-destroyed hook */
1002 sibling = hook_list->hooks;
1003 while (sibling && !sibling->hook_id)
1004 sibling = sibling->next;
1010 g_hook_ref (hook_list, sibling);
1011 if (func (hook, sibling) <= 0 && sibling->hook_id)
1013 g_hook_unref (hook_list, sibling);
1017 /* next non-destroyed hook */
1018 tmp = sibling->next;
1019 while (tmp && !tmp->hook_id)
1022 g_hook_unref (hook_list, sibling);
1027 g_hook_insert_before (hook_list, sibling, hook);
1031 * g_hook_compare_ids:
1032 * @new_hook: a #GHook
1033 * @sibling: a #GHook to compare with @new_hook
1035 * Compares the ids of two #GHook elements, returning a negative value
1036 * if the second id is greater than the first.
1038 * Returns: a value <= 0 if the id of @sibling is >= the id of @new_hook
1041 g_hook_compare_ids (GHook *new_hook,
1044 if (new_hook->hook_id < sibling->hook_id)
1046 else if (new_hook->hook_id > sibling->hook_id)