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 #define G_HOOKS_PREALLOC (16)
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));
61 g_return_if_fail (hook_size < 65536);
63 hook_list->seq_id = 1;
64 hook_list->hook_size = hook_size;
65 hook_list->is_setup = TRUE;
66 hook_list->hooks = NULL;
67 hook_list->hook_memchunk = g_mem_chunk_new ("GHook Memchunk",
69 hook_size * G_HOOKS_PREALLOC,
71 hook_list->finalize_hook = default_finalize_hook;
72 hook_list->dummy[0] = NULL;
73 hook_list->dummy[1] = NULL;
77 g_hook_list_clear (GHookList *hook_list)
79 g_return_if_fail (hook_list != NULL);
81 if (hook_list->is_setup)
85 hook_list->is_setup = FALSE;
87 hook = hook_list->hooks;
90 g_mem_chunk_destroy (hook_list->hook_memchunk);
91 hook_list->hook_memchunk = NULL;
98 g_hook_ref (hook_list, hook);
99 g_hook_destroy_link (hook_list, hook);
101 g_hook_unref (hook_list, hook);
105 if (hook_list->hook_memchunk)
106 g_warning (G_STRLOC ": failed to clear hooklist, unconsolidated references on hooks left");
111 g_hook_alloc (GHookList *hook_list)
115 g_return_val_if_fail (hook_list != NULL, NULL);
116 g_return_val_if_fail (hook_list->is_setup, NULL);
118 hook = g_chunk_new0 (GHook, hook_list->hook_memchunk);
122 hook->flags = G_HOOK_FLAG_ACTIVE;
126 hook->destroy = NULL;
132 g_hook_free (GHookList *hook_list,
135 g_return_if_fail (hook_list != NULL);
136 g_return_if_fail (hook_list->is_setup);
137 g_return_if_fail (hook != NULL);
138 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
139 g_return_if_fail (!G_HOOK_IN_CALL (hook));
141 hook_list->finalize_hook (hook_list, hook);
142 g_chunk_free (hook, hook_list->hook_memchunk);
146 g_hook_destroy_link (GHookList *hook_list,
149 g_return_if_fail (hook_list != NULL);
150 g_return_if_fail (hook != NULL);
152 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
156 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
161 g_hook_destroy (GHookList *hook_list,
166 g_return_val_if_fail (hook_list != NULL, FALSE);
167 g_return_val_if_fail (hook_id > 0, FALSE);
169 hook = g_hook_get (hook_list, hook_id);
172 g_hook_destroy_link (hook_list, hook);
180 g_hook_unref (GHookList *hook_list,
183 g_return_if_fail (hook_list != NULL);
184 g_return_if_fail (hook_list->hook_memchunk != NULL);
185 g_return_if_fail (hook != NULL);
186 g_return_if_fail (hook->ref_count > 0);
189 if (!hook->ref_count)
191 g_return_if_fail (hook->hook_id == 0);
192 g_return_if_fail (!G_HOOK_IN_CALL (hook));
195 hook->prev->next = hook->next;
197 hook_list->hooks = hook->next;
200 hook->next->prev = hook->prev;
205 if (!hook_list->is_setup)
207 hook_list->is_setup = TRUE;
208 g_hook_free (hook_list, hook);
209 hook_list->is_setup = FALSE;
211 if (!hook_list->hooks)
213 g_mem_chunk_destroy (hook_list->hook_memchunk);
214 hook_list->hook_memchunk = NULL;
218 g_hook_free (hook_list, hook);
223 g_hook_ref (GHookList *hook_list,
226 g_return_if_fail (hook_list != NULL);
227 g_return_if_fail (hook != NULL);
228 g_return_if_fail (hook->ref_count > 0);
234 g_hook_prepend (GHookList *hook_list,
237 g_return_if_fail (hook_list != NULL);
239 g_hook_insert_before (hook_list, hook_list->hooks, hook);
243 g_hook_insert_before (GHookList *hook_list,
247 g_return_if_fail (hook_list != NULL);
248 g_return_if_fail (hook_list->is_setup);
249 g_return_if_fail (hook != NULL);
250 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
251 g_return_if_fail (hook->ref_count == 0);
253 hook->hook_id = hook_list->seq_id++;
254 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
260 hook->prev = sibling->prev;
261 hook->prev->next = hook;
262 hook->next = sibling;
263 sibling->prev = hook;
267 hook_list->hooks = hook;
268 hook->next = sibling;
269 sibling->prev = hook;
274 if (hook_list->hooks)
276 sibling = hook_list->hooks;
277 while (sibling->next)
278 sibling = sibling->next;
279 hook->prev = sibling;
280 sibling->next = hook;
283 hook_list->hooks = hook;
288 g_hook_list_invoke (GHookList *hook_list,
289 gboolean may_recurse)
293 g_return_if_fail (hook_list != NULL);
294 g_return_if_fail (hook_list->is_setup);
296 hook = g_hook_first_valid (hook_list, may_recurse);
300 gboolean was_in_call;
302 func = (GHookFunc) hook->func;
304 was_in_call = G_HOOK_IN_CALL (hook);
305 hook->flags |= G_HOOK_FLAG_IN_CALL;
308 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
310 hook = g_hook_next_valid (hook_list, hook, may_recurse);
315 g_hook_list_invoke_check (GHookList *hook_list,
316 gboolean may_recurse)
320 g_return_if_fail (hook_list != NULL);
321 g_return_if_fail (hook_list->is_setup);
323 hook = g_hook_first_valid (hook_list, may_recurse);
327 gboolean was_in_call;
328 gboolean need_destroy;
330 func = (GHookCheckFunc) hook->func;
332 was_in_call = G_HOOK_IN_CALL (hook);
333 hook->flags |= G_HOOK_FLAG_IN_CALL;
334 need_destroy = !func (hook->data);
336 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
338 g_hook_destroy_link (hook_list, hook);
340 hook = g_hook_next_valid (hook_list, hook, may_recurse);
345 g_hook_list_marshal_check (GHookList *hook_list,
346 gboolean may_recurse,
347 GHookCheckMarshaller marshaller,
352 g_return_if_fail (hook_list != NULL);
353 g_return_if_fail (hook_list->is_setup);
354 g_return_if_fail (marshaller != NULL);
356 hook = g_hook_first_valid (hook_list, may_recurse);
359 gboolean was_in_call;
360 gboolean need_destroy;
362 was_in_call = G_HOOK_IN_CALL (hook);
363 hook->flags |= G_HOOK_FLAG_IN_CALL;
364 need_destroy = !marshaller (hook, data);
366 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
368 g_hook_destroy_link (hook_list, hook);
370 hook = g_hook_next_valid (hook_list, hook, may_recurse);
375 g_hook_list_marshal (GHookList *hook_list,
376 gboolean may_recurse,
377 GHookMarshaller marshaller,
382 g_return_if_fail (hook_list != NULL);
383 g_return_if_fail (hook_list->is_setup);
384 g_return_if_fail (marshaller != NULL);
386 hook = g_hook_first_valid (hook_list, may_recurse);
389 gboolean was_in_call;
391 was_in_call = G_HOOK_IN_CALL (hook);
392 hook->flags |= G_HOOK_FLAG_IN_CALL;
393 marshaller (hook, data);
395 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
397 hook = g_hook_next_valid (hook_list, hook, may_recurse);
402 g_hook_first_valid (GHookList *hook_list,
403 gboolean may_be_in_call)
405 g_return_val_if_fail (hook_list != NULL, NULL);
407 if (hook_list->is_setup)
411 hook = hook_list->hooks;
414 g_hook_ref (hook_list, hook);
415 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
418 return g_hook_next_valid (hook_list, hook, may_be_in_call);
426 g_hook_next_valid (GHookList *hook_list,
428 gboolean may_be_in_call)
432 g_return_val_if_fail (hook_list != NULL, NULL);
440 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
442 g_hook_ref (hook_list, hook);
443 g_hook_unref (hook_list, ohook);
449 g_hook_unref (hook_list, ohook);
455 g_hook_get (GHookList *hook_list,
460 g_return_val_if_fail (hook_list != NULL, NULL);
461 g_return_val_if_fail (hook_id > 0, NULL);
463 hook = hook_list->hooks;
466 if (hook->hook_id == hook_id)
475 g_hook_find (GHookList *hook_list,
476 gboolean need_valids,
482 g_return_val_if_fail (hook_list != NULL, NULL);
483 g_return_val_if_fail (func != NULL, NULL);
485 hook = hook_list->hooks;
490 /* test only non-destroyed hooks */
497 g_hook_ref (hook_list, hook);
499 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
501 g_hook_unref (hook_list, hook);
507 g_hook_unref (hook_list, hook);
515 g_hook_find_data (GHookList *hook_list,
516 gboolean need_valids,
521 g_return_val_if_fail (hook_list != NULL, NULL);
523 hook = hook_list->hooks;
526 /* test only non-destroyed hooks */
527 if (hook->data == data &&
529 (!need_valids || G_HOOK_ACTIVE (hook)))
539 g_hook_find_func (GHookList *hook_list,
540 gboolean need_valids,
545 g_return_val_if_fail (hook_list != NULL, NULL);
546 g_return_val_if_fail (func != NULL, NULL);
548 hook = hook_list->hooks;
551 /* test only non-destroyed hooks */
552 if (hook->func == func &&
554 (!need_valids || G_HOOK_ACTIVE (hook)))
564 g_hook_find_func_data (GHookList *hook_list,
565 gboolean need_valids,
571 g_return_val_if_fail (hook_list != NULL, NULL);
572 g_return_val_if_fail (func != NULL, NULL);
574 hook = hook_list->hooks;
577 /* test only non-destroyed hooks */
578 if (hook->data == data &&
579 hook->func == func &&
581 (!need_valids || G_HOOK_ACTIVE (hook)))
591 g_hook_insert_sorted (GHookList *hook_list,
593 GHookCompareFunc func)
597 g_return_if_fail (hook_list != NULL);
598 g_return_if_fail (hook_list->is_setup);
599 g_return_if_fail (hook != NULL);
600 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
601 g_return_if_fail (hook->func != NULL);
602 g_return_if_fail (func != NULL);
604 /* first non-destroyed hook */
605 sibling = hook_list->hooks;
606 while (sibling && !sibling->hook_id)
607 sibling = sibling->next;
613 g_hook_ref (hook_list, sibling);
614 if (func (hook, sibling) <= 0 && sibling->hook_id)
616 g_hook_unref (hook_list, sibling);
620 /* next non-destroyed hook */
622 while (tmp && !tmp->hook_id)
625 g_hook_unref (hook_list, sibling);
629 g_hook_insert_before (hook_list, sibling, hook);
633 g_hook_compare_ids (GHook *new_hook,
636 if (new_hook->hook_id < sibling->hook_id)
638 else if (new_hook->hook_id > sibling->hook_id)