]> gitweb.factorcode.org Git - factor.git/blob - vm/os-windows.cpp
b9af2ec054ea609f058db9207b2ab30dccaf3038
[factor.git] / vm / os-windows.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 HMODULE hFactorDll;
7
8 void factor_vm::init_ffi()
9 {
10         hFactorDll = GetModuleHandle(FACTOR_DLL);
11         if(!hFactorDll)
12                 fatal_error("GetModuleHandle() failed", 0);
13 }
14
15 void factor_vm::ffi_dlopen(dll *dll)
16 {
17         dll->handle = LoadLibraryEx((WCHAR *)alien_offset(dll->path), NULL, 0);
18 }
19
20 void *factor_vm::ffi_dlsym(dll *dll, symbol_char *symbol)
21 {
22         return (void *)GetProcAddress(dll ? (HMODULE)dll->handle : hFactorDll, symbol);
23 }
24
25 void factor_vm::ffi_dlclose(dll *dll)
26 {
27         FreeLibrary((HMODULE)dll->handle);
28         dll->handle = NULL;
29 }
30
31 BOOL factor_vm::windows_stat(vm_char *path)
32 {
33         BY_HANDLE_FILE_INFORMATION bhfi;
34         HANDLE h = CreateFileW(path,
35                         GENERIC_READ,
36                         FILE_SHARE_READ,
37                         NULL,
38                         OPEN_EXISTING,
39                         FILE_FLAG_BACKUP_SEMANTICS,
40                         NULL);
41
42         if(h == INVALID_HANDLE_VALUE)
43         {
44                 // FindFirstFile is the only call that can stat c:\pagefile.sys
45                 WIN32_FIND_DATA st;
46                 HANDLE h;
47
48                 if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
49                         return false;
50                 FindClose(h);
51                 return true;
52         }
53         BOOL ret = GetFileInformationByHandle(h, &bhfi);
54         CloseHandle(h);
55         return ret;
56 }
57
58 void factor_vm::windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length)
59 {
60         wcsncpy(temp_path, full_path, length - 1);
61         size_t full_path_len = wcslen(full_path);
62         if (full_path_len < length - 1)
63                 wcsncat(temp_path, L".image", length - full_path_len - 1);
64         temp_path[length - 1] = 0;
65 }
66
67 /* You must free() this yourself. */
68 const vm_char *factor_vm::default_image_path()
69 {
70         vm_char full_path[MAX_UNICODE_PATH];
71         vm_char *ptr;
72         vm_char temp_path[MAX_UNICODE_PATH];
73
74         if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
75                 fatal_error("GetModuleFileName() failed", 0);
76
77         if((ptr = wcsrchr(full_path, '.')))
78                 *ptr = 0;
79
80         wcsncpy(temp_path, full_path, MAX_UNICODE_PATH - 1);
81         size_t full_path_len = wcslen(full_path);
82         if (full_path_len < MAX_UNICODE_PATH - 1)
83                 wcsncat(temp_path, L".image", MAX_UNICODE_PATH - full_path_len - 1);
84         temp_path[MAX_UNICODE_PATH - 1] = 0;
85
86         return safe_strdup(temp_path);
87 }
88
89 /* You must free() this yourself. */
90 const vm_char *factor_vm::vm_executable_path()
91 {
92         vm_char full_path[MAX_UNICODE_PATH];
93         if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
94                 fatal_error("GetModuleFileName() failed", 0);
95         return safe_strdup(full_path);
96 }
97
98 void factor_vm::primitive_existsp()
99 {
100         vm_char *path = untag_check<byte_array>(ctx->pop())->data<vm_char>();
101         ctx->push(tag_boolean(windows_stat(path)));
102 }
103
104 segment::segment(cell size_, bool executable_p)
105 {
106         size = size_;
107
108         char *mem;
109         DWORD ignore;
110
111         if((mem = (char *)VirtualAlloc(NULL, getpagesize() * 2 + size,
112                 MEM_COMMIT, executable_p ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE)) == 0)
113                 out_of_memory();
114
115         if (!VirtualProtect(mem, getpagesize(), PAGE_NOACCESS, &ignore))
116                 fatal_error("Cannot allocate low guard page", (cell)mem);
117
118         if (!VirtualProtect(mem + size + getpagesize(),
119                 getpagesize(), PAGE_NOACCESS, &ignore))
120                 fatal_error("Cannot allocate high guard page", (cell)mem);
121
122         start = (cell)mem + getpagesize();
123         end = start + size;
124 }
125
126 segment::~segment()
127 {
128         SYSTEM_INFO si;
129         GetSystemInfo(&si);
130         if(!VirtualFree((void*)(start - si.dwPageSize), 0, MEM_RELEASE))
131                 fatal_error("Segment deallocation failed",0);
132 }
133
134 long getpagesize()
135 {
136         static long g_pagesize = 0;
137         if(!g_pagesize)
138         {
139                 SYSTEM_INFO system_info;
140                 GetSystemInfo (&system_info);
141                 g_pagesize = system_info.dwPageSize;
142         }
143         return g_pagesize;
144 }
145
146 void factor_vm::move_file(const vm_char *path1, const vm_char *path2)
147 {
148         if(MoveFileEx((path1),(path2),MOVEFILE_REPLACE_EXISTING) == false)
149                 general_error(ERROR_IO,tag_fixnum(GetLastError()),false_object);
150 }
151
152 void factor_vm::init_signals() {}
153
154 }