1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 #pragma once 23 24 #include <MTTypes.h> 25 26 // 27 // micro windows header is used to avoid including heavy windows header to MTPlatform.h 28 // 29 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 30 31 32 #define MW_WINBASEAPI __declspec(dllimport) 33 #define MW_WINAPI __stdcall 34 35 36 #if defined(_WINDOWS_) 37 38 // 39 // if windows.h is already included simply create aliases to the MW_ types 40 // 41 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 42 typedef LARGE_INTEGER MW_LARGE_INTEGER; 43 typedef BOOL MW_BOOL; 44 typedef HANDLE MW_HANDLE; 45 46 typedef DWORD MW_DWORD; 47 typedef WORD MW_WORD; 48 typedef DWORD64 MW_DWORD64; 49 50 typedef LPTHREAD_START_ROUTINE TThreadStartFunc; 51 52 typedef SYSTEM_INFO MW_SYSTEM_INFO; 53 54 typedef CRITICAL_SECTION MW_CRITICAL_SECTION; 55 56 typedef CONTEXT MW_CONTEXT; 57 58 #define MW_INFINITE (INFINITE) 59 #define MW_WAIT_OBJECT_0 (WAIT_OBJECT_0) 60 #define MW_MEM_COMMIT (MEM_COMMIT) 61 #define MW_PAGE_READWRITE (PAGE_READWRITE) 62 #define MW_PAGE_NOACCESS (PAGE_NOACCESS) 63 #define MW_MEM_RELEASE (MEM_RELEASE) 64 65 66 #define MW_STACK_BASE_OFFSET (FIELD_OFFSET(NT_TIB, StackBase)) 67 #define MW_STACK_STACK_LIMIT_OFFSET (FIELD_OFFSET(NT_TIB, StackLimit)) 68 #define MW_CONTEXT_FULL (CONTEXT_FULL) 69 70 71 #else 72 73 74 75 76 // 77 // define types 78 // 79 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 80 81 struct MW_LARGE_INTEGER 82 { 83 int64 QuadPart; 84 }; 85 86 typedef int MW_BOOL; 87 typedef void* MW_HANDLE; 88 89 typedef unsigned long MW_DWORD; 90 typedef unsigned short MW_WORD; 91 typedef unsigned __int64 MW_DWORD64; 92 93 // 94 // define thread function 95 // 96 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 97 typedef MW_DWORD ( __stdcall *TThreadStartFunc )(void* lpThreadParameter); 98 99 100 // 101 // system info structure, only used members are declared 102 // 103 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 104 struct MW_SYSTEM_INFO 105 { 106 uint8 _unused_01[4]; 107 MW_DWORD dwPageSize; 108 void* _unused_02[3]; 109 MW_DWORD dwNumberOfProcessors; 110 uint8 _unused_03[12]; 111 }; 112 113 #if defined(_M_X64) 114 115 // 116 // x64 critical section, only used members are declared 117 // 118 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 119 struct MW_CRITICAL_SECTION 120 { 121 uint8 _unused[40]; 122 }; 123 124 // 125 // x64 machine context, only used members are declared 126 // 127 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 128 struct __declspec(align(16)) MW_CONTEXT 129 { 130 uint8 _unused_01[48]; 131 MW_DWORD ContextFlags; 132 uint8 _unused_02[100]; 133 MW_DWORD64 Rsp; 134 uint8 _unused_03[88]; 135 MW_DWORD64 Rip; 136 uint8 _unused_04[976]; 137 }; 138 139 #define MW_STACK_BASE_OFFSET (8) 140 #define MW_STACK_STACK_LIMIT_OFFSET (16) 141 #define MW_CONTEXT_FULL (0x10000B) 142 143 144 #else 145 146 // 147 // x86 critical section, only used members are declared 148 // 149 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 150 struct MW_CRITICAL_SECTION 151 { 152 uint8 _unused[24]; 153 }; 154 155 // 156 // x86 machine context, only used members are declared 157 // 158 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 159 struct MW_CONTEXT 160 { 161 MW_DWORD ContextFlags; 162 uint8 _unused_01[180]; 163 MW_DWORD Eip; 164 uint8 _unused_02[8]; 165 MW_DWORD Esp; 166 uint8 _unused_03[516]; 167 }; 168 169 170 #define MW_STACK_BASE_OFFSET (4) 171 #define MW_STACK_STACK_LIMIT_OFFSET (8) 172 #define MW_CONTEXT_FULL (0x10007) 173 174 175 #endif 176 177 178 // 179 // defines and flags 180 // 181 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 182 #define MW_INFINITE (0xFFFFFFFF) 183 #define MW_WAIT_OBJECT_0 (0) 184 #define MW_MEM_COMMIT (0x1000) 185 #define MW_PAGE_READWRITE (0x04) 186 #define MW_PAGE_NOACCESS (0x01) 187 #define MW_MEM_RELEASE (0x8000) 188 189 190 #endif 191 192 193 194 #if !defined(MW_SKIP_FUNCTIONS) && !defined(_WINDOWS_) 195 196 // 197 // functions 198 // 199 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 200 201 202 extern "C" { 203 204 MW_WINBASEAPI MW_BOOL MW_WINAPI QueryPerformanceFrequency(MW_LARGE_INTEGER* lpFrequency); 205 MW_WINBASEAPI MW_BOOL MW_WINAPI QueryPerformanceCounter(MW_LARGE_INTEGER* lpPerformanceCount); 206 207 MW_WINBASEAPI MW_HANDLE MW_WINAPI CreateThread(void* lpThreadAttributes, size_t dwStackSize, TThreadStartFunc lpStartAddress, void* lpParameter, MW_DWORD dwCreationFlags, MW_DWORD* lpThreadId); 208 MW_WINBASEAPI MW_BOOL MW_WINAPI CloseHandle(MW_HANDLE hObject); 209 MW_WINBASEAPI MW_HANDLE MW_WINAPI GetCurrentThread(); 210 MW_WINBASEAPI MW_DWORD MW_WINAPI GetCurrentThreadId(); 211 212 MW_WINBASEAPI void MW_WINAPI GetSystemInfo(MW_SYSTEM_INFO* lpSystemInfo); 213 214 MW_WINBASEAPI void MW_WINAPI Sleep(MW_DWORD dwMilliseconds); 215 MW_WINBASEAPI MW_DWORD MW_WINAPI WaitForSingleObject(MW_HANDLE hHandle, MW_DWORD dwMilliseconds); 216 217 MW_WINBASEAPI bool MW_WINAPI InitializeCriticalSectionAndSpinCount(MW_CRITICAL_SECTION* lpCriticalSection, MW_DWORD dwSpinCount ); 218 MW_WINBASEAPI void MW_WINAPI DeleteCriticalSection(MW_CRITICAL_SECTION* lpCriticalSection ); 219 MW_WINBASEAPI void MW_WINAPI EnterCriticalSection(MW_CRITICAL_SECTION* lpCriticalSection ); 220 MW_WINBASEAPI void MW_WINAPI LeaveCriticalSection(MW_CRITICAL_SECTION* lpCriticalSection ); 221 222 MW_WINBASEAPI MW_HANDLE MW_WINAPI CreateEventA(MW_CRITICAL_SECTION* lpEventAttributes, MW_BOOL bManualReset, MW_BOOL bInitialState, const char* lpName ); 223 MW_WINBASEAPI MW_HANDLE MW_WINAPI CreateEventW(MW_CRITICAL_SECTION* lpEventAttributes, MW_BOOL bManualReset, MW_BOOL bInitialState, const wchar_t* lpName ); 224 MW_WINBASEAPI MW_BOOL MW_WINAPI SetEvent( MW_HANDLE hEvent ); 225 MW_WINBASEAPI MW_BOOL MW_WINAPI ResetEvent( MW_HANDLE hEvent ); 226 227 MW_WINBASEAPI MW_BOOL MW_WINAPI GetThreadContext( MW_HANDLE hThread, MW_CONTEXT* lpContext ); 228 MW_WINBASEAPI MW_BOOL MW_WINAPI SetThreadContext( MW_HANDLE hThread, const MW_CONTEXT* lpContext ); 229 230 MW_WINBASEAPI void* MW_WINAPI VirtualAlloc( void* lpAddress, size_t dwSize, MW_DWORD flAllocationType, MW_DWORD flProtect ); 231 MW_WINBASEAPI MW_BOOL MW_WINAPI VirtualProtect( void* lpAddress, size_t dwSize, MW_DWORD flNewProtect, MW_DWORD* lpflOldProtect ); 232 MW_WINBASEAPI MW_BOOL MW_WINAPI VirtualFree( void* lpAddress, size_t dwSize, MW_DWORD dwFreeType ); 233 234 } 235 236 #endif 237 238 239 /* 240 #ifdef WINBASEAPI 241 242 #define STR_HELPER(x) #x 243 #define STR(x) STR_HELPER(x) 244 245 #pragma message ("WINBASEAPI = " STR(WINBASEAPI)) 246 #pragma message ("MW_WINBASEAPI = " STR(MW_WINBASEAPI)) 247 #pragma message ("WINAPI = " STR(WINAPI)) 248 #pragma message ("MW_WINAPI = " STR(MW_WINAPI)) 249 250 #endif 251 */ 252