55 68
81
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
#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 *stacktest(
void *param
){
TestResult *testresult = (TestResult *)param;
if (Coroutine_CanStartCoroutine(DEMO_STACK_SIZE)){
Coroutine *cor = Coroutine_New(DEMO_STACK_SIZE, stacktest);
Coroutine_Delete(cor);
}
testresult->headroom = Coroutine_GetStackHeadroom();
testresult->canstartcoroutine = Coroutine_CanStartCoroutine(DEMO_STACK_SIZE);
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
unsigned char *stack_now = (unsigned char *)&argc;
printf("Various stack headrooms:\n");
intptr_t limitnocoroutine;
TestResult testresult;
// what stack do we get with no stack limit set
Coroutine_StartSystem();
cor_main = Coroutine_New(DEMO_STACK_SIZE, stacktest);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run_Coroutine(cor_main, &testresult);
Coroutine_Delete(cor_main);
printf("No stack limit: %ld %ld %d\n", limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
Coroutine_StopSystem();
// what stack do we get with an undersize stack limit
for (size_t stacksize = DEMO_STACK_SIZE/2; stacksize < DEMO_STACK_SIZE*4; stacksize += DEMO_STACK_SIZE/2){
Coroutine_StartSystem();
Coroutine_SetStackLimit(stack_now - stacksize);
cor_main = Coroutine_New(DEMO_STACK_SIZE, stacktest);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run_Coroutine(cor_main, &testresult);
Coroutine_Delete(cor_main);
printf("Stack=%ld: %ld, %ld %d\n", stacksize, limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
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
#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 *stacktest(
void *param
){
TestResult *testresult = (TestResult *)param;
if (Coroutine_CanStartCoroutine(DEMO_STACK_SIZE)){
Coroutine *cor = Coroutine_New(DEMO_STACK_SIZE, stacktest);
Coroutine_Delete(cor);
}
testresult->headroom = Coroutine_GetStackHeadroom();
testresult->canstartcoroutine = Coroutine_CanStartCoroutine(DEMO_STACK_SIZE);
return NULL;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
unsigned char *stack_now = (unsigned char *)&argc;
printf("Various stack headrooms:\n");
intptr_t limitnocoroutine;
TestResult testresult;
// what stack do we get with no stack limit set
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run(DEMO_STACK_SIZE, stacktest, &testresult, NULL);
printf("No stack limit: %ld %ld %d\n", limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
// what stack do we get with an undersize stack limit
for (size_t stacksize = DEMO_STACK_SIZE/2; stacksize < DEMO_STACK_SIZE*4; stacksize += DEMO_STACK_SIZE/2){
Coroutine_SetStackLimit(stack_now - stacksize);
limitnocoroutine = Coroutine_GetStackHeadroom();
Coroutine_Run(DEMO_STACK_SIZE, stacktest, &testresult, NULL);
printf("Stack=%ld: %ld, %ld %d\n", stacksize, limitnocoroutine, testresult.headroom, testresult.canstartcoroutine);
}
}