WinAdapter.cpp 2.9 KB

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