-
Notifications
You must be signed in to change notification settings - Fork 1
/
call_out.c
569 lines (503 loc) · 13.9 KB
/
call_out.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
249
250
251
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include "std.h"
#include "call_out.h"
#include "backend.h"
#include "comm.h"
#include "port.h"
#include "eoperators.h"
#include "sprintf.h"
#include "eval.h"
#define DBG(x) debug(call_out, x)
/*
* This file implements delayed calls of functions.
* Static functions can not be called this way.
*
* Allocate the structures several in one chunk, to get rid of malloc
* overhead.
*/
#define CHUNK_SIZE 20
typedef struct pending_call_s {
int delta;
union string_or_func function;
object_t *ob;
array_t *vs;
struct pending_call_s *next;
#ifdef THIS_PLAYER_IN_CALL_OUT
object_t *command_giver;
#endif
#ifdef CALLOUT_HANDLES
int handle;
#endif
} pending_call_t;
static pending_call_t *call_list[CALLOUT_CYCLE_SIZE];
static pending_call_t *call_list_free;
static int num_call;
#ifdef CALLOUT_HANDLES
static int unique = 0;
#endif
static void free_call (pending_call_t *);
static void free_called_call (pending_call_t *);
void remove_all_call_out (object_t *);
/*
* Free a call out structure.
*/
static void free_called_call (pending_call_t * cop)
{
cop->next = call_list_free;
if (cop->ob) {
free_string(cop->function.s);
free_object(&cop->ob, "free_call");
} else {
free_funp(cop->function.f);
}
cop->function.s = 0;
#ifdef THIS_PLAYER_IN_CALL_OUT
if (cop->command_giver){
free_object(&cop->command_giver, "free_call");
cop->command_giver = 0;
}
#endif
cop->ob = 0;
call_list_free = cop;
}
INLINE_STATIC void free_call (pending_call_t * cop)
{
if (cop->vs)
free_array(cop->vs);
free_called_call(cop);
}
/*
* Setup a new call out.
*/
#ifdef CALLOUT_HANDLES
int
#else
void
#endif
new_call_out (object_t * ob, svalue_t * fun, int delay,
int num_args, svalue_t * arg)
{
pending_call_t *cop, **copp;
int tm;
if (delay < 0)
delay = 0;
DBG(("new_call_out: /%s delay %i", ob->obname, delay));
if (!call_list_free) {
int i;
call_list_free = CALLOCATE(CHUNK_SIZE, pending_call_t,
TAG_CALL_OUT, "new_call_out: call_list_free");
for (i = 0; i < CHUNK_SIZE - 1; i++)
call_list_free[i].next = &call_list_free[i + 1];
call_list_free[CHUNK_SIZE - 1].next = 0;
num_call += CHUNK_SIZE;
}
cop = call_list_free;
call_list_free = call_list_free->next;
if (fun->type == T_STRING) {
DBG((" function: %s", fun->u.string));
cop->function.s = make_shared_string(fun->u.string);
cop->ob = ob;
add_ref(ob, "call_out");
} else {
DBG((" function: <function>"));
cop->function.f = fun->u.fp;
fun->u.fp->hdr.ref++;
cop->ob = 0;
}
#ifdef THIS_PLAYER_IN_CALL_OUT
cop->command_giver = command_giver; /* save current user context */
if (command_giver)
add_ref(command_giver, "new_call_out"); /* Bump its ref */
#endif
if (num_args > 0) {
cop->vs = allocate_empty_array(num_args);
memcpy(cop->vs->item, arg, sizeof(svalue_t) * num_args);
} else
cop->vs = 0;
/* Find out which slot this one fits in */
tm = (delay + current_time) & (CALLOUT_CYCLE_SIZE - 1);
/* number of cycles */
delay = delay / CALLOUT_CYCLE_SIZE;
DBG(("Current time: %i Executes at: %i Slot: %i Delay: %i",
current_time, current_time + delay, tm, delay));
for (copp = &call_list[tm]; *copp; copp = &(*copp)->next) {
if ((*copp)->delta > delay) {
(*copp)->delta -= delay;
cop->delta = delay;
cop->next = *copp;
*copp = cop;
#ifdef CALLOUT_HANDLES
tm += CALLOUT_CYCLE_SIZE * ++unique;
cop->handle = tm;
return tm;
#else
return;
#endif
}
delay -= (*copp)->delta;
}
*copp = cop;
cop->delta = delay;
cop->next = 0;
#ifdef CALLOUT_HANDLES
tm += CALLOUT_CYCLE_SIZE * ++unique;
cop->handle = tm;
return tm;
#endif
}
/*
* See if there are any call outs to be called. Set the 'command_giver'
* if it is a living object. Check for shadowing objects, which may also
* be living objects.
*/
void call_out()
{
int extra, real_time;
static pending_call_t *cop = 0;
error_context_t econ;
VOLATILE int tm;
current_interactive = 0;
/* could be still allocated if an error occured during a call_out */
if (cop) {
free_called_call(cop);
cop = 0;
}
real_time = get_current_time();
DBG(("Calling call_outs: current_time: %i real_time: %i difference: %i",
current_time, real_time, real_time - current_time));
/* Slowly advance the clock forward towards real_time, doing call_outs
* as we go.
*/
save_context(&econ);
while (1) {
tm = current_time & (CALLOUT_CYCLE_SIZE - 1);
DBG((" slot %i", tm));
while (call_list[tm] && call_list[tm]->delta == 0) {
object_t *ob, *new_command_giver;
/*
* Move the first call_out out of the chain.
*/
cop = call_list[tm];
call_list[tm] = call_list[tm]->next;
ob = (cop->ob ? cop->ob : cop->function.f->hdr.owner);
DBG((" /%s", (ob ? ob->obname : "(null)")));
if (!ob || (ob->flags & O_DESTRUCTED)) {
DBG((" (destructed)"));
free_call(cop);
cop = 0;
} else {
if (SETJMP(econ.context)) {
restore_context(&econ);
if (max_eval_error) {
debug_message("Maximum evaluation cost reached while trying to process call_outs\n");
pop_context(&econ);
return;
}
} else {
object_t *ob;
ob = cop->ob;
#ifndef NO_SHADOWS
if (ob)
while (ob->shadowing)
ob = ob->shadowing;
#endif
new_command_giver = 0;
#ifdef THIS_PLAYER_IN_CALL_OUT
if (cop->command_giver &&
!(cop->command_giver->flags & O_DESTRUCTED)) {
new_command_giver = cop->command_giver;
} else if (ob && (ob->flags & O_LISTENER)) {
new_command_giver = ob;
}
if (new_command_giver)
DBG((" command_giver: /%s", new_command_giver->obname));
#endif
save_command_giver(new_command_giver);
/* current object no longer set */
if (cop->vs) {
array_t *vec = cop->vs;
svalue_t *svp = vec->item + vec->size;
while (svp-- > vec->item) {
if (svp->type == T_OBJECT &&
(svp->u.ob->flags & O_DESTRUCTED)) {
free_object(&svp->u.ob, "call_out");
*svp = const0u;
}
}
/* cop->vs is ref one */
extra = cop->vs->size;
transfer_push_some_svalues(cop->vs->item, extra);
free_empty_array(cop->vs);
} else
extra = 0;
//reset_eval_cost();
set_eval(max_cost);
if (cop->ob) {
if (cop->function.s[0] == APPLY___INIT_SPECIAL_CHAR)
error("Illegal function name\n");
(void) apply(cop->function.s, cop->ob, extra,
ORIGIN_INTERNAL);
} else {
(void) call_function_pointer(cop->function.f, extra);
}
restore_command_giver();
}
free_called_call(cop);
cop = 0;
}
}
/* Ok, no more scheduled call_outs for current_time */
if (current_time < real_time) {
/* Time marches onward! */
if (call_list[tm])
call_list[tm]->delta--;
current_time++;
DBG((" current_time = %i", current_time));
if(!(current_time%HEARTBEAT_INTERVAL))
call_heart_beat();
} else {
/* We're done! */
break;
}
}
DBG(("Done."));
pop_context(&econ);
}
static int time_left (int slot, int delay) {
int current_slot = current_time & (CALLOUT_CYCLE_SIZE - 1);
if (slot >= current_slot) {
return (slot - current_slot) + delay * CALLOUT_CYCLE_SIZE;
} else {
return (slot - current_slot) + (delay + 1) * CALLOUT_CYCLE_SIZE;
}
}
/*
* Throw away a call out. First call to this function is discarded.
* The time left until execution is returned.
* -1 is returned if no call out pending.
*/
int remove_call_out (object_t * ob, const char * fun)
{
pending_call_t **copp, *cop;
int delay;
int i;
if (!ob) return -1;
DBG(("remove_call_out: /%s \"%s\"", ob->obname, fun));
for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
delay = 0;
for (copp = &call_list[i]; *copp; copp = &(*copp)->next) {
DBG((" Slot: %i\n", i));
delay += (*copp)->delta;
if ((*copp)->ob == ob && strcmp((*copp)->function.s, fun) == 0) {
cop = *copp;
if (cop->next)
cop->next->delta += cop->delta;
*copp = cop->next;
free_call(cop);
DBG((" found."));
return time_left(i, delay);
}
}
}
DBG((" not found."));
return -1;
}
#ifdef CALLOUT_HANDLES
int remove_call_out_by_handle (int handle)
{
pending_call_t **copp, *cop;
int delay = 0;
DBG(("remove_call_out_by_handle: handle: %i slot: %i",
handle, handle & (CALLOUT_CYCLE_SIZE - 1)));
for (copp = &call_list[handle & (CALLOUT_CYCLE_SIZE - 1)]; *copp; copp = &(*copp)->next) {
delay += (*copp)->delta;
if ((*copp)->handle == handle) {
cop = *copp;
if (cop->next)
cop->next->delta += cop->delta;
*copp = cop->next;
free_call(cop);
return time_left(handle & (CALLOUT_CYCLE_SIZE - 1), delay);
}
}
return -1;
}
int find_call_out_by_handle (int handle)
{
pending_call_t *cop;
int delay = 0;
DBG(("find_call_out_by_handle: handle: %i slot: %i",
handle, handle & (CALLOUT_CYCLE_SIZE - 1)));
for (cop = call_list[handle & (CALLOUT_CYCLE_SIZE - 1)]; cop; cop = cop->next) {
delay += cop->delta;
if (cop->handle == handle)
return time_left(handle & (CALLOUT_CYCLE_SIZE - 1), delay);
}
return -1;
}
#endif
int find_call_out (object_t * ob, const char * fun)
{
pending_call_t *cop;
int delay;
int i;
if (!ob) return -1;
DBG(("find_call_out: /%s \"%s\"", ob->obname, fun));
for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
delay = 0;
DBG((" Slot: %i", i));
for (cop = call_list[i]; cop; cop = cop->next) {
delay += cop->delta;
if (cop->ob == ob && strcmp(cop->function.s, fun) == 0)
return time_left(i, delay);
}
}
return -1;
}
int print_call_out_usage (outbuffer_t * ob, int verbose)
{
int i, j;
pending_call_t *cop;
for (i = 0, j = 0; j < CALLOUT_CYCLE_SIZE; j++)
for (cop = call_list[j]; cop; cop = cop->next)
i++;
if (verbose == 1) {
outbuf_add(ob, "Call out information:\n");
outbuf_add(ob, "---------------------\n");
outbuf_addv(ob, "Number of allocated call outs: %8d, %8d bytes\n",
num_call, num_call * sizeof(pending_call_t));
outbuf_addv(ob, "Current length: %d\n", i);
} else {
if (verbose != -1)
outbuf_addv(ob, "call out:\t\t\t%8d %8d (current length %d)\n", num_call,
num_call * sizeof(pending_call_t), i);
}
return num_call * sizeof(pending_call_t);
}
#ifdef DEBUGMALLOC_EXTENSIONS
#ifdef DEBUG
void mark_call_outs()
{
pending_call_t *cop;
int i;
for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
for (cop = call_list[i]; cop; cop = cop->next) {
if (cop->vs)
cop->vs->extra_ref++;
if (cop->ob) {
cop->ob->extra_ref++;
EXTRA_REF(BLOCK(cop->function.s))++;
} else {
cop->function.f->hdr.extra_ref++;
}
#ifdef THIS_PLAYER_IN_CALL_OUT
if (cop->command_giver)
cop->command_giver->extra_ref++;
#endif
}
}
}
#endif
#endif
/*
* Construct an array of all pending call_outs. Every item in the array
* consists of 3 items (but only if the object not is destructed):
* 0: The object.
* 1: The function (string).
* 2: The delay.
*/
array_t *get_all_call_outs()
{
int i, j, delay, tm;
pending_call_t *cop;
array_t *v;
for (i = 0, j = 0; j < CALLOUT_CYCLE_SIZE; j++)
for (cop = call_list[j]; cop; cop = cop->next) {
object_t *ob = (cop->ob ? cop->ob : cop->function.f->hdr.owner);
if (ob && !(ob->flags & O_DESTRUCTED))
i++;
}
v = allocate_empty_array(i);
tm = current_time & (CALLOUT_CYCLE_SIZE-1);
for (i = 0, j = 0; j < CALLOUT_CYCLE_SIZE; j++) {
delay = 0;
for (cop = call_list[j]; cop; cop = cop->next) {
array_t *vv;
object_t *ob;
delay += cop->delta;
ob = (cop->ob ? cop->ob : cop->function.f->hdr.owner);
if (!ob || (ob->flags & O_DESTRUCTED))
continue;
vv = allocate_empty_array(3);
if (cop->ob) {
vv->item[0].type = T_OBJECT;
vv->item[0].u.ob = cop->ob;
add_ref(cop->ob, "get_all_call_outs");
vv->item[1].type = T_STRING;
vv->item[1].subtype = STRING_SHARED;
vv->item[1].u.string = make_shared_string(cop->function.s);
} else {
outbuffer_t tmpbuf;
svalue_t tmpval;
tmpbuf.real_size = 0;
tmpbuf.buffer = 0;
tmpval.type = T_FUNCTION;
tmpval.u.fp = cop->function.f;
svalue_to_string(&tmpval, &tmpbuf, 0, 0, 0);
vv->item[0].type = T_OBJECT;
vv->item[0].u.ob = cop->function.f->hdr.owner;
add_ref(cop->function.f->hdr.owner, "get_all_call_outs");
vv->item[1].type = T_STRING;
vv->item[1].subtype = STRING_SHARED;
vv->item[1].u.string = make_shared_string(tmpbuf.buffer);
FREE_MSTR(tmpbuf.buffer);
}
vv->item[2].type = T_NUMBER;
vv->item[2].u.number = time_left(j, delay);
v->item[i].type = T_ARRAY;
v->item[i++].u.arr = vv; /* Ref count is already 1 */
}
}
return v;
}
void
remove_all_call_out (object_t * obj)
{
pending_call_t **copp, *cop;
int i;
for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
copp = &call_list[i];
while (*copp) {
if ( ((*copp)->ob &&
(((*copp)->ob == obj) || ((*copp)->ob->flags & O_DESTRUCTED))) ||
(!(*copp)->ob &&
((*copp)->function.f->hdr.owner == obj ||
!(*copp)->function.f->hdr.owner ||
(*copp)->function.f->hdr.owner->flags & O_DESTRUCTED)) )
{
cop = *copp;
if (cop->next)
cop->next->delta += cop->delta;
*copp = cop->next;
free_call(cop);
} else
copp = &(*copp)->next;
}
}
}
void reclaim_call_outs() {
pending_call_t *cop;
int i;
remove_all_call_out(0); /* removes call_outs to destructed objects */
#ifdef THIS_PLAYER_IN_CALL_OUT
for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
cop = call_list[i];
while (cop) {
if (cop->command_giver && (cop->command_giver->flags & O_DESTRUCTED)) {
free_object(&cop->command_giver, "reclaim_call_outs");
cop->command_giver = 0;
}
cop = cop->next;
}
}
#endif
}