4 * Copyright 1997 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(olemalloc);
39 /******************************************************************************
40 * IMalloc32 implementation
43 * For supporting CoRegisterMallocSpy the IMalloc implementation must know if
44 * a given memory block was allocated with a spy active.
46 *****************************************************************************/
47 /* set the vtable later */
48 static ICOM_VTABLE(IMalloc) VT_IMalloc32;
52 DWORD dummy; /* nothing, we are static */
53 IMallocSpy * pSpy; /* the spy when active */
54 DWORD SpyedAllocationsLeft; /* number of spyed allocations left */
55 BOOL SpyReleasePending; /* CoRevokeMallocSpy called with spyed allocations left*/
56 LPVOID * SpyedBlocks; /* root of the table */
57 int SpyedBlockTableLength; /* size of the table*/
60 /* this is the static object instance */
61 _Malloc32 Malloc32 = {&VT_IMalloc32, 0, NULL, 0, 0, NULL, 0};
63 /* with a spy active all calls from pre to post methods are threadsave */
64 static CRITICAL_SECTION IMalloc32_SpyCS;
65 static CRITICAL_SECTION_DEBUG critsect_debug =
67 0, 0, &IMalloc32_SpyCS,
68 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
69 0, 0, { 0, (DWORD)(__FILE__ ": IMalloc32_SpyCS") }
71 static CRITICAL_SECTION IMalloc32_SpyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
73 /* resize the old table */
74 static int SetSpyedBlockTableLength ( int NewLength )
76 LPVOID *NewSpyedBlocks;
78 if (!Malloc32.SpyedBlocks) NewSpyedBlocks = LocalAlloc(GMEM_ZEROINIT, NewLength);
79 else NewSpyedBlocks = LocalReAlloc(Malloc32.SpyedBlocks, NewLength, GMEM_ZEROINIT);
81 Malloc32.SpyedBlocks = NewSpyedBlocks;
82 Malloc32.SpyedBlockTableLength = NewLength;
85 return NewSpyedBlocks != NULL;
88 /* add a location to the table */
89 static int AddMemoryLocation(LPVOID * pMem)
93 /* allocate the table if not already allocated */
94 if (!Malloc32.SpyedBlockTableLength) {
95 if (!SetSpyedBlockTableLength(0x1000)) return 0;
98 /* find a free location */
99 Current = Malloc32.SpyedBlocks;
102 if (Current >= Malloc32.SpyedBlocks + Malloc32.SpyedBlockTableLength) {
103 /* no more space in table, grow it */
104 if (!SetSpyedBlockTableLength( Malloc32.SpyedBlockTableLength + 0x1000 )) return 0;
108 /* put the location in our table */
110 Malloc32.SpyedAllocationsLeft++;
111 /*TRACE("%lu\n",Malloc32.SpyedAllocationsLeft);*/
115 static int RemoveMemoryLocation(LPVOID * pMem)
119 /* allocate the table if not already allocated */
120 if (!Malloc32.SpyedBlockTableLength) {
121 if (!SetSpyedBlockTableLength(0x1000)) return 0;
124 Current = Malloc32.SpyedBlocks;
126 /* find the location */
127 while (*Current != pMem) {
129 if (Current >= Malloc32.SpyedBlocks + Malloc32.SpyedBlockTableLength) return 0; /* not found */
133 Malloc32.SpyedAllocationsLeft--;
134 /*TRACE("%lu\n",Malloc32.SpyedAllocationsLeft);*/
139 /******************************************************************************
140 * IMalloc32_QueryInterface [VTABLE]
142 static HRESULT WINAPI IMalloc_fnQueryInterface(LPMALLOC iface,REFIID refiid,LPVOID *obj) {
144 TRACE("(%s,%p)\n",debugstr_guid(refiid),obj);
146 if (IsEqualIID(&IID_IUnknown,refiid) || IsEqualIID(&IID_IMalloc,refiid)) {
147 *obj = (LPMALLOC)&Malloc32;
150 return E_NOINTERFACE;
153 /******************************************************************************
154 * IMalloc32_AddRefRelease [VTABLE]
156 static ULONG WINAPI IMalloc_fnAddRefRelease (LPMALLOC iface) {
160 /******************************************************************************
161 * IMalloc32_Alloc [VTABLE]
163 static LPVOID WINAPI IMalloc_fnAlloc(LPMALLOC iface, DWORD cb) {
170 DWORD preAllocResult;
172 EnterCriticalSection(&IMalloc32_SpyCS);
173 preAllocResult = IMallocSpy_PreAlloc(Malloc32.pSpy, cb);
174 if ((cb != 0) && (preAllocResult == 0)) {
175 /* PreAlloc can force Alloc to fail, but not if cb == 0 */
176 TRACE("returning null\n");
177 LeaveCriticalSection(&IMalloc32_SpyCS);
182 addr = HeapAlloc(GetProcessHeap(),0,cb);
185 addr = IMallocSpy_PostAlloc(Malloc32.pSpy, addr);
186 if (addr) AddMemoryLocation(addr);
187 LeaveCriticalSection(&IMalloc32_SpyCS);
190 TRACE("--(%p)\n",addr);
194 /******************************************************************************
195 * IMalloc32_Realloc [VTABLE]
197 static LPVOID WINAPI IMalloc_fnRealloc(LPMALLOC iface,LPVOID pv,DWORD cb) {
201 TRACE("(%p,%ld)\n",pv,cb);
207 EnterCriticalSection(&IMalloc32_SpyCS);
208 fSpyed = RemoveMemoryLocation(pv);
209 cb = IMallocSpy_PreRealloc(Malloc32.pSpy, pv, cb, &pRealMemory, fSpyed);
211 /* check if can release the spy */
212 if(Malloc32.SpyReleasePending && !Malloc32.SpyedAllocationsLeft) {
213 IMallocSpy_Release(Malloc32.pSpy);
214 Malloc32.SpyReleasePending = FALSE;
215 Malloc32.pSpy = NULL;
219 /* PreRealloc can force Realloc to fail */
220 LeaveCriticalSection(&IMalloc32_SpyCS);
226 if (!pv) pNewMemory = HeapAlloc(GetProcessHeap(),0,cb);
227 else if (cb) pNewMemory = HeapReAlloc(GetProcessHeap(),0,pv,cb);
229 HeapFree(GetProcessHeap(),0,pv);
234 pNewMemory = IMallocSpy_PostRealloc(Malloc32.pSpy, pNewMemory, TRUE);
235 if (pNewMemory) AddMemoryLocation(pNewMemory);
236 LeaveCriticalSection(&IMalloc32_SpyCS);
239 TRACE("--(%p)\n",pNewMemory);
243 /******************************************************************************
244 * IMalloc32_Free [VTABLE]
246 static VOID WINAPI IMalloc_fnFree(LPMALLOC iface,LPVOID pv) {
253 EnterCriticalSection(&IMalloc32_SpyCS);
254 fSpyed = RemoveMemoryLocation(pv);
255 pv = IMallocSpy_PreFree(Malloc32.pSpy, pv, fSpyed);
258 HeapFree(GetProcessHeap(),0,pv);
261 IMallocSpy_PostFree(Malloc32.pSpy, fSpyed);
263 /* check if can release the spy */
264 if(Malloc32.SpyReleasePending && !Malloc32.SpyedAllocationsLeft) {
265 IMallocSpy_Release(Malloc32.pSpy);
266 Malloc32.SpyReleasePending = FALSE;
267 Malloc32.pSpy = NULL;
270 LeaveCriticalSection(&IMalloc32_SpyCS);
274 /******************************************************************************
275 * IMalloc32_GetSize [VTABLE]
279 * win95: size allocated (4 byte boundarys)
280 * win2k: size originally requested !!! (allocated on 8 byte boundarys)
282 static DWORD WINAPI IMalloc_fnGetSize(LPMALLOC iface,LPVOID pv) {
290 EnterCriticalSection(&IMalloc32_SpyCS);
291 pv = IMallocSpy_PreGetSize(Malloc32.pSpy, pv, fSpyed);
294 cb = HeapSize(GetProcessHeap(),0,pv);
297 cb = IMallocSpy_PostGetSize(Malloc32.pSpy, cb, fSpyed);
298 LeaveCriticalSection(&IMalloc32_SpyCS);
304 /******************************************************************************
305 * IMalloc32_DidAlloc [VTABLE]
307 static INT WINAPI IMalloc_fnDidAlloc(LPMALLOC iface,LPVOID pv) {
315 EnterCriticalSection(&IMalloc32_SpyCS);
316 pv = IMallocSpy_PreDidAlloc(Malloc32.pSpy, pv, fSpyed);
322 didAlloc = IMallocSpy_PostDidAlloc(Malloc32.pSpy, pv, fSpyed, didAlloc);
323 LeaveCriticalSection(&IMalloc32_SpyCS);
328 /******************************************************************************
329 * IMalloc32_HeapMinimize [VTABLE]
331 static VOID WINAPI IMalloc_fnHeapMinimize(LPMALLOC iface) {
335 EnterCriticalSection(&IMalloc32_SpyCS);
336 IMallocSpy_PreHeapMinimize(Malloc32.pSpy);
340 IMallocSpy_PostHeapMinimize(Malloc32.pSpy);
341 LeaveCriticalSection(&IMalloc32_SpyCS);
345 static ICOM_VTABLE(IMalloc) VT_IMalloc32 =
347 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
348 IMalloc_fnQueryInterface,
349 IMalloc_fnAddRefRelease,
350 IMalloc_fnAddRefRelease,
356 IMalloc_fnHeapMinimize
359 /******************************************************************************
360 * IMallocSpy implementation
361 *****************************************************************************/
363 /* set the vtable later */
364 static ICOM_VTABLE(IMallocSpy) VT_IMallocSpy;
367 ICOM_VFIELD(IMallocSpy);
371 /* this is the static object instance */
372 _MallocSpy MallocSpy = {&VT_IMallocSpy, 0};
374 /******************************************************************************
375 * IMalloc32_QueryInterface [VTABLE]
377 static HRESULT WINAPI IMallocSpy_fnQueryInterface(LPMALLOCSPY iface,REFIID refiid,LPVOID *obj)
380 TRACE("(%s,%p)\n",debugstr_guid(refiid),obj);
382 if (IsEqualIID(&IID_IUnknown,refiid) || IsEqualIID(&IID_IMallocSpy,refiid)) {
383 *obj = (LPMALLOC)&MallocSpy;
386 return E_NOINTERFACE;
389 /******************************************************************************
390 * IMalloc32_AddRef [VTABLE]
392 static ULONG WINAPI IMallocSpy_fnAddRef (LPMALLOCSPY iface)
395 ICOM_THIS (_MallocSpy, iface);
397 TRACE ("(%p)->(count=%lu)\n", This, This->ref);
399 return ++(This->ref);
402 /******************************************************************************
403 * IMalloc32_AddRelease [VTABLE]
406 * Our MallocSpy is static. If the count reaches 0 we dump the leaks
408 static ULONG WINAPI IMallocSpy_fnRelease (LPMALLOCSPY iface)
411 ICOM_THIS (_MallocSpy, iface);
413 TRACE ("(%p)->(count=%lu)\n", This, This->ref);
415 if (!--(This->ref)) {
416 /* our allocation list MUST be empty here */
421 static ULONG WINAPI IMallocSpy_fnPreAlloc(LPMALLOCSPY iface, ULONG cbRequest)
423 ICOM_THIS (_MallocSpy, iface);
424 TRACE ("(%p)->(%lu)\n", This, cbRequest);
427 static PVOID WINAPI IMallocSpy_fnPostAlloc(LPMALLOCSPY iface, void* pActual)
429 ICOM_THIS (_MallocSpy, iface);
430 TRACE ("(%p)->(%p)\n", This, pActual);
434 static PVOID WINAPI IMallocSpy_fnPreFree(LPMALLOCSPY iface, void* pRequest, BOOL fSpyed)
436 ICOM_THIS (_MallocSpy, iface);
437 TRACE ("(%p)->(%p %u)\n", This, pRequest, fSpyed);
440 static void WINAPI IMallocSpy_fnPostFree(LPMALLOCSPY iface, BOOL fSpyed)
442 ICOM_THIS (_MallocSpy, iface);
443 TRACE ("(%p)->(%u)\n", This, fSpyed);
446 static ULONG WINAPI IMallocSpy_fnPreRealloc(LPMALLOCSPY iface, void* pRequest, ULONG cbRequest, void** ppNewRequest, BOOL fSpyed)
448 ICOM_THIS (_MallocSpy, iface);
449 TRACE ("(%p)->(%p %lu %u)\n", This, pRequest, cbRequest, fSpyed);
450 *ppNewRequest = pRequest;
454 static PVOID WINAPI IMallocSpy_fnPostRealloc(LPMALLOCSPY iface, void* pActual, BOOL fSpyed)
456 ICOM_THIS (_MallocSpy, iface);
457 TRACE ("(%p)->(%p %u)\n", This, pActual, fSpyed);
461 static PVOID WINAPI IMallocSpy_fnPreGetSize(LPMALLOCSPY iface, void* pRequest, BOOL fSpyed)
463 ICOM_THIS (_MallocSpy, iface);
464 TRACE ("(%p)->(%p %u)\n", This, pRequest, fSpyed);
468 static ULONG WINAPI IMallocSpy_fnPostGetSize(LPMALLOCSPY iface, ULONG cbActual, BOOL fSpyed)
470 ICOM_THIS (_MallocSpy, iface);
471 TRACE ("(%p)->(%lu %u)\n", This, cbActual, fSpyed);
475 static PVOID WINAPI IMallocSpy_fnPreDidAlloc(LPMALLOCSPY iface, void* pRequest, BOOL fSpyed)
477 ICOM_THIS (_MallocSpy, iface);
478 TRACE ("(%p)->(%p %u)\n", This, pRequest, fSpyed);
482 static int WINAPI IMallocSpy_fnPostDidAlloc(LPMALLOCSPY iface, void* pRequest, BOOL fSpyed, int fActual)
484 ICOM_THIS (_MallocSpy, iface);
485 TRACE ("(%p)->(%p %u %u)\n", This, pRequest, fSpyed, fActual);
489 static void WINAPI IMallocSpy_fnPreHeapMinimize(LPMALLOCSPY iface)
491 ICOM_THIS (_MallocSpy, iface);
492 TRACE ("(%p)->()\n", This);
495 static void WINAPI IMallocSpy_fnPostHeapMinimize(LPMALLOCSPY iface)
497 ICOM_THIS (_MallocSpy, iface);
498 TRACE ("(%p)->()\n", This);
501 static void MallocSpyDumpLeaks() {
502 TRACE("leaks: %lu\n", Malloc32.SpyedAllocationsLeft);
505 static ICOM_VTABLE(IMallocSpy) VT_IMallocSpy =
507 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
508 IMallocSpy_fnQueryInterface,
510 IMallocSpy_fnRelease,
511 IMallocSpy_fnPreAlloc,
512 IMallocSpy_fnPostAlloc,
513 IMallocSpy_fnPreFree,
514 IMallocSpy_fnPostFree,
515 IMallocSpy_fnPreRealloc,
516 IMallocSpy_fnPostRealloc,
517 IMallocSpy_fnPreGetSize,
518 IMallocSpy_fnPostGetSize,
519 IMallocSpy_fnPreDidAlloc,
520 IMallocSpy_fnPostDidAlloc,
521 IMallocSpy_fnPreHeapMinimize,
522 IMallocSpy_fnPostHeapMinimize
525 /******************************************************************************
526 * CoGetMalloc [OLE32.@]
531 HRESULT WINAPI CoGetMalloc(DWORD dwMemContext, LPMALLOC *lpMalloc)
533 *lpMalloc = (LPMALLOC)&Malloc32;
537 /***********************************************************************
538 * CoTaskMemAlloc [OLE32.@]
540 * pointer to newly allocated block
542 LPVOID WINAPI CoTaskMemAlloc(ULONG size)
544 return IMalloc_Alloc((LPMALLOC)&Malloc32,size);
546 /***********************************************************************
547 * CoTaskMemFree [OLE32.@]
549 VOID WINAPI CoTaskMemFree(LPVOID ptr)
551 IMalloc_Free((LPMALLOC)&Malloc32, ptr);
554 /***********************************************************************
555 * CoTaskMemRealloc [OLE32.@]
557 * pointer to newly allocated block
559 LPVOID WINAPI CoTaskMemRealloc(LPVOID pvOld, ULONG size)
561 return IMalloc_Realloc((LPMALLOC)&Malloc32, pvOld, size);
564 /***********************************************************************
565 * CoRegisterMallocSpy [OLE32.@]
568 * if a mallocspy is already registered, we can't do it again since
569 * only the spy knows, how to free a memory block
571 HRESULT WINAPI CoRegisterMallocSpy(LPMALLOCSPY pMallocSpy)
574 HRESULT hres = E_INVALIDARG;
578 /* HACK TO ACTIVATE OUT SPY */
579 if (pMallocSpy == (LPVOID)-1) pMallocSpy =(IMallocSpy*)&MallocSpy;
581 if(Malloc32.pSpy) return CO_E_OBJISREG;
583 EnterCriticalSection(&IMalloc32_SpyCS);
585 if (SUCCEEDED(IUnknown_QueryInterface(pMallocSpy, &IID_IMallocSpy, (LPVOID*)&pSpy))) {
586 Malloc32.pSpy = pSpy;
590 LeaveCriticalSection(&IMalloc32_SpyCS);
595 /***********************************************************************
596 * CoRevokeMallocSpy [OLE32.@]
599 * we can't revoke a malloc spy as long as memory blocks allocated with
600 * the spy are active since only the spy knows how to free them
602 HRESULT WINAPI CoRevokeMallocSpy(void)
607 EnterCriticalSection(&IMalloc32_SpyCS);
609 /* if it's our spy it's time to dump the leaks */
610 if (Malloc32.pSpy == (IMallocSpy*)&MallocSpy) {
611 MallocSpyDumpLeaks();
614 if (Malloc32.SpyedAllocationsLeft) {
615 TRACE("SpyReleasePending with %lu allocations left\n", Malloc32.SpyedAllocationsLeft);
616 Malloc32.SpyReleasePending = TRUE;
617 hres = E_ACCESSDENIED;
619 IMallocSpy_Release(Malloc32.pSpy);
620 Malloc32.pSpy = NULL;
622 LeaveCriticalSection(&IMalloc32_SpyCS);
627 /******************************************************************************
628 * IsValidInterface [OLE32.@]
631 * True, if the passed pointer is a valid interface
633 BOOL WINAPI IsValidInterface(
634 LPUNKNOWN punk /* [in] interface to be tested */
637 IsBadReadPtr(punk,4) ||
638 IsBadReadPtr(punk->lpVtbl,4) ||
639 IsBadReadPtr(punk->lpVtbl->QueryInterface,9) ||
640 IsBadCodePtr((FARPROC)punk->lpVtbl->QueryInterface)