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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
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/.
40 /* --- functions --- */
42 default_finalize_hook (GHookList *hook_list,
45 GDestroyNotify destroy = hook->destroy;
55 g_hook_list_init (GHookList *hook_list,
58 g_return_if_fail (hook_list != NULL);
59 g_return_if_fail (hook_size >= sizeof (GHook));
61 hook_list->seq_id = 1;
62 hook_list->hook_size = hook_size;
63 hook_list->is_setup = TRUE;
64 hook_list->hooks = NULL;
65 hook_list->dummy3 = NULL;
66 hook_list->finalize_hook = default_finalize_hook;
67 hook_list->dummy[0] = NULL;
68 hook_list->dummy[1] = NULL;
72 g_hook_list_clear (GHookList *hook_list)
74 g_return_if_fail (hook_list != NULL);
76 if (hook_list->is_setup)
80 hook_list->is_setup = FALSE;
82 hook = hook_list->hooks;
85 /* destroy hook_list->hook_memchunk */
92 g_hook_ref (hook_list, hook);
93 g_hook_destroy_link (hook_list, hook);
95 g_hook_unref (hook_list, hook);
103 g_hook_alloc (GHookList *hook_list)
107 g_return_val_if_fail (hook_list != NULL, NULL);
108 g_return_val_if_fail (hook_list->is_setup, NULL);
110 hook = g_slice_alloc0 (hook_list->hook_size);
114 hook->flags = G_HOOK_FLAG_ACTIVE;
118 hook->destroy = NULL;
124 g_hook_free (GHookList *hook_list,
127 g_return_if_fail (hook_list != NULL);
128 g_return_if_fail (hook_list->is_setup);
129 g_return_if_fail (hook != NULL);
130 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
131 g_return_if_fail (!G_HOOK_IN_CALL (hook));
133 if(hook_list->finalize_hook != NULL)
134 hook_list->finalize_hook (hook_list, hook);
135 g_slice_free1 (hook_list->hook_size, hook);
139 g_hook_destroy_link (GHookList *hook_list,
142 g_return_if_fail (hook_list != NULL);
143 g_return_if_fail (hook != NULL);
145 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
149 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
154 g_hook_destroy (GHookList *hook_list,
159 g_return_val_if_fail (hook_list != NULL, FALSE);
160 g_return_val_if_fail (hook_id > 0, FALSE);
162 hook = g_hook_get (hook_list, hook_id);
165 g_hook_destroy_link (hook_list, hook);
173 g_hook_unref (GHookList *hook_list,
176 g_return_if_fail (hook_list != NULL);
177 g_return_if_fail (hook != NULL);
178 g_return_if_fail (hook->ref_count > 0);
181 if (!hook->ref_count)
183 g_return_if_fail (hook->hook_id == 0);
184 g_return_if_fail (!G_HOOK_IN_CALL (hook));
187 hook->prev->next = hook->next;
189 hook_list->hooks = hook->next;
192 hook->next->prev = hook->prev;
197 if (!hook_list->is_setup)
199 hook_list->is_setup = TRUE;
200 g_hook_free (hook_list, hook);
201 hook_list->is_setup = FALSE;
203 if (!hook_list->hooks)
205 /* destroy hook_list->hook_memchunk */
209 g_hook_free (hook_list, hook);
214 g_hook_ref (GHookList *hook_list,
217 g_return_val_if_fail (hook_list != NULL, NULL);
218 g_return_val_if_fail (hook != NULL, NULL);
219 g_return_val_if_fail (hook->ref_count > 0, NULL);
227 g_hook_prepend (GHookList *hook_list,
230 g_return_if_fail (hook_list != NULL);
232 g_hook_insert_before (hook_list, hook_list->hooks, hook);
236 g_hook_insert_before (GHookList *hook_list,
240 g_return_if_fail (hook_list != NULL);
241 g_return_if_fail (hook_list->is_setup);
242 g_return_if_fail (hook != NULL);
243 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
244 g_return_if_fail (hook->ref_count == 0);
246 hook->hook_id = hook_list->seq_id++;
247 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
253 hook->prev = sibling->prev;
254 hook->prev->next = hook;
255 hook->next = sibling;
256 sibling->prev = hook;
260 hook_list->hooks = hook;
261 hook->next = sibling;
262 sibling->prev = hook;
267 if (hook_list->hooks)
269 sibling = hook_list->hooks;
270 while (sibling->next)
271 sibling = sibling->next;
272 hook->prev = sibling;
273 sibling->next = hook;
276 hook_list->hooks = hook;
281 g_hook_list_invoke (GHookList *hook_list,
282 gboolean may_recurse)
286 g_return_if_fail (hook_list != NULL);
287 g_return_if_fail (hook_list->is_setup);
289 hook = g_hook_first_valid (hook_list, may_recurse);
293 gboolean was_in_call;
295 func = (GHookFunc) hook->func;
297 was_in_call = G_HOOK_IN_CALL (hook);
298 hook->flags |= G_HOOK_FLAG_IN_CALL;
301 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
303 hook = g_hook_next_valid (hook_list, hook, may_recurse);
308 g_hook_list_invoke_check (GHookList *hook_list,
309 gboolean may_recurse)
313 g_return_if_fail (hook_list != NULL);
314 g_return_if_fail (hook_list->is_setup);
316 hook = g_hook_first_valid (hook_list, may_recurse);
320 gboolean was_in_call;
321 gboolean need_destroy;
323 func = (GHookCheckFunc) hook->func;
325 was_in_call = G_HOOK_IN_CALL (hook);
326 hook->flags |= G_HOOK_FLAG_IN_CALL;
327 need_destroy = !func (hook->data);
329 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
331 g_hook_destroy_link (hook_list, hook);
333 hook = g_hook_next_valid (hook_list, hook, may_recurse);
338 g_hook_list_marshal_check (GHookList *hook_list,
339 gboolean may_recurse,
340 GHookCheckMarshaller marshaller,
345 g_return_if_fail (hook_list != NULL);
346 g_return_if_fail (hook_list->is_setup);
347 g_return_if_fail (marshaller != NULL);
349 hook = g_hook_first_valid (hook_list, may_recurse);
352 gboolean was_in_call;
353 gboolean need_destroy;
355 was_in_call = G_HOOK_IN_CALL (hook);
356 hook->flags |= G_HOOK_FLAG_IN_CALL;
357 need_destroy = !marshaller (hook, data);
359 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
361 g_hook_destroy_link (hook_list, hook);
363 hook = g_hook_next_valid (hook_list, hook, may_recurse);
368 g_hook_list_marshal (GHookList *hook_list,
369 gboolean may_recurse,
370 GHookMarshaller marshaller,
375 g_return_if_fail (hook_list != NULL);
376 g_return_if_fail (hook_list->is_setup);
377 g_return_if_fail (marshaller != NULL);
379 hook = g_hook_first_valid (hook_list, may_recurse);
382 gboolean was_in_call;
384 was_in_call = G_HOOK_IN_CALL (hook);
385 hook->flags |= G_HOOK_FLAG_IN_CALL;
386 marshaller (hook, data);
388 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
390 hook = g_hook_next_valid (hook_list, hook, may_recurse);
395 g_hook_first_valid (GHookList *hook_list,
396 gboolean may_be_in_call)
398 g_return_val_if_fail (hook_list != NULL, NULL);
400 if (hook_list->is_setup)
404 hook = hook_list->hooks;
407 g_hook_ref (hook_list, hook);
408 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
411 return g_hook_next_valid (hook_list, hook, may_be_in_call);
419 g_hook_next_valid (GHookList *hook_list,
421 gboolean may_be_in_call)
425 g_return_val_if_fail (hook_list != NULL, NULL);
433 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
435 g_hook_ref (hook_list, hook);
436 g_hook_unref (hook_list, ohook);
442 g_hook_unref (hook_list, ohook);
448 g_hook_get (GHookList *hook_list,
453 g_return_val_if_fail (hook_list != NULL, NULL);
454 g_return_val_if_fail (hook_id > 0, NULL);
456 hook = hook_list->hooks;
459 if (hook->hook_id == hook_id)
468 g_hook_find (GHookList *hook_list,
469 gboolean need_valids,
475 g_return_val_if_fail (hook_list != NULL, NULL);
476 g_return_val_if_fail (func != NULL, NULL);
478 hook = hook_list->hooks;
483 /* test only non-destroyed hooks */
490 g_hook_ref (hook_list, hook);
492 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
494 g_hook_unref (hook_list, hook);
500 g_hook_unref (hook_list, hook);
508 g_hook_find_data (GHookList *hook_list,
509 gboolean need_valids,
514 g_return_val_if_fail (hook_list != NULL, NULL);
516 hook = hook_list->hooks;
519 /* test only non-destroyed hooks */
520 if (hook->data == data &&
522 (!need_valids || G_HOOK_ACTIVE (hook)))
532 g_hook_find_func (GHookList *hook_list,
533 gboolean need_valids,
538 g_return_val_if_fail (hook_list != NULL, NULL);
539 g_return_val_if_fail (func != NULL, NULL);
541 hook = hook_list->hooks;
544 /* test only non-destroyed hooks */
545 if (hook->func == func &&
547 (!need_valids || G_HOOK_ACTIVE (hook)))
557 g_hook_find_func_data (GHookList *hook_list,
558 gboolean need_valids,
564 g_return_val_if_fail (hook_list != NULL, NULL);
565 g_return_val_if_fail (func != NULL, NULL);
567 hook = hook_list->hooks;
570 /* test only non-destroyed hooks */
571 if (hook->data == data &&
572 hook->func == func &&
574 (!need_valids || G_HOOK_ACTIVE (hook)))
584 g_hook_insert_sorted (GHookList *hook_list,
586 GHookCompareFunc func)
590 g_return_if_fail (hook_list != NULL);
591 g_return_if_fail (hook_list->is_setup);
592 g_return_if_fail (hook != NULL);
593 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
594 g_return_if_fail (hook->func != NULL);
595 g_return_if_fail (func != NULL);
597 /* first non-destroyed hook */
598 sibling = hook_list->hooks;
599 while (sibling && !sibling->hook_id)
600 sibling = sibling->next;
606 g_hook_ref (hook_list, sibling);
607 if (func (hook, sibling) <= 0 && sibling->hook_id)
609 g_hook_unref (hook_list, sibling);
613 /* next non-destroyed hook */
615 while (tmp && !tmp->hook_id)
618 g_hook_unref (hook_list, sibling);
622 g_hook_insert_before (hook_list, sibling, hook);
626 g_hook_compare_ids (GHook *new_hook,
629 if (new_hook->hook_id < sibling->hook_id)
631 else if (new_hook->hook_id > sibling->hook_id)
638 #include "galiasdef.c"