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;
51 hook_list->hook_destroy = NULL;
55 g_hook_list_clear (GHookList *hook_list)
57 g_return_if_fail (hook_list != NULL);
59 if (hook_list->is_setup)
63 hook_list->is_setup = FALSE;
65 hook = hook_list->hooks;
68 g_mem_chunk_destroy (hook_list->hook_memchunk);
69 hook_list->hook_memchunk = NULL;
76 g_hook_ref (hook_list, hook);
77 g_hook_destroy_link (hook_list, hook);
79 g_hook_unref (hook_list, hook);
87 g_hook_alloc (GHookList *hook_list)
91 g_return_val_if_fail (hook_list != NULL, NULL);
92 g_return_val_if_fail (hook_list->is_setup, NULL);
94 hook = g_chunk_new0 (GHook, hook_list->hook_memchunk);
98 hook->flags = G_HOOK_FLAG_ACTIVE;
102 hook->destroy = NULL;
108 g_hook_free (GHookList *hook_list,
111 g_return_if_fail (hook_list != NULL);
112 g_return_if_fail (hook_list->is_setup);
113 g_return_if_fail (hook != NULL);
114 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
116 if (hook_list->hook_free)
117 hook_list->hook_free (hook_list, hook);
119 g_chunk_free (hook, hook_list->hook_memchunk);
123 g_hook_destroy_link (GHookList *hook_list,
126 g_return_if_fail (hook_list != NULL);
127 g_return_if_fail (hook != NULL);
132 hook->flags &= ~G_HOOK_FLAG_ACTIVE;
133 if (hook_list->hook_destroy)
135 if (hook_list->hook_destroy != G_HOOK_DEFERRED_DESTROY)
136 hook_list->hook_destroy (hook_list, hook);
138 else if (hook->destroy)
140 hook->destroy (hook->data);
143 hook->destroy = NULL;
145 g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
150 g_hook_destroy (GHookList *hook_list,
155 g_return_val_if_fail (hook_list != NULL, FALSE);
156 g_return_val_if_fail (hook_id > 0, FALSE);
158 hook = g_hook_get (hook_list, hook_id);
161 g_hook_destroy_link (hook_list, hook);
169 g_hook_unref (GHookList *hook_list,
172 g_return_if_fail (hook_list != NULL);
173 g_return_if_fail (hook != NULL);
174 g_return_if_fail (hook->ref_count > 0);
177 if (!hook->ref_count)
179 g_return_if_fail (hook->hook_id == 0);
180 g_return_if_fail (!G_HOOK_IN_CALL (hook));
183 hook->prev->next = hook->next;
185 hook_list->hooks = hook->next;
188 hook->next->prev = hook->prev;
193 g_hook_free (hook_list, hook);
195 if (!hook_list->hooks &&
196 !hook_list->is_setup)
198 g_mem_chunk_destroy (hook_list->hook_memchunk);
199 hook_list->hook_memchunk = NULL;
205 g_hook_ref (GHookList *hook_list,
208 g_return_if_fail (hook_list != NULL);
209 g_return_if_fail (hook != NULL);
210 g_return_if_fail (hook->ref_count > 0);
216 g_hook_prepend (GHookList *hook_list,
219 g_return_if_fail (hook_list != NULL);
221 g_hook_insert_before (hook_list, hook_list->hooks, hook);
225 g_hook_insert_before (GHookList *hook_list,
229 g_return_if_fail (hook_list != NULL);
230 g_return_if_fail (hook_list->is_setup);
231 g_return_if_fail (hook != NULL);
232 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
233 g_return_if_fail (hook->func != NULL);
235 hook->hook_id = hook_list->seq_id++;
236 hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
242 hook->prev = sibling->prev;
243 hook->prev->next = hook;
244 hook->next = sibling;
245 sibling->prev = hook;
249 hook_list->hooks = hook;
250 hook->next = sibling;
251 sibling->prev = hook;
256 if (hook_list->hooks)
258 sibling = hook_list->hooks;
259 while (sibling->next)
260 sibling = sibling->next;
261 hook->prev = sibling;
262 sibling->next = hook;
265 hook_list->hooks = hook;
270 g_hook_list_invoke (GHookList *hook_list,
271 gboolean may_recurse)
275 g_return_if_fail (hook_list != NULL);
276 g_return_if_fail (hook_list->is_setup);
278 hook = g_hook_first_valid (hook_list, may_recurse);
282 gboolean was_in_call;
284 func = (GHookFunc) hook->func;
286 was_in_call = G_HOOK_IN_CALL (hook);
287 hook->flags |= G_HOOK_FLAG_IN_CALL;
290 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
292 hook = g_hook_next_valid (hook_list, hook, may_recurse);
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);
309 gboolean was_in_call;
310 gboolean need_destroy;
312 func = (GHookCheckFunc) hook->func;
314 was_in_call = G_HOOK_IN_CALL (hook);
315 hook->flags |= G_HOOK_FLAG_IN_CALL;
316 need_destroy = !func (hook->data);
318 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
320 g_hook_destroy_link (hook_list, hook);
322 hook = g_hook_next_valid (hook_list, hook, may_recurse);
327 g_hook_list_marshal_check (GHookList *hook_list,
328 gboolean may_recurse,
329 GHookCheckMarshaller marshaller,
334 g_return_if_fail (hook_list != NULL);
335 g_return_if_fail (hook_list->is_setup);
336 g_return_if_fail (marshaller != NULL);
338 hook = g_hook_first_valid (hook_list, may_recurse);
341 gboolean was_in_call;
342 gboolean need_destroy;
344 was_in_call = G_HOOK_IN_CALL (hook);
345 hook->flags |= G_HOOK_FLAG_IN_CALL;
346 need_destroy = !marshaller (hook, data);
348 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
350 g_hook_destroy_link (hook_list, hook);
352 hook = g_hook_next_valid (hook_list, hook, may_recurse);
357 g_hook_list_marshal (GHookList *hook_list,
358 gboolean may_recurse,
359 GHookMarshaller marshaller,
364 g_return_if_fail (hook_list != NULL);
365 g_return_if_fail (hook_list->is_setup);
366 g_return_if_fail (marshaller != NULL);
368 hook = g_hook_first_valid (hook_list, may_recurse);
371 gboolean was_in_call;
373 was_in_call = G_HOOK_IN_CALL (hook);
374 hook->flags |= G_HOOK_FLAG_IN_CALL;
375 marshaller (hook, data);
377 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
379 hook = g_hook_next_valid (hook_list, hook, may_recurse);
384 g_hook_first_valid (GHookList *hook_list,
385 gboolean may_be_in_call)
387 g_return_val_if_fail (hook_list != NULL, NULL);
389 if (hook_list->is_setup)
393 hook = hook_list->hooks;
396 g_hook_ref (hook_list, hook);
397 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
400 return g_hook_next_valid (hook_list, hook, may_be_in_call);
408 g_hook_next_valid (GHookList *hook_list,
410 gboolean may_be_in_call)
414 g_return_val_if_fail (hook_list != NULL, NULL);
422 if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
424 g_hook_ref (hook_list, hook);
425 g_hook_unref (hook_list, ohook);
431 g_hook_unref (hook_list, ohook);
437 g_hook_get (GHookList *hook_list,
442 g_return_val_if_fail (hook_list != NULL, NULL);
443 g_return_val_if_fail (hook_id > 0, NULL);
445 hook = hook_list->hooks;
448 if (hook->hook_id == hook_id)
457 g_hook_find (GHookList *hook_list,
458 gboolean need_valids,
464 g_return_val_if_fail (hook_list != NULL, NULL);
465 g_return_val_if_fail (func != NULL, NULL);
467 hook = hook_list->hooks;
472 /* test only non-destroyed hooks */
479 g_hook_ref (hook_list, hook);
481 if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
483 g_hook_unref (hook_list, hook);
489 g_hook_unref (hook_list, hook);
497 g_hook_find_data (GHookList *hook_list,
498 gboolean need_valids,
503 g_return_val_if_fail (hook_list != NULL, NULL);
505 hook = hook_list->hooks;
508 /* test only non-destroyed hooks */
509 if (hook->data == data &&
511 (!need_valids || G_HOOK_ACTIVE (hook)))
521 g_hook_find_func (GHookList *hook_list,
522 gboolean need_valids,
527 g_return_val_if_fail (hook_list != NULL, NULL);
528 g_return_val_if_fail (func != NULL, NULL);
530 hook = hook_list->hooks;
533 /* test only non-destroyed hooks */
534 if (hook->func == func &&
536 (!need_valids || G_HOOK_ACTIVE (hook)))
546 g_hook_find_func_data (GHookList *hook_list,
547 gboolean need_valids,
553 g_return_val_if_fail (hook_list != NULL, NULL);
554 g_return_val_if_fail (func != NULL, NULL);
556 hook = hook_list->hooks;
559 /* test only non-destroyed hooks */
560 if (hook->data == data &&
561 hook->func == func &&
563 (!need_valids || G_HOOK_ACTIVE (hook)))
573 g_hook_insert_sorted (GHookList *hook_list,
575 GHookCompareFunc func)
579 g_return_if_fail (hook_list != NULL);
580 g_return_if_fail (hook_list->is_setup);
581 g_return_if_fail (hook != NULL);
582 g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
583 g_return_if_fail (hook->func != NULL);
584 g_return_if_fail (func != NULL);
586 /* first non-destroyed hook */
587 sibling = hook_list->hooks;
588 while (sibling && !sibling->hook_id)
589 sibling = sibling->next;
595 g_hook_ref (hook_list, sibling);
596 if (func (hook, sibling) <= 0 && sibling->hook_id)
598 g_hook_unref (hook_list, sibling);
602 /* next non-destroyed hook */
604 while (tmp && !tmp->hook_id)
607 g_hook_unref (hook_list, sibling);
611 g_hook_insert_before (hook_list, sibling, hook);
615 g_hook_compare_ids (GHook *new_hook,
618 return ((glong) new_hook->hook_id) - ((glong) sibling->hook_id);