2005-07-31 12:11:56 +00:00
|
|
|
/*
|
|
|
|
* basic interfaces
|
|
|
|
*
|
|
|
|
* Copyright 1997 Marcus Meissner
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2007-04-20 12:23:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-07-31 12:11:56 +00:00
|
|
|
*/
|
|
|
|
|
2018-03-20 11:38:17 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define COBJMACROS
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winuser.h"
|
|
|
|
#include "ole2.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
2005-07-31 12:11:56 +00:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(olemalloc);
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IMalloc32 implementation
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* For supporting CoRegisterMallocSpy the IMalloc implementation must know if
|
|
|
|
* a given memory block was allocated with a spy active.
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/* set the vtable later */
|
2005-08-03 22:31:39 +00:00
|
|
|
static const IMallocVtbl VT_IMalloc32;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2012-05-17 15:16:51 +00:00
|
|
|
IMalloc IMalloc_iface;
|
2005-07-31 12:11:56 +00:00
|
|
|
DWORD dummy; /* nothing, we are static */
|
|
|
|
IMallocSpy * pSpy; /* the spy when active */
|
|
|
|
DWORD SpyedAllocationsLeft; /* number of spyed allocations left */
|
|
|
|
BOOL SpyReleasePending; /* CoRevokeMallocSpy called with spyed allocations left*/
|
|
|
|
LPVOID * SpyedBlocks; /* root of the table */
|
2007-07-27 09:49:52 +00:00
|
|
|
DWORD SpyedBlockTableLength;/* size of the table*/
|
2005-07-31 12:11:56 +00:00
|
|
|
} _Malloc32;
|
|
|
|
|
|
|
|
/* this is the static object instance */
|
2012-05-17 15:16:51 +00:00
|
|
|
static _Malloc32 Malloc32 = {{&VT_IMalloc32}, 0, NULL, 0, 0, NULL, 0};
|
2005-07-31 12:11:56 +00:00
|
|
|
|
|
|
|
/* with a spy active all calls from pre to post methods are threadsave */
|
|
|
|
static CRITICAL_SECTION IMalloc32_SpyCS;
|
|
|
|
static CRITICAL_SECTION_DEBUG critsect_debug =
|
|
|
|
{
|
|
|
|
0, 0, &IMalloc32_SpyCS,
|
|
|
|
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
|
2005-11-20 15:01:10 +00:00
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": IMalloc32_SpyCS") }
|
2005-07-31 12:11:56 +00:00
|
|
|
};
|
|
|
|
static CRITICAL_SECTION IMalloc32_SpyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
/* resize the old table */
|
2014-04-24 12:14:13 +00:00
|
|
|
static BOOL SetSpyedBlockTableLength ( DWORD NewLength )
|
2005-07-31 12:11:56 +00:00
|
|
|
{
|
|
|
|
LPVOID *NewSpyedBlocks;
|
|
|
|
|
|
|
|
if (!Malloc32.SpyedBlocks) NewSpyedBlocks = LocalAlloc(LMEM_ZEROINIT, NewLength * sizeof(PVOID));
|
2013-09-26 13:58:03 +00:00
|
|
|
else NewSpyedBlocks = LocalReAlloc(Malloc32.SpyedBlocks, NewLength * sizeof(PVOID), LMEM_ZEROINIT | LMEM_MOVEABLE);
|
2005-07-31 12:11:56 +00:00
|
|
|
if (NewSpyedBlocks) {
|
|
|
|
Malloc32.SpyedBlocks = NewSpyedBlocks;
|
|
|
|
Malloc32.SpyedBlockTableLength = NewLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewSpyedBlocks != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a location to the table */
|
2014-04-24 12:14:13 +00:00
|
|
|
static BOOL AddMemoryLocation(LPVOID * pMem)
|
2005-07-31 12:11:56 +00:00
|
|
|
{
|
|
|
|
LPVOID * Current;
|
|
|
|
|
|
|
|
/* allocate the table if not already allocated */
|
2014-04-24 12:14:13 +00:00
|
|
|
if (!Malloc32.SpyedBlockTableLength && !SetSpyedBlockTableLength(0x1000))
|
|
|
|
return FALSE;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
|
|
|
/* find a free location */
|
|
|
|
Current = Malloc32.SpyedBlocks;
|
|
|
|
while (*Current) {
|
|
|
|
Current++;
|
|
|
|
if (Current >= Malloc32.SpyedBlocks + Malloc32.SpyedBlockTableLength) {
|
|
|
|
/* no more space in table, grow it */
|
2007-07-27 09:49:52 +00:00
|
|
|
DWORD old_length = Malloc32.SpyedBlockTableLength;
|
2014-04-24 12:14:13 +00:00
|
|
|
if (!SetSpyedBlockTableLength( Malloc32.SpyedBlockTableLength + 0x1000))
|
|
|
|
return FALSE;
|
2007-07-27 09:49:52 +00:00
|
|
|
Current = Malloc32.SpyedBlocks + old_length;
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* put the location in our table */
|
|
|
|
*Current = pMem;
|
|
|
|
Malloc32.SpyedAllocationsLeft++;
|
|
|
|
/*TRACE("%lu\n",Malloc32.SpyedAllocationsLeft);*/
|
2014-04-24 12:14:13 +00:00
|
|
|
return TRUE;
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 13:01:19 +00:00
|
|
|
static void** mallocspy_is_allocation_spyed(const void *mem)
|
|
|
|
{
|
|
|
|
void **current = Malloc32.SpyedBlocks;
|
|
|
|
|
|
|
|
while (*current != mem)
|
|
|
|
{
|
|
|
|
current++;
|
|
|
|
if (current >= Malloc32.SpyedBlocks + Malloc32.SpyedBlockTableLength)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2014-04-24 12:14:13 +00:00
|
|
|
static BOOL RemoveMemoryLocation(LPCVOID pMem)
|
2005-07-31 12:11:56 +00:00
|
|
|
{
|
|
|
|
LPVOID * Current;
|
|
|
|
|
|
|
|
/* allocate the table if not already allocated */
|
2014-04-24 12:14:13 +00:00
|
|
|
if (!Malloc32.SpyedBlockTableLength && !SetSpyedBlockTableLength(0x1000))
|
|
|
|
return FALSE;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
2020-05-01 13:01:19 +00:00
|
|
|
if (!(Current = mallocspy_is_allocation_spyed(pMem)))
|
|
|
|
return FALSE;
|
|
|
|
|
2005-07-31 12:11:56 +00:00
|
|
|
/* location found */
|
|
|
|
Malloc32.SpyedAllocationsLeft--;
|
|
|
|
/*TRACE("%lu\n",Malloc32.SpyedAllocationsLeft);*/
|
|
|
|
*Current = NULL;
|
2014-04-24 12:14:13 +00:00
|
|
|
return TRUE;
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IMalloc32_QueryInterface [VTABLE]
|
|
|
|
*/
|
2016-03-04 09:25:52 +00:00
|
|
|
static HRESULT WINAPI IMalloc_fnQueryInterface(IMalloc *iface, REFIID refiid, void **obj)
|
|
|
|
{
|
2005-07-31 12:11:56 +00:00
|
|
|
TRACE("(%s,%p)\n",debugstr_guid(refiid),obj);
|
|
|
|
|
|
|
|
if (IsEqualIID(&IID_IUnknown,refiid) || IsEqualIID(&IID_IMalloc,refiid)) {
|
2009-03-03 09:12:43 +00:00
|
|
|
*obj = &Malloc32;
|
2005-07-31 12:11:56 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IMalloc32_AddRefRelease [VTABLE]
|
|
|
|
*/
|
2016-03-04 09:25:52 +00:00
|
|
|
static ULONG WINAPI IMalloc_fnAddRefRelease(IMalloc *iface)
|
|
|
|
{
|
2005-07-31 12:11:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IMalloc32_Alloc [VTABLE]
|
|
|
|
*/
|
2016-03-04 09:25:52 +00:00
|
|
|
static void * WINAPI IMalloc_fnAlloc(IMalloc *iface, SIZE_T cb)
|
|
|
|
{
|
|
|
|
void *addr;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
2016-03-04 09:25:52 +00:00
|
|
|
TRACE("(%ld)\n",cb);
|
2005-07-31 12:11:56 +00:00
|
|
|
|
|
|
|
if(Malloc32.pSpy) {
|
2016-03-04 09:25:52 +00:00
|
|
|
SIZE_T preAllocResult;
|
2008-01-14 15:45:45 +00:00
|
|
|
|
2005-07-31 12:11:56 +00:00
|
|
|
EnterCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
preAllocResult = IMallocSpy_PreAlloc(Malloc32.pSpy, cb);
|
|
|
|
if ((cb != 0) && (preAllocResult == 0)) {
|
|
|
|
/* PreAlloc can force Alloc to fail, but not if cb == 0 */
|
|
|
|
TRACE("returning null\n");
|
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-01-14 15:45:45 +00:00
|
|
|
|
2005-07-31 12:11:56 +00:00
|
|
|
addr = HeapAlloc(GetProcessHeap(),0,cb);
|
|
|
|
|
|
|
|
if(Malloc32.pSpy) {
|
|
|
|
addr = IMallocSpy_PostAlloc(Malloc32.pSpy, addr);
|
|
|
|
if (addr) AddMemoryLocation(addr);
|
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("--(%p)\n",addr);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IMalloc32_Realloc [VTABLE]
|
|
|
|
*/
|
2016-03-04 09:25:52 +00:00
|
|
|
static void * WINAPI IMalloc_fnRealloc(IMalloc *iface, void *pv, SIZE_T cb)
|
|
|
|
{
|
|
|
|
void *pNewMemory;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
2016-03-04 09:25:52 +00:00
|
|
|
TRACE("(%p,%ld)\n",pv,cb);
|
2005-07-31 12:11:56 +00:00
|
|
|
|
|
|
|
if(Malloc32.pSpy) {
|
2016-03-04 09:25:52 +00:00
|
|
|
void *pRealMemory;
|
2005-07-31 12:11:56 +00:00
|
|
|
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;
|
2013-09-26 13:58:03 +00:00
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0==cb) {
|
2013-09-26 13:58:03 +00:00
|
|
|
/* PreRealloc can force Realloc to fail */
|
|
|
|
if (Malloc32.pSpy)
|
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
2005-07-31 12:11:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-09-26 13:58:03 +00:00
|
|
|
|
2005-07-31 12:11:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IMalloc32_Free [VTABLE]
|
|
|
|
*/
|
2016-03-04 09:25:52 +00:00
|
|
|
static void WINAPI IMalloc_fnFree(IMalloc *iface, void *pv)
|
|
|
|
{
|
2014-04-24 12:14:13 +00:00
|
|
|
BOOL fSpyed = FALSE;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
|
|
|
TRACE("(%p)\n",pv);
|
|
|
|
|
2016-04-09 08:31:49 +00:00
|
|
|
if(!pv)
|
|
|
|
return;
|
|
|
|
|
2005-07-31 12:11:56 +00:00
|
|
|
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_GetSize [VTABLE]
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* FIXME returns:
|
|
|
|
* win95: size allocated (4 byte boundarys)
|
|
|
|
* win2k: size originally requested !!! (allocated on 8 byte boundarys)
|
|
|
|
*/
|
2020-05-01 13:01:19 +00:00
|
|
|
static SIZE_T WINAPI IMalloc_fnGetSize(IMalloc *iface, void *mem)
|
2016-03-04 09:25:52 +00:00
|
|
|
{
|
2020-05-01 13:01:19 +00:00
|
|
|
BOOL spyed_block = FALSE, spy_active = FALSE;
|
|
|
|
SIZE_T size;
|
|
|
|
|
|
|
|
TRACE("(%p)\n", mem);
|
|
|
|
|
|
|
|
if (!mem)
|
|
|
|
return (SIZE_T)-1;
|
|
|
|
|
|
|
|
if (Malloc32.pSpy)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
spyed_block = !!mallocspy_is_allocation_spyed(mem);
|
|
|
|
spy_active = TRUE;
|
|
|
|
mem = IMallocSpy_PreGetSize(Malloc32.pSpy, mem, spyed_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
size = HeapSize(GetProcessHeap(), 0, mem);
|
|
|
|
|
|
|
|
if (spy_active)
|
|
|
|
{
|
|
|
|
size = IMallocSpy_PostGetSize(Malloc32.pSpy, size, spyed_block);
|
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
}
|
|
|
|
return size;
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IMalloc32_DidAlloc [VTABLE]
|
|
|
|
*/
|
2020-05-01 13:01:19 +00:00
|
|
|
static INT WINAPI IMalloc_fnDidAlloc(IMalloc *iface, void *mem)
|
2016-03-04 09:25:52 +00:00
|
|
|
{
|
2020-05-01 13:01:19 +00:00
|
|
|
BOOL spyed_block = FALSE, spy_active = FALSE;
|
|
|
|
int did_alloc;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
2020-05-01 13:01:19 +00:00
|
|
|
TRACE("(%p)\n", mem);
|
2005-07-31 12:11:56 +00:00
|
|
|
|
2020-05-01 13:01:19 +00:00
|
|
|
if (!mem)
|
|
|
|
return -1;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
2020-05-01 13:01:19 +00:00
|
|
|
if (Malloc32.pSpy)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
spyed_block = !!mallocspy_is_allocation_spyed(mem);
|
|
|
|
spy_active = TRUE;
|
|
|
|
mem = IMallocSpy_PreDidAlloc(Malloc32.pSpy, mem, spyed_block);
|
|
|
|
}
|
2005-07-31 12:11:56 +00:00
|
|
|
|
2020-05-01 13:01:19 +00:00
|
|
|
did_alloc = HeapValidate(GetProcessHeap(), 0, mem);
|
|
|
|
|
|
|
|
if (spy_active)
|
|
|
|
{
|
|
|
|
did_alloc = IMallocSpy_PostDidAlloc(Malloc32.pSpy, mem, spyed_block, did_alloc);
|
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return did_alloc;
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IMalloc32_HeapMinimize [VTABLE]
|
|
|
|
*/
|
2016-03-04 09:25:52 +00:00
|
|
|
static void WINAPI IMalloc_fnHeapMinimize(IMalloc *iface)
|
|
|
|
{
|
2005-07-31 12:11:56 +00:00
|
|
|
TRACE("()\n");
|
|
|
|
|
|
|
|
if(Malloc32.pSpy) {
|
|
|
|
EnterCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
IMallocSpy_PreHeapMinimize(Malloc32.pSpy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Malloc32.pSpy) {
|
|
|
|
IMallocSpy_PostHeapMinimize(Malloc32.pSpy);
|
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-03 22:31:39 +00:00
|
|
|
static const IMallocVtbl VT_IMalloc32 =
|
2005-07-31 12:11:56 +00:00
|
|
|
{
|
|
|
|
IMalloc_fnQueryInterface,
|
|
|
|
IMalloc_fnAddRefRelease,
|
|
|
|
IMalloc_fnAddRefRelease,
|
|
|
|
IMalloc_fnAlloc,
|
|
|
|
IMalloc_fnRealloc,
|
|
|
|
IMalloc_fnFree,
|
|
|
|
IMalloc_fnGetSize,
|
|
|
|
IMalloc_fnDidAlloc,
|
|
|
|
IMalloc_fnHeapMinimize
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* CoGetMalloc [OLE32.@]
|
|
|
|
*
|
|
|
|
* Retrieves the current IMalloc interface for the process.
|
|
|
|
*
|
|
|
|
* PARAMS
|
2016-03-04 09:25:52 +00:00
|
|
|
* context [I] Should always be MEMCTX_TASK.
|
|
|
|
* imalloc [O] Address where memory allocator object will be stored.
|
2005-07-31 12:11:56 +00:00
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: S_OK.
|
|
|
|
* Failure: HRESULT code.
|
|
|
|
*/
|
2016-03-04 09:25:52 +00:00
|
|
|
HRESULT WINAPI CoGetMalloc(DWORD context, IMalloc **imalloc)
|
2005-07-31 12:11:56 +00:00
|
|
|
{
|
2016-03-04 09:25:52 +00:00
|
|
|
if (context != MEMCTX_TASK) {
|
|
|
|
*imalloc = NULL;
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*imalloc = &Malloc32.IMalloc_iface;
|
|
|
|
return S_OK;
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CoTaskMemAlloc [OLE32.@]
|
|
|
|
*
|
|
|
|
* Allocates memory using the current process memory allocator.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* size [I] Size of the memory block to allocate.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: Pointer to newly allocated memory block.
|
|
|
|
* Failure: NULL.
|
|
|
|
*/
|
2016-03-03 17:54:32 +00:00
|
|
|
LPVOID WINAPI CoTaskMemAlloc(SIZE_T size)
|
2005-07-31 12:11:56 +00:00
|
|
|
{
|
2012-05-17 15:16:51 +00:00
|
|
|
return IMalloc_Alloc(&Malloc32.IMalloc_iface,size);
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CoTaskMemFree [OLE32.@]
|
|
|
|
*
|
|
|
|
* Frees memory allocated from the current process memory allocator.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* ptr [I] Memory block to free.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Nothing.
|
|
|
|
*/
|
|
|
|
VOID WINAPI CoTaskMemFree(LPVOID ptr)
|
|
|
|
{
|
2012-05-17 15:16:51 +00:00
|
|
|
IMalloc_Free(&Malloc32.IMalloc_iface, ptr);
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CoTaskMemRealloc [OLE32.@]
|
|
|
|
*
|
|
|
|
* Allocates memory using the current process memory allocator.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* pvOld [I] Pointer to old memory block.
|
|
|
|
* size [I] Size of the new memory block.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: Pointer to newly allocated memory block.
|
|
|
|
* Failure: NULL.
|
|
|
|
*/
|
2016-03-03 17:54:32 +00:00
|
|
|
LPVOID WINAPI CoTaskMemRealloc(LPVOID pvOld, SIZE_T size)
|
2005-07-31 12:11:56 +00:00
|
|
|
{
|
2012-05-17 15:16:51 +00:00
|
|
|
return IMalloc_Realloc(&Malloc32.IMalloc_iface, pvOld, size);
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CoRegisterMallocSpy [OLE32.@]
|
|
|
|
*
|
|
|
|
* Registers an object that receives notifications on memory allocations and
|
|
|
|
* frees.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* pMallocSpy [I] New spy object.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: S_OK.
|
|
|
|
* Failure: HRESULT code.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* if a mallocspy is already registered, we can't do it again since
|
|
|
|
* only the spy knows, how to free a memory block
|
|
|
|
*/
|
|
|
|
HRESULT WINAPI CoRegisterMallocSpy(LPMALLOCSPY pMallocSpy)
|
|
|
|
{
|
|
|
|
IMallocSpy* pSpy;
|
|
|
|
HRESULT hres = E_INVALIDARG;
|
|
|
|
|
2016-06-07 10:47:21 +00:00
|
|
|
TRACE("%p\n", pMallocSpy);
|
2005-07-31 12:11:56 +00:00
|
|
|
|
2016-06-07 10:47:21 +00:00
|
|
|
if(!pMallocSpy) return E_INVALIDARG;
|
2005-07-31 12:11:56 +00:00
|
|
|
|
|
|
|
EnterCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
|
2016-06-07 10:47:21 +00:00
|
|
|
if (Malloc32.pSpy)
|
|
|
|
hres = CO_E_OBJISREG;
|
|
|
|
else if (SUCCEEDED(IMallocSpy_QueryInterface(pMallocSpy, &IID_IMallocSpy, (void**)&pSpy))) {
|
2005-07-31 12:11:56 +00:00
|
|
|
Malloc32.pSpy = pSpy;
|
|
|
|
hres = S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CoRevokeMallocSpy [OLE32.@]
|
|
|
|
*
|
2008-07-10 09:14:19 +00:00
|
|
|
* Revokes a previously registered object that receives notifications on memory
|
2005-07-31 12:11:56 +00:00
|
|
|
* 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);
|
|
|
|
|
2016-06-07 10:47:21 +00:00
|
|
|
if (!Malloc32.pSpy)
|
|
|
|
hres = CO_E_OBJNOTREG;
|
|
|
|
else if (Malloc32.SpyedAllocationsLeft) {
|
2007-04-20 12:23:52 +00:00
|
|
|
TRACE("SpyReleasePending with %u allocations left\n", Malloc32.SpyedAllocationsLeft);
|
2005-07-31 12:11:56 +00:00
|
|
|
Malloc32.SpyReleasePending = TRUE;
|
|
|
|
hres = E_ACCESSDENIED;
|
|
|
|
} else {
|
|
|
|
IMallocSpy_Release(Malloc32.pSpy);
|
|
|
|
Malloc32.pSpy = NULL;
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&IMalloc32_SpyCS);
|
|
|
|
|
2010-05-29 11:34:57 +00:00
|
|
|
return hres;
|
2005-07-31 12:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* IsValidInterface [OLE32.@]
|
|
|
|
*
|
|
|
|
* Determines whether a pointer is a valid interface.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* punk [I] Interface to be tested.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE, if the passed pointer is a valid interface, or FALSE otherwise.
|
|
|
|
*/
|
|
|
|
BOOL WINAPI IsValidInterface(LPUNKNOWN punk)
|
|
|
|
{
|
|
|
|
return !(
|
|
|
|
IsBadReadPtr(punk,4) ||
|
|
|
|
IsBadReadPtr(punk->lpVtbl,4) ||
|
|
|
|
IsBadReadPtr(punk->lpVtbl->QueryInterface,9) ||
|
|
|
|
IsBadCodePtr((FARPROC)punk->lpVtbl->QueryInterface)
|
|
|
|
);
|
|
|
|
}
|