add an unlimit word, refactor limited-streams, better docs
[factor/jcg.git] / vm / os-windows.c
blobc3e9e50cee7ce0ab164f392ca4ac1b28d9358f16
1 #include "master.h"
3 F_STRING *get_error_message(void)
5 DWORD id = GetLastError();
6 F_CHAR *msg = error_message(id);
7 F_STRING *string = from_u16_string(msg);
8 LocalFree(msg);
9 return string;
12 /* You must LocalFree() the return value! */
13 F_CHAR *error_message(DWORD id)
15 F_CHAR *buffer;
16 int index;
18 DWORD ret = FormatMessage(
19 FORMAT_MESSAGE_ALLOCATE_BUFFER |
20 FORMAT_MESSAGE_FROM_SYSTEM,
21 NULL,
22 id,
23 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
24 (LPTSTR)(void *) &buffer,
25 0, NULL);
26 if(ret == 0)
27 return error_message(GetLastError());
29 /* strip whitespace from end */
30 index = wcslen(buffer) - 1;
31 while(index >= 0 && isspace(buffer[index]))
32 buffer[index--] = 0;
34 return buffer;
37 HMODULE hFactorDll;
39 void init_ffi(void)
41 hFactorDll = GetModuleHandle(FACTOR_DLL);
42 if(!hFactorDll)
43 fatal_error("GetModuleHandle(\"" FACTOR_DLL_NAME "\") failed", 0);
46 void ffi_dlopen(F_DLL *dll)
48 dll->dll = LoadLibraryEx(alien_offset(dll->path), NULL, 0);
51 void *ffi_dlsym(F_DLL *dll, F_SYMBOL *symbol)
53 return GetProcAddress(dll ? (HMODULE)dll->dll : hFactorDll, symbol);
56 void ffi_dlclose(F_DLL *dll)
58 FreeLibrary((HMODULE)dll->dll);
59 dll->dll = NULL;
62 /* You must free() this yourself. */
63 const F_CHAR *default_image_path(void)
65 F_CHAR full_path[MAX_UNICODE_PATH];
66 F_CHAR *ptr;
67 F_CHAR path_temp[MAX_UNICODE_PATH];
69 if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
70 fatal_error("GetModuleFileName() failed", 0);
72 if((ptr = wcsrchr(full_path, '.')))
73 *ptr = 0;
75 snwprintf(path_temp, sizeof(path_temp)-1, L"%s.image", full_path);
76 path_temp[sizeof(path_temp) - 1] = 0;
78 return safe_strdup(path_temp);
81 /* You must free() this yourself. */
82 const F_CHAR *vm_executable_path(void)
84 F_CHAR full_path[MAX_UNICODE_PATH];
85 if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
86 fatal_error("GetModuleFileName() failed", 0);
87 return safe_strdup(full_path);
90 void primitive_existsp(void)
92 BY_HANDLE_FILE_INFORMATION bhfi;
94 F_CHAR *path = unbox_u16_string();
95 HANDLE h = CreateFileW(path,
96 GENERIC_READ,
97 FILE_SHARE_READ,
98 NULL,
99 OPEN_EXISTING,
100 FILE_FLAG_BACKUP_SEMANTICS,
101 NULL);
103 if(h == INVALID_HANDLE_VALUE)
105 // FindFirstFile is the only call that can stat c:\pagefile.sys
106 WIN32_FIND_DATA st;
107 HANDLE h;
109 if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
110 dpush(F);
111 else
113 FindClose(h);
114 dpush(T);
116 return;
119 box_boolean(GetFileInformationByHandle(h, &bhfi));
120 CloseHandle(h);
123 F_SEGMENT *alloc_segment(CELL size)
125 char *mem;
126 DWORD ignore;
128 if((mem = (char *)VirtualAlloc(NULL, getpagesize() * 2 + size,
129 MEM_COMMIT, PAGE_EXECUTE_READWRITE)) == 0)
130 out_of_memory();
132 if (!VirtualProtect(mem, getpagesize(), PAGE_NOACCESS, &ignore))
133 fatal_error("Cannot allocate low guard page", (CELL)mem);
135 if (!VirtualProtect(mem + size + getpagesize(),
136 getpagesize(), PAGE_NOACCESS, &ignore))
137 fatal_error("Cannot allocate high guard page", (CELL)mem);
139 F_SEGMENT *block = safe_malloc(sizeof(F_SEGMENT));
141 block->start = (CELL)mem + getpagesize();
142 block->size = size;
143 block->end = block->start + size;
145 return block;
148 void dealloc_segment(F_SEGMENT *block)
150 SYSTEM_INFO si;
151 GetSystemInfo(&si);
152 if(!VirtualFree((void*)(block->start - si.dwPageSize), 0, MEM_RELEASE))
153 fatal_error("dealloc_segment failed",0);
154 free(block);
157 long getpagesize(void)
159 static long g_pagesize = 0;
160 if (! g_pagesize)
162 SYSTEM_INFO system_info;
163 GetSystemInfo (&system_info);
164 g_pagesize = system_info.dwPageSize;
166 return g_pagesize;
169 void sleep_micros(u64 usec)
171 Sleep((DWORD)(usec / 1000));