69 73
81
86
Remove need for malloc by Coroutine, and add Coroutine_Err
on 12:28 AM Feb 20 2026
80
81
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
#include <stdio.h>
#include <stddef.h>
#include "coroutine.h"
#include "generator.h"
#include "asleep.h"
#include "task.h"
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define DEMO_STACK_SIZE (8192*sizeof(void *))
Coroutine *cor_main;
typedef struct {
intptr_t headroom;
bool canstartcoroutine;
} TestResult;
void *dotests(
void *param
){
(void)param;
_Coroutine_Dump();
Coroutine *active = Coroutine_GetActive();
Coroutine *a = Coroutine_New(DEMO_STACK_SIZE, dotests);
Coroutine *b = Coroutine_New(DEMO_STACK_SIZE, dotests);
Coroutine *c = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbcc]\n");
_Coroutine_Dump();
printf("%ld %ld %ld (a, b, c: a < b < c)\n", (unsigned char *)active - (unsigned char *)a, (unsigned char *)active - (unsigned char *)b, (unsigned char *)active - (unsigned char *)c);
Coroutine_Delete(b);
printf("[stack=aaffcc]\n");
_Coroutine_Dump();
b = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbcc]\n");
_Coroutine_Dump();
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
Coroutine_Delete(b);
printf("[stack=aaffcc]\n");
_Coroutine_Dump();
Coroutine *d = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aaffccdddd]\n");
printf("%ld (d: c < d)\n", (unsigned char *)active - (unsigned char *)d);
_Coroutine_Dump();
printf("[testing merge before]\n");
Coroutine_Delete(c);
printf("[stack=aaffffdddd]\n");
_Coroutine_Dump();
b = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aabbbbdddd]\n");
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
_Coroutine_Dump();
printf("[testing merge after]\n");
Coroutine_Delete(b);
printf("[stack=aaffffdddd]\n");
_Coroutine_Dump();
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
_Coroutine_Dump();
a = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aaaaffdddd]\n");
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
_Coroutine_Dump();
printf("[testing merge both ways]\n");
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
_Coroutine_Dump();
a = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aaffffdddd]\n");
_Coroutine_Dump();
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
b = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbffdddd]\n");
_Coroutine_Dump();
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
c = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbccdddd]\n");
printf("%ld (c again)\n", (unsigned char *)active - (unsigned char *)c);
Coroutine_Delete(a);
printf("[stack=ffbbccdddd]\n");
Coroutine_Delete(c);
printf("[stack=ffbbffdddd]\n");
Coroutine_Delete(b);
printf("[stack=ffffffdddd]\n");
_Coroutine_Dump();
a = Coroutine_New(DEMO_STACK_SIZE*3, dotests);
printf("[stack=aaaaaadddd]\n");
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
_Coroutine_Dump();
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
_Coroutine_Dump();
Coroutine_Delete(d);
printf("[stack=fffffffffff]\n");
_Coroutine_Dump();
a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=affffffffff]\n");
_Coroutine_Dump();
b = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=abfffffffff]\n");
_Coroutine_Dump();
c = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=abcffffffff]\n");
_Coroutine_Dump();
Coroutine_Delete(a);
printf("[stack=fbcffffffff]\n");
_Coroutine_Dump();
Coroutine_Delete(b);
printf("[stack=ffcffffffff]\n");
_Coroutine_Dump();
a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=afcffffffff]\n");
_Coroutine_Dump();
Coroutine_Delete(c);
printf("[stack=affffffffff]\n");
_Coroutine_Dump();
Coroutine_Delete(a);
printf("[stack=fffffffffff]\n");
_Coroutine_Dump();
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
printf("No StartSystem\n");
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
printf("\n\n\n\nStartSystem\n");
Coroutine_StartSystem();
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
Coroutine_StopSystem();
printf("\n\n\n\nSartSystem with stack limit\n");
Coroutine_StartSystem();
Coroutine_SetStackLimit((unsigned char *)&argc - 1024*1024);
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
Coroutine_StopSystem();
}
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
#include <stdio.h>
#include <stddef.h>
#include "coroutine.h"
#include "generator.h"
#include "asleep.h"
#include "task.h"
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define DEMO_STACK_SIZE (8192*sizeof(void *))
Coroutine *cor_main;
typedef struct {
intptr_t headroom;
bool canstartcoroutine;
} TestResult;
void *dotests(
void *param
){
(void)param;
_Coroutine_Dump();
Coroutine *active = Coroutine_GetActive();
Coroutine *a = Coroutine_New(DEMO_STACK_SIZE, dotests);
Coroutine *b = Coroutine_New(DEMO_STACK_SIZE, dotests);
Coroutine *c = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbcc]\n");
_Coroutine_Dump();
printf("%ld %ld %ld (a, b, c: a < b < c)\n", (unsigned char *)active - (unsigned char *)a, (unsigned char *)active - (unsigned char *)b, (unsigned char *)active - (unsigned char *)c);
Coroutine_Delete(b);
printf("[stack=aaffcc]\n");
_Coroutine_Dump();
b = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbcc]\n");
_Coroutine_Dump();
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
Coroutine_Delete(b);
printf("[stack=aaffcc]\n");
_Coroutine_Dump();
Coroutine *d = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aaffccdddd]\n");
printf("%ld (d: c < d)\n", (unsigned char *)active - (unsigned char *)d);
_Coroutine_Dump();
printf("[testing merge before]\n");
Coroutine_Delete(c);
printf("[stack=aaffffdddd]\n");
_Coroutine_Dump();
b = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aabbbbdddd]\n");
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
_Coroutine_Dump();
printf("[testing merge after]\n");
Coroutine_Delete(b);
printf("[stack=aaffffdddd]\n");
_Coroutine_Dump();
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
_Coroutine_Dump();
a = Coroutine_New(DEMO_STACK_SIZE*2, dotests);
printf("[stack=aaaaffdddd]\n");
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
_Coroutine_Dump();
printf("[testing merge both ways]\n");
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
_Coroutine_Dump();
a = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aaffffdddd]\n");
_Coroutine_Dump();
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
b = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbffdddd]\n");
_Coroutine_Dump();
printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b);
c = Coroutine_New(DEMO_STACK_SIZE, dotests);
printf("[stack=aabbccdddd]\n");
printf("%ld (c again)\n", (unsigned char *)active - (unsigned char *)c);
Coroutine_Delete(a);
printf("[stack=ffbbccdddd]\n");
Coroutine_Delete(c);
printf("[stack=ffbbffdddd]\n");
Coroutine_Delete(b);
printf("[stack=ffffffdddd]\n");
_Coroutine_Dump();
a = Coroutine_New(DEMO_STACK_SIZE*3, dotests);
printf("[stack=aaaaaadddd]\n");
printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a);
_Coroutine_Dump();
Coroutine_Delete(a);
printf("[stack=ffffffdddd]\n");
_Coroutine_Dump();
Coroutine_Delete(d);
printf("[stack=fffffffffff]\n");
_Coroutine_Dump();
a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=affffffffff]\n");
_Coroutine_Dump();
b = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=abfffffffff]\n");
_Coroutine_Dump();
c = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=abcffffffff]\n");
_Coroutine_Dump();
Coroutine_Delete(a);
printf("[stack=fbcffffffff]\n");
_Coroutine_Dump();
Coroutine_Delete(b);
printf("[stack=ffcffffffff]\n");
_Coroutine_Dump();
a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests);
printf("[stack=afcffffffff]\n");
_Coroutine_Dump();
Coroutine_Delete(c);
printf("[stack=affffffffff]\n");
_Coroutine_Dump();
Coroutine_Delete(a);
printf("[stack=fffffffffff]\n");
_Coroutine_Dump();
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
printf("No StartSystem\n");
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
printf("\n\n\n\nStartSystem\n");
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
printf("\n\n\n\nSartSystem with stack limit\n");
Coroutine_SetStackLimit((unsigned char *)&argc - 1024*1024);
Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL);
}