]> gitweb.factorcode.org Git - factor.git/blob - vm/os-windows.hpp
git: fix tests
[factor.git] / vm / os-windows.hpp
1 #include <ctype.h>
2
3 #ifndef wcslen
4 // for cygwin
5 #include <wchar.h>
6 #endif
7
8 #ifndef UNICODE
9 #define UNICODE
10 #endif
11
12 #include <windows.h>
13 #include <shellapi.h>
14
15 #ifdef _MSC_VER
16 #undef min
17 #undef max
18 #endif
19
20 // Difference between Jan 1 00:00:00 1601 and Jan 1 00:00:00 1970
21 #define EPOCH_OFFSET 0x019db1ded53e8000LL
22
23 namespace factor {
24
25 typedef wchar_t vm_char;
26 typedef char symbol_char;
27 typedef HANDLE THREADHANDLE;
28
29 #define STRING_LITERAL(string) L##string
30
31 #define MAX_UNICODE_PATH 32768
32 #define VM_C_API extern "C" __declspec(dllexport)
33 #define SSCANF swscanf
34 #define STRCMP wcscmp
35 #define STRNCMP wcsncmp
36 #define STRDUP _wcsdup
37
38 #ifdef _MSC_VER
39 #define FTELL ftell
40 #define FSEEK fseek
41 #else
42 #define FTELL ftello64
43 #define FSEEK fseeko64
44 #endif
45
46 #define FACTOR_OS_STRING "windows"
47
48 // SSE traps raise these exception codes, which are defined in internal NT
49 // headers
50 // but not winbase.h
51 #ifndef STATUS_FLOAT_MULTIPLE_FAULTS
52 #define STATUS_FLOAT_MULTIPLE_FAULTS 0xC00002B4
53 #endif
54
55 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
56 #define STATUS_FLOAT_MULTIPLE_TRAPS 0xC00002B5
57 #endif
58
59 #define OPEN_READ(path) _wfopen((path), L"rb")
60 #define OPEN_WRITE(path) _wfopen((path), L"wb")
61 #define THREADSAFE_STRERROR(errnum, buf, buflen) strerror_s(buf, buflen, errnum)
62
63 inline static void early_init() {}
64 uint64_t nano_count();
65 void sleep_nanos(uint64_t nsec);
66 long getpagesize();
67 VM_C_API LONG exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,
68                                 void* dispatch);
69 THREADHANDLE start_thread(void* (*start_routine)(void*), void* args);
70
71 inline static THREADHANDLE thread_id() {
72   DWORD id = GetCurrentThreadId();
73   HANDLE threadHandle = OpenThread(
74       THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME, FALSE,
75       id);
76   FACTOR_ASSERT(threadHandle != NULL);
77   return threadHandle;
78 }
79
80 inline static void breakpoint() { DebugBreak(); }
81
82 #define CODE_TO_FUNCTION_POINTER(code) (void)0
83 #define CODE_TO_FUNCTION_POINTER_CALLBACK(vm, code) (void)0
84 #define FUNCTION_CODE_POINTER(ptr) ptr
85 #define FUNCTION_TOC_POINTER(ptr) ptr
86
87 extern HANDLE boot_thread;
88
89 inline static std::string to_utf8(const wchar_t* buffer, int len) {
90   int nChars = ::WideCharToMultiByte(
91     CP_UTF8,
92     0,
93     buffer,
94     len,
95     NULL,
96     0,
97     NULL,
98     NULL);
99   if (nChars == 0) return "";
100
101   std::string newbuffer;
102   newbuffer.resize(nChars) ;
103   ::WideCharToMultiByte(
104     CP_UTF8,
105     0,
106     buffer,
107     len,
108     const_cast<char*>(newbuffer.c_str()),
109     nChars,
110     NULL,
111     NULL);
112   return newbuffer;
113 }
114
115 inline static std::string to_utf8(const std::wstring& str) {
116   return to_utf8(str.c_str(), (int)str.size());
117 }
118
119 #define AS_UTF8(ptr) to_utf8(ptr)
120
121 }