X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/bd043f62d4d7bf22adb68d5193ec1bb2aaae3c7b..f07101941f4397554a6cee4fbaf3286137865064:/src/xbt/win32_ucontext.c?ds=sidebyside diff --git a/src/xbt/win32_ucontext.c b/src/xbt/win32_ucontext.c index 1d7d453e1b..757eb43a70 100644 --- a/src/xbt/win32_ucontext.c +++ b/src/xbt/win32_ucontext.c @@ -15,82 +15,87 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with QueueUserAPCEx in the file COPYING.LIB; + * License along with SimGrid in the file LICENSE-LGPL-2.1; * if not, write to the Free Software Foundation, Inc., * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - + */ + #include "win32_ucontext.h" - -int getcontext(ucontext_t *ucp) +int getcontext(ucontext_t * ucp) { - int ret; - - /* Retrieve the full machine context */ - ucp->uc_mcontext.ContextFlags = CONTEXT_FULL; - ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext); - - return (ret == 0) ? -1: 0; + int ret; + + /* Retrieve the full machine context */ + ucp->uc_mcontext.ContextFlags = CONTEXT_FULL; + ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext); + return (ret == 0) ? -1 : 0; } -int setcontext(const ucontext_t *ucp) +int setcontext(const ucontext_t * ucp) { - int ret; - - /* Restore the full machine context (already set) */ - ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext); - - return (ret == 0) ? -1: 0; + int ret; + + /* Restore the full machine context (already set) */ + ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext); + return (ret == 0) ? -1 : 0; } -int makecontext(ucontext_t *ucp, void (*func)(), int argc, ...) +int makecontext(ucontext_t * ucp, void (*func) (), int argc, ...) { - int i; - va_list ap; - char *sp; - - /* Stack grows down */ - sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size; - - /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */ - sp -= argc*8; - - if ( sp < (char *)ucp->uc_stack.ss_sp) { - /* errno = ENOMEM;*/ - return -1; - } - - /* Set the instruction and the stack pointer */ - ucp->uc_mcontext.Eip = (unsigned long) func; - ucp->uc_mcontext.Esp = (unsigned long) sp - 4; - - /* Save/Restore the full machine context */ - ucp->uc_mcontext.ContextFlags = CONTEXT_FULL; - - /* Copy the arguments */ - va_start (ap, argc); - for (i=0; iuc_stack.ss_sp + ucp->uc_stack.ss_size; + + /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */ + sp -= argc * 8; + if (sp < (char *) ucp->uc_stack.ss_sp) { + + /* errno = ENOMEM; */ + return -1; + } + + /* Set the instruction and the stack pointer */ + #ifdef I_X86_ + ucp->uc_mcontext.Eip = (unsigned long) func; + ucp->uc_mcontext.Esp = (unsigned long) sp - 4; + #endif + #ifdef _IA64_ + # error "_IA64_" + #endif + #ifdef _AMD64_ + ucp->uc_mcontext.Rip = (unsigned long long) func; + ucp->uc_mcontext.Rsp = (unsigned long long) sp - 8; + #endif + + /* Save/Restore the full machine context */ + ucp->uc_mcontext.ContextFlags = CONTEXT_FULL; + + /* Copy the arguments */ + va_start(ap, argc); + for (i = 0; i < argc; i++) { + memcpy(sp, ap, 8); + ap += 8; + sp += 8; + } + va_end(ap); + return 0; } -int swapcontext(ucontext_t *oucp, const ucontext_t *ucp) +int swapcontext(ucontext_t * oucp, const ucontext_t * ucp) { - int ret; - - if ((oucp == NULL) || (ucp == NULL)) { - /*errno = EINVAL;*/ - return -1; - } - - ret = getcontext(oucp); - if (ret == 0) { - ret = setcontext(ucp); - } - return ret; + int ret; + if ((oucp == NULL) || (ucp == NULL)) { + + /*errno = EINVAL; */ + return -1; + } + ret = getcontext(oucp); + if (ret == 0) { + ret = setcontext(ucp); + } + return ret; } +