| 1 | #include <stdio.h> |
| 2 | #include <stddef.h> |
| 3 | #include "coroutine.h" |
| 4 | #include "generator.h" |
| 5 | #include "asleep.h" |
| 6 | #include "task.h" |
| 7 | |
| 8 | #include <dirent.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <time.h> |
| 12 | |
| 13 | |
| 14 | Coroutine *cor_main; |
| 15 | |
| 16 | typedef struct { |
| 17 | intptr_t headroom; |
| 18 | bool canstartcoroutine; |
| 19 | } TestResult; |
| 20 | |
| 21 | |
| 22 | void *stacktest( |
| 23 | void *param |
| 24 | ){ |
| 25 | TestResult *testresult = (TestResult *)param; |
| 26 | if (Coroutine_CanStartCoroutine()){ |
| 27 | Coroutine *cor = Coroutine_New(stacktest); |
| 28 | Coroutine_Delete(cor); |
| 29 | } |
| 30 | testresult->headroom = Coroutine_GetStackHeadroom(); |
| 31 | testresult->canstartcoroutine = Coroutine_CanStartCoroutine(); |
| 32 | |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | int main(int argc, char *argv[]) { |
| 38 | (void)argc; |
| 39 | (void)argv; |
| 40 | |
| 41 | unsigned char *stack_now = (unsigned char *)&argc; |
| 42 | |
| 43 | printf("Various stack headrooms:\n"); |
| 44 | intptr_t limitnocoroutine; |
| 45 | TestResult testresult; |
| 46 | |
| 47 | // what stack do we get with no stack limit set |
| 48 | Coroutine_StartSystem(); |
| 49 | cor_main = Coroutine_New(stacktest); |
| 50 | limitnocoroutine = Coroutine_GetStackHeadroom(); |
| 51 | Coroutine_Run_Coroutine(cor_main, &testresult); |
| 52 | Coroutine_Delete(cor_main); |
| 53 | printf("No stack limit: %ld %ld %d\n", limitnocoroutine, testresult.headroom, testresult.canstartcoroutine); |
| 54 | Coroutine_StopSystem(); |
| 55 | |
| 56 | // what stack do we get with an undersize stack limit |
| 57 | for (intptr_t stacksize = COROUTINE_STACK_SIZE/2; stacksize < COROUTINE_STACK_SIZE*4; stacksize += COROUTINE_STACK_SIZE/2){ |
| 58 | Coroutine_StartSystem(); |
| 59 | Coroutine_SetStackLimit(stack_now - stacksize); |
| 60 | cor_main = Coroutine_New(stacktest); |
| 61 | limitnocoroutine = Coroutine_GetStackHeadroom(); |
| 62 | Coroutine_Run_Coroutine(cor_main, &testresult); |
| 63 | Coroutine_Delete(cor_main); |
| 64 | printf("Stack=%ld: %ld, %ld %d\n", stacksize, limitnocoroutine, testresult.headroom, testresult.canstartcoroutine); |
| 65 | Coroutine_StopSystem(); |
| 66 | } |
| 67 | } |
| 68 | |