1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "pike_macros.h" 
#include "callback.h" 
#include "pike_error.h" 
#include "block_allocator.h" 
#include "pike_embed.h" 
 
struct callback_list fork_child_callback; 
 
/* 
 * This file is used to simplify the management of callbacks when certain 
 * events occur. The callbacks are managed as linked lists, allocated in 
 * chunks. 
 */ 
 
/* FIXME: free all chunks of memory at exit */ 
 
struct callback 
{ 
  struct callback *next; 
  callback_func call; 
  callback_func free_func; 
  void *arg; 
}; 
 
#define CALLBACK_CHUNK 128 
 
static struct block_allocator callback_allocator 
    = BA_INIT(sizeof(struct callback), CALLBACK_CHUNK); 
 
void count_memory_in_callbacks(size_t * num, size_t * size) { 
    ba_count_all(&callback_allocator, num, size); 
} 
 
 
#ifdef PIKE_DEBUG 
static void check_callback_chain(struct callback_list *lst) 
{ 
  int len=0; 
  struct callback *foo; 
  if(d_flag>4) 
  { 
    for(foo=lst->callbacks;foo;foo=foo->next) 
    { 
      if((len & 1024)==1023) 
      { 
        int len2=0; 
        struct callback *tmp; 
        for(tmp=foo->next;tmp && len2<=len;tmp=tmp->next) 
        { 
#ifdef PIKE_DEBUG 
        if(tmp==foo) 
            Pike_fatal("Callback list is cyclic!!!\n"); 
#endif 
      } 
      } 
      len++; 
    } 
  } 
} 
#else 
#define check_callback_chain(X) 
#endif 
 
/** 
 * Traverse a linked list of callbacks and call all the active callbacks 
 * in the list. Deactivated callbacks are freed and placed in the free list. 
 */ 
PMOD_EXPORT void low_call_callback(struct callback_list *lst, void *arg) 
{ 
  int this_call; 
  struct callback *l,**ptr; 
 
  lst->num_calls++; 
  this_call=lst->num_calls; 
 
  check_callback_chain(lst); 
  ptr=&lst->callbacks; 
  while((l=*ptr)) 
  { 
    if(l->call) 
    { 
      l->call(l,l->arg, arg); 
      if(lst->num_calls != this_call) return; 
    } 
 
    if(!l->call) 
    { 
      if(l->free_func) 
        l->free_func(l, l->arg, 0); 
 
      while(*ptr != l) 
      { 
        ptr=&(ptr[0]->next); 
#ifdef PIKE_DEBUG 
      if(!*ptr) 
        { 
          /* We totally failed to find where we are in the linked list.. */ 
          Pike_fatal("Callback linked list breakdown.\n"); 
        } 
#endif 
      } 
 
      *ptr=l->next; 
      ba_free(&callback_allocator, l); 
    }else{ 
      ptr=& l->next; 
    } 
    check_callback_chain(lst); 
  } 
} 
 
/** 
 * Add a callback to the linked list pointed to by ptr. 
 */ 
PMOD_EXPORT struct callback *debug_add_to_callback(struct callback_list *lst, 
                                                   callback_func call, 
                                                   void *arg, 
                                                   callback_func free_func) 
{ 
  struct callback *l; 
  l=ba_alloc(&callback_allocator); 
  l->call=call; 
  l->arg=arg; 
  l->free_func=free_func; 
 
  l->next=lst->callbacks; 
  lst->callbacks=l; 
 
  check_callback_chain(lst); 
 
  return l; 
} 
 
/** 
 * Deactivates a callback. 
 * It is not actually freed until next time the callback is "called". 
 */ 
PMOD_EXPORT void *remove_callback(struct callback *l) 
{ 
  l->call=0; 
  l->free_func=0; 
  return l->arg; 
} 
 
/** 
 * Free all the callbacks in a linked list of callbacks. 
 */ 
void free_callback_list(struct callback_list *lst) 
{ 
  struct callback *l,**ptr; 
  check_callback_chain(lst); 
  ptr=& lst->callbacks; 
  while((l=*ptr)) 
  { 
    if(l->free_func) 
      l->free_func(l, l->arg, 0); 
    *ptr=l->next; 
    ba_free(&callback_allocator, l); 
  } 
} 
 
/** 
 * Free all callbacks. 
 */ 
void cleanup_callbacks(void) 
{ 
  ba_destroy(&callback_allocator); 
}