Skip to content

Commit

Permalink
WinAdapter: Add three missing virtual functions to IMalloc interface
Browse files Browse the repository at this point in the history
To prevent unexpected vtable breakage, add the missing functions from
the [documentation].  Note that they are listed in the wrong order, the
right order is retrieved from the `ObjIdl.h` header and implementations
for `IMalloc` in DirectXShaderCompiler.  All implementations are now
properly using the `override` keyword too, to enforce virtual method
existence in the base class.

[documentation]: https://docs.microsoft.com/en-us/windows/win32/api/objidl/nn-objidl-imalloc
  • Loading branch information
MarijnS95 authored and pow2clk committed Jan 24, 2022
1 parent e8fb0ce commit 5867ea9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
3 changes: 3 additions & 0 deletions include/dxc/Support/WinAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,9 @@ struct IMalloc : public IUnknown {
virtual void *Alloc(size_t size) = 0;
virtual void *Realloc(void *ptr, size_t size) = 0;
virtual void Free(void *ptr) = 0;
virtual size_t GetSize(void *pv) = 0;
virtual int DidAlloc(void *pv) = 0;
virtual void HeapMinimize(void) = 0;
};

CROSS_PLATFORM_UUIDOF(ISequentialStream, "0C733A30-2A1C-11CE-ADE5-00AA0044773D")
Expand Down
4 changes: 2 additions & 2 deletions lib/DxcSupport/FileIOHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ struct HeapMalloc final : public IMalloc {

SIZE_T STDMETHODCALLTYPE GetSize(
/* [annotation][in] */
_In_opt_ _Post_writable_byte_size_(return) void *pv)
_In_opt_ _Post_writable_byte_size_(return) void *pv) override
{
return HeapSize(GetProcessHeap(), 0, pv);
}

int STDMETHODCALLTYPE DidAlloc(
/* [annotation][in] */
_In_opt_ void *pv)
_In_opt_ void *pv) override
{
return -1; // don't know
}
Expand Down
3 changes: 3 additions & 0 deletions lib/DxcSupport/WinFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ struct CoMalloc final : public IMalloc {
void *STDMETHODCALLTYPE Alloc(size_t size) override { return malloc(size); }
void *STDMETHODCALLTYPE Realloc(void *ptr, size_t size) override { return realloc(ptr, size); }
void STDMETHODCALLTYPE Free(void *ptr) override { free(ptr); }
size_t STDMETHODCALLTYPE GetSize(void *pv) override { return -1; }
int STDMETHODCALLTYPE DidAlloc(void *pv) override { return -1; }
void STDMETHODCALLTYPE HeapMinimize(void) override {}

private:
DXC_MICROCOM_REF_FIELD(m_dwRef)
Expand Down
18 changes: 9 additions & 9 deletions tools/clang/unittests/HLSL/CompilerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2841,17 +2841,17 @@ struct InstrumentedHeapMalloc : public IMalloc {
m_FailAlloc = index;
}

ULONG STDMETHODCALLTYPE AddRef() {
ULONG STDMETHODCALLTYPE AddRef() override {
return ++m_RefCount;
}
ULONG STDMETHODCALLTYPE Release() {
ULONG STDMETHODCALLTYPE Release() override {
if (m_RefCount == 0) VERIFY_FAIL();
return --m_RefCount;
}
STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) {
STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) override {
return DoBasicQueryInterface<IMalloc>(this, iid, ppvObject);
}
virtual void *STDMETHODCALLTYPE Alloc(_In_ SIZE_T cb) {
virtual void *STDMETHODCALLTYPE Alloc(_In_ SIZE_T cb) override {
++m_AllocCount;
if (m_FailAlloc && m_AllocCount >= m_FailAlloc) {
return nullptr; // breakpoint for i failure - m_FailAlloc == 1+VAL
Expand All @@ -2875,7 +2875,7 @@ struct InstrumentedHeapMalloc : public IMalloc {
return P + 1;
}

virtual void *STDMETHODCALLTYPE Realloc(_In_opt_ void *pv, _In_ SIZE_T cb) {
virtual void *STDMETHODCALLTYPE Realloc(_In_opt_ void *pv, _In_ SIZE_T cb) override {
SIZE_T priorSize = pv == nullptr ? (SIZE_T)0 : GetSize(pv);
void *R = Alloc(cb);
if (!R)
Expand All @@ -2886,7 +2886,7 @@ struct InstrumentedHeapMalloc : public IMalloc {
return R;
}

virtual void STDMETHODCALLTYPE Free(_In_opt_ void *pv) {
virtual void STDMETHODCALLTYPE Free(_In_opt_ void *pv) override {
if (!pv)
return;
PtrData *P = DataFromPtr(pv);
Expand All @@ -2904,18 +2904,18 @@ struct InstrumentedHeapMalloc : public IMalloc {

virtual SIZE_T STDMETHODCALLTYPE GetSize(
/* [annotation][in] */
_In_opt_ _Post_writable_byte_size_(return) void *pv)
_In_opt_ _Post_writable_byte_size_(return) void *pv) override
{
if (pv == nullptr) return 0;
return DataFromPtr(pv)->Size;
}

virtual int STDMETHODCALLTYPE DidAlloc(
_In_opt_ void *pv) {
_In_opt_ void *pv) override {
return -1; // don't know
}

virtual void STDMETHODCALLTYPE HeapMinimize(void) {}
virtual void STDMETHODCALLTYPE HeapMinimize(void) override {}

void DumpLeaks() {
PtrData *ptr = (PtrData*)AllocList.Flink;;
Expand Down

0 comments on commit 5867ea9

Please sign in to comment.