ReactOS  0.4.15-dev-1177-g6cb3b62
CHeapPtrList.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4  * PURPOSE: Test for CHeapPtrList
5  * COPYRIGHT: Copyright 2020 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #ifdef HAVE_APITEST
9 #include <apitest.h>
10 #else
11 #include "atltest.h"
12 #endif
13 #include <atlcoll.h>
14 
15 static PDWORD
17 {
18  PDWORD ptr = (PDWORD)::CoTaskMemAlloc(sizeof(DWORD));
19  *ptr = value;
20  return ptr;
21 }
22 
23 // We use the CComAllocator, so we can easily spy on it
24 template <typename T>
26  public CHeapPtrList<T, CComAllocator>
27 {
28 public:
29  CComHeapPtrList(_In_ UINT nBlockSize = 10) throw()
30  :CHeapPtrList<T, CComAllocator>(nBlockSize)
31  {
32  }
33 };
34 
35 
36 
38 static LONG g_Reallocations = 0;
39 
41 {
43  {
44  if (IsEqualGUID(riid, IID_IMallocSpy))
45  {
46  *ppvObject = this;
47  }
48  return S_OK;
49  }
50 
51  virtual ULONG STDMETHODCALLTYPE AddRef() { return 1; }
52  virtual ULONG STDMETHODCALLTYPE Release() { return 1; }
53  virtual SIZE_T STDMETHODCALLTYPE PreAlloc(SIZE_T cbRequest) { return cbRequest; }
55  {
57  return pActual;
58  }
59  virtual LPVOID STDMETHODCALLTYPE PreFree(LPVOID pRequest, BOOL) { return pRequest; }
60  virtual void STDMETHODCALLTYPE PostFree(BOOL fSpyed)
61  {
62  if (fSpyed)
64  }
65  virtual SIZE_T STDMETHODCALLTYPE PreRealloc(LPVOID pRequest, SIZE_T cbRequest, LPVOID *ppNewRequest, BOOL)
66  {
67  *ppNewRequest = pRequest;
68  return cbRequest;
69  }
70  virtual LPVOID STDMETHODCALLTYPE PostRealloc(LPVOID pActual, BOOL fSpyed)
71  {
72  if (fSpyed)
74  return pActual;
75  }
76  virtual LPVOID STDMETHODCALLTYPE PreGetSize(LPVOID pRequest, BOOL) { return pRequest; }
77  virtual SIZE_T STDMETHODCALLTYPE PostGetSize(SIZE_T cbActual, BOOL) { return cbActual; }
78  virtual LPVOID STDMETHODCALLTYPE PreDidAlloc(LPVOID pRequest, BOOL) { return pRequest; }
79  virtual int STDMETHODCALLTYPE PostDidAlloc(LPVOID, BOOL, int fActual) { return fActual; }
82 };
83 
85 
86 
88 {
90  ok(SUCCEEDED(hr), "Expected CoRegisterMallocSpy to succeed, but it failed: 0x%lx\n", hr);
91 
92  {
93  ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
94  CComHeapPtrList<DWORD> heapPtr1;
95  ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
96  PDWORD Ptr = test_Alloc(0x11111111);
97  ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
99  ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
100  heapPtr1.AddTail(tmp);
101  ok(tmp.m_pData == NULL, "Expected m_pData to be transfered\n");
102  ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
103  Ptr = test_Alloc(0x22222222);
104  ok(g_OpenAllocations == 2, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
105 #if defined(_MSC_VER) && !defined(__clang__)
106  heapPtr1.AddTail(CComHeapPtr<DWORD>(Ptr));
107 #else
108  CComHeapPtr<DWORD> xxx(Ptr);
109  heapPtr1.AddTail(xxx);
110 #endif
111  ok(g_OpenAllocations == 2, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
112  }
113  ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
114 
115  hr = CoRevokeMallocSpy();
116  ok(SUCCEEDED(hr), "Expected CoRevokeMallocSpy to succeed, but it failed: 0x%lx\n", hr);
117 }
CComHeapPtrList(_In_ UINT nBlockSize=10)
#define REFIID
Definition: guiddef.h:118
virtual LPVOID STDMETHODCALLTYPE PostAlloc(LPVOID pActual)
HRESULT hr
Definition: shlfolder.c:183
static CHeapPtrListMallocSpy g_Spy
REFIID riid
Definition: precomp.h:44
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
virtual LPVOID STDMETHODCALLTYPE PreFree(LPVOID pRequest, BOOL)
#define T
Definition: mbstring.h:31
START_TEST(CHeapPtrList)
static LONG g_Reallocations
unsigned int BOOL
Definition: ntddk_ex.h:94
virtual void STDMETHODCALLTYPE PostFree(BOOL fSpyed)
long LONG
Definition: pedump.c:60
HRESULT WINAPI CoRevokeMallocSpy(void)
Definition: ifs.c:522
static PVOID ptr
Definition: dispmode.c:27
#define STDMETHODIMP
Definition: basetyps.h:43
virtual void STDMETHODCALLTYPE PostHeapMinimize()
smooth NULL
Definition: ftsmooth.c:416
virtual LPVOID STDMETHODCALLTYPE PreGetSize(LPVOID pRequest, BOOL)
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject)
virtual ULONG STDMETHODCALLTYPE AddRef()
virtual void STDMETHODCALLTYPE PreHeapMinimize()
LONG HRESULT
Definition: typedefs.h:79
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
unsigned long DWORD
Definition: ntddk_ex.h:95
virtual LPVOID STDMETHODCALLTYPE PostRealloc(LPVOID pActual, BOOL fSpyed)
virtual SIZE_T STDMETHODCALLTYPE PreAlloc(SIZE_T cbRequest)
#define InterlockedDecrement
Definition: armddk.h:52
virtual ULONG STDMETHODCALLTYPE Release()
static LONG g_OpenAllocations
REFIID LPVOID * ppvObject
Definition: precomp.h:44
GLsizei const GLfloat * value
Definition: glext.h:6069
#define _In_
Definition: no_sal2.h:204
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define S_OK
Definition: intsafe.h:51
#define InterlockedIncrement
Definition: armddk.h:53
virtual LPVOID STDMETHODCALLTYPE PreDidAlloc(LPVOID pRequest, BOOL)
static PDWORD test_Alloc(DWORD value)
#define ok(value,...)
Definition: atltest.h:57
HRESULT WINAPI CoRegisterMallocSpy(LPMALLOCSPY pMallocSpy)
Definition: ifs.c:482
unsigned int UINT
Definition: ndis.h:50
BOOL WINAPI IsEqualGUID(REFGUID rguid1, REFGUID rguid2)
Definition: compobj.c:4112
virtual int STDMETHODCALLTYPE PostDidAlloc(LPVOID, BOOL, int fActual)
DWORD * PDWORD
Definition: pedump.c:68
virtual SIZE_T STDMETHODCALLTYPE PreRealloc(LPVOID pRequest, SIZE_T cbRequest, LPVOID *ppNewRequest, BOOL)
unsigned int ULONG
Definition: retypes.h:1
LPVOID WINAPI CoTaskMemAlloc(SIZE_T size)
Definition: ifs.c:426
virtual SIZE_T STDMETHODCALLTYPE PostGetSize(SIZE_T cbActual, BOOL)
#define SUCCEEDED(hr)
Definition: intsafe.h:49