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 * 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/.
38 #include "gtestutils.h"
43 * @title: Hook Functions
44 * @short_description: support for manipulating lists of hook functions
46 * The #GHookList, #GHook and their related functions provide support for
47 * lists of hook functions. Functions can be added and removed from the lists,
48 * and the list of hook functions can be invoked.
53 * @seq_id: the next free #GHook id
54 * @hook_size: the size of the #GHookList elements, in bytes
55 * @is_setup: 1 if the #GHookList has been initialized
56 * @hooks: the first #GHook element in the list
58 * @finalize_hook: the function to call to finalize a #GHook element.
59 * The default behaviour is to call the hooks @destroy function
62 * The #GHookList struct represents a list of hook functions.
67 * @hook_list: a #GHookList
68 * @hook: the hook in @hook_list that gets finalized
70 * Defines the type of function to be called when a hook in a
71 * list of hooks gets finalized.
76 * @G_HOOK_FLAG_ACTIVE: set if the hook has not been destroyed
77 * @G_HOOK_FLAG_IN_CALL: set if the hook is currently being run
78 * @G_HOOK_FLAG_MASK: A mask covering all bits reserved for
79 * hook flags; see %G_HOOK_FLAG_USER_SHIFT
81 * Flags used internally in the #GHook implementation.
88 * Gets the flags of a hook.
92 * G_HOOK_FLAG_USER_SHIFT:
94 * The position of the first bit which is not reserved for internal
95 * use be the #GHook implementation, i.e.
96 * `1 << G_HOOK_FLAG_USER_SHIFT` is the first
97 * bit which can be used for application-defined flags.
104 * Casts a pointer to a `GHook*`.
111 * Returns %TRUE if the #GHook is valid, i.e. it is in a #GHookList,
112 * it is active and it has not been destroyed.
114 * Returns: %TRUE if the #GHook is valid
121 * Returns %TRUE if the #GHook is active, which is normally the case
122 * until the #GHook is destroyed.
124 * Returns: %TRUE if the #GHook is active
131 * Returns %TRUE if the #GHook function is currently executing.
133 * Returns: %TRUE if the #GHook function is currently executing
137 * G_HOOK_IS_UNLINKED:
140 * Returns %TRUE if the #GHook is not in a #GHookList.
142 * Returns: %TRUE if the #GHook is not in a #GHookList
147 * @data: data which is passed to func when this hook is invoked
148 * @next: pointer to the next hook in the list
149 * @prev: pointer to the previous hook in the list
150 * @ref_count: the reference count of this hook
151 * @hook_id: the id of this hook, which is unique within its list
152 * @flags: flags which are set for this hook. See #GHookFlagMask for
154 * @func: the function to call when this hook is invoked. The possible
155 * signatures for this function are #GHookFunc and #GHookCheckFunc
156 * @destroy: the default @finalize_hook function of a #GHookList calls
157 * this member of the hook that is being finalized
159 * The #GHook struct represents a single hook function in a #GHookList.
164 * @data: the data field of the #GHook is passed to the hook function here
166 * Defines the type of a hook function that can be invoked
167 * by g_hook_list_invoke().
172 * @data: the data field of the #GHook is passed to the hook function here
174 * Defines the type of a hook function that can be invoked
175 * by g_hook_list_invoke_check().
177 * Returns: %FALSE if the #GHook should be destroyed
180 /* --- functions --- */
182 default_finalize_hook (GHookList *hook_list,
185 GDestroyNotify destroy = hook->destroy;
189 hook->destroy = NULL;
190 destroy (hook->data);
196 * @hook_list: a #GHookList
197 * @hook_size: the size of each element in the #GHookList,
198 * typically `sizeof (GHook)`.
200 * Initializes a #GHookList.
201 * This must be called before the #GHookList is used.
204 g_hook_list_init (GHookList *hook_list,
207 g_return_if_fail (hook_list != NULL);
208 g_return_if_fail (hook_size >= sizeof (GHook));
210 hook_list->seq_id = 1;
211 hook_list->hook_size = hook_size;
212 hook_list->is_setup = TRUE;
213 hook_list->hooks = NULL;
214 hook_list->dummy3 = NULL;
215 hook_list->finalize_hook = default_finalize_hook;
216 hook_list->dummy[0] = NULL;
217 hook_list->dummy[1] = NULL;
222 * @hook_list: a #GHookList
224 * Removes all the #GHook elements from a #GHookList.
227 g_hook_list_clear (GHookList *hook_list)
229 g_return_if_fail (hook_list != NULL);
231 if (hook_list->is_setup)
235 hook_list->is_setup = FALSE;
237 hook = hook_list->hooks;
240 /* destroy hook_list->hook_memchunk */
247 g_hook_ref (hook_list, hook);
248 g_hook_destroy_link (hook_list, hook);
250 g_hook_unref (hook_list, hook);
259 * @hook_list: a #GHookList
261 * Allocates space for a #GHook and initializes it.
263 * Returns: a new #GHook
266 g_hook_alloc (GHookList *hook_list)
270 g_return_val_if_fail (hook_list != NULL, NULL);
271 g_return_val_if_fail (hook_list->is_setup, NULL);
273 hook = g_slice_alloc0 (hook_list->hook_size);
277 hook->flags = G_HOOK_FLAG_ACTIVE;
281 hook->destroy = NULL;
287 * @hook_list: a #GHookList
288 * @hook: the #GHook to free
290 * Calls the #GHookList @finalize_hook function if it exists,
291 * and frees the memory allocated for the #GHook.
294 g_hook_free (GHookList *hook_list,
297 g_return_if_fail (hook_list != NULL);
298 g_return_if_fail (hook_list->is_setup);
299 g_return_if_fail (hook != NULL);
300 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
301 g_return_if_fail (!G_HOOK_IN_CALL (hook));
303 if(hook_list->finalize_hook != NULL)
304 hook_list->finalize_hook (hook_list, hook);
305 g_slice_free1 (hook_list->hook_size, hook);
309 * g_hook_destroy_link:
310 * @hook_list: a #GHookList
311 * @hook: the #GHook to remove
313 * Removes one #GHook from a #GHookList, marking it
314 * inactive and calling g_hook_unref() on it.
317 g_hook_destroy_link (GHookList *hook_list,
320 g_return_if_fail (hook_list != NULL);
321 g_return_if_fail (hook != NULL);
323 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
327 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
333 * @hook_list: a #GHookList
334 * @hook_id: a hook ID
336 * Destroys a #GHook, given its ID.
338 * Returns: %TRUE if the #GHook was found in the #GHookList and destroyed
341 g_hook_destroy (GHookList *hook_list,
346 g_return_val_if_fail (hook_list != NULL, FALSE);
347 g_return_val_if_fail (hook_id > 0, FALSE);
349 hook = g_hook_get (hook_list, hook_id);
352 g_hook_destroy_link (hook_list, hook);
361 * @hook_list: a #GHookList
362 * @hook: the #GHook to unref
364 * Decrements the reference count of a #GHook.
365 * If the reference count falls to 0, the #GHook is removed
366 * from the #GHookList and g_hook_free() is called to free it.
369 g_hook_unref (GHookList *hook_list,
372 g_return_if_fail (hook_list != NULL);
373 g_return_if_fail (hook != NULL);
374 g_return_if_fail (hook->ref_count > 0);
377 if (!hook->ref_count)
379 g_return_if_fail (hook->hook_id == 0);
380 g_return_if_fail (!G_HOOK_IN_CALL (hook));
383 hook->prev->next = hook->next;
385 hook_list->hooks = hook->next;
388 hook->next->prev = hook->prev;
393 if (!hook_list->is_setup)
395 hook_list->is_setup = TRUE;
396 g_hook_free (hook_list, hook);
397 hook_list->is_setup = FALSE;
399 if (!hook_list->hooks)
401 /* destroy hook_list->hook_memchunk */
405 g_hook_free (hook_list, hook);
411 * @hook_list: a #GHookList
412 * @hook: the #GHook to increment the reference count of
414 * Increments the reference count for a #GHook.
416 * Returns: the @hook that was passed in (since 2.6)
419 g_hook_ref (GHookList *hook_list,
422 g_return_val_if_fail (hook_list != NULL, NULL);
423 g_return_val_if_fail (hook != NULL, NULL);
424 g_return_val_if_fail (hook->ref_count > 0, NULL);
433 * @hook_list: a #GHookList
434 * @hook: the #GHook to add to the end of @hook_list
436 * Appends a #GHook onto the end of a #GHookList.
441 * @hook_list: a #GHookList
442 * @hook: the #GHook to add to the start of @hook_list
444 * Prepends a #GHook on the start of a #GHookList.
447 g_hook_prepend (GHookList *hook_list,
450 g_return_if_fail (hook_list != NULL);
452 g_hook_insert_before (hook_list, hook_list->hooks, hook);
456 * g_hook_insert_before:
457 * @hook_list: a #GHookList
458 * @sibling: (nullable): the #GHook to insert the new #GHook before
459 * @hook: the #GHook to insert
461 * Inserts a #GHook into a #GHookList, before a given #GHook.
464 g_hook_insert_before (GHookList *hook_list,
468 g_return_if_fail (hook_list != NULL);
469 g_return_if_fail (hook_list->is_setup);
470 g_return_if_fail (hook != NULL);
471 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
472 g_return_if_fail (hook->ref_count == 0);
474 hook->hook_id = hook_list->seq_id++;
475 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
481 hook->prev = sibling->prev;
482 hook->prev->next = hook;
483 hook->next = sibling;
484 sibling->prev = hook;
488 hook_list->hooks = hook;
489 hook->next = sibling;
490 sibling->prev = hook;
495 if (hook_list->hooks)
497 sibling = hook_list->hooks;
498 while (sibling->next)
499 sibling = sibling->next;
500 hook->prev = sibling;
501 sibling->next = hook;
504 hook_list->hooks = hook;
509 * g_hook_list_invoke:
510 * @hook_list: a #GHookList
511 * @may_recurse: %TRUE if functions which are already running
512 * (e.g. in another thread) can be called. If set to %FALSE,
515 * Calls all of the #GHook functions in a #GHookList.
518 g_hook_list_invoke (GHookList *hook_list,
519 gboolean may_recurse)
523 g_return_if_fail (hook_list != NULL);
524 g_return_if_fail (hook_list->is_setup);
526 hook = g_hook_first_valid (hook_list, may_recurse);
530 gboolean was_in_call;
532 func = (GHookFunc) hook->func;
534 was_in_call = G_HOOK_IN_CALL (hook);
535 hook->flags |= G_HOOK_FLAG_IN_CALL;
538 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
540 hook = g_hook_next_valid (hook_list, hook, may_recurse);
545 * g_hook_list_invoke_check:
546 * @hook_list: a #GHookList
547 * @may_recurse: %TRUE if functions which are already running
548 * (e.g. in another thread) can be called. If set to %FALSE,
551 * Calls all of the #GHook functions in a #GHookList.
552 * Any function which returns %FALSE is removed from the #GHookList.
555 g_hook_list_invoke_check (GHookList *hook_list,
556 gboolean may_recurse)
560 g_return_if_fail (hook_list != NULL);
561 g_return_if_fail (hook_list->is_setup);
563 hook = g_hook_first_valid (hook_list, may_recurse);
567 gboolean was_in_call;
568 gboolean need_destroy;
570 func = (GHookCheckFunc) hook->func;
572 was_in_call = G_HOOK_IN_CALL (hook);
573 hook->flags |= G_HOOK_FLAG_IN_CALL;
574 need_destroy = !func (hook->data);
576 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
578 g_hook_destroy_link (hook_list, hook);
580 hook = g_hook_next_valid (hook_list, hook, may_recurse);
585 * GHookCheckMarshaller:
587 * @user_data: user data
589 * Defines the type of function used by g_hook_list_marshal_check().
591 * Returns: %FALSE if @hook should be destroyed
595 * g_hook_list_marshal_check:
596 * @hook_list: a #GHookList
597 * @may_recurse: %TRUE if hooks which are currently running
598 * (e.g. in another thread) are considered valid. If set to %FALSE,
600 * @marshaller: the function to call for each #GHook
601 * @marshal_data: data to pass to @marshaller
603 * Calls a function on each valid #GHook and destroys it if the
604 * function returns %FALSE.
607 g_hook_list_marshal_check (GHookList *hook_list,
608 gboolean may_recurse,
609 GHookCheckMarshaller marshaller,
614 g_return_if_fail (hook_list != NULL);
615 g_return_if_fail (hook_list->is_setup);
616 g_return_if_fail (marshaller != NULL);
618 hook = g_hook_first_valid (hook_list, may_recurse);
621 gboolean was_in_call;
622 gboolean need_destroy;
624 was_in_call = G_HOOK_IN_CALL (hook);
625 hook->flags |= G_HOOK_FLAG_IN_CALL;
626 need_destroy = !marshaller (hook, data);
628 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
630 g_hook_destroy_link (hook_list, hook);
632 hook = g_hook_next_valid (hook_list, hook, may_recurse);
639 * @user_data: user data
641 * Defines the type of function used by g_hook_list_marshal().
645 * g_hook_list_marshal:
646 * @hook_list: a #GHookList
647 * @may_recurse: %TRUE if hooks which are currently running
648 * (e.g. in another thread) are considered valid. If set to %FALSE,
650 * @marshaller: the function to call for each #GHook
651 * @marshal_data: data to pass to @marshaller
653 * Calls a function on each valid #GHook.
656 g_hook_list_marshal (GHookList *hook_list,
657 gboolean may_recurse,
658 GHookMarshaller marshaller,
663 g_return_if_fail (hook_list != NULL);
664 g_return_if_fail (hook_list->is_setup);
665 g_return_if_fail (marshaller != NULL);
667 hook = g_hook_first_valid (hook_list, may_recurse);
670 gboolean was_in_call;
672 was_in_call = G_HOOK_IN_CALL (hook);
673 hook->flags |= G_HOOK_FLAG_IN_CALL;
674 marshaller (hook, data);
676 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
678 hook = g_hook_next_valid (hook_list, hook, may_recurse);
683 * g_hook_first_valid:
684 * @hook_list: a #GHookList
685 * @may_be_in_call: %TRUE if hooks which are currently running
686 * (e.g. in another thread) are considered valid. If set to %FALSE,
689 * Returns the first #GHook in a #GHookList which has not been destroyed.
690 * The reference count for the #GHook is incremented, so you must call
691 * g_hook_unref() to restore it when no longer needed. (Or call
692 * g_hook_next_valid() if you are stepping through the #GHookList.)
694 * Returns: the first valid #GHook, or %NULL if none are valid
697 g_hook_first_valid (GHookList *hook_list,
698 gboolean may_be_in_call)
700 g_return_val_if_fail (hook_list != NULL, NULL);
702 if (hook_list->is_setup)
706 hook = hook_list->hooks;
709 g_hook_ref (hook_list, hook);
710 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
713 return g_hook_next_valid (hook_list, hook, may_be_in_call);
722 * @hook_list: a #GHookList
723 * @hook: the current #GHook
724 * @may_be_in_call: %TRUE if hooks which are currently running
725 * (e.g. in another thread) are considered valid. If set to %FALSE,
728 * Returns the next #GHook in a #GHookList which has not been destroyed.
729 * The reference count for the #GHook is incremented, so you must call
730 * g_hook_unref() to restore it when no longer needed. (Or continue to call
731 * g_hook_next_valid() until %NULL is returned.)
733 * Returns: the next valid #GHook, or %NULL if none are valid
736 g_hook_next_valid (GHookList *hook_list,
738 gboolean may_be_in_call)
742 g_return_val_if_fail (hook_list != NULL, NULL);
750 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
752 g_hook_ref (hook_list, hook);
753 g_hook_unref (hook_list, ohook);
759 g_hook_unref (hook_list, ohook);
766 * @hook_list: a #GHookList
767 * @hook_id: a hook id
769 * Returns the #GHook with the given id, or %NULL if it is not found.
771 * Returns: the #GHook with the given id, or %NULL if it is not found
774 g_hook_get (GHookList *hook_list,
779 g_return_val_if_fail (hook_list != NULL, NULL);
780 g_return_val_if_fail (hook_id > 0, NULL);
782 hook = hook_list->hooks;
785 if (hook->hook_id == hook_id)
796 * @user_data: user data passed to g_hook_find_func()
798 * Defines the type of the function passed to g_hook_find().
800 * Returns: %TRUE if the required #GHook has been found
805 * @hook_list: a #GHookList
806 * @need_valids: %TRUE if #GHook elements which have been destroyed
808 * @func: the function to call for each #GHook, which should return
809 * %TRUE when the #GHook has been found
810 * @data: the data to pass to @func
812 * Finds a #GHook in a #GHookList using the given function to
815 * Returns: the found #GHook or %NULL if no matching #GHook is found
818 g_hook_find (GHookList *hook_list,
819 gboolean need_valids,
825 g_return_val_if_fail (hook_list != NULL, NULL);
826 g_return_val_if_fail (func != NULL, NULL);
828 hook = hook_list->hooks;
833 /* test only non-destroyed hooks */
840 g_hook_ref (hook_list, hook);
842 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
844 g_hook_unref (hook_list, hook);
850 g_hook_unref (hook_list, hook);
859 * @hook_list: a #GHookList
860 * @need_valids: %TRUE if #GHook elements which have been destroyed
862 * @data: the data to find
864 * Finds a #GHook in a #GHookList with the given data.
866 * Returns: the #GHook with the given @data or %NULL if no matching
870 g_hook_find_data (GHookList *hook_list,
871 gboolean need_valids,
876 g_return_val_if_fail (hook_list != NULL, NULL);
878 hook = hook_list->hooks;
881 /* test only non-destroyed hooks */
882 if (hook->data == data &&
884 (!need_valids || G_HOOK_ACTIVE (hook)))
895 * @hook_list: a #GHookList
896 * @need_valids: %TRUE if #GHook elements which have been destroyed
898 * @func: the function to find
900 * Finds a #GHook in a #GHookList with the given function.
902 * Returns: the #GHook with the given @func or %NULL if no matching
906 g_hook_find_func (GHookList *hook_list,
907 gboolean need_valids,
912 g_return_val_if_fail (hook_list != NULL, NULL);
913 g_return_val_if_fail (func != NULL, NULL);
915 hook = hook_list->hooks;
918 /* test only non-destroyed hooks */
919 if (hook->func == func &&
921 (!need_valids || G_HOOK_ACTIVE (hook)))
931 * g_hook_find_func_data:
932 * @hook_list: a #GHookList
933 * @need_valids: %TRUE if #GHook elements which have been destroyed
935 * @func: (not nullable): the function to find
936 * @data: the data to find
938 * Finds a #GHook in a #GHookList with the given function and data.
940 * Returns: the #GHook with the given @func and @data or %NULL if
941 * no matching #GHook is found
944 g_hook_find_func_data (GHookList *hook_list,
945 gboolean need_valids,
951 g_return_val_if_fail (hook_list != NULL, NULL);
952 g_return_val_if_fail (func != NULL, NULL);
954 hook = hook_list->hooks;
957 /* test only non-destroyed hooks */
958 if (hook->data == data &&
959 hook->func == func &&
961 (!need_valids || G_HOOK_ACTIVE (hook)))
972 * @new_hook: the #GHook being inserted
973 * @sibling: the #GHook to compare with @new_hook
975 * Defines the type of function used to compare #GHook elements in
976 * g_hook_insert_sorted().
978 * Returns: a value <= 0 if @new_hook should be before @sibling
982 * g_hook_insert_sorted:
983 * @hook_list: a #GHookList
984 * @hook: the #GHook to insert
985 * @func: the comparison function used to sort the #GHook elements
987 * Inserts a #GHook into a #GHookList, sorted by the given function.
990 g_hook_insert_sorted (GHookList *hook_list,
992 GHookCompareFunc func)
996 g_return_if_fail (hook_list != NULL);
997 g_return_if_fail (hook_list->is_setup);
998 g_return_if_fail (hook != NULL);
999 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
1000 g_return_if_fail (hook->func != NULL);
1001 g_return_if_fail (func != NULL);
1003 /* first non-destroyed hook */
1004 sibling = hook_list->hooks;
1005 while (sibling && !sibling->hook_id)
1006 sibling = sibling->next;
1012 g_hook_ref (hook_list, sibling);
1013 if (func (hook, sibling) <= 0 && sibling->hook_id)
1015 g_hook_unref (hook_list, sibling);
1019 /* next non-destroyed hook */
1020 tmp = sibling->next;
1021 while (tmp && !tmp->hook_id)
1024 g_hook_unref (hook_list, sibling);
1029 g_hook_insert_before (hook_list, sibling, hook);
1033 * g_hook_compare_ids:
1034 * @new_hook: a #GHook
1035 * @sibling: a #GHook to compare with @new_hook
1037 * Compares the ids of two #GHook elements, returning a negative value
1038 * if the second id is greater than the first.
1040 * Returns: a value <= 0 if the id of @sibling is >= the id of @new_hook
1043 g_hook_compare_ids (GHook *new_hook,
1046 if (new_hook->hook_id < sibling->hook_id)
1048 else if (new_hook->hook_id > sibling->hook_id)