Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] WinAdapter: Export helper functions to interact with shimmed types #3265

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions include/dxc/Support/WinAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
#define C_ASSERT(expr) static_assert((expr), "")
#define ATLASSERT assert

#define CoTaskMemAlloc malloc
#define CoTaskMemFree free

#define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))

#define _countof(a) (sizeof(a) / sizeof(*(a)))
Expand Down Expand Up @@ -216,6 +213,10 @@
#define UInt32Add UIntAdd
#define Int32ToUInt32 IntToUInt

#ifndef DXC_API_IMPORT
#define DXC_API_IMPORT __attribute__((visibility("default")))
#endif

//===--------------------- HRESULT Related Macros -------------------------===//

#define S_OK ((HRESULT)0L)
Expand Down Expand Up @@ -935,9 +936,17 @@ class CHeapPtr : public CHeapPtrBase<T, Allocator> {

//===--------------------------- BSTR Allocation --------------------------===//

void SysFreeString(BSTR bstrString);
extern "C" DXC_API_IMPORT void __stdcall SysFreeString(BSTR bstrString);
// Allocate string with length prefix
BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui);
extern "C" DXC_API_IMPORT BSTR __stdcall SysAllocStringLen(const OLECHAR *strIn,
UINT ui);
extern "C" DXC_API_IMPORT UINT __stdcall SysStringByteLen(BSTR bstr);
extern "C" DXC_API_IMPORT UINT __stdcall SysStringLen(BSTR pbstr);

//===-------------------------- CoTask Allocation -------------------------===//

extern "C" DXC_API_IMPORT LPVOID __stdcall CoTaskMemAlloc(SIZE_T cb);
extern "C" DXC_API_IMPORT void __stdcall CoTaskMemFree(LPVOID pv);

//===--------------------- UTF-8 Related Types ----------------------------===//

Expand Down
21 changes: 19 additions & 2 deletions lib/DxcSupport/WinAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ void CAllocator::Free(void *p) throw() { free(p); }

//===--------------------------- BSTR Allocation --------------------------===//

void SysFreeString(BSTR bstrString) {
DXC_API_IMPORT void __stdcall SysFreeString(BSTR bstrString) {
if (bstrString)
free((void *)((uintptr_t)bstrString - sizeof(uint32_t)));
}

// Allocate string with length prefix
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr
BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui) {
DXC_API_IMPORT BSTR __stdcall SysAllocStringLen(const OLECHAR *strIn, UINT ui) {
uint32_t *blobOut =
(uint32_t *)malloc(sizeof(uint32_t) + (ui + 1) * sizeof(OLECHAR));

Expand All @@ -75,6 +75,23 @@ BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui) {
return strOut;
}

DXC_API_IMPORT UINT __stdcall SysStringByteLen(BSTR bstr) {
if (!bstr)
return 0;

return ((UINT *)bstr)[-1];
}

DXC_API_IMPORT UINT __stdcall SysStringLen(BSTR pbstr) {
return SysStringByteLen(pbstr) / 4;
}

//===-------------------------- CoTask Allocation -------------------------===//

DXC_API_IMPORT LPVOID __stdcall CoTaskMemAlloc(SIZE_T cb) { return malloc(cb); }

DXC_API_IMPORT void __stdcall CoTaskMemFree(LPVOID pv) { free(pv); }

//===---------------------- Char converstion ------------------------------===//

const char *CPToLocale(uint32_t CodePage) {
Expand Down
6 changes: 4 additions & 2 deletions tools/clang/tools/dxcompiler/dxcapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
// //
///////////////////////////////////////////////////////////////////////////////

#include "dxc/Support/WinIncludes.h"

#ifndef DXC_API_IMPORT
#ifdef _WIN32
#define DXC_API_IMPORT __declspec(dllexport)
#else
#define DXC_API_IMPORT __attribute__ ((visibility ("default")))
#endif
#endif

#include "dxc/Support/WinIncludes.h"

#include "dxc/dxcisense.h"
#include "dxc/dxctools.h"
Expand Down