WinAdapter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===-- WinAdapter.cpp - Windows Adapter 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. #ifndef _WIN32
  10. #include "dxc/Support/WinAdapter.h"
  11. #include "dxc/Support/WinFunctions.h"
  12. //===--------------------- UUID Related Macros ----------------------------===//
  13. #ifdef __EMULATE_UUID
  14. size_t UuidStrHash(const char* k) {
  15. long h = 0;
  16. while (*k) {
  17. h = (h << 4) + *(k++);
  18. long g = h & 0xF0000000L;
  19. if (g != 0)
  20. h ^= g >> 24;
  21. h &= ~g;
  22. }
  23. return h;
  24. }
  25. #endif // __EMULATE_UUID
  26. DEFINE_CROSS_PLATFORM_UUIDOF(IUnknown)
  27. DEFINE_CROSS_PLATFORM_UUIDOF(INoMarshal)
  28. DEFINE_CROSS_PLATFORM_UUIDOF(IStream)
  29. DEFINE_CROSS_PLATFORM_UUIDOF(ISequentialStream)
  30. //===--------------------------- IUnknown ---------------------------------===//
  31. ULONG IUnknown::AddRef() {
  32. ++m_count;
  33. return m_count;
  34. }
  35. ULONG IUnknown::Release() {
  36. ULONG result = --m_count;
  37. if (m_count == 0) {
  38. delete this;
  39. }
  40. return result;
  41. }
  42. IUnknown::~IUnknown() {}
  43. //===--------------------------- IMalloc ----------------------------------===//
  44. void *IMalloc::Alloc(size_t size) { return malloc(size); }
  45. void *IMalloc::Realloc(void *ptr, size_t size) { return realloc(ptr, size); }
  46. void IMalloc::Free(void *ptr) { free(ptr); }
  47. HRESULT IMalloc::QueryInterface(REFIID riid, void **ppvObject) {
  48. assert(false && "QueryInterface not implemented for IMalloc.");
  49. return E_NOINTERFACE;
  50. }
  51. //===--------------------------- CAllocator -------------------------------===//
  52. void *CAllocator::Reallocate(void *p, size_t nBytes) throw() {
  53. return realloc(p, nBytes);
  54. }
  55. void *CAllocator::Allocate(size_t nBytes) throw() { return malloc(nBytes); }
  56. void CAllocator::Free(void *p) throw() { free(p); }
  57. //===--------------------------- BSTR Allocation --------------------------===//
  58. void SysFreeString(BSTR bstrString) {
  59. if (bstrString)
  60. free((void *)((uintptr_t)bstrString - sizeof(uint32_t)));
  61. }
  62. // Allocate string with length prefix
  63. // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr
  64. BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui) {
  65. uint32_t *blobOut =
  66. (uint32_t *)malloc(sizeof(uint32_t) + (ui + 1) * sizeof(OLECHAR));
  67. if (!blobOut)
  68. return nullptr;
  69. // Size in bytes without trailing NULL character
  70. blobOut[0] = ui * sizeof(OLECHAR);
  71. BSTR strOut = (BSTR)&blobOut[1];
  72. if (strIn)
  73. memcpy(strOut, strIn, blobOut[0]);
  74. // Write trailing NULL character:
  75. strOut[ui] = 0;
  76. return strOut;
  77. }
  78. //===---------------------- Char converstion ------------------------------===//
  79. const char *CPToLocale(uint32_t CodePage) {
  80. #ifdef __APPLE__
  81. static const char *utf8 = "en_US.UTF-8";
  82. static const char *iso88591 = "en_US.ISO8859-1";
  83. #else
  84. static const char *utf8 = "en_US.utf8";
  85. static const char *iso88591 = "en_US.iso88591";
  86. #endif
  87. if (CodePage == CP_UTF8) {
  88. return utf8;
  89. } else if (CodePage == CP_ACP) {
  90. // Experimentation suggests that ACP is expected to be ISO-8859-1
  91. return iso88591;
  92. }
  93. return nullptr;
  94. }
  95. //===--------------------------- CHandle -------------------------------===//
  96. CHandle::CHandle(HANDLE h) { m_h = h; }
  97. CHandle::~CHandle() { CloseHandle(m_h); }
  98. CHandle::operator HANDLE() const throw() { return m_h; }
  99. #endif