1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
|
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* GHook: Callback maintenance functions
* Copyright (C) 1998 Tim Janik
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
/*
* MT safe
*/
#include "config.h"
#include "ghook.h"
#include "gtestutils.h"
#include "gslice.h"
/**
* GHookList:
* @seq_id: the next free #GHook id
* @hook_size: the size of the #GHookList elements, in bytes
* @is_setup: 1 if the #GHookList has been initialized
* @hooks: the first #GHook element in the list
* @dummy3: unused
* @finalize_hook: the function to call to finalize a #GHook element.
* The default behaviour is to call the hooks @destroy function
* @dummy: unused
*
* The #GHookList struct represents a list of hook functions.
*/
/**
* GHookFinalizeFunc:
* @hook_list: a #GHookList
* @hook: the hook in @hook_list that gets finalized
*
* Defines the type of function to be called when a hook in a
* list of hooks gets finalized.
*/
/**
* GHookFlagMask:
* @G_HOOK_FLAG_ACTIVE: set if the hook has not been destroyed
* @G_HOOK_FLAG_IN_CALL: set if the hook is currently being run
* @G_HOOK_FLAG_MASK: A mask covering all bits reserved for
* hook flags; see %G_HOOK_FLAG_USER_SHIFT
*
* Flags used internally in the #GHook implementation.
*/
/**
* G_HOOK_FLAGS:
* @hook: a #GHook
*
* Gets the flags of a hook.
*/
/**
* G_HOOK_FLAG_USER_SHIFT:
*
* The position of the first bit which is not reserved for internal
* use be the #GHook implementation, i.e.
* `1 << G_HOOK_FLAG_USER_SHIFT` is the first
* bit which can be used for application-defined flags.
*/
/**
* G_HOOK:
* @hook: a pointer
*
* Casts a pointer to a `GHook*`.
*/
/**
* G_HOOK_IS_VALID:
* @hook: a #GHook
*
* Returns %TRUE if the #GHook is valid, i.e. it is in a #GHookList,
* it is active and it has not been destroyed.
*
* Returns: %TRUE if the #GHook is valid
*/
/**
* G_HOOK_ACTIVE:
* @hook: a #GHook
*
* Returns %TRUE if the #GHook is active, which is normally the case
* until the #GHook is destroyed.
*
* Returns: %TRUE if the #GHook is active
*/
/**
* G_HOOK_IN_CALL:
* @hook: a #GHook
*
* Returns %TRUE if the #GHook function is currently executing.
*
* Returns: %TRUE if the #GHook function is currently executing
*/
/**
* G_HOOK_IS_UNLINKED:
* @hook: a #GHook
*
* Returns %TRUE if the #GHook is not in a #GHookList.
*
* Returns: %TRUE if the #GHook is not in a #GHookList
*/
/**
* GHook:
* @data: data which is passed to func when this hook is invoked
* @next: pointer to the next hook in the list
* @prev: pointer to the previous hook in the list
* @ref_count: the reference count of this hook
* @hook_id: the id of this hook, which is unique within its list
* @flags: flags which are set for this hook. See #GHookFlagMask for
* predefined flags
* @func: the function to call when this hook is invoked. The possible
* signatures for this function are #GHookFunc and #GHookCheckFunc
* @destroy: the default @finalize_hook function of a #GHookList calls
* this member of the hook that is being finalized
*
* The #GHook struct represents a single hook function in a #GHookList.
*/
/**
* GHookFunc:
* @data: the data field of the #GHook is passed to the hook function here
*
* Defines the type of a hook function that can be invoked
* by g_hook_list_invoke().
*/
/**
* GHookCheckFunc:
* @data: the data field of the #GHook is passed to the hook function here
*
* Defines the type of a hook function that can be invoked
* by g_hook_list_invoke_check().
*
* Returns: %FALSE if the #GHook should be destroyed
*/
/* --- functions --- */
static void
default_finalize_hook (GHookList *hook_list,
GHook *hook)
{
GDestroyNotify destroy = hook->destroy;
if (destroy)
{
hook->destroy = NULL;
destroy (hook->data);
}
}
/**
* g_hook_list_init:
* @hook_list: a #GHookList
* @hook_size: the size of each element in the #GHookList,
* typically `sizeof (GHook)`.
*
* Initializes a #GHookList.
* This must be called before the #GHookList is used.
*/
void
g_hook_list_init (GHookList *hook_list,
guint hook_size)
{
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook_size >= sizeof (GHook));
hook_list->seq_id = 1;
hook_list->hook_size = hook_size;
hook_list->is_setup = TRUE;
hook_list->hooks = NULL;
hook_list->dummy3 = NULL;
hook_list->finalize_hook = default_finalize_hook;
hook_list->dummy[0] = NULL;
hook_list->dummy[1] = NULL;
}
/**
* g_hook_list_clear:
* @hook_list: a #GHookList
*
* Removes all the #GHook elements from a #GHookList.
*/
void
g_hook_list_clear (GHookList *hook_list)
{
g_return_if_fail (hook_list != NULL);
if (hook_list->is_setup)
{
GHook *hook;
hook_list->is_setup = FALSE;
hook = hook_list->hooks;
if (!hook)
{
/* destroy hook_list->hook_memchunk */
}
else
do
{
GHook *tmp;
g_hook_ref (hook_list, hook);
g_hook_destroy_link (hook_list, hook);
tmp = hook->next;
g_hook_unref (hook_list, hook);
hook = tmp;
}
while (hook);
}
}
/**
* g_hook_alloc:
* @hook_list: a #GHookList
*
* Allocates space for a #GHook and initializes it.
*
* Returns: a new #GHook
*/
GHook*
g_hook_alloc (GHookList *hook_list)
{
GHook *hook;
g_return_val_if_fail (hook_list != NULL, NULL);
g_return_val_if_fail (hook_list->is_setup, NULL);
hook = g_slice_alloc0 (hook_list->hook_size);
hook->data = NULL;
hook->next = NULL;
hook->prev = NULL;
hook->flags = G_HOOK_FLAG_ACTIVE;
hook->ref_count = 0;
hook->hook_id = 0;
hook->func = NULL;
hook->destroy = NULL;
return hook;
}
/**
* g_hook_free:
* @hook_list: a #GHookList
* @hook: the #GHook to free
*
* Calls the #GHookList @finalize_hook function if it exists,
* and frees the memory allocated for the #GHook.
*/
void
g_hook_free (GHookList *hook_list,
GHook *hook)
{
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook_list->is_setup);
g_return_if_fail (hook != NULL);
g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
g_return_if_fail (!G_HOOK_IN_CALL (hook));
if(hook_list->finalize_hook != NULL)
hook_list->finalize_hook (hook_list, hook);
g_slice_free1 (hook_list->hook_size, hook);
}
/**
* g_hook_destroy_link:
* @hook_list: a #GHookList
* @hook: the #GHook to remove
*
* Removes one #GHook from a #GHookList, marking it
* inactive and calling g_hook_unref() on it.
*/
void
g_hook_destroy_link (GHookList *hook_list,
GHook *hook)
{
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook != NULL);
hook->flags &= ~G_HOOK_FLAG_ACTIVE;
if (hook->hook_id)
{
hook->hook_id = 0;
g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
}
}
/**
* g_hook_destroy:
* @hook_list: a #GHookList
* @hook_id: a hook ID
*
* Destroys a #GHook, given its ID.
*
* Returns: %TRUE if the #GHook was found in the #GHookList and destroyed
*/
gboolean
g_hook_destroy (GHookList *hook_list,
gulong hook_id)
{
GHook *hook;
g_return_val_if_fail (hook_list != NULL, FALSE);
g_return_val_if_fail (hook_id > 0, FALSE);
hook = g_hook_get (hook_list, hook_id);
if (hook)
{
g_hook_destroy_link (hook_list, hook);
return TRUE;
}
return FALSE;
}
/**
* g_hook_unref:
* @hook_list: a #GHookList
* @hook: the #GHook to unref
*
* Decrements the reference count of a #GHook.
* If the reference count falls to 0, the #GHook is removed
* from the #GHookList and g_hook_free() is called to free it.
*/
void
g_hook_unref (GHookList *hook_list,
GHook *hook)
{
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook != NULL);
g_return_if_fail (hook->ref_count > 0);
hook->ref_count--;
if (!hook->ref_count)
{
g_return_if_fail (hook->hook_id == 0);
g_return_if_fail (!G_HOOK_IN_CALL (hook));
if (hook->prev)
hook->prev->next = hook->next;
else
hook_list->hooks = hook->next;
if (hook->next)
{
hook->next->prev = hook->prev;
hook->next = NULL;
}
hook->prev = NULL;
if (!hook_list->is_setup)
{
hook_list->is_setup = TRUE;
g_hook_free (hook_list, hook);
hook_list->is_setup = FALSE;
if (!hook_list->hooks)
{
/* destroy hook_list->hook_memchunk */
}
}
else
g_hook_free (hook_list, hook);
}
}
/**
* g_hook_ref:
* @hook_list: a #GHookList
* @hook: the #GHook to increment the reference count of
*
* Increments the reference count for a #GHook.
*
* Returns: (transfer full): the @hook that was passed in (since 2.6)
*/
GHook *
g_hook_ref (GHookList *hook_list,
GHook *hook)
{
g_return_val_if_fail (hook_list != NULL, NULL);
g_return_val_if_fail (hook != NULL, NULL);
g_return_val_if_fail (hook->ref_count > 0, NULL);
hook->ref_count++;
return hook;
}
/**
* g_hook_append:
* @hook_list: a #GHookList
* @hook: the #GHook to add to the end of @hook_list
*
* Appends a #GHook onto the end of a #GHookList.
*/
/**
* g_hook_prepend:
* @hook_list: a #GHookList
* @hook: the #GHook to add to the start of @hook_list
*
* Prepends a #GHook on the start of a #GHookList.
*/
void
g_hook_prepend (GHookList *hook_list,
GHook *hook)
{
g_return_if_fail (hook_list != NULL);
g_hook_insert_before (hook_list, hook_list->hooks, hook);
}
/**
* g_hook_insert_before:
* @hook_list: a #GHookList
* @sibling: (nullable): the #GHook to insert the new #GHook before
* @hook: the #GHook to insert
*
* Inserts a #GHook into a #GHookList, before a given #GHook.
*/
void
g_hook_insert_before (GHookList *hook_list,
GHook *sibling,
GHook *hook)
{
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook_list->is_setup);
g_return_if_fail (hook != NULL);
g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
g_return_if_fail (hook->ref_count == 0);
hook->hook_id = hook_list->seq_id++;
hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
if (sibling)
{
if (sibling->prev)
{
hook->prev = sibling->prev;
hook->prev->next = hook;
hook->next = sibling;
sibling->prev = hook;
}
else
{
hook_list->hooks = hook;
hook->next = sibling;
sibling->prev = hook;
}
}
else
{
if (hook_list->hooks)
{
sibling = hook_list->hooks;
while (sibling->next)
sibling = sibling->next;
hook->prev = sibling;
sibling->next = hook;
}
else
hook_list->hooks = hook;
}
}
/**
* g_hook_list_invoke:
* @hook_list: a #GHookList
* @may_recurse: %TRUE if functions which are already running
* (e.g. in another thread) can be called. If set to %FALSE,
* these are skipped
*
* Calls all of the #GHook functions in a #GHookList.
*/
void
g_hook_list_invoke (GHookList *hook_list,
gboolean may_recurse)
{
GHook *hook;
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook_list->is_setup);
hook = g_hook_first_valid (hook_list, may_recurse);
while (hook)
{
GHookFunc func;
gboolean was_in_call;
func = (GHookFunc) hook->func;
was_in_call = G_HOOK_IN_CALL (hook);
hook->flags |= G_HOOK_FLAG_IN_CALL;
func (hook->data);
if (!was_in_call)
hook->flags &= ~G_HOOK_FLAG_IN_CALL;
hook = g_hook_next_valid (hook_list, hook, may_recurse);
}
}
/**
* g_hook_list_invoke_check:
* @hook_list: a #GHookList
* @may_recurse: %TRUE if functions which are already running
* (e.g. in another thread) can be called. If set to %FALSE,
* these are skipped
*
* Calls all of the #GHook functions in a #GHookList.
* Any function which returns %FALSE is removed from the #GHookList.
*/
void
g_hook_list_invoke_check (GHookList *hook_list,
gboolean may_recurse)
{
GHook *hook;
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook_list->is_setup);
hook = g_hook_first_valid (hook_list, may_recurse);
while (hook)
{
GHookCheckFunc func;
gboolean was_in_call;
gboolean need_destroy;
func = (GHookCheckFunc) hook->func;
was_in_call = G_HOOK_IN_CALL (hook);
hook->flags |= G_HOOK_FLAG_IN_CALL;
need_destroy = !func (hook->data);
if (!was_in_call)
hook->flags &= ~G_HOOK_FLAG_IN_CALL;
if (need_destroy)
g_hook_destroy_link (hook_list, hook);
hook = g_hook_next_valid (hook_list, hook, may_recurse);
}
}
/**
* GHookCheckMarshaller:
* @hook: a #GHook
* @marshal_data: user data
*
* Defines the type of function used by g_hook_list_marshal_check().
*
* Returns: %FALSE if @hook should be destroyed
*/
/**
* g_hook_list_marshal_check:
* @hook_list: a #GHookList
* @may_recurse: %TRUE if hooks which are currently running
* (e.g. in another thread) are considered valid. If set to %FALSE,
* these are skipped
* @marshaller: (scope call): the function to call for each #GHook
* @marshal_data: data to pass to @marshaller
*
* Calls a function on each valid #GHook and destroys it if the
* function returns %FALSE.
*/
void
g_hook_list_marshal_check (GHookList *hook_list,
gboolean may_recurse,
GHookCheckMarshaller marshaller,
gpointer data)
{
GHook *hook;
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook_list->is_setup);
g_return_if_fail (marshaller != NULL);
hook = g_hook_first_valid (hook_list, may_recurse);
while (hook)
{
gboolean was_in_call;
gboolean need_destroy;
was_in_call = G_HOOK_IN_CALL (hook);
hook->flags |= G_HOOK_FLAG_IN_CALL;
need_destroy = !marshaller (hook, data);
if (!was_in_call)
hook->flags &= ~G_HOOK_FLAG_IN_CALL;
if (need_destroy)
g_hook_destroy_link (hook_list, hook);
hook = g_hook_next_valid (hook_list, hook, may_recurse);
}
}
/**
* GHookMarshaller:
* @hook: a #GHook
* @marshal_data: user data
*
* Defines the type of function used by g_hook_list_marshal().
*/
/**
* g_hook_list_marshal:
* @hook_list: a #GHookList
* @may_recurse: %TRUE if hooks which are currently running
* (e.g. in another thread) are considered valid. If set to %FALSE,
* these are skipped
* @marshaller: (scope call): the function to call for each #GHook
* @marshal_data: data to pass to @marshaller
*
* Calls a function on each valid #GHook.
*/
void
g_hook_list_marshal (GHookList *hook_list,
gboolean may_recurse,
GHookMarshaller marshaller,
gpointer data)
{
GHook *hook;
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook_list->is_setup);
g_return_if_fail (marshaller != NULL);
hook = g_hook_first_valid (hook_list, may_recurse);
while (hook)
{
gboolean was_in_call;
was_in_call = G_HOOK_IN_CALL (hook);
hook->flags |= G_HOOK_FLAG_IN_CALL;
marshaller (hook, data);
if (!was_in_call)
hook->flags &= ~G_HOOK_FLAG_IN_CALL;
hook = g_hook_next_valid (hook_list, hook, may_recurse);
}
}
/**
* g_hook_first_valid:
* @hook_list: a #GHookList
* @may_be_in_call: %TRUE if hooks which are currently running
* (e.g. in another thread) are considered valid. If set to %FALSE,
* these are skipped
*
* Returns the first #GHook in a #GHookList which has not been destroyed.
* The reference count for the #GHook is incremented, so you must call
* g_hook_unref() to restore it when no longer needed. (Or call
* g_hook_next_valid() if you are stepping through the #GHookList.)
*
* Returns: the first valid #GHook, or %NULL if none are valid
*/
GHook*
g_hook_first_valid (GHookList *hook_list,
gboolean may_be_in_call)
{
g_return_val_if_fail (hook_list != NULL, NULL);
if (hook_list->is_setup)
{
GHook *hook;
hook = hook_list->hooks;
if (hook)
{
g_hook_ref (hook_list, hook);
if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
return hook;
else
return g_hook_next_valid (hook_list, hook, may_be_in_call);
}
}
return NULL;
}
/**
* g_hook_next_valid:
* @hook_list: a #GHookList
* @hook: the current #GHook
* @may_be_in_call: %TRUE if hooks which are currently running
* (e.g. in another thread) are considered valid. If set to %FALSE,
* these are skipped
*
* Returns the next #GHook in a #GHookList which has not been destroyed.
* The reference count for the #GHook is incremented, so you must call
* g_hook_unref() to restore it when no longer needed. (Or continue to call
* g_hook_next_valid() until %NULL is returned.)
*
* Returns: the next valid #GHook, or %NULL if none are valid
*/
GHook*
g_hook_next_valid (GHookList *hook_list,
GHook *hook,
gboolean may_be_in_call)
{
GHook *ohook = hook;
g_return_val_if_fail (hook_list != NULL, NULL);
if (!hook)
return NULL;
hook = hook->next;
while (hook)
{
if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
{
g_hook_ref (hook_list, hook);
g_hook_unref (hook_list, ohook);
return hook;
}
hook = hook->next;
}
g_hook_unref (hook_list, ohook);
return NULL;
}
/**
* g_hook_get:
* @hook_list: a #GHookList
* @hook_id: a hook id
*
* Returns the #GHook with the given id, or %NULL if it is not found.
*
* Returns: the #GHook with the given id, or %NULL if it is not found
*/
GHook*
g_hook_get (GHookList *hook_list,
gulong hook_id)
{
GHook *hook;
g_return_val_if_fail (hook_list != NULL, NULL);
g_return_val_if_fail (hook_id > 0, NULL);
hook = hook_list->hooks;
while (hook)
{
if (hook->hook_id == hook_id)
return hook;
hook = hook->next;
}
return NULL;
}
/**
* GHookFindFunc:
* @hook: a #GHook
* @data: user data passed to g_hook_find_func()
*
* Defines the type of the function passed to g_hook_find().
*
* Returns: %TRUE if the required #GHook has been found
*/
/**
* g_hook_find:
* @hook_list: a #GHookList
* @need_valids: %TRUE if #GHook elements which have been destroyed
* should be skipped
* @func: (scope call): the function to call for each #GHook, which should return
* %TRUE when the #GHook has been found
* @data: the data to pass to @func
*
* Finds a #GHook in a #GHookList using the given function to
* test for a match.
*
* Returns: the found #GHook or %NULL if no matching #GHook is found
*/
GHook*
g_hook_find (GHookList *hook_list,
gboolean need_valids,
GHookFindFunc func,
gpointer data)
{
GHook *hook;
g_return_val_if_fail (hook_list != NULL, NULL);
g_return_val_if_fail (func != NULL, NULL);
hook = hook_list->hooks;
while (hook)
{
GHook *tmp;
/* test only non-destroyed hooks */
if (!hook->hook_id)
{
hook = hook->next;
continue;
}
g_hook_ref (hook_list, hook);
if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
{
g_hook_unref (hook_list, hook);
return hook;
}
tmp = hook->next;
g_hook_unref (hook_list, hook);
hook = tmp;
}
return NULL;
}
/**
* g_hook_find_data:
* @hook_list: a #GHookList
* @need_valids: %TRUE if #GHook elements which have been destroyed
* should be skipped
* @data: the data to find
*
* Finds a #GHook in a #GHookList with the given data.
*
* Returns: the #GHook with the given @data or %NULL if no matching
* #GHook is found
*/
GHook*
g_hook_find_data (GHookList *hook_list,
gboolean need_valids,
gpointer data)
{
GHook *hook;
g_return_val_if_fail (hook_list != NULL, NULL);
hook = hook_list->hooks;
while (hook)
{
/* test only non-destroyed hooks */
if (hook->data == data &&
hook->hook_id &&
(!need_valids || G_HOOK_ACTIVE (hook)))
return hook;
hook = hook->next;
}
return NULL;
}
/**
* g_hook_find_func:
* @hook_list: a #GHookList
* @need_valids: %TRUE if #GHook elements which have been destroyed
* should be skipped
* @func: the function to find
*
* Finds a #GHook in a #GHookList with the given function.
*
* Returns: the #GHook with the given @func or %NULL if no matching
* #GHook is found
*/
GHook*
g_hook_find_func (GHookList *hook_list,
gboolean need_valids,
gpointer func)
{
GHook *hook;
g_return_val_if_fail (hook_list != NULL, NULL);
g_return_val_if_fail (func != NULL, NULL);
hook = hook_list->hooks;
while (hook)
{
/* test only non-destroyed hooks */
if (hook->func == func &&
hook->hook_id &&
(!need_valids || G_HOOK_ACTIVE (hook)))
return hook;
hook = hook->next;
}
return NULL;
}
/**
* g_hook_find_func_data:
* @hook_list: a #GHookList
* @need_valids: %TRUE if #GHook elements which have been destroyed
* should be skipped
* @func: (not nullable): the function to find
* @data: the data to find
*
* Finds a #GHook in a #GHookList with the given function and data.
*
* Returns: the #GHook with the given @func and @data or %NULL if
* no matching #GHook is found
*/
GHook*
g_hook_find_func_data (GHookList *hook_list,
gboolean need_valids,
gpointer func,
gpointer data)
{
GHook *hook;
g_return_val_if_fail (hook_list != NULL, NULL);
g_return_val_if_fail (func != NULL, NULL);
hook = hook_list->hooks;
while (hook)
{
/* test only non-destroyed hooks */
if (hook->data == data &&
hook->func == func &&
hook->hook_id &&
(!need_valids || G_HOOK_ACTIVE (hook)))
return hook;
hook = hook->next;
}
return NULL;
}
/**
* GHookCompareFunc:
* @new_hook: the #GHook being inserted
* @sibling: the #GHook to compare with @new_hook
*
* Defines the type of function used to compare #GHook elements in
* g_hook_insert_sorted().
*
* Returns: a value <= 0 if @new_hook should be before @sibling
*/
/**
* g_hook_insert_sorted:
* @hook_list: a #GHookList
* @hook: the #GHook to insert
* @func: (scope call): the comparison function used to sort the #GHook elements
*
* Inserts a #GHook into a #GHookList, sorted by the given function.
*/
void
g_hook_insert_sorted (GHookList *hook_list,
GHook *hook,
GHookCompareFunc func)
{
GHook *sibling;
g_return_if_fail (hook_list != NULL);
g_return_if_fail (hook_list->is_setup);
g_return_if_fail (hook != NULL);
g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
g_return_if_fail (hook->func != NULL);
g_return_if_fail (func != NULL);
/* first non-destroyed hook */
sibling = hook_list->hooks;
while (sibling && !sibling->hook_id)
sibling = sibling->next;
while (sibling)
{
GHook *tmp;
g_hook_ref (hook_list, sibling);
if (func (hook, sibling) <= 0 && sibling->hook_id)
{
g_hook_unref (hook_list, sibling);
break;
}
/* next non-destroyed hook */
tmp = sibling->next;
while (tmp && !tmp->hook_id)
tmp = tmp->next;
g_hook_unref (hook_list, sibling);
sibling = tmp;
}
g_hook_insert_before (hook_list, sibling, hook);
}
/**
* g_hook_compare_ids:
* @new_hook: a #GHook
* @sibling: a #GHook to compare with @new_hook
*
* Compares the ids of two #GHook elements, returning a negative value
* if the second id is greater than the first.
*
* Returns: a value <= 0 if the id of @sibling is >= the id of @new_hook
*/
gint
g_hook_compare_ids (GHook *new_hook,
GHook *sibling)
{
if (new_hook->hook_id < sibling->hook_id)
return -1;
else if (new_hook->hook_id > sibling->hook_id)
return 1;
return 0;
}
|