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 Library 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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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.
31 #define G_HOOKS_PREALLOC (16)
34 /* --- functions --- */
36 g_hook_list_init (GHookList *hook_list,
39 g_return_if_fail (hook_list != NULL);
40 g_return_if_fail (hook_size >= sizeof (GHook));
42 hook_list->seq_id = 1;
43 hook_list->hook_size = hook_size;
44 hook_list->is_setup = TRUE;
45 hook_list->hooks = NULL;
46 hook_list->hook_memchunk = g_mem_chunk_new ("GHook Memchunk",
48 hook_size * G_HOOKS_PREALLOC,
50 hook_list->hook_free = NULL;
54 g_hook_list_clear (GHookList *hook_list)
56 g_return_if_fail (hook_list != NULL);
58 if (hook_list->is_setup)
62 hook_list->is_setup = FALSE;
64 hook = hook_list->hooks;
67 g_mem_chunk_destroy (hook_list->hook_memchunk);
68 hook_list->hook_memchunk = NULL;
75 g_hook_ref (hook_list, hook);
76 g_hook_destroy_link (hook_list, hook);
78 g_hook_unref (hook_list, hook);
86 g_hook_alloc (GHookList *hook_list)
90 g_return_val_if_fail (hook_list != NULL, NULL);
91 g_return_val_if_fail (hook_list->is_setup, NULL);
93 hook = g_chunk_new0 (GHook, hook_list->hook_memchunk);
97 hook->flags = G_HOOK_FLAG_ACTIVE;
101 hook->destroy = NULL;
107 g_hook_free (GHookList *hook_list,
110 g_return_if_fail (hook_list != NULL);
111 g_return_if_fail (hook_list->is_setup);
112 g_return_if_fail (hook != NULL);
113 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
115 if (hook_list->hook_free)
116 hook_list->hook_free (hook_list, hook);
118 g_chunk_free (hook, hook_list->hook_memchunk);
122 g_hook_destroy_link (GHookList *hook_list,
125 g_return_if_fail (hook_list != NULL);
126 g_return_if_fail (hook != NULL);
131 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
134 GDestroyNotify destroy;
136 destroy = hook->destroy;
137 hook->destroy = NULL;
138 destroy (hook->data);
140 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
145 g_hook_destroy (GHookList *hook_list,
150 g_return_val_if_fail (hook_list != NULL, FALSE);
151 g_return_val_if_fail (hook_id > 0, FALSE);
153 hook = g_hook_get (hook_list, hook_id);
156 g_hook_destroy_link (hook_list, hook);
164 g_hook_unref (GHookList *hook_list,
167 g_return_if_fail (hook_list != NULL);
168 g_return_if_fail (hook != NULL);
169 g_return_if_fail (hook->ref_count > 0);
172 if (!hook->ref_count)
174 g_return_if_fail (hook->hook_id == 0);
175 g_return_if_fail (!G_HOOK_IN_CALL (hook));
178 hook->prev->next = hook->next;
180 hook_list->hooks = hook->next;
183 hook->next->prev = hook->prev;
188 g_hook_free (hook_list, hook);
190 if (!hook_list->hooks &&
191 !hook_list->is_setup)
193 g_mem_chunk_destroy (hook_list->hook_memchunk);
194 hook_list->hook_memchunk = NULL;
200 g_hook_ref (GHookList *hook_list,
203 g_return_if_fail (hook_list != NULL);
204 g_return_if_fail (hook != NULL);
205 g_return_if_fail (hook->ref_count > 0);
211 g_hook_prepend (GHookList *hook_list,
214 g_return_if_fail (hook_list != NULL);
216 g_hook_insert_before (hook_list, hook_list->hooks, hook);
220 g_hook_insert_before (GHookList *hook_list,
224 g_return_if_fail (hook_list != NULL);
225 g_return_if_fail (hook_list->is_setup);
226 g_return_if_fail (hook != NULL);
227 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
228 g_return_if_fail (hook->func != NULL);
230 hook->hook_id = hook_list->seq_id++;
231 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
237 hook->prev = sibling->prev;
238 hook->prev->next = hook;
239 hook->next = sibling;
240 sibling->prev = hook;
244 hook_list->hooks = hook;
245 hook->next = sibling;
246 sibling->prev = hook;
251 if (hook_list->hooks)
253 sibling = hook_list->hooks;
254 while (sibling->next)
255 sibling = sibling->next;
256 hook->prev = sibling;
257 sibling->next = hook;
260 hook_list->hooks = hook;
265 g_hook_list_invoke (GHookList *hook_list,
266 gboolean may_recurse)
270 g_return_if_fail (hook_list != NULL);
271 g_return_if_fail (hook_list->is_setup);
273 hook = g_hook_first_valid (hook_list, may_recurse);
278 gboolean was_in_call;
280 g_hook_ref (hook_list, hook);
281 func = (GHookFunc) hook->func;
283 was_in_call = G_HOOK_IN_CALL (hook);
284 hook->flags |= G_HOOK_FLAG_IN_CALL;
287 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
289 tmp = g_hook_next_valid (hook_list, hook, may_recurse);
291 g_hook_unref (hook_list, hook);
297 g_hook_list_invoke_check (GHookList *hook_list,
298 gboolean may_recurse)
302 g_return_if_fail (hook_list != NULL);
303 g_return_if_fail (hook_list->is_setup);
305 hook = g_hook_first_valid (hook_list, may_recurse);
310 gboolean was_in_call;
311 gboolean need_destroy;
313 g_hook_ref (hook_list, hook);
314 func = (GHookCheckFunc) hook->func;
316 was_in_call = G_HOOK_IN_CALL (hook);
317 hook->flags |= G_HOOK_FLAG_IN_CALL;
318 need_destroy = !func (hook->data);
320 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
322 g_hook_destroy_link (hook_list, hook);
324 tmp = g_hook_next_valid (hook_list, hook, may_recurse);
326 g_hook_unref (hook_list, hook);
332 g_hook_list_marshal_check (GHookList *hook_list,
333 gboolean may_recurse,
334 GHookCheckMarshaller marshaller,
339 g_return_if_fail (hook_list != NULL);
340 g_return_if_fail (hook_list->is_setup);
341 g_return_if_fail (marshaller != NULL);
343 hook = g_hook_first_valid (hook_list, may_recurse);
347 gboolean was_in_call;
348 gboolean need_destroy;
350 g_hook_ref (hook_list, hook);
352 was_in_call = G_HOOK_IN_CALL (hook);
353 hook->flags |= G_HOOK_FLAG_IN_CALL;
354 need_destroy = !marshaller (hook, data);
356 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
358 g_hook_destroy_link (hook_list, hook);
360 tmp = g_hook_next_valid (hook_list, hook, may_recurse);
362 g_hook_unref (hook_list, hook);
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);
383 gboolean was_in_call;
385 g_hook_ref (hook_list, hook);
387 was_in_call = G_HOOK_IN_CALL (hook);
388 hook->flags |= G_HOOK_FLAG_IN_CALL;
389 marshaller (hook, data);
391 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
393 tmp = g_hook_next_valid (hook_list, hook, may_recurse);
395 g_hook_unref (hook_list, hook);
401 g_hook_first_valid (GHookList *hook_list,
402 gboolean may_be_in_call)
404 g_return_val_if_fail (hook_list != NULL, NULL);
406 if (hook_list->is_setup)
410 hook = hook_list->hooks;
413 g_hook_ref (hook_list, hook);
414 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
417 return g_hook_next_valid (hook_list, hook, may_be_in_call);
425 g_hook_next_valid (GHookList *hook_list,
427 gboolean may_be_in_call)
431 g_return_val_if_fail (hook_list != NULL, NULL);
439 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
441 g_hook_ref (hook_list, hook);
442 g_hook_unref (hook_list, ohook);
448 g_hook_unref (hook_list, ohook);
454 g_hook_get (GHookList *hook_list,
459 g_return_val_if_fail (hook_list != NULL, NULL);
460 g_return_val_if_fail (hook_id > 0, NULL);
462 hook = hook_list->hooks;
465 if (hook->hook_id == hook_id)
474 g_hook_find (GHookList *hook_list,
475 gboolean need_valids,
481 g_return_val_if_fail (hook_list != NULL, NULL);
482 g_return_val_if_fail (func != NULL, NULL);
484 hook = hook_list->hooks;
489 /* test only non-destroyed hooks */
496 g_hook_ref (hook_list, hook);
498 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
500 g_hook_unref (hook_list, hook);
506 g_hook_unref (hook_list, hook);
514 g_hook_find_data (GHookList *hook_list,
515 gboolean need_valids,
520 g_return_val_if_fail (hook_list != NULL, NULL);
522 hook = hook_list->hooks;
525 /* test only non-destroyed hooks */
526 if (hook->data == data &&
528 (!need_valids || G_HOOK_ACTIVE (hook)))
538 g_hook_find_func (GHookList *hook_list,
539 gboolean need_valids,
544 g_return_val_if_fail (hook_list != NULL, NULL);
545 g_return_val_if_fail (func != NULL, NULL);
547 hook = hook_list->hooks;
550 /* test only non-destroyed hooks */
551 if (hook->func == func &&
553 (!need_valids || G_HOOK_ACTIVE (hook)))
563 g_hook_find_func_data (GHookList *hook_list,
564 gboolean need_valids,
570 g_return_val_if_fail (hook_list != NULL, NULL);
571 g_return_val_if_fail (func != NULL, NULL);
573 hook = hook_list->hooks;
576 /* test only non-destroyed hooks */
577 if (hook->data == data &&
578 hook->func == func &&
580 (!need_valids || G_HOOK_ACTIVE (hook)))
590 g_hook_insert_sorted (GHookList *hook_list,
592 GHookCompareFunc func)
596 g_return_if_fail (hook_list != NULL);
597 g_return_if_fail (hook_list->is_setup);
598 g_return_if_fail (hook != NULL);
599 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
600 g_return_if_fail (hook->func != NULL);
601 g_return_if_fail (func != NULL);
603 /* first non-destroyed hook */
604 sibling = hook_list->hooks;
605 while (sibling && !sibling->hook_id)
606 sibling = sibling->next;
612 g_hook_ref (hook_list, sibling);
613 if (func (hook, sibling) <= 0 && sibling->hook_id)
615 g_hook_unref (hook_list, sibling);
619 /* next non-destroyed hook */
621 while (tmp && !tmp->hook_id)
624 g_hook_unref (hook_list, sibling);
628 g_hook_insert_before (hook_list, sibling, hook);
632 g_hook_compare_ids (GHook *new_hook,
635 return ((glong) new_hook->hook_id) - ((glong) sibling->hook_id);