8 void factor_vm::init_ffi()
10 hFactorDll = GetModuleHandle(FACTOR_DLL);
12 fatal_error("GetModuleHandle() failed", 0);
15 void factor_vm::ffi_dlopen(dll *dll)
17 dll->handle = LoadLibraryEx((WCHAR *)alien_offset(dll->path), NULL, 0);
20 void *factor_vm::ffi_dlsym(dll *dll, symbol_char *symbol)
22 return (void *)GetProcAddress(dll ? (HMODULE)dll->handle : hFactorDll, symbol);
25 void factor_vm::ffi_dlclose(dll *dll)
27 FreeLibrary((HMODULE)dll->handle);
31 BOOL factor_vm::windows_stat(vm_char *path)
33 BY_HANDLE_FILE_INFORMATION bhfi;
34 HANDLE h = CreateFileW(path,
39 FILE_FLAG_BACKUP_SEMANTICS,
42 if(h == INVALID_HANDLE_VALUE)
44 // FindFirstFile is the only call that can stat c:\pagefile.sys
48 if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
53 BOOL ret = GetFileInformationByHandle(h, &bhfi);
58 void factor_vm::windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length)
60 SNWPRINTF(temp_path, length-1, L"%s.image", full_path);
61 temp_path[length - 1] = 0;
64 /* You must free() this yourself. */
65 const vm_char *factor_vm::default_image_path()
67 vm_char full_path[MAX_UNICODE_PATH];
69 vm_char temp_path[MAX_UNICODE_PATH];
71 if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
72 fatal_error("GetModuleFileName() failed", 0);
74 if((ptr = wcsrchr(full_path, '.')))
77 SNWPRINTF(temp_path, MAX_UNICODE_PATH-1, L"%s.image", full_path);
78 temp_path[MAX_UNICODE_PATH - 1] = 0;
80 return safe_strdup(temp_path);
83 /* You must free() this yourself. */
84 const vm_char *factor_vm::vm_executable_path()
86 vm_char full_path[MAX_UNICODE_PATH];
87 if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
88 fatal_error("GetModuleFileName() failed", 0);
89 return safe_strdup(full_path);
92 void factor_vm::primitive_existsp()
94 vm_char *path = untag_check<byte_array>(ctx->pop())->data<vm_char>();
95 ctx->push(tag_boolean(windows_stat(path)));
98 segment::segment(cell size_, bool executable_p)
105 if((mem = (char *)VirtualAlloc(NULL, getpagesize() * 2 + size,
106 MEM_COMMIT, executable_p ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE)) == 0)
109 if (!VirtualProtect(mem, getpagesize(), PAGE_NOACCESS, &ignore))
110 fatal_error("Cannot allocate low guard page", (cell)mem);
112 if (!VirtualProtect(mem + size + getpagesize(),
113 getpagesize(), PAGE_NOACCESS, &ignore))
114 fatal_error("Cannot allocate high guard page", (cell)mem);
116 start = (cell)mem + getpagesize();
124 if(!VirtualFree((void*)(start - si.dwPageSize), 0, MEM_RELEASE))
125 fatal_error("Segment deallocation failed",0);
130 static long g_pagesize = 0;
133 SYSTEM_INFO system_info;
134 GetSystemInfo (&system_info);
135 g_pagesize = system_info.dwPageSize;
141 Windows argument parsing ported to work on
142 int main(int argc, wchar_t **argv).
144 Based on MinGW's public domain char** version.
146 Used by WinMain() implementation in main-windows-ce.cpp
147 and main-windows-nt.cpp.
151 VM_C_API int parse_tokens(wchar_t *string, wchar_t ***tokens, int length)
153 /* Extract whitespace- and quotes- delimited tokens from the given string
154 and put them into the tokens array. Returns number of tokens
155 extracted. Length specifies the current size of tokens[].
156 THIS METHOD MODIFIES string. */
158 const wchar_t *whitespace = L" \t\r\n";
159 wchar_t *tokenEnd = 0;
160 const wchar_t *quoteCharacters = L"\"\'";
161 wchar_t *end = string + wcslen(string);
169 /* Skip over initial whitespace. */
170 string += wcsspn(string, whitespace);
174 for (q = quoteCharacters; *q; ++q)
181 /* Token is quoted. */
182 wchar_t quote = *string++;
183 tokenEnd = wcschr(string, quote);
184 /* If there is no endquote, the token is the rest of the string. */
190 tokenEnd = string + wcscspn(string, whitespace);
196 wchar_t **new_tokens;
197 int newlen = length + 1;
198 new_tokens = (wchar_t **)realloc (*tokens, sizeof (wchar_t**) * newlen);
205 *tokens = new_tokens;
206 (*tokens)[length] = string;
211 string = tokenEnd + 1;
216 VM_C_API void parse_args(int *argc, wchar_t ***argv, wchar_t *cmdlinePtrW)
223 cmdlineLen = wcslen(cmdlinePtrW);
225 /* gets realloc()'d later */
227 *argv = (wchar_t **)malloc (sizeof (wchar_t**));
233 wchar_t cmdnameBufW[MAX_UNICODE_PATH];
235 /* argv[0] is the path of invoked program - get this from CE. */
237 GetModuleFileNameW(NULL, cmdnameBufW, sizeof (cmdnameBufW)/sizeof (cmdnameBufW[0]));
239 (*argv)[0] = wcsdup(cmdnameBufW);
242 /* Add one to account for argv[0] */
248 wchar_t *argv1 = wcsdup(cmdlinePtrW);
251 *argc = parse_tokens(argv1, argv, *argc);