···66 */
7788/* In case we are building against the MS headers, we need to disable assertions. */
99+#undef ATLASSERT
910#define ATLASSERT(x)
1011#define _ATL_NO_VARIANT_THROW
1112
+117
modules/rostests/apitests/atl/CHeapPtrList.cpp
···11+/*
22+ * PROJECT: ReactOS api tests
33+ * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
44+ * PURPOSE: Test for CHeapPtrList
55+ * COPYRIGHT: Copyright 2020 Mark Jansen (mark.jansen@reactos.org)
66+ */
77+88+#ifdef HAVE_APITEST
99+#include <apitest.h>
1010+#else
1111+#include "atltest.h"
1212+#endif
1313+#include <atlcoll.h>
1414+1515+static PDWORD
1616+test_Alloc(DWORD value)
1717+{
1818+ PDWORD ptr = (PDWORD)::CoTaskMemAlloc(sizeof(DWORD));
1919+ *ptr = value;
2020+ return ptr;
2121+}
2222+2323+// We use the CComAllocator, so we can easily spy on it
2424+template <typename T>
2525+class CComHeapPtrList :
2626+ public CHeapPtrList<T, CComAllocator>
2727+{
2828+public:
2929+ CComHeapPtrList(_In_ UINT nBlockSize = 10) throw()
3030+ :CHeapPtrList<T, CComAllocator>(nBlockSize)
3131+ {
3232+ }
3333+};
3434+3535+3636+3737+static LONG g_OpenAllocations = 0;
3838+static LONG g_Reallocations = 0;
3939+4040+struct CHeapPtrListMallocSpy : public IMallocSpy
4141+{
4242+ STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject)
4343+ {
4444+ if (IsEqualGUID(riid, IID_IMallocSpy))
4545+ {
4646+ *ppvObject = this;
4747+ }
4848+ return S_OK;
4949+ }
5050+5151+ virtual ULONG STDMETHODCALLTYPE AddRef() { return 1; }
5252+ virtual ULONG STDMETHODCALLTYPE Release() { return 1; }
5353+ virtual SIZE_T STDMETHODCALLTYPE PreAlloc(SIZE_T cbRequest) { return cbRequest; }
5454+ virtual LPVOID STDMETHODCALLTYPE PostAlloc(LPVOID pActual)
5555+ {
5656+ InterlockedIncrement(&g_OpenAllocations);
5757+ return pActual;
5858+ }
5959+ virtual LPVOID STDMETHODCALLTYPE PreFree(LPVOID pRequest, BOOL) { return pRequest; }
6060+ virtual void STDMETHODCALLTYPE PostFree(BOOL fSpyed)
6161+ {
6262+ if (fSpyed)
6363+ InterlockedDecrement(&g_OpenAllocations);
6464+ }
6565+ virtual SIZE_T STDMETHODCALLTYPE PreRealloc(LPVOID pRequest, SIZE_T cbRequest, LPVOID *ppNewRequest, BOOL)
6666+ {
6767+ *ppNewRequest = pRequest;
6868+ return cbRequest;
6969+ }
7070+ virtual LPVOID STDMETHODCALLTYPE PostRealloc(LPVOID pActual, BOOL fSpyed)
7171+ {
7272+ if (fSpyed)
7373+ InterlockedIncrement(&g_Reallocations);
7474+ return pActual;
7575+ }
7676+ virtual LPVOID STDMETHODCALLTYPE PreGetSize(LPVOID pRequest, BOOL) { return pRequest; }
7777+ virtual SIZE_T STDMETHODCALLTYPE PostGetSize(SIZE_T cbActual, BOOL) { return cbActual; }
7878+ virtual LPVOID STDMETHODCALLTYPE PreDidAlloc(LPVOID pRequest, BOOL) { return pRequest; }
7979+ virtual int STDMETHODCALLTYPE PostDidAlloc(LPVOID, BOOL, int fActual) { return fActual; }
8080+ virtual void STDMETHODCALLTYPE PreHeapMinimize() {}
8181+ virtual void STDMETHODCALLTYPE PostHeapMinimize() {}
8282+};
8383+8484+static CHeapPtrListMallocSpy g_Spy;
8585+8686+8787+START_TEST(CHeapPtrList)
8888+{
8989+ HRESULT hr = CoRegisterMallocSpy(&g_Spy);
9090+ ok(SUCCEEDED(hr), "Expected CoRegisterMallocSpy to succeed, but it failed: 0x%lx\n", hr);
9191+9292+ {
9393+ ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
9494+ CComHeapPtrList<DWORD> heapPtr1;
9595+ ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
9696+ PDWORD Ptr = test_Alloc(0x11111111);
9797+ ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
9898+ CComHeapPtr<DWORD> tmp(Ptr);
9999+ ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
100100+ heapPtr1.AddTail(tmp);
101101+ ok(tmp.m_pData == NULL, "Expected m_pData to be transfered\n");
102102+ ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
103103+ Ptr = test_Alloc(0x22222222);
104104+ ok(g_OpenAllocations == 2, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
105105+#ifdef _MSC_VER
106106+ heapPtr1.AddTail(CComHeapPtr<DWORD>(Ptr));
107107+#else
108108+ CComHeapPtr<DWORD> xxx(Ptr);
109109+ heapPtr1.AddTail(xxx);
110110+#endif
111111+ ok(g_OpenAllocations == 2, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
112112+ }
113113+ ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
114114+115115+ hr = CoRevokeMallocSpy();
116116+ ok(SUCCEEDED(hr), "Expected CoRevokeMallocSpy to succeed, but it failed: 0x%lx\n", hr);
117117+}