-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshift0ops.c
80 lines (67 loc) · 1.84 KB
/
shift0ops.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
/* Definition of freecont and derived control operators.
* shift0ops.c
*
* Copyright (c) 2011, Marek Materzok <[email protected]>
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The software is provided 'as is', without any warranty.
*/
#include <stdlib.h>
#include "shift0.h"
void *runcont_reentry(struct cont *fun, void *val);
void *runcont_dirty(struct cont *fun, void *val);
void freecont(struct cont* c)
{
void *(*f)(struct cont*, void*) =
*((void *(**)(struct cont*, void*))c);
if (f == runcont_reentry) abort();
if (f == runcont_dirty) {
void *p = ((void**)c)[7];
free(p);
}
free(c);
}
#undef shift0
#undef reset
void *shift0(shiftable_t f)
{
return shift0_arg((shiftable_arg_t) f, 0);
}
void *reset(resettable_t f)
{
return reset_arg((resettable_arg_t) f, 0);
}
struct shift_str {
shiftable_arg_t f;
void *arg;
struct cont *c;
};
void *_shift_arg_internal2(struct shift_str *s)
{
return s->f(s->c, s->arg);
}
void *_shift_arg_internal(struct cont *c, struct shift_str *s)
{
s->c = c;
return reset_arg((resettable_arg_t) _shift_arg_internal2, (void*) s);
}
void *shift_arg(shiftable_arg_t f, void *arg)
{
struct shift_str s;
s.f = f;
s.arg = arg;
return shift0_arg((shiftable_arg_t) _shift_arg_internal, (void*) &s);
}
void *_shift_internal(struct cont *c, shiftable_t f)
{
return reset_arg((resettable_arg_t)f, (void*)c);
}
void *shift(void *(*f)(struct cont *c))
{
return shift0_arg((shiftable_arg_t) _shift_internal, (void*) f);
}