WinFunctions.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //===-- WinFunctions.cpp - Windows Functions for other platforms --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines Windows-specific functions used in the codebase for
  11. // non-Windows platforms.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef _WIN32
  15. #include <fcntl.h>
  16. #include <map>
  17. #include <string.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #include "dxc/Support/WinFunctions.h"
  21. HRESULT StringCchCopyEx(LPSTR pszDest, size_t cchDest, LPCSTR pszSrc,
  22. LPSTR *ppszDestEnd, size_t *pcchRemaining, DWORD dwFlags) {
  23. assert(dwFlags == 0 && "dwFlag values not supported in StringCchCopyEx");
  24. char *zPtr = 0;
  25. zPtr = stpncpy(pszDest, pszSrc, cchDest);
  26. if (ppszDestEnd)
  27. *ppszDestEnd = zPtr;
  28. if (pcchRemaining)
  29. *pcchRemaining = cchDest - (zPtr - pszDest);
  30. return S_OK;
  31. }
  32. HRESULT StringCchPrintfA(char *dst, size_t dstSize, const char *format, ...) {
  33. va_list args;
  34. va_start(args, format);
  35. va_list argscopy;
  36. va_copy(argscopy, args);
  37. // C++11 snprintf can return the size of the resulting string if it was to be
  38. // constructed.
  39. size_t size = vsnprintf(nullptr, 0, format, argscopy) + 1; // Extra space for '\0'
  40. if (size > dstSize) {
  41. *dst = '\0';
  42. } else {
  43. vsnprintf(dst, size, format, args);
  44. }
  45. va_end(argscopy);
  46. va_end(args);
  47. return S_OK;
  48. }
  49. HRESULT UIntAdd(UINT uAugend, UINT uAddend, UINT *puResult) {
  50. HRESULT hr;
  51. if ((uAugend + uAddend) >= uAugend) {
  52. *puResult = (uAugend + uAddend);
  53. hr = S_OK;
  54. } else {
  55. *puResult = 0xffffffff;
  56. hr = ERROR_ARITHMETIC_OVERFLOW;
  57. }
  58. return hr;
  59. }
  60. HRESULT IntToUInt(int in, UINT *out) {
  61. HRESULT hr;
  62. if (in >= 0) {
  63. *out = (UINT)in;
  64. hr = S_OK;
  65. } else {
  66. *out = 0xffffffff;
  67. hr = ERROR_ARITHMETIC_OVERFLOW;
  68. }
  69. return hr;
  70. }
  71. HRESULT SizeTToInt(size_t in, int *out) {
  72. HRESULT hr;
  73. if(in <= INT_MAX) {
  74. *out = (int)in;
  75. hr = S_OK;
  76. }
  77. else {
  78. *out = 0xffffffff;
  79. hr = ERROR_ARITHMETIC_OVERFLOW;
  80. }
  81. return hr;
  82. }
  83. HRESULT UInt32Mult(UINT a, UINT b, UINT *out) {
  84. uint64_t result = (uint64_t)a * (uint64_t)b;
  85. if (result > uint64_t(UINT_MAX))
  86. return ERROR_ARITHMETIC_OVERFLOW;
  87. *out = (uint32_t)result;
  88. return S_OK;
  89. }
  90. int _stricmp(const char *str1, const char *str2) {
  91. size_t i = 0;
  92. for (; str1[i] && str2[i]; ++i) {
  93. int d = std::tolower(str1[i]) - std::tolower(str2[i]);
  94. if (d != 0)
  95. return d;
  96. }
  97. return str1[i] - str2[i];
  98. }
  99. int _wcsicmp(const wchar_t *str1, const wchar_t *str2) {
  100. size_t i = 0;
  101. for (; str1[i] && str2[i]; ++i) {
  102. int d = std::towlower(str1[i]) - std::towlower(str2[i]);
  103. if (d != 0)
  104. return d;
  105. }
  106. return str1[i] - str2[i];
  107. }
  108. int _wcsnicmp(const wchar_t *str1, const wchar_t *str2, size_t n) {
  109. size_t i = 0;
  110. for (; i < n && str1[i] && str2[i]; ++i) {
  111. int d = std::towlower(str1[i]) - std::towlower(str2[i]);
  112. if (d != 0)
  113. return d;
  114. }
  115. if (i >= n) return 0;
  116. return str1[i] - str2[i];
  117. }
  118. unsigned char _BitScanForward(unsigned long * Index, unsigned long Mask) {
  119. unsigned long l;
  120. if (!Mask) return 0;
  121. for (l=0; !(Mask&1); l++) Mask >>= 1;
  122. *Index = l;
  123. return 1;
  124. }
  125. HRESULT CoGetMalloc(DWORD dwMemContext, IMalloc **ppMalloc) {
  126. *ppMalloc = new IMalloc;
  127. (*ppMalloc)->AddRef();
  128. return S_OK;
  129. }
  130. HANDLE CreateFile2(_In_ LPCWSTR lpFileName, _In_ DWORD dwDesiredAccess,
  131. _In_ DWORD dwShareMode, _In_ DWORD dwCreationDisposition,
  132. _In_opt_ void *pCreateExParams) {
  133. return CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, pCreateExParams,
  134. dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
  135. }
  136. HANDLE CreateFileW(_In_ LPCWSTR lpFileName, _In_ DWORD dwDesiredAccess,
  137. _In_ DWORD dwShareMode, _In_opt_ void *lpSecurityAttributes,
  138. _In_ DWORD dwCreationDisposition,
  139. _In_ DWORD dwFlagsAndAttributes,
  140. _In_opt_ HANDLE hTemplateFile) {
  141. CW2A pUtf8FileName(lpFileName);
  142. size_t fd = -1;
  143. int flags = 0;
  144. if (dwDesiredAccess & GENERIC_WRITE)
  145. if (dwDesiredAccess & GENERIC_READ)
  146. flags |= O_RDWR;
  147. else
  148. flags |= O_WRONLY;
  149. else // dwDesiredAccess may be 0, but open() demands something here. This is mostly harmless
  150. flags |= O_RDONLY;
  151. if (dwCreationDisposition == CREATE_ALWAYS)
  152. flags |= (O_CREAT | O_TRUNC);
  153. if (dwCreationDisposition == OPEN_ALWAYS)
  154. flags |= O_CREAT;
  155. else if (dwCreationDisposition == CREATE_NEW)
  156. flags |= (O_CREAT | O_EXCL);
  157. else if (dwCreationDisposition == TRUNCATE_EXISTING)
  158. flags |= O_TRUNC;
  159. // OPEN_EXISTING represents default open() behavior
  160. // Catch Implementation limitations.
  161. assert(!lpSecurityAttributes && "security attributes not supported in CreateFileW yet");
  162. assert(!hTemplateFile && "template file not supported in CreateFileW yet");
  163. assert(dwFlagsAndAttributes == FILE_ATTRIBUTE_NORMAL &&
  164. "Attributes other than NORMAL not supported in CreateFileW yet");
  165. while ((int)(fd = open(pUtf8FileName, flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
  166. if (errno != EINTR)
  167. return INVALID_HANDLE_VALUE;
  168. }
  169. return (HANDLE)fd;
  170. }
  171. BOOL GetFileSizeEx(_In_ HANDLE hFile, _Out_ PLARGE_INTEGER lpFileSize) {
  172. int fd = (size_t)hFile;
  173. struct stat fdstat;
  174. int rv = fstat(fd, &fdstat);
  175. if (!rv) {
  176. lpFileSize->QuadPart = (LONGLONG)fdstat.st_size;
  177. return true;
  178. }
  179. return false;
  180. }
  181. BOOL ReadFile(_In_ HANDLE hFile, _Out_ LPVOID lpBuffer,
  182. _In_ DWORD nNumberOfBytesToRead,
  183. _Out_opt_ LPDWORD lpNumberOfBytesRead,
  184. _Inout_opt_ void *lpOverlapped) {
  185. size_t fd = (size_t)hFile;
  186. ssize_t rv = -1;
  187. // Implementation limitation
  188. assert(!lpOverlapped && "Overlapping not supported in ReadFile yet.");
  189. rv = read(fd, lpBuffer, nNumberOfBytesToRead);
  190. if (rv < 0)
  191. return false;
  192. *lpNumberOfBytesRead = rv;
  193. return true;
  194. }
  195. BOOL WriteFile(_In_ HANDLE hFile, _In_ LPCVOID lpBuffer,
  196. _In_ DWORD nNumberOfBytesToWrite,
  197. _Out_opt_ LPDWORD lpNumberOfBytesWritten,
  198. _Inout_opt_ void *lpOverlapped) {
  199. size_t fd = (size_t)hFile;
  200. ssize_t rv = -1;
  201. // Implementation limitation
  202. assert(!lpOverlapped && "Overlapping not supported in WriteFile yet.");
  203. rv = write(fd, lpBuffer, nNumberOfBytesToWrite);
  204. if (rv < 0)
  205. return false;
  206. *lpNumberOfBytesWritten = rv;
  207. return true;
  208. }
  209. BOOL CloseHandle(_In_ HANDLE hObject) {
  210. int fd = (size_t)hObject;
  211. return !close(fd);
  212. }
  213. // Half-hearted implementation of a heap structure
  214. // Enables size queries, maximum allocation limit, and collective free at heap destruction
  215. // Does not perform any preallocation or allocation organization.
  216. // Does not respect any flags except for HEAP_ZERO_MEMORY
  217. struct SimpleAllocation {
  218. LPVOID ptr;
  219. SIZE_T size;
  220. };
  221. struct SimpleHeap {
  222. std::map<LPCVOID, SimpleAllocation> allocs;
  223. SIZE_T maxSize, curSize;
  224. };
  225. HANDLE HeapCreate(DWORD flOptions, SIZE_T dwInitialSize , SIZE_T dwMaximumSize) {
  226. SimpleHeap *simpHeap = new SimpleHeap;
  227. simpHeap->maxSize = dwMaximumSize;
  228. simpHeap->curSize = 0;
  229. return (HANDLE)simpHeap;
  230. }
  231. BOOL HeapDestroy(HANDLE hHeap) {
  232. SimpleHeap *simpHeap = (SimpleHeap*)hHeap;
  233. for (auto it = simpHeap->allocs.begin(), e = simpHeap->allocs.end(); it != e; it++)
  234. free(it->second.ptr);
  235. delete simpHeap;
  236. return true;
  237. }
  238. LPVOID HeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes) {
  239. LPVOID ptr = nullptr;
  240. SimpleHeap *simpHeap = (SimpleHeap*)hHeap;
  241. if (simpHeap->maxSize && simpHeap->curSize + dwBytes > simpHeap->maxSize)
  242. return nullptr;
  243. if (dwFlags == HEAP_ZERO_MEMORY)
  244. ptr = calloc(1, dwBytes);
  245. else
  246. ptr = malloc(dwBytes);
  247. simpHeap->allocs[ptr] = {ptr, dwBytes};
  248. simpHeap->curSize += dwBytes;
  249. return ptr;
  250. }
  251. LPVOID HeapReAlloc(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem, SIZE_T dwBytes) {
  252. LPVOID ptr = nullptr;
  253. SimpleHeap *simpHeap = (SimpleHeap*)hHeap;
  254. SIZE_T oSize = simpHeap->allocs[lpMem].size;
  255. if (simpHeap->maxSize && simpHeap->curSize - oSize + dwBytes > simpHeap->maxSize)
  256. return nullptr;
  257. ptr = realloc(lpMem, dwBytes);
  258. if (dwFlags == HEAP_ZERO_MEMORY && oSize < dwBytes)
  259. memset((char*)ptr + oSize, 0, dwBytes - oSize);
  260. simpHeap->allocs.erase(lpMem);
  261. simpHeap->curSize -= oSize;
  262. simpHeap->allocs[ptr] = {ptr, dwBytes};
  263. simpHeap->curSize += dwBytes;
  264. return ptr;
  265. }
  266. BOOL HeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) {
  267. SimpleHeap *simpHeap = (SimpleHeap*)hHeap;
  268. SIZE_T oSize = simpHeap->allocs[lpMem].size;
  269. free(lpMem);
  270. simpHeap->allocs.erase(lpMem);
  271. simpHeap->curSize -= oSize;
  272. return true;
  273. }
  274. SIZE_T HeapSize(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem) {
  275. SimpleHeap *simpHeap = (SimpleHeap*)hHeap;
  276. return simpHeap->allocs[lpMem].size;
  277. }
  278. static SimpleHeap g_processHeap;
  279. HANDLE GetProcessHeap() {
  280. return (HANDLE)&g_processHeap;
  281. }
  282. #endif // _WIN32