ghook.c 13.5 KB
Newer Older
Tim Janik's avatar
Tim Janik committed
1
2
3
4
5
6
7
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * GHook: Callback maintenance functions
 * Copyright (C) 1998 Tim Janik
 *
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
9
10
11
12
13
14
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
15
 * Lesser General Public License for more details.
Tim Janik's avatar
Tim Janik committed
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
18
19
20
21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
22

23
/*
24
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
25
26
27
28
29
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

30
31
32
33
/* 
 * MT safe
 */

Tim Janik's avatar
Tim Janik committed
34
35
36
37
38
39
40
41
#include	"glib.h"


/* --- defines --- */
#define	G_HOOKS_PREALLOC	(16)


/* --- functions --- */
42
43
44
45
46
47
48
49
50
51
52
53
54
static void
default_finalize_hook (GHookList *hook_list,
		       GHook     *hook)
{
  GDestroyNotify destroy = hook->destroy;

  if (destroy)
    {
      hook->destroy = NULL;
      destroy (hook->data);
    }
}

Tim Janik's avatar
Tim Janik committed
55
56
57
58
59
60
void
g_hook_list_init (GHookList *hook_list,
		  guint	     hook_size)
{
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook_size >= sizeof (GHook));
61
  g_return_if_fail (hook_size < 65536);
Tim Janik's avatar
Tim Janik committed
62
63
64
65
66
67
68
69
70
  
  hook_list->seq_id = 1;
  hook_list->hook_size = hook_size;
  hook_list->is_setup = TRUE;
  hook_list->hooks = NULL;
  hook_list->hook_memchunk = g_mem_chunk_new ("GHook Memchunk",
					      hook_size,
					      hook_size * G_HOOKS_PREALLOC,
					      G_ALLOC_AND_FREE);
71
  hook_list->finalize_hook = default_finalize_hook;
Tim Janik's avatar
Tim Janik committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
}

void
g_hook_list_clear (GHookList *hook_list)
{
  g_return_if_fail (hook_list != NULL);
  
  if (hook_list->is_setup)
    {
      GHook *hook;
      
      hook_list->is_setup = FALSE;
      
      hook = hook_list->hooks;
      if (!hook)
	{
	  g_mem_chunk_destroy (hook_list->hook_memchunk);
	  hook_list->hook_memchunk = NULL;
	}
      else
	do
	  {
94
	    GHook *tmp;
Tim Janik's avatar
Tim Janik committed
95
96
97
98
99
100
101
102
	    
	    g_hook_ref (hook_list, hook);
	    g_hook_destroy_link (hook_list, hook);
	    tmp = hook->next;
	    g_hook_unref (hook_list, hook);
	    hook = tmp;
	  }
	while (hook);
103
104
      if (hook_list->hook_memchunk)
	g_warning (G_STRLOC ": failed to clear hooklist, unconsolidated references on hooks left");
Tim Janik's avatar
Tim Janik committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    }
}

GHook*
g_hook_alloc (GHookList *hook_list)
{
  GHook *hook;
  
  g_return_val_if_fail (hook_list != NULL, NULL);
  g_return_val_if_fail (hook_list->is_setup, NULL);
  
  hook = g_chunk_new0 (GHook, hook_list->hook_memchunk);
  hook->data = NULL;
  hook->next = NULL;
  hook->prev = NULL;
120
  hook->flags = G_HOOK_FLAG_ACTIVE;
Tim Janik's avatar
Tim Janik committed
121
  hook->ref_count = 0;
122
  hook->hook_id = 0;
Tim Janik's avatar
Tim Janik committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  hook->func = NULL;
  hook->destroy = NULL;
  
  return hook;
}

void
g_hook_free (GHookList *hook_list,
	     GHook     *hook)
{
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook_list->is_setup);
  g_return_if_fail (hook != NULL);
  g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
137
  g_return_if_fail (!G_HOOK_IN_CALL (hook));
138

139
  hook_list->finalize_hook (hook_list, hook);
Tim Janik's avatar
Tim Janik committed
140
141
142
143
144
145
146
147
148
  g_chunk_free (hook, hook_list->hook_memchunk);
}

void
g_hook_destroy_link (GHookList *hook_list,
		     GHook     *hook)
{
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook != NULL);
149
150

  hook->flags &= ~G_HOOK_FLAG_ACTIVE;
151
  if (hook->hook_id)
Tim Janik's avatar
Tim Janik committed
152
    {
153
      hook->hook_id = 0;
Tim Janik's avatar
Tim Janik committed
154
155
156
157
158
159
      g_hook_unref (hook_list, hook); /* counterpart to g_hook_insert_before */
    }
}

gboolean
g_hook_destroy (GHookList   *hook_list,
Tim Janik's avatar
Tim Janik committed
160
		gulong	     hook_id)
Tim Janik's avatar
Tim Janik committed
161
162
163
164
{
  GHook *hook;
  
  g_return_val_if_fail (hook_list != NULL, FALSE);
165
  g_return_val_if_fail (hook_id > 0, FALSE);
Tim Janik's avatar
Tim Janik committed
166
  
167
  hook = g_hook_get (hook_list, hook_id);
Tim Janik's avatar
Tim Janik committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  if (hook)
    {
      g_hook_destroy_link (hook_list, hook);
      return TRUE;
    }
  
  return FALSE;
}

void
g_hook_unref (GHookList *hook_list,
	      GHook	*hook)
{
  g_return_if_fail (hook_list != NULL);
182
  g_return_if_fail (hook_list->hook_memchunk != NULL);
Tim Janik's avatar
Tim Janik committed
183
184
185
186
187
188
  g_return_if_fail (hook != NULL);
  g_return_if_fail (hook->ref_count > 0);
  
  hook->ref_count--;
  if (!hook->ref_count)
    {
189
      g_return_if_fail (hook->hook_id == 0);
190
      g_return_if_fail (!G_HOOK_IN_CALL (hook));
191

Tim Janik's avatar
Tim Janik committed
192
193
194
195
196
197
198
199
200
201
      if (hook->prev)
	hook->prev->next = hook->next;
      else
	hook_list->hooks = hook->next;
      if (hook->next)
	{
	  hook->next->prev = hook->prev;
	  hook->next = NULL;
	}
      hook->prev = NULL;
202
203

      if (!hook_list->is_setup)
Tim Janik's avatar
Tim Janik committed
204
	{
205
206
207
208
209
210
211
212
213
	  hook_list->is_setup = TRUE;
	  g_hook_free (hook_list, hook);
	  hook_list->is_setup = FALSE;
      
	  if (!hook_list->hooks)
	    {
	      g_mem_chunk_destroy (hook_list->hook_memchunk);
	      hook_list->hook_memchunk = NULL;
	    }
Tim Janik's avatar
Tim Janik committed
214
	}
215
216
      else
	g_hook_free (hook_list, hook);
Tim Janik's avatar
Tim Janik committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    }
}

void
g_hook_ref (GHookList *hook_list,
	    GHook     *hook)
{
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook != NULL);
  g_return_if_fail (hook->ref_count > 0);
  
  hook->ref_count++;
}

void
g_hook_prepend (GHookList *hook_list,
		GHook	  *hook)
{
  g_return_if_fail (hook_list != NULL);
  
  g_hook_insert_before (hook_list, hook_list->hooks, hook);
}

void
g_hook_insert_before (GHookList *hook_list,
		      GHook	*sibling,
		      GHook	*hook)
{
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook_list->is_setup);
  g_return_if_fail (hook != NULL);
  g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
249
  g_return_if_fail (hook->ref_count == 0);
Tim Janik's avatar
Tim Janik committed
250
  
251
  hook->hook_id = hook_list->seq_id++;
Tim Janik's avatar
Tim Janik committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  hook->ref_count = 1; /* counterpart to g_hook_destroy_link */
  
  if (sibling)
    {
      if (sibling->prev)
	{
	  hook->prev = sibling->prev;
	  hook->prev->next = hook;
	  hook->next = sibling;
	  sibling->prev = hook;
	}
      else
	{
	  hook_list->hooks = hook;
	  hook->next = sibling;
	  sibling->prev = hook;
	}
    }
  else
    {
      if (hook_list->hooks)
	{
	  sibling = hook_list->hooks;
	  while (sibling->next)
	    sibling = sibling->next;
	  hook->prev = sibling;
	  sibling->next = hook;
	}
      else
	hook_list->hooks = hook;
    }
}

void
g_hook_list_invoke (GHookList *hook_list,
		    gboolean   may_recurse)
{
  GHook *hook;
  
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook_list->is_setup);
293

294
  hook = g_hook_first_valid (hook_list, may_recurse);
Tim Janik's avatar
Tim Janik committed
295
296
  while (hook)
    {
297
      GHookFunc func;
Tim Janik's avatar
Tim Janik committed
298
299
      gboolean was_in_call;
      
300
      func = (GHookFunc) hook->func;
Tim Janik's avatar
Tim Janik committed
301
      
302
303
      was_in_call = G_HOOK_IN_CALL (hook);
      hook->flags |= G_HOOK_FLAG_IN_CALL;
Tim Janik's avatar
Tim Janik committed
304
305
      func (hook->data);
      if (!was_in_call)
306
	hook->flags &= ~G_HOOK_FLAG_IN_CALL;
Tim Janik's avatar
Tim Janik committed
307
      
308
      hook = g_hook_next_valid (hook_list, hook, may_recurse);
Tim Janik's avatar
Tim Janik committed
309
310
311
312
313
314
315
316
317
318
319
320
    }
}

void
g_hook_list_invoke_check (GHookList *hook_list,
			  gboolean   may_recurse)
{
  GHook *hook;
  
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook_list->is_setup);
  
321
  hook = g_hook_first_valid (hook_list, may_recurse);
Tim Janik's avatar
Tim Janik committed
322
323
  while (hook)
    {
324
      GHookCheckFunc func;
Tim Janik's avatar
Tim Janik committed
325
      gboolean was_in_call;
326
      gboolean need_destroy;
Tim Janik's avatar
Tim Janik committed
327
      
328
      func = (GHookCheckFunc) hook->func;
Tim Janik's avatar
Tim Janik committed
329
      
330
331
      was_in_call = G_HOOK_IN_CALL (hook);
      hook->flags |= G_HOOK_FLAG_IN_CALL;
Tim Janik's avatar
Tim Janik committed
332
333
      need_destroy = !func (hook->data);
      if (!was_in_call)
334
	hook->flags &= ~G_HOOK_FLAG_IN_CALL;
Tim Janik's avatar
Tim Janik committed
335
336
337
      if (need_destroy)
	g_hook_destroy_link (hook_list, hook);
      
338
      hook = g_hook_next_valid (hook_list, hook, may_recurse);
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    }
}

void
g_hook_list_marshal_check (GHookList	       *hook_list,
			   gboolean		may_recurse,
			   GHookCheckMarshaller marshaller,
			   gpointer		data)
{
  GHook *hook;
  
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook_list->is_setup);
  g_return_if_fail (marshaller != NULL);
  
  hook = g_hook_first_valid (hook_list, may_recurse);
  while (hook)
    {
      gboolean was_in_call;
      gboolean need_destroy;
      
      was_in_call = G_HOOK_IN_CALL (hook);
      hook->flags |= G_HOOK_FLAG_IN_CALL;
      need_destroy = !marshaller (hook, data);
      if (!was_in_call)
	hook->flags &= ~G_HOOK_FLAG_IN_CALL;
      if (need_destroy)
	g_hook_destroy_link (hook_list, hook);
      
368
      hook = g_hook_next_valid (hook_list, hook, may_recurse);
Tim Janik's avatar
Tim Janik committed
369
370
371
372
    }
}

void
373
374
375
376
g_hook_list_marshal (GHookList		     *hook_list,
		     gboolean		      may_recurse,
		     GHookMarshaller	      marshaller,
		     gpointer		      data)
Tim Janik's avatar
Tim Janik committed
377
378
379
380
381
382
383
{
  GHook *hook;
  
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook_list->is_setup);
  g_return_if_fail (marshaller != NULL);
  
384
  hook = g_hook_first_valid (hook_list, may_recurse);
Tim Janik's avatar
Tim Janik committed
385
386
387
388
  while (hook)
    {
      gboolean was_in_call;
      
389
390
      was_in_call = G_HOOK_IN_CALL (hook);
      hook->flags |= G_HOOK_FLAG_IN_CALL;
Tim Janik's avatar
Tim Janik committed
391
392
      marshaller (hook, data);
      if (!was_in_call)
393
	hook->flags &= ~G_HOOK_FLAG_IN_CALL;
Tim Janik's avatar
Tim Janik committed
394
      
395
      hook = g_hook_next_valid (hook_list, hook, may_recurse);
Tim Janik's avatar
Tim Janik committed
396
397
398
399
    }
}

GHook*
400
401
g_hook_first_valid (GHookList *hook_list,
		    gboolean   may_be_in_call)
Tim Janik's avatar
Tim Janik committed
402
403
404
405
406
407
408
409
410
411
{
  g_return_val_if_fail (hook_list != NULL, NULL);
  
  if (hook_list->is_setup)
    {
      GHook *hook;
      
      hook = hook_list->hooks;
      if (hook)
	{
412
	  g_hook_ref (hook_list, hook);
413
	  if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
Tim Janik's avatar
Tim Janik committed
414
415
	    return hook;
	  else
416
	    return g_hook_next_valid (hook_list, hook, may_be_in_call);
Tim Janik's avatar
Tim Janik committed
417
418
419
420
421
422
423
	}
    }
  
  return NULL;
}

GHook*
424
425
426
g_hook_next_valid (GHookList *hook_list,
		   GHook     *hook,
		   gboolean   may_be_in_call)
Tim Janik's avatar
Tim Janik committed
427
{
428
429
430
431
  GHook *ohook = hook;

  g_return_val_if_fail (hook_list != NULL, NULL);

Tim Janik's avatar
Tim Janik committed
432
433
434
435
436
437
  if (!hook)
    return NULL;
  
  hook = hook->next;
  while (hook)
    {
438
      if (G_HOOK_IS_VALID (hook) && (may_be_in_call || !G_HOOK_IN_CALL (hook)))
439
440
441
442
443
444
	{
	  g_hook_ref (hook_list, hook);
	  g_hook_unref (hook_list, ohook);
	  
	  return hook;
	}
Tim Janik's avatar
Tim Janik committed
445
446
      hook = hook->next;
    }
447
448
  g_hook_unref (hook_list, ohook);

Tim Janik's avatar
Tim Janik committed
449
450
451
452
453
  return NULL;
}

GHook*
g_hook_get (GHookList *hook_list,
Tim Janik's avatar
Tim Janik committed
454
	    gulong     hook_id)
Tim Janik's avatar
Tim Janik committed
455
456
457
458
{
  GHook *hook;
  
  g_return_val_if_fail (hook_list != NULL, NULL);
459
  g_return_val_if_fail (hook_id > 0, NULL);
Tim Janik's avatar
Tim Janik committed
460
461
462
463
  
  hook = hook_list->hooks;
  while (hook)
    {
464
      if (hook->hook_id == hook_id)
Tim Janik's avatar
Tim Janik committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
	return hook;
      hook = hook->next;
    }
  
  return NULL;
}

GHook*
g_hook_find (GHookList	  *hook_list,
	     gboolean	   need_valids,
	     GHookFindFunc func,
	     gpointer	   data)
{
  GHook *hook;
  
  g_return_val_if_fail (hook_list != NULL, NULL);
  g_return_val_if_fail (func != NULL, NULL);
  
483
  hook = hook_list->hooks;
Tim Janik's avatar
Tim Janik committed
484
485
  while (hook)
    {
486
487
488
489
490
491
492
493
      GHook *tmp;

      /* test only non-destroyed hooks */
      if (!hook->hook_id)
	{
	  hook = hook->next;
	  continue;
	}
Tim Janik's avatar
Tim Janik committed
494
495
496
      
      g_hook_ref (hook_list, hook);
      
497
      if (func (hook, data) && hook->hook_id && (!need_valids || G_HOOK_ACTIVE (hook)))
Tim Janik's avatar
Tim Janik committed
498
499
500
501
502
	{
	  g_hook_unref (hook_list, hook);
	  
	  return hook;
	}
503
504

      tmp = hook->next;
Tim Janik's avatar
Tim Janik committed
505
506
507
508
509
510
511
512
513
514
515
516
      g_hook_unref (hook_list, hook);
      hook = tmp;
    }
  
  return NULL;
}

GHook*
g_hook_find_data (GHookList *hook_list,
		  gboolean   need_valids,
		  gpointer   data)
{
517
  GHook *hook;
Tim Janik's avatar
Tim Janik committed
518
519
520
  
  g_return_val_if_fail (hook_list != NULL, NULL);
  
521
  hook = hook_list->hooks;
Tim Janik's avatar
Tim Janik committed
522
523
  while (hook)
    {
524
525
526
527
      /* test only non-destroyed hooks */
      if (hook->data == data &&
	  hook->hook_id &&
	  (!need_valids || G_HOOK_ACTIVE (hook)))
Tim Janik's avatar
Tim Janik committed
528
	return hook;
529
530

      hook = hook->next;
Tim Janik's avatar
Tim Janik committed
531
532
533
534
535
536
537
538
539
540
    }
  
  return NULL;
}

GHook*
g_hook_find_func (GHookList *hook_list,
		  gboolean   need_valids,
		  gpointer   func)
{
541
  GHook *hook;
Tim Janik's avatar
Tim Janik committed
542
543
544
545
  
  g_return_val_if_fail (hook_list != NULL, NULL);
  g_return_val_if_fail (func != NULL, NULL);
  
546
  hook = hook_list->hooks;
Tim Janik's avatar
Tim Janik committed
547
548
  while (hook)
    {
549
550
551
552
      /* test only non-destroyed hooks */
      if (hook->func == func &&
	  hook->hook_id &&
	  (!need_valids || G_HOOK_ACTIVE (hook)))
Tim Janik's avatar
Tim Janik committed
553
	return hook;
554
555

      hook = hook->next;
Tim Janik's avatar
Tim Janik committed
556
557
558
559
560
561
562
563
564
565
566
    }
  
  return NULL;
}

GHook*
g_hook_find_func_data (GHookList *hook_list,
		       gboolean	  need_valids,
		       gpointer	  func,
		       gpointer	  data)
{
567
  GHook *hook;
Tim Janik's avatar
Tim Janik committed
568
569
570
571
  
  g_return_val_if_fail (hook_list != NULL, NULL);
  g_return_val_if_fail (func != NULL, NULL);
  
572
  hook = hook_list->hooks;
Tim Janik's avatar
Tim Janik committed
573
574
  while (hook)
    {
575
576
577
578
579
      /* test only non-destroyed hooks */
      if (hook->data == data &&
	  hook->func == func &&
	  hook->hook_id &&
	  (!need_valids || G_HOOK_ACTIVE (hook)))
Tim Janik's avatar
Tim Janik committed
580
	return hook;
581
582

      hook = hook->next;
Tim Janik's avatar
Tim Janik committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
    }
  
  return NULL;
}

void
g_hook_insert_sorted (GHookList	      *hook_list,
		      GHook	      *hook,
		      GHookCompareFunc func)
{
  GHook *sibling;
  
  g_return_if_fail (hook_list != NULL);
  g_return_if_fail (hook_list->is_setup);
  g_return_if_fail (hook != NULL);
  g_return_if_fail (G_HOOK_IS_UNLINKED (hook));
  g_return_if_fail (hook->func != NULL);
  g_return_if_fail (func != NULL);
601
602
603
604
605

  /* first non-destroyed hook */
  sibling = hook_list->hooks;
  while (sibling && !sibling->hook_id)
    sibling = sibling->next;
Tim Janik's avatar
Tim Janik committed
606
607
608
  
  while (sibling)
    {
609
      GHook *tmp;
Tim Janik's avatar
Tim Janik committed
610
611
      
      g_hook_ref (hook_list, sibling);
612
      if (func (hook, sibling) <= 0 && sibling->hook_id)
Tim Janik's avatar
Tim Janik committed
613
614
615
616
	{
	  g_hook_unref (hook_list, sibling);
	  break;
	}
617
618
619
620
621
622

      /* next non-destroyed hook */
      tmp = sibling->next;
      while (tmp && !tmp->hook_id)
	tmp = tmp->next;

Tim Janik's avatar
Tim Janik committed
623
624
625
626
627
628
629
630
631
632
633
      g_hook_unref (hook_list, sibling);
      sibling = tmp;
    }
  
  g_hook_insert_before (hook_list, sibling, hook);
}

gint
g_hook_compare_ids (GHook *new_hook,
		    GHook *sibling)
{
634
  return ((glong) new_hook->hook_id) - ((glong) sibling->hook_id);
Tim Janik's avatar
Tim Janik committed
635
}