Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics (change 'self' identifier to 'me' for g++ compatibility)
[simgrid.git] / src / xbt / win32_ucontext.c
1 /*\r
2  *      win32-ucontext: Unix ucontext_t operations on Windows platforms\r
3  *      Copyright(C) 2007 Panagiotis E. Hadjidoukas\r
4  *\r
5  *      Contact Email: phadjido@cs.uoi.gr, xdoukas@ceid.upatras.gr\r
6  *\r
7  *      win32-ucontext is free software; you can redistribute it and/or\r
8  *      modify it under the terms of the GNU Lesser General Public\r
9  *      License as published by the Free Software Foundation; either\r
10  *      version 2 of the License, or (at your option) any later version.\r
11  *\r
12  *      win32-ucontext is distributed in the hope that it will be useful,\r
13  *      but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
15  *      Lesser General Public License for more details.\r
16  *\r
17  *      You should have received a copy of the GNU Lesser General Public\r
18  *      License along with QueueUserAPCEx in the file COPYING.LIB;\r
19  *      if not, write to the Free Software Foundation, Inc.,\r
20  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA\r
21  */  \r
22     \r
23 #include "win32_ucontext.h"\r
24 int getcontext(ucontext_t * ucp) \r
25 {
26   \rint ret;
27   \r\r
28       /* Retrieve the full machine context */ \r
29       ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
30   \rret = GetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
31   \rreturn (ret == 0) ? -1 : 0;
32 \r}
33
34 \r\rint setcontext(const ucontext_t * ucp) \r
35 {
36   \rint ret;
37   \r\r
38       /* Restore the full machine context (already set) */ \r
39       ret = SetThreadContext(GetCurrentThread(), &ucp->uc_mcontext);
40   \rreturn (ret == 0) ? -1 : 0;
41 \r}
42
43 \r\rint makecontext(ucontext_t * ucp, void (*func) (), int argc, ...) \r
44 {
45   \rint i;
46   \rva_list ap;
47   \rchar *sp;
48   \r\r
49       /* Stack grows down */ \r
50       sp = (char *) (size_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size;
51   \r\r
52       /* Reserve stack space for the arguments (maximum possible: argc*(8 bytes per argument)) */ \r
53       sp -= argc * 8;
54   \rif (sp < (char *) ucp->uc_stack.ss_sp) {
55     \r\r
56         /* errno = ENOMEM; */ \r
57         return -1;
58   \r}
59   \r\r
60       /* Set the instruction and the stack pointer */ \r
61       ucp->uc_mcontext.Eip = (unsigned long) func;
62   \rucp->uc_mcontext.Esp = (unsigned long) sp - 4;
63   \r\r
64       /* Save/Restore the full machine context */ \r
65       ucp->uc_mcontext.ContextFlags = CONTEXT_FULL;
66   \r\r
67       /* Copy the arguments */ \r
68       va_start(ap, argc);
69   \rfor (i = 0; i < argc; i++) {
70     \rmemcpy(sp, ap, 8);
71     \rap += 8;
72     \rsp += 8;
73   \r}
74   \rva_end(ap);
75   \rreturn 0;
76 \r}
77
78 \r\rint swapcontext(ucontext_t * oucp, const ucontext_t * ucp) \r
79 {
80   \rint ret;
81   \rif ((oucp == NULL) || (ucp == NULL)) {
82     \r\r
83         /*errno = EINVAL; */ \r
84         return -1;
85   \r}
86   \rret = getcontext(oucp);
87   \rif (ret == 0) {
88     \rret = setcontext(ucp);
89   \r}
90   \rreturn ret;
91 \r}
92
93 \r\r\r