diff --git a/amd64.c b/amd64.c index ab62bd92..664730a1 100644 --- a/amd64.c +++ b/amd64.c @@ -129,10 +129,11 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) if(!co_active_handle) co_active_handle = &co_active_buffer; if(handle = (cothread_t)memory) { - long long *p = (long long*)((char*)handle + size); /* seek to top of stack */ - *--p = (long long)crash; /* crash if entrypoint returns */ - *--p = (long long)entrypoint; /* start of function */ - *(long long*)handle = (long long)p; /* stack pointer */ + unsigned int offset = (size & ~15) - 32; + long long *p = (long long*)((char*)handle + offset); /* seek to top of stack */ + *--p = (long long)crash; /* crash if entrypoint returns */ + *--p = (long long)entrypoint; /* start of function */ + *(long long*)handle = (long long)p; /* stack pointer */ } return handle; @@ -145,14 +146,13 @@ cothread_t co_create(unsigned int size, void (*entrypoint)(void)) { co_swap = (void (*)(cothread_t, cothread_t))co_swap_function; } if(!co_active_handle) co_active_handle = &co_active_buffer; - size += 512; /* allocate additional space for storage */ - size &= ~15; /* align stack to 16-byte boundary */ if(handle = (cothread_t)malloc(size)) { - long long *p = (long long*)((char*)handle + size); /* seek to top of stack */ - *--p = (long long)crash; /* crash if entrypoint returns */ - *--p = (long long)entrypoint; /* start of function */ - *(long long*)handle = (long long)p; /* stack pointer */ + unsigned int offset = (size & ~15) - 32; + long long *p = (long long*)((char*)handle + offset); /* seek to top of stack */ + *--p = (long long)crash; /* crash if entrypoint returns */ + *--p = (long long)entrypoint; /* start of function */ + *(long long*)handle = (long long)p; /* stack pointer */ } return handle; diff --git a/libco.h b/libco.h index b7f60852..a436957c 100644 --- a/libco.h +++ b/libco.h @@ -1,5 +1,5 @@ /* - libco v19 (2019-02-18) + libco v20 (2019-10-14) author: byuu license: ISC */ diff --git a/x86.c b/x86.c index d79306c6..3eb2e21e 100644 --- a/x86.c +++ b/x86.c @@ -83,10 +83,11 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) if(!co_active_handle) co_active_handle = &co_active_buffer; if(handle = (cothread_t)memory) { - long *p = (long*)((char*)handle + size); /* seek to top of stack */ - *--p = (long)crash; /* crash if entrypoint returns */ - *--p = (long)entrypoint; /* start of function */ - *(long*)handle = (long)p; /* stack pointer */ + unsigned int offset = (size & ~15) - 32; + long *p = (long*)((char*)handle + offset); /* seek to top of stack */ + *--p = (long)crash; /* crash if entrypoint returns */ + *--p = (long)entrypoint; /* start of function */ + *(long*)handle = (long)p; /* stack pointer */ } return handle; @@ -99,14 +100,13 @@ cothread_t co_create(unsigned int size, void (*entrypoint)(void)) { co_swap = (void (fastcall*)(cothread_t, cothread_t))co_swap_function; } if(!co_active_handle) co_active_handle = &co_active_buffer; - size += 256; /* allocate additional space for storage */ - size &= ~15; /* align stack to 16-byte boundary */ if(handle = (cothread_t)malloc(size)) { - long *p = (long*)((char*)handle + size); /* seek to top of stack */ - *--p = (long)crash; /* crash if entrypoint returns */ - *--p = (long)entrypoint; /* start of function */ - *(long*)handle = (long)p; /* stack pointer */ + unsigned int offset = (size & ~15) - 32; + long *p = (long*)((char*)handle + offset); /* seek to top of stack */ + *--p = (long)crash; /* crash if entrypoint returns */ + *--p = (long)entrypoint; /* start of function */ + *(long*)handle = (long)p; /* stack pointer */ } return handle;