factor/vm/os-windows.c

211 lines
4.0 KiB
C
Raw Normal View History

#include "master.h"
2006-08-03 14:40:13 -04:00
F_STRING *get_error_message()
{
DWORD id = GetLastError();
F_CHAR *msg = error_message(id);
F_STRING *string = from_u16_string(msg);
2006-11-02 21:28:44 -05:00
LocalFree(msg);
return string;
2006-08-03 14:40:13 -04:00
}
2006-11-02 21:28:44 -05:00
/* You must LocalFree() the return value! */
F_CHAR *error_message(DWORD id)
2006-08-03 14:40:13 -04:00
{
F_CHAR *buffer;
2006-08-03 14:40:13 -04:00
int index;
2006-10-31 15:48:34 -05:00
DWORD ret = FormatMessage(
2006-08-03 14:40:13 -04:00
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
id,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)(void *) &buffer,
2006-08-03 14:40:13 -04:00
0, NULL);
if(ret == 0)
return error_message(GetLastError());
2006-08-03 14:40:13 -04:00
/* strip whitespace from end */
index = wcslen(buffer) - 1;
2006-08-03 14:40:13 -04:00
while(index >= 0 && isspace(buffer[index]))
buffer[index--] = 0;
2006-10-31 15:48:34 -05:00
2006-11-02 21:28:44 -05:00
return buffer;
2006-08-03 14:40:13 -04:00
}
HMODULE hFactorDll;
void init_ffi()
2006-08-03 14:40:13 -04:00
{
hFactorDll = GetModuleHandle(L"factor.dll");
if(!hFactorDll)
fatal_error("GetModuleHandle(\"factor.dll\") failed", 0);
2006-08-03 14:40:13 -04:00
}
2006-11-04 11:52:57 -05:00
void ffi_dlopen (F_DLL *dll, bool error)
2006-08-03 14:40:13 -04:00
{
HMODULE module = LoadLibraryEx(alien_offset(dll->path), NULL, 0);
2006-08-03 14:40:13 -04:00
if (!module)
{
dll->dll = NULL;
if(error)
simple_error(ERROR_FFI,F,
tag_object(get_error_message()));
2006-08-03 14:40:13 -04:00
else
return;
}
dll->dll = module;
}
void *ffi_dlsym (F_DLL *dll, F_SYMBOL *symbol)
2006-08-03 14:40:13 -04:00
{
return GetProcAddress(dll ? (HMODULE)dll->dll : hFactorDll, symbol);
2006-08-03 14:40:13 -04:00
}
void ffi_dlclose(F_DLL *dll)
2006-08-03 14:40:13 -04:00
{
FreeLibrary((HMODULE)dll->dll);
dll->dll = NULL;
}
static F_CHAR *image_path = 0;
const F_CHAR *default_image_path(void)
{
F_CHAR path_temp[MAX_UNICODE_PATH];
if(!image_path)
{
int ret;
if(!(ret = GetModuleFileName(GetModuleHandle(NULL),
path_temp,MAX_UNICODE_PATH)))
return 0;
F_CHAR *ptr;
ptr = wcsrchr(path_temp, '\\');
if(!ptr)
return 0;
snwprintf(ptr, MAX_UNICODE_PATH - (ptr - path_temp),
L"\\factor.image");
image_path = _wcsdup(path_temp);
if(!image_path)
fatal_error("Out of memory in default_image_path",0);
}
return image_path;
}
F_CHAR *char_to_F_CHAR(char *ptr)
{
F_CHAR buffer[MAX_UNICODE_PATH];
mbstowcs(buffer, ptr, MAX_UNICODE_PATH-2);
return _wcsdup(buffer);
}
2006-08-03 14:40:13 -04:00
void primitive_stat(void)
{
WIN32_FIND_DATA st;
HANDLE h;
2006-08-03 14:40:13 -04:00
F_CHAR *path = unbox_u16_string();
if(INVALID_HANDLE_VALUE == (h = FindFirstFile(
path,
&st)))
2006-08-03 14:40:13 -04:00
{
dpush(F);
2006-10-31 00:52:02 -05:00
dpush(F);
dpush(F);
dpush(F);
2006-10-31 15:48:34 -05:00
}
else
2006-08-03 14:40:13 -04:00
{
2006-10-31 00:52:02 -05:00
box_boolean(st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
box_signed_4(0);
box_unsigned_8(
(u64)st.nFileSizeLow | (u64)st.nFileSizeHigh << 32);
box_unsigned_8(
((*(u64*)&st.ftLastWriteTime - EPOCH_OFFSET) / 10000000));
FindClose(h);
2006-08-03 14:40:13 -04:00
}
}
void primitive_read_dir(void)
{
HANDLE dir;
WIN32_FIND_DATA find_data;
F_CHAR *path = unbox_u16_string();
2006-08-03 14:40:13 -04:00
GROWABLE_ARRAY(result);
2006-08-03 14:40:13 -04:00
2006-10-31 15:48:34 -05:00
if(INVALID_HANDLE_VALUE != (dir = FindFirstFile(path, &find_data)))
2006-08-03 14:40:13 -04:00
{
do
{
2006-10-31 16:45:12 -05:00
REGISTER_ARRAY(result);
CELL name = tag_object(from_u16_string(
2006-10-31 16:45:12 -05:00
find_data.cFileName));
UNREGISTER_ARRAY(result);
GROWABLE_ADD(result,name);
2006-10-31 15:48:34 -05:00
}
2006-08-03 14:40:13 -04:00
while (FindNextFile(dir, &find_data));
CloseHandle(dir);
}
GROWABLE_TRIM(result);
2006-08-03 14:40:13 -04:00
dpush(tag_object(result));
}
2006-11-02 18:29:11 -05:00
F_SEGMENT *alloc_segment(CELL size)
2006-08-03 14:40:13 -04:00
{
char *mem;
DWORD ignore;
2006-08-03 14:40:13 -04:00
if((mem = (char *)VirtualAlloc(NULL, getpagesize() * 2 + size,
MEM_COMMIT, PAGE_EXECUTE_READWRITE)) == 0)
fatal_error("Out of memory in alloc_segment",0);
2006-08-03 14:40:13 -04:00
if (!VirtualProtect(mem, getpagesize(), PAGE_NOACCESS, &ignore))
fatal_error("Cannot allocate low guard page", (CELL)mem);
2006-08-03 14:40:13 -04:00
if (!VirtualProtect(mem + size + getpagesize(),
getpagesize(), PAGE_NOACCESS, &ignore))
fatal_error("Cannot allocate high guard page", (CELL)mem);
2006-08-03 14:40:13 -04:00
2006-11-02 18:29:11 -05:00
F_SEGMENT *block = safe_malloc(sizeof(F_SEGMENT));
2006-08-03 14:40:13 -04:00
block->start = (CELL)mem + getpagesize();
block->size = size;
block->end = block->start + size;
2006-08-03 14:40:13 -04:00
return block;
2006-08-03 14:40:13 -04:00
}
2006-11-02 18:29:11 -05:00
void dealloc_segment(F_SEGMENT *block)
2006-08-03 14:40:13 -04:00
{
SYSTEM_INFO si;
GetSystemInfo(&si);
if(!VirtualFree((void*)(block->start - si.dwPageSize), 0, MEM_RELEASE))
fatal_error("dealloc_segment failed",0);
free(block);
2006-08-03 14:40:13 -04:00
}
long getpagesize(void)
2006-09-24 15:28:44 -04:00
{
static long g_pagesize = 0;
if (! g_pagesize)
{
SYSTEM_INFO system_info;
GetSystemInfo (&system_info);
g_pagesize = system_info.dwPageSize;
}
return g_pagesize;
}
2006-10-15 00:10:54 -04:00
void run(void)
2006-08-03 14:40:13 -04:00
{
2006-10-15 00:10:54 -04:00
interpreter();
}