-/*\r
- * win32-ucontext: Unix ucontext_t operations on Windows platforms\r
- * Copyright(C) 2007 Panagiotis E. Hadjidoukas\r
- *\r
- * Contact Email: phadjido@cs.uoi.gr, xdoukas@ceid.upatras.gr\r
- *\r
- * win32-ucontext is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Lesser General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2 of the License, or (at your option) any later version.\r
- *\r
- * win32-ucontext is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
- * Lesser General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Lesser General Public\r
- * License along with QueueUserAPCEx in the file COPYING.LIB;\r
- * if not, write to the Free Software Foundation, Inc.,\r
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA\r
- */ \r
- \r
-#include "win32_ucontext.h"\r
-int getcontext(ucontext_t * ucp) \r
+/* Copyright (c) 2010-2012, 2014-2015. The SimGrid Team.
+ * All rights reserved. */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+/*
+ * win32-ucontext: Unix ucontext_t operations on Windows platforms
+ * Copyright(C) 2007 Panagiotis E. Hadjidoukas
+ *
+ * Contact Email: phadjido@cs.uoi.gr, xdoukas@ceid.upatras.gr
+ *
+ * win32-ucontext is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * win32-ucontext is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * 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 "xbt/win32_ucontext.h"
+
+int getcontext(ucontext_t * ucp)
{
- \rint ret;
- \r\r
- /* Retrieve the full machine context */ \r
- ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
- \rret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
- \rreturn (ret == 0) ? -1 : 0;
-\r}
+ int ret;
+
+ /* Retrieve the full machine context */
+ ucp->uc_mcontext.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
+ ret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
+ return (ret == 0) ? -1 : 0;
+}
-\r\rint setcontext(const ucontext_t * ucp) \r
+int setcontext(const ucontext_t * ucp)
{
- \rint ret;
- \r\r
- /* Restore the full machine context (already set) */ \r
+ int ret;
+
+ /* Restore the full machine context (already set) */
ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
- \rreturn (ret == 0) ? -1 : 0;
-\r}
+ return (ret == 0) ? -1 : 0;
+}
-\r\rint makecontext(ucontext_t * ucp, void (*func) (), int argc, ...) \r
+int makecontext(ucontext_t * ucp, void (*func) (), int argc, ...)
{
- \rint i;
- \rva_list ap;
- \rchar *sp;
- \r\r
- /* Stack grows down */ \r
+ int i;
+ va_list ap;
+ char *sp;
+
+ /* Stack grows down */
sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size;
- \r\r
- /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */ \r
- sp -= argc * 8;
- \rif (sp < (char *) ucp->uc_stack.ss_sp) {
- \r\r
- /* errno = ENOMEM; */ \r
+
+ /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */
+ sp -= argc * sizeof(void*);
+ if (sp < (char *) ucp->uc_stack.ss_sp) {
+
+ /* errno = ENOMEM; */
return -1;
- \r}
- \r\r
- /* Set the instruction and the stack pointer */ \r
- ucp->uc_mcontext.Eip = (unsigned long) func;
- \rucp->uc_mcontext.Esp = (unsigned long) sp - 4;
- \r\r
- /* Save/Restore the full machine context */ \r
- ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
- \r\r
- /* Copy the arguments */ \r
+ }
+
+ /* Set the instruction and the stack pointer */
+ #if defined(_I_X86_) || defined(__i386) || defined(__i386__) || defined(_M_IX86)
+ ucp->uc_mcontext.Eip = (DWORD) func;
+ ucp->uc_mcontext.Esp = (DWORD) sp - sizeof(void*);
+ #elif defined(_IA64_) || defined(__ia64) || defined(__ia64__)
+ # error "_IA64_"
+ #elif defined _AMD64_ || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)
+ ucp->uc_mcontext.Rip = (DWORD64) func;
+ ucp->uc_mcontext.Rsp = (DWORD64) sp - sizeof(void*);
+ #else
+ #error "No architecture defined for Windows build."
+ #endif
+
+ /* Save/Restore the full machine context */
+ ucp->uc_mcontext.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
+
+ /* Copy the arguments */
va_start(ap, argc);
- \rfor (i = 0; i < argc; i++) {
- \rmemcpy(sp, ap, 8);
- \rap += 8;
- \rsp += 8;
- \r}
- \rva_end(ap);
- \rreturn 0;
-\r}
+ for (i = 0; i < argc; i++) {
+ memcpy(sp, ap, sizeof(void*));
+ ap += sizeof(void*);
+ sp += sizeof(void*);
+ }
+ va_end(ap);
+ return 0;
+}
-\r\rint swapcontext(ucontext_t * oucp, const ucontext_t * ucp) \r
+int swapcontext(ucontext_t * oucp, const ucontext_t * ucp)
{
- \rint ret;
- \rif ((oucp == NULL) || (ucp == NULL)) {
- \r\r
- /*errno = EINVAL; */ \r
+ int ret;
+ if ((oucp == NULL) || (ucp == NULL)) {
+
+ /*errno = EINVAL; */
return -1;
- \r}
- \rret = getcontext(oucp);
- \rif (ret == 0) {
- \rret = setcontext(ucp);
- \r}
- \rreturn ret;
-\r}
-
-\r\r\r
+ }
+ ret = getcontext(oucp);
+ if (ret == 0) {
+ ret = setcontext(ucp);
+ }
+ return ret;
+}