diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/algo/coroutine.h | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 4ba39e63..7ea53a0c 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -61,14 +61,15 @@ int main(void) { #include <stc/ccommon.h> #define cco_begin(c) \ - int* _state = &(c)->cco_state; \ - switch (*_state) { \ + int *_state = &(c)->cco_state, _nodyn; \ + switch (*_state) { \ case 0: #define cco_end() \ *_state = 0; break; \ default: assert(!"missing cco_final: or illegal state"); \ - } *_state *= -1 + } \ + *_state *= -1; (void)_nodyn #define cco_yield(retval) \ do { \ @@ -84,7 +85,22 @@ int main(void) { } while (0) #define cco_final case -1 -#define cco_alive(c) ((c)->cco_state > 0) -#define cco_stop(c) ((c)->cco_state = cco_alive(c) ? -1 : -2, c) +#define cco_alive(c) ((c) && (c)->cco_state > 0) +#define cco_stop(c) ((c)->cco_state = (c)->cco_state ? -1 : -2, c) + +// --------------------------------------------------------------- + +#define cco_begin_dyn(cp) \ + if (!*(cp)) *(cp) = malloc(sizeof **(cp)), (*(cp))->cco_state = 0; \ + int* _state = &(*(cp))->cco_state, _dyn; \ + switch (*_state) { \ + case 0: + +#define cco_end_dyn(cp) \ + *_state = 0; break; \ + default: assert(!"missing cco_final: or illegal state"); \ + } \ + *_state *= -1; (void)_dyn; \ + free(*(cp)); *(cp) = NULL #endif |
