]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/os-windows.cpp
Use ParseCommandLineArgvw() on Windows again, instead of hand-rolled parser. Update...
[factor.git] / vm / os-windows.cpp
index 796a1c718474a0586915c6bd01db53d840f903e9..df2a57f2e80de2aa3fa51fd00675d57f4086b93c 100755 (executable)
@@ -5,30 +5,30 @@ namespace factor
 
 HMODULE hFactorDll;
 
-void init_ffi(void)
+void factor_vm::init_ffi()
 {
        hFactorDll = GetModuleHandle(FACTOR_DLL);
        if(!hFactorDll)
-               fatal_error("GetModuleHandle(\"" FACTOR_DLL_NAME "\") failed", 0);
+               fatal_error("GetModuleHandle() failed", 0);
 }
 
-void ffi_dlopen(dll *dll)
+void factor_vm::ffi_dlopen(dll *dll)
 {
-       dll->dll = LoadLibraryEx(alien_offset(dll->path), NULL, 0);
+       dll->handle = LoadLibraryEx((WCHAR *)alien_offset(dll->path), NULL, 0);
 }
 
-void *ffi_dlsym(dll *dll, symbol_char *symbol)
+void *factor_vm::ffi_dlsym(dll *dll, symbol_char *symbol)
 {
-       return GetProcAddress(dll ? (HMODULE)dll->dll : hFactorDll, symbol);
+       return (void *)GetProcAddress(dll ? (HMODULE)dll->handle : hFactorDll, symbol);
 }
 
-void ffi_dlclose(dll *dll)
+void factor_vm::ffi_dlclose(dll *dll)
 {
-       FreeLibrary((HMODULE)dll->dll);
-       dll->dll = NULL;
+       FreeLibrary((HMODULE)dll->handle);
+       dll->handle = NULL;
 }
 
-bool windows_stat(vm_char *path)
+BOOL factor_vm::windows_stat(vm_char *path)
 {
        BY_HANDLE_FILE_INFORMATION bhfi;
        HANDLE h = CreateFileW(path,
@@ -50,20 +50,19 @@ bool windows_stat(vm_char *path)
                FindClose(h);
                return true;
        }
-       bool ret;
-       ret = GetFileInformationByHandle(h, &bhfi);
+       BOOL ret = GetFileInformationByHandle(h, &bhfi);
        CloseHandle(h);
        return ret;
 }
 
-void windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length)
+void factor_vm::windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length)
 {
-       snwprintf(temp_path, length-1, L"%s.image", full_path); 
-       temp_path[sizeof(temp_path) - 1] = 0;
+       SNWPRINTF(temp_path, length-1, L"%s.image", full_path); 
+       temp_path[length - 1] = 0;
 }
 
 /* You must free() this yourself. */
-const vm_char *default_image_path(void)
+const vm_char *factor_vm::default_image_path()
 {
        vm_char full_path[MAX_UNICODE_PATH];
        vm_char *ptr;
@@ -75,14 +74,14 @@ const vm_char *default_image_path(void)
        if((ptr = wcsrchr(full_path, '.')))
                *ptr = 0;
 
-       snwprintf(temp_path, sizeof(temp_path)-1, L"%s.image", full_path); 
-       temp_path[sizeof(temp_path) - 1] = 0;
+       SNWPRINTF(temp_path, MAX_UNICODE_PATH-1, L"%s.image", full_path); 
+       temp_path[MAX_UNICODE_PATH - 1] = 0;
 
        return safe_strdup(temp_path);
 }
 
 /* You must free() this yourself. */
-const vm_char *vm_executable_path(void)
+const vm_char *factor_vm::vm_executable_path()
 {
        vm_char full_path[MAX_UNICODE_PATH];
        if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
@@ -90,20 +89,21 @@ const vm_char *vm_executable_path(void)
        return safe_strdup(full_path);
 }
 
-
-PRIMITIVE(existsp)
+void factor_vm::primitive_existsp()
 {
-       vm_char *path = (vm_char *)(untag_check<byte_array>(dpop()) + 1);
-       box_boolean(windows_stat(path));
+       vm_char *path = untag_check<byte_array>(ctx->pop())->data<vm_char>();
+       ctx->push(tag_boolean(windows_stat(path)));
 }
 
-segment *alloc_segment(cell size)
+segment::segment(cell size_, bool executable_p)
 {
+       size = size_;
+
        char *mem;
        DWORD ignore;
 
        if((mem = (char *)VirtualAlloc(NULL, getpagesize() * 2 + size,
-               MEM_COMMIT, PAGE_EXECUTE_READWRITE)) == 0)
+               MEM_COMMIT, executable_p ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE)) == 0)
                out_of_memory();
 
        if (!VirtualProtect(mem, getpagesize(), PAGE_NOACCESS, &ignore))
@@ -113,25 +113,19 @@ segment *alloc_segment(cell size)
                getpagesize(), PAGE_NOACCESS, &ignore))
                fatal_error("Cannot allocate high guard page", (cell)mem);
 
-       segment *block = safe_malloc(sizeof(segment));
-
-       block->start = (cell)mem + getpagesize();
-       block->size = size;
-       block->end = block->start + size;
-
-       return block;
+       start = (cell)mem + getpagesize();
+       end = start + size;
 }
 
-void dealloc_segment(segment *block)
+segment::~segment()
 {
        SYSTEM_INFO si;
        GetSystemInfo(&si);
-       if(!VirtualFree((void*)(block->start - si.dwPageSize), 0, MEM_RELEASE))
-               fatal_error("dealloc_segment failed",0);
-       free(block);
+       if(!VirtualFree((void*)(start - si.dwPageSize), 0, MEM_RELEASE))
+               fatal_error("Segment deallocation failed",0);
 }
 
-long getpagesize(void)
+long getpagesize()
 {
        static long g_pagesize = 0;
        if (! g_pagesize)
@@ -143,9 +137,4 @@ long getpagesize(void)
        return g_pagesize;
 }
 
-void sleep_micros(u64 usec)
-{
-       Sleep((DWORD)(usec / 1000));
-}
-
 }