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/.
38 #include "gtestutils.h"
41 /* --- functions --- */
43 default_finalize_hook (GHookList *hook_list,
46 GDestroyNotify destroy = hook->destroy;
56 g_hook_list_init (GHookList *hook_list,
59 g_return_if_fail (hook_list != NULL);
60 g_return_if_fail (hook_size >= sizeof (GHook));
62 hook_list->seq_id = 1;
63 hook_list->hook_size = hook_size;
64 hook_list->is_setup = TRUE;
65 hook_list->hooks = NULL;
66 hook_list->dummy3 = NULL;
67 hook_list->finalize_hook = default_finalize_hook;
68 hook_list->dummy[0] = NULL;
69 hook_list->dummy[1] = NULL;
73 g_hook_list_clear (GHookList *hook_list)
75 g_return_if_fail (hook_list != NULL);
77 if (hook_list->is_setup)
81 hook_list->is_setup = FALSE;
83 hook = hook_list->hooks;
86 /* destroy hook_list->hook_memchunk */
93 g_hook_ref (hook_list, hook);
94 g_hook_destroy_link (hook_list, hook);
96 g_hook_unref (hook_list, hook);
104 g_hook_alloc (GHookList *hook_list)
108 g_return_val_if_fail (hook_list != NULL, NULL);
109 g_return_val_if_fail (hook_list->is_setup, NULL);
111 hook = g_slice_alloc0 (hook_list->hook_size);
115 hook->flags = G_HOOK_FLAG_ACTIVE;
119 hook->destroy = NULL;
125 g_hook_free (GHookList *hook_list,
128 g_return_if_fail (hook_list != NULL);
129 g_return_if_fail (hook_list->is_setup);
130 g_return_if_fail (hook != NULL);
131 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
132 g_return_if_fail (!G_HOOK_IN_CALL (hook));
134 if(hook_list->finalize_hook != NULL)
135 hook_list->finalize_hook (hook_list, hook);
136 g_slice_free1 (hook_list->hook_size, hook);
140 g_hook_destroy_link (GHookList *hook_list,
143 g_return_if_fail (hook_list != NULL);
144 g_return_if_fail (hook != NULL);
146 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
150 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
155 g_hook_destroy (GHookList *hook_list,
160 g_return_val_if_fail (hook_list != NULL, FALSE);
161 g_return_val_if_fail (hook_id > 0, FALSE);
163 hook = g_hook_get (hook_list, hook_id);
166 g_hook_destroy_link (hook_list, hook);
174 g_hook_unref (GHookList *hook_list,
177 g_return_if_fail (hook_list != NULL);
178 g_return_if_fail (hook != NULL);
179 g_return_if_fail (hook->ref_count > 0);
182 if (!hook->ref_count)
184 g_return_if_fail (hook->hook_id == 0);
185 g_return_if_fail (!G_HOOK_IN_CALL (hook));
188 hook->prev->next = hook->next;
190 hook_list->hooks = hook->next;
193 hook->next->prev = hook->prev;
198 if (!hook_list->is_setup)
200 hook_list->is_setup = TRUE;
201 g_hook_free (hook_list, hook);
202 hook_list->is_setup = FALSE;
204 if (!hook_list->hooks)
206 /* destroy hook_list->hook_memchunk */
210 g_hook_free (hook_list, hook);
215 g_hook_ref (GHookList *hook_list,
218 g_return_val_if_fail (hook_list != NULL, NULL);
219 g_return_val_if_fail (hook != NULL, NULL);
220 g_return_val_if_fail (hook->ref_count > 0, NULL);
228 g_hook_prepend (GHookList *hook_list,
231 g_return_if_fail (hook_list != NULL);
233 g_hook_insert_before (hook_list, hook_list->hooks, hook);
237 g_hook_insert_before (GHookList *hook_list,
241 g_return_if_fail (hook_list != NULL);
242 g_return_if_fail (hook_list->is_setup);
243 g_return_if_fail (hook != NULL);
244 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
245 g_return_if_fail (hook->ref_count == 0);
247 hook->hook_id = hook_list->seq_id++;
248 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
254 hook->prev = sibling->prev;
255 hook->prev->next = hook;
256 hook->next = sibling;
257 sibling->prev = hook;
261 hook_list->hooks = hook;
262 hook->next = sibling;
263 sibling->prev = hook;
268 if (hook_list->hooks)
270 sibling = hook_list->hooks;
271 while (sibling->next)
272 sibling = sibling->next;
273 hook->prev = sibling;
274 sibling->next = hook;
277 hook_list->hooks = hook;
282 g_hook_list_invoke (GHookList *hook_list,
283 gboolean may_recurse)
287 g_return_if_fail (hook_list != NULL);
288 g_return_if_fail (hook_list->is_setup);
290 hook = g_hook_first_valid (hook_list, may_recurse);
294 gboolean was_in_call;
296 func = (GHookFunc) hook->func;
298 was_in_call = G_HOOK_IN_CALL (hook);
299 hook->flags |= G_HOOK_FLAG_IN_CALL;
302 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
304 hook = g_hook_next_valid (hook_list, hook, may_recurse);
309 g_hook_list_invoke_check (GHookList *hook_list,
310 gboolean may_recurse)
314 g_return_if_fail (hook_list != NULL);
315 g_return_if_fail (hook_list->is_setup);
317 hook = g_hook_first_valid (hook_list, may_recurse);
321 gboolean was_in_call;
322 gboolean need_destroy;
324 func = (GHookCheckFunc) hook->func;
326 was_in_call = G_HOOK_IN_CALL (hook);
327 hook->flags |= G_HOOK_FLAG_IN_CALL;
328 need_destroy = !func (hook->data);
330 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
332 g_hook_destroy_link (hook_list, hook);
334 hook = g_hook_next_valid (hook_list, hook, may_recurse);
339 g_hook_list_marshal_check (GHookList *hook_list,
340 gboolean may_recurse,
341 GHookCheckMarshaller marshaller,
346 g_return_if_fail (hook_list != NULL);
347 g_return_if_fail (hook_list->is_setup);
348 g_return_if_fail (marshaller != NULL);
350 hook = g_hook_first_valid (hook_list, may_recurse);
353 gboolean was_in_call;
354 gboolean need_destroy;
356 was_in_call = G_HOOK_IN_CALL (hook);
357 hook->flags |= G_HOOK_FLAG_IN_CALL;
358 need_destroy = !marshaller (hook, data);
360 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
362 g_hook_destroy_link (hook_list, hook);
364 hook = g_hook_next_valid (hook_list, hook, may_recurse);
369 g_hook_list_marshal (GHookList *hook_list,
370 gboolean may_recurse,
371 GHookMarshaller marshaller,
376 g_return_if_fail (hook_list != NULL);
377 g_return_if_fail (hook_list->is_setup);
378 g_return_if_fail (marshaller != NULL);
380 hook = g_hook_first_valid (hook_list, may_recurse);
383 gboolean was_in_call;
385 was_in_call = G_HOOK_IN_CALL (hook);
386 hook->flags |= G_HOOK_FLAG_IN_CALL;
387 marshaller (hook, data);
389 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
391 hook = g_hook_next_valid (hook_list, hook, may_recurse);
396 g_hook_first_valid (GHookList *hook_list,
397 gboolean may_be_in_call)
399 g_return_val_if_fail (hook_list != NULL, NULL);
401 if (hook_list->is_setup)
405 hook = hook_list->hooks;
408 g_hook_ref (hook_list, hook);
409 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
412 return g_hook_next_valid (hook_list, hook, may_be_in_call);
420 g_hook_next_valid (GHookList *hook_list,
422 gboolean may_be_in_call)
426 g_return_val_if_fail (hook_list != NULL, NULL);
434 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
436 g_hook_ref (hook_list, hook);
437 g_hook_unref (hook_list, ohook);
443 g_hook_unref (hook_list, ohook);
449 g_hook_get (GHookList *hook_list,
454 g_return_val_if_fail (hook_list != NULL, NULL);
455 g_return_val_if_fail (hook_id > 0, NULL);
457 hook = hook_list->hooks;
460 if (hook->hook_id == hook_id)
469 g_hook_find (GHookList *hook_list,
470 gboolean need_valids,
476 g_return_val_if_fail (hook_list != NULL, NULL);
477 g_return_val_if_fail (func != NULL, NULL);
479 hook = hook_list->hooks;
484 /* test only non-destroyed hooks */
491 g_hook_ref (hook_list, hook);
493 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
495 g_hook_unref (hook_list, hook);
501 g_hook_unref (hook_list, hook);
509 g_hook_find_data (GHookList *hook_list,
510 gboolean need_valids,
515 g_return_val_if_fail (hook_list != NULL, NULL);
517 hook = hook_list->hooks;
520 /* test only non-destroyed hooks */
521 if (hook->data == data &&
523 (!need_valids || G_HOOK_ACTIVE (hook)))
533 g_hook_find_func (GHookList *hook_list,
534 gboolean need_valids,
539 g_return_val_if_fail (hook_list != NULL, NULL);
540 g_return_val_if_fail (func != NULL, NULL);
542 hook = hook_list->hooks;
545 /* test only non-destroyed hooks */
546 if (hook->func == func &&
548 (!need_valids || G_HOOK_ACTIVE (hook)))
558 g_hook_find_func_data (GHookList *hook_list,
559 gboolean need_valids,
565 g_return_val_if_fail (hook_list != NULL, NULL);
566 g_return_val_if_fail (func != NULL, NULL);
568 hook = hook_list->hooks;
571 /* test only non-destroyed hooks */
572 if (hook->data == data &&
573 hook->func == func &&
575 (!need_valids || G_HOOK_ACTIVE (hook)))
585 g_hook_insert_sorted (GHookList *hook_list,
587 GHookCompareFunc func)
591 g_return_if_fail (hook_list != NULL);
592 g_return_if_fail (hook_list->is_setup);
593 g_return_if_fail (hook != NULL);
594 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
595 g_return_if_fail (hook->func != NULL);
596 g_return_if_fail (func != NULL);
598 /* first non-destroyed hook */
599 sibling = hook_list->hooks;
600 while (sibling && !sibling->hook_id)
601 sibling = sibling->next;
607 g_hook_ref (hook_list, sibling);
608 if (func (hook, sibling) <= 0 && sibling->hook_id)
610 g_hook_unref (hook_list, sibling);
614 /* next non-destroyed hook */
616 while (tmp && !tmp->hook_id)
619 g_hook_unref (hook_list, sibling);
623 g_hook_insert_before (hook_list, sibling, hook);
627 g_hook_compare_ids (GHook *new_hook,
630 if (new_hook->hook_id < sibling->hook_id)
632 else if (new_hook->hook_id > sibling->hook_id)