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 #define G_HOOKS_PREALLOC (16)
43 /* --- functions --- */
45 default_finalize_hook (GHookList *hook_list,
48 GDestroyNotify destroy = hook->destroy;
58 g_hook_list_init (GHookList *hook_list,
61 g_return_if_fail (hook_list != NULL);
62 g_return_if_fail (hook_size >= sizeof (GHook));
63 g_return_if_fail (hook_size < 65536);
65 hook_list->seq_id = 1;
66 hook_list->hook_size = hook_size;
67 hook_list->is_setup = TRUE;
68 hook_list->hooks = NULL;
69 hook_list->hook_memchunk = g_mem_chunk_new ("GHook Memchunk",
71 hook_size * G_HOOKS_PREALLOC,
73 hook_list->finalize_hook = default_finalize_hook;
74 hook_list->dummy[0] = NULL;
75 hook_list->dummy[1] = NULL;
79 g_hook_list_clear (GHookList *hook_list)
81 g_return_if_fail (hook_list != NULL);
83 if (hook_list->is_setup)
87 hook_list->is_setup = FALSE;
89 hook = hook_list->hooks;
92 g_mem_chunk_destroy (hook_list->hook_memchunk);
93 hook_list->hook_memchunk = NULL;
100 g_hook_ref (hook_list, hook);
101 g_hook_destroy_link (hook_list, hook);
103 g_hook_unref (hook_list, hook);
107 if (hook_list->hook_memchunk)
108 g_warning (G_STRLOC ": failed to clear hooklist, unconsolidated references on hooks left");
113 g_hook_alloc (GHookList *hook_list)
117 g_return_val_if_fail (hook_list != NULL, NULL);
118 g_return_val_if_fail (hook_list->is_setup, NULL);
120 hook = g_chunk_new0 (GHook, hook_list->hook_memchunk);
124 hook->flags = G_HOOK_FLAG_ACTIVE;
128 hook->destroy = NULL;
134 g_hook_free (GHookList *hook_list,
137 g_return_if_fail (hook_list != NULL);
138 g_return_if_fail (hook_list->is_setup);
139 g_return_if_fail (hook != NULL);
140 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
141 g_return_if_fail (!G_HOOK_IN_CALL (hook));
143 hook_list->finalize_hook (hook_list, hook);
144 g_chunk_free (hook, hook_list->hook_memchunk);
148 g_hook_destroy_link (GHookList *hook_list,
151 g_return_if_fail (hook_list != NULL);
152 g_return_if_fail (hook != NULL);
154 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
158 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
163 g_hook_destroy (GHookList *hook_list,
168 g_return_val_if_fail (hook_list != NULL, FALSE);
169 g_return_val_if_fail (hook_id > 0, FALSE);
171 hook = g_hook_get (hook_list, hook_id);
174 g_hook_destroy_link (hook_list, hook);
182 g_hook_unref (GHookList *hook_list,
185 g_return_if_fail (hook_list != NULL);
186 g_return_if_fail (hook_list->hook_memchunk != NULL);
187 g_return_if_fail (hook != NULL);
188 g_return_if_fail (hook->ref_count > 0);
191 if (!hook->ref_count)
193 g_return_if_fail (hook->hook_id == 0);
194 g_return_if_fail (!G_HOOK_IN_CALL (hook));
197 hook->prev->next = hook->next;
199 hook_list->hooks = hook->next;
202 hook->next->prev = hook->prev;
207 if (!hook_list->is_setup)
209 hook_list->is_setup = TRUE;
210 g_hook_free (hook_list, hook);
211 hook_list->is_setup = FALSE;
213 if (!hook_list->hooks)
215 g_mem_chunk_destroy (hook_list->hook_memchunk);
216 hook_list->hook_memchunk = NULL;
220 g_hook_free (hook_list, hook);
225 g_hook_ref (GHookList *hook_list,
228 g_return_if_fail (hook_list != NULL);
229 g_return_if_fail (hook != NULL);
230 g_return_if_fail (hook->ref_count > 0);
236 g_hook_prepend (GHookList *hook_list,
239 g_return_if_fail (hook_list != NULL);
241 g_hook_insert_before (hook_list, hook_list->hooks, hook);
245 g_hook_insert_before (GHookList *hook_list,
249 g_return_if_fail (hook_list != NULL);
250 g_return_if_fail (hook_list->is_setup);
251 g_return_if_fail (hook != NULL);
252 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
253 g_return_if_fail (hook->ref_count == 0);
255 hook->hook_id = hook_list->seq_id++;
256 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
262 hook->prev = sibling->prev;
263 hook->prev->next = hook;
264 hook->next = sibling;
265 sibling->prev = hook;
269 hook_list->hooks = hook;
270 hook->next = sibling;
271 sibling->prev = hook;
276 if (hook_list->hooks)
278 sibling = hook_list->hooks;
279 while (sibling->next)
280 sibling = sibling->next;
281 hook->prev = sibling;
282 sibling->next = hook;
285 hook_list->hooks = hook;
290 g_hook_list_invoke (GHookList *hook_list,
291 gboolean may_recurse)
295 g_return_if_fail (hook_list != NULL);
296 g_return_if_fail (hook_list->is_setup);
298 hook = g_hook_first_valid (hook_list, may_recurse);
302 gboolean was_in_call;
304 func = (GHookFunc) hook->func;
306 was_in_call = G_HOOK_IN_CALL (hook);
307 hook->flags |= G_HOOK_FLAG_IN_CALL;
310 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
312 hook = g_hook_next_valid (hook_list, hook, may_recurse);
317 g_hook_list_invoke_check (GHookList *hook_list,
318 gboolean may_recurse)
322 g_return_if_fail (hook_list != NULL);
323 g_return_if_fail (hook_list->is_setup);
325 hook = g_hook_first_valid (hook_list, may_recurse);
329 gboolean was_in_call;
330 gboolean need_destroy;
332 func = (GHookCheckFunc) hook->func;
334 was_in_call = G_HOOK_IN_CALL (hook);
335 hook->flags |= G_HOOK_FLAG_IN_CALL;
336 need_destroy = !func (hook->data);
338 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
340 g_hook_destroy_link (hook_list, hook);
342 hook = g_hook_next_valid (hook_list, hook, may_recurse);
347 g_hook_list_marshal_check (GHookList *hook_list,
348 gboolean may_recurse,
349 GHookCheckMarshaller marshaller,
354 g_return_if_fail (hook_list != NULL);
355 g_return_if_fail (hook_list->is_setup);
356 g_return_if_fail (marshaller != NULL);
358 hook = g_hook_first_valid (hook_list, may_recurse);
361 gboolean was_in_call;
362 gboolean need_destroy;
364 was_in_call = G_HOOK_IN_CALL (hook);
365 hook->flags |= G_HOOK_FLAG_IN_CALL;
366 need_destroy = !marshaller (hook, data);
368 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
370 g_hook_destroy_link (hook_list, hook);
372 hook = g_hook_next_valid (hook_list, hook, may_recurse);
377 g_hook_list_marshal (GHookList *hook_list,
378 gboolean may_recurse,
379 GHookMarshaller marshaller,
384 g_return_if_fail (hook_list != NULL);
385 g_return_if_fail (hook_list->is_setup);
386 g_return_if_fail (marshaller != NULL);
388 hook = g_hook_first_valid (hook_list, may_recurse);
391 gboolean was_in_call;
393 was_in_call = G_HOOK_IN_CALL (hook);
394 hook->flags |= G_HOOK_FLAG_IN_CALL;
395 marshaller (hook, data);
397 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
399 hook = g_hook_next_valid (hook_list, hook, may_recurse);
404 g_hook_first_valid (GHookList *hook_list,
405 gboolean may_be_in_call)
407 g_return_val_if_fail (hook_list != NULL, NULL);
409 if (hook_list->is_setup)
413 hook = hook_list->hooks;
416 g_hook_ref (hook_list, hook);
417 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
420 return g_hook_next_valid (hook_list, hook, may_be_in_call);
428 g_hook_next_valid (GHookList *hook_list,
430 gboolean may_be_in_call)
434 g_return_val_if_fail (hook_list != NULL, NULL);
442 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
444 g_hook_ref (hook_list, hook);
445 g_hook_unref (hook_list, ohook);
451 g_hook_unref (hook_list, ohook);
457 g_hook_get (GHookList *hook_list,
462 g_return_val_if_fail (hook_list != NULL, NULL);
463 g_return_val_if_fail (hook_id > 0, NULL);
465 hook = hook_list->hooks;
468 if (hook->hook_id == hook_id)
477 g_hook_find (GHookList *hook_list,
478 gboolean need_valids,
484 g_return_val_if_fail (hook_list != NULL, NULL);
485 g_return_val_if_fail (func != NULL, NULL);
487 hook = hook_list->hooks;
492 /* test only non-destroyed hooks */
499 g_hook_ref (hook_list, hook);
501 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
503 g_hook_unref (hook_list, hook);
509 g_hook_unref (hook_list, hook);
517 g_hook_find_data (GHookList *hook_list,
518 gboolean need_valids,
523 g_return_val_if_fail (hook_list != NULL, NULL);
525 hook = hook_list->hooks;
528 /* test only non-destroyed hooks */
529 if (hook->data == data &&
531 (!need_valids || G_HOOK_ACTIVE (hook)))
541 g_hook_find_func (GHookList *hook_list,
542 gboolean need_valids,
547 g_return_val_if_fail (hook_list != NULL, NULL);
548 g_return_val_if_fail (func != NULL, NULL);
550 hook = hook_list->hooks;
553 /* test only non-destroyed hooks */
554 if (hook->func == func &&
556 (!need_valids || G_HOOK_ACTIVE (hook)))
566 g_hook_find_func_data (GHookList *hook_list,
567 gboolean need_valids,
573 g_return_val_if_fail (hook_list != NULL, NULL);
574 g_return_val_if_fail (func != NULL, NULL);
576 hook = hook_list->hooks;
579 /* test only non-destroyed hooks */
580 if (hook->data == data &&
581 hook->func == func &&
583 (!need_valids || G_HOOK_ACTIVE (hook)))
593 g_hook_insert_sorted (GHookList *hook_list,
595 GHookCompareFunc func)
599 g_return_if_fail (hook_list != NULL);
600 g_return_if_fail (hook_list->is_setup);
601 g_return_if_fail (hook != NULL);
602 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
603 g_return_if_fail (hook->func != NULL);
604 g_return_if_fail (func != NULL);
606 /* first non-destroyed hook */
607 sibling = hook_list->hooks;
608 while (sibling && !sibling->hook_id)
609 sibling = sibling->next;
615 g_hook_ref (hook_list, sibling);
616 if (func (hook, sibling) <= 0 && sibling->hook_id)
618 g_hook_unref (hook_list, sibling);
622 /* next non-destroyed hook */
624 while (tmp && !tmp->hook_id)
627 g_hook_unref (hook_list, sibling);
631 g_hook_insert_before (hook_list, sibling, hook);
635 g_hook_compare_ids (GHook *new_hook,
638 if (new_hook->hook_id < sibling->hook_id)
640 else if (new_hook->hook_id > sibling->hook_id)