/****************************************************************************** * IMalloc32_Free [VTABLE] */ static VOID WINAPI IMalloc_fnFree(LPMALLOC iface,LPVOID pv) { BOOL fSpyed = 0; TRACE("(%p)\n",pv); if(Malloc32.pSpy) { EnterCriticalSection(&IMalloc32_SpyCS); fSpyed = RemoveMemoryLocation(pv); pv = IMallocSpy_PreFree(Malloc32.pSpy, pv, fSpyed); } HeapFree(GetProcessHeap(),0,pv); if(Malloc32.pSpy) { IMallocSpy_PostFree(Malloc32.pSpy, fSpyed); /* check if can release the spy */ if(Malloc32.SpyReleasePending && !Malloc32.SpyedAllocationsLeft) { IMallocSpy_Release(Malloc32.pSpy); Malloc32.SpyReleasePending = FALSE; Malloc32.pSpy = NULL; } LeaveCriticalSection(&IMalloc32_SpyCS); } }
/****************************************************************************** * IMalloc32_Realloc [VTABLE] */ static LPVOID WINAPI IMalloc_fnRealloc(LPMALLOC iface,LPVOID pv,DWORD cb) { LPVOID pNewMemory; TRACE("(%p,%d)\n",pv,cb); if(Malloc32.pSpy) { LPVOID pRealMemory; BOOL fSpyed; EnterCriticalSection(&IMalloc32_SpyCS); fSpyed = RemoveMemoryLocation(pv); cb = IMallocSpy_PreRealloc(Malloc32.pSpy, pv, cb, &pRealMemory, fSpyed); /* check if can release the spy */ if(Malloc32.SpyReleasePending && !Malloc32.SpyedAllocationsLeft) { IMallocSpy_Release(Malloc32.pSpy); Malloc32.SpyReleasePending = FALSE; Malloc32.pSpy = NULL; LeaveCriticalSection(&IMalloc32_SpyCS); } if (0==cb) { /* PreRealloc can force Realloc to fail */ if (Malloc32.pSpy) LeaveCriticalSection(&IMalloc32_SpyCS); return NULL; } pv = pRealMemory; } if (!pv) pNewMemory = HeapAlloc(GetProcessHeap(),0,cb); else if (cb) pNewMemory = HeapReAlloc(GetProcessHeap(),0,pv,cb); else { HeapFree(GetProcessHeap(),0,pv); pNewMemory = NULL; } if(Malloc32.pSpy) { pNewMemory = IMallocSpy_PostRealloc(Malloc32.pSpy, pNewMemory, TRUE); if (pNewMemory) AddMemoryLocation(pNewMemory); LeaveCriticalSection(&IMalloc32_SpyCS); } TRACE("--(%p)\n",pNewMemory); return pNewMemory; }
/*********************************************************************** * CoRevokeMallocSpy [OLE32.@] * * Revokes a previously registered object that receives notifications on memory * allocations and frees. * * PARAMS * pMallocSpy [I] New spy object. * * RETURNS * Success: S_OK. * Failure: HRESULT code. * * NOTES * we can't revoke a malloc spy as long as memory blocks allocated with * the spy are active since only the spy knows how to free them */ HRESULT WINAPI CoRevokeMallocSpy(void) { HRESULT hres = S_OK; TRACE("\n"); EnterCriticalSection(&IMalloc32_SpyCS); if (Malloc32.SpyedAllocationsLeft) { TRACE("SpyReleasePending with %u allocations left\n", Malloc32.SpyedAllocationsLeft); Malloc32.SpyReleasePending = TRUE; hres = E_ACCESSDENIED; } else { IMallocSpy_Release(Malloc32.pSpy); Malloc32.pSpy = NULL; } LeaveCriticalSection(&IMalloc32_SpyCS); return hres; }