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