SPY.CPP

//----------------------------------------------------------------------------- 
// Microsoft OLE DB TABLECOPY Sample
// Copyright (C) 1996 By Microsoft Corporation.
//
// @doc
//
// @module SPY.CPP
//
//-----------------------------------------------------------------------------


/////////////////////////////////////////////////////////////////////////////
// Includes
//
/////////////////////////////////////////////////////////////////////////////
#include "spy.h"
#include "common.h"


/////////////////////////////////////////////////////////////////////////////
// Defines
//
/////////////////////////////////////////////////////////////////////////////

// HEADER + BUFFER + FOOTER
// HEADER = HEADSIGNITURE + BUFFERSIZE + BUFFERID
// FOOTER = TAILSIGNITURE

//All the header info must be ULONGs,
//so that the user buffer falls on a word boundary
//The tail must be a byte, since if it was a ULONG it would
//also require a word boundary, but the users buffer could
//be an odd number of bytes, so instead of rounding up, just use BYTE

const ULONG HEADSIZE= sizeof(ULONG); //HEADSIGNITURE
const ULONG LENGTHSIZE= sizeof(ULONG); //BUFFERSIZE
const ULONG IDSIZE= sizeof(ULONG); //BUFFERIF
const ULONG TAILSIZE= sizeof(BYTE); //TAILSIGNITURE

const ULONG HEADERSIZE = ROUNDUP(HEADSIZE + LENGTHSIZE + IDSIZE);
const ULONG FOOTERSIZE = TAILSIZE;

const BYTE HEADSIGN = '{';
const BYTE TAILSIGN = '}';

const BYTE ALLOCSIGN = '$';
const BYTE FREESIGN = 'Z';

#define HEAD_OFFSET(pHeader)((BYTE*)pHeader)
#define TAIL_OFFSET(pHeader)(USERS_OFFSET(pHeader)+BUFFER_LENGTH(pHeader))

#define USERS_OFFSET(pHeader)(HEAD_OFFSET(pHeader) + HEADERSIZE)
#define HEADER_OFFSET(pUserBuffer) ((BYTE*)(pUserBuffer) - HEADERSIZE)

#define LENGTH_OFFSET(pHeader)(HEAD_OFFSET(pHeader) + HEADSIZE)
#define BUFFER_LENGTH(pHeader)(*(ULONG*)LENGTH_OFFSET(pHeader))

#define ID_OFFSET(pHeader)(LENGTH_OFFSET(pHeader) + LENGTHSIZE)
#define BUFFER_ID(pHeader)(*(ULONG*)ID_OFFSET(pHeader))

#define HEAD_SIGNITURE(pHeader)(*(ULONG*)HEAD_OFFSET(pHeader))
#define TAIL_SIGNITURE(pHeader)(*(BYTE*)TAIL_OFFSET(pHeader))


/////////////////////////////////////////////////////////////////////////////
// CMallocSpy::CMallocSpy()
//
/////////////////////////////////////////////////////////////////////////////
CMallocSpy::CMallocSpy()
{
m_cRef = 0;
m_cbRequest = 0;

//AddRef, so delete is required
AddRef();
}

/////////////////////////////////////////////////////////////////////////////
// CMallocSpy::~CMallocSpy()
//
/////////////////////////////////////////////////////////////////////////////
CMallocSpy::~CMallocSpy()
{
//Remove all the elements of the list
CAllocList.RemoveAll();
}


/////////////////////////////////////////////////////////////////////////////
// BOOL CMallocSpy::Add
//
/////////////////////////////////////////////////////////////////////////////
BOOL CMallocSpy::Add(void* pv)
{
ASSERT(pv);

//Add this element to the list
CAllocList.AddTail(pv);
return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// BOOL CMallocSpy::Remove
//
/////////////////////////////////////////////////////////////////////////////
BOOL CMallocSpy::Remove(void* pv)
{
ASSERT(pv);

//Remove this element from the list
CAllocList.RemoveAt(CAllocList.Find(pv));
return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// BOOL CMallocSpy::DumpLeaks
//
/////////////////////////////////////////////////////////////////////////////
BOOL CMallocSpy::DumpLeaks()
{
ULONG ulTotalLeaked = 0;

//Display Leaks to the Output Window
while(!CAllocList.IsEmpty())
{
//Obtain the pointer to the leaked memory
void* pUsersBuffer = CAllocList.RemoveHead();
ASSERT(pUsersBuffer);

void* pHeader = HEADER_OFFSET(pUsersBuffer);
ASSERT(pHeader);

//Make sure that the head/tail signitures are intact
if(HEAD_SIGNITURE(pHeader) != HEADSIGN)
TRACE(L"-- IMallocSpy HeadSigniture Corrupted! - 0x%08x, ID=%08lu, %lu bytes\n", pUsersBuffer, BUFFER_ID(pHeader), BUFFER_LENGTH(pHeader));

if(TAIL_SIGNITURE(pHeader) != TAILSIGN)
TRACE(L"-- IMallocSpy TailSigniture Corrupted! - 0x%08x, ID=%08lu, %lu bytes\n", pUsersBuffer, BUFFER_ID(pHeader), BUFFER_LENGTH(pHeader));

ULONG ulSize = BUFFER_LENGTH(pHeader);
ULONG ulID = BUFFER_ID(pHeader);

TRACE(L"-- IMallocSpy LEAK! - 0x%08x, ID=%08lu, %lu bytes\n", pUsersBuffer, ulID, ulSize);
ulTotalLeaked += ulSize;
}

if(ulTotalLeaked)
TRACE(L"-- IMallocSpy Total LEAKED! - %lu bytes\n", ulTotalLeaked);

return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// HRESULT CMallocSpy::QueryInterface
//
/////////////////////////////////////////////////////////////////////////////
HRESULT CMallocSpy::QueryInterface(REFIID riid, void** ppIUnknown)
{
if(!ppIUnknown)
return E_INVALIDARG;

*ppIUnknown = NULL;

//IID_IUnknown
if(riid == IID_IUnknown)
*ppIUnknown = this;
//IDD_IMallocSpy
else if(riid == IID_IMallocSpy)
*ppIUnknown = this;

if(*ppIUnknown)
{
((IUnknown*)*ppIUnknown)->AddRef();
return S_OK;
}

return E_NOINTERFACE;
}

/////////////////////////////////////////////////////////////////////////////
// ULONG CMallocSpy::AddRef
//
/////////////////////////////////////////////////////////////////////////////
ULONG CMallocSpy::AddRef()
{
return ++m_cRef;
}

/////////////////////////////////////////////////////////////////////////////
// ULONG CMallocSpy::Release
//
/////////////////////////////////////////////////////////////////////////////
ULONG CMallocSpy::Release()
{
if(--m_cRef)
return m_cRef;

TRACE(L"Releasing IMallocSpy\n");

delete this;
return 0;
}


/////////////////////////////////////////////////////////////////////////////
// ULONG CMallocSpy::PreAlloc
//
/////////////////////////////////////////////////////////////////////////////
ULONG CMallocSpy::PreAlloc(ULONG cbRequest)
{
//cbRequest is the orginal number of bytes requested by the user
//Store the users requested size
m_cbRequest = cbRequest;

//Return the total size requested, plus extra for header/footer
return (m_cbRequest + HEADERSIZE + FOOTERSIZE);
}

/////////////////////////////////////////////////////////////////////////////
// void* CMallocSpy::PostAlloc
//
/////////////////////////////////////////////////////////////////////////////
void* CMallocSpy::PostAlloc(void* pHeader)
{
//pActual is the pointer to the head of the buffer, including the header
//Add the users pointer to the list
Add(USERS_OFFSET(pHeader));

//Place the HeadSigniture in the HEADER
HEAD_SIGNITURE(pHeader) = HEADSIGN;

//Place the Size in the HEADER
BUFFER_LENGTH(pHeader) = m_cbRequest;

//Place the ID in the HEADER
static ULONG ulID = 0;
BUFFER_ID(pHeader) = ++ulID;

//Set the UsersBuffer to a known char
memset(USERS_OFFSET(pHeader), ALLOCSIGN, m_cbRequest);

//Place the TailSigniture in the HEADER
TAIL_SIGNITURE(pHeader) = TAILSIGN;

#ifdef FINDLEAKS
TRACE(L"-- IMallocSpy Alloc - 0x%08x, ID=%08lu, %lu bytes\n", USERS_OFFSET(pHeader), ulID, m_cbRequest);
#endif // FINDLEAKS

// Return the actual users buffer
return USERS_OFFSET(pHeader);
}

/////////////////////////////////////////////////////////////////////////////
// void* CMallocSpy::PreFree
//
/////////////////////////////////////////////////////////////////////////////
void* CMallocSpy::PreFree(void* pUsersBuffer, BOOL fSpyed)
{
//pUsersBuffer is the users pointer to thier buffer, not the header

// Check for NULL
if(pUsersBuffer == NULL)
return NULL;

//If this memory was alloced under IMallocSpy, need to remove it
if(fSpyed)
{
//Remove this pointer form the list
Remove(pUsersBuffer);

void* pHeader = HEADER_OFFSET(pUsersBuffer);

//Make sure that the head/tail signitures are intact
if(HEAD_SIGNITURE(pHeader) != HEADSIGN)
TRACE(L"-- IMallocSpy HeadSigniture Corrupted! - 0x%08x, ID=%08lu, %lu bytes\n", pUsersBuffer, BUFFER_ID(pHeader), BUFFER_LENGTH(pHeader));

if(TAIL_SIGNITURE(pHeader) != TAILSIGN)
TRACE(L"-- IMallocSpy TailSigniture Corrupted! - 0x%08x, ID=%08lu, %lu bytes\n", pUsersBuffer, BUFFER_ID(pHeader), BUFFER_LENGTH(pHeader));

//Set the UsersBuffer to a known char
memset(pUsersBuffer, FREESIGN, BUFFER_LENGTH(pHeader));

//Need to return the actual header pointer to
//free the entire buffer including the heading
return pHeader;
}

//else
return pUsersBuffer;
}


/////////////////////////////////////////////////////////////////////////////
// void CMallocSpy::PostFree
//
/////////////////////////////////////////////////////////////////////////////
void CMallocSpy::PostFree(BOOL fSpyed)
{
// Note the free or whatever
return;
}


/////////////////////////////////////////////////////////////////////////////
// ULONG CMallocSpy::PreRealloc
//
/////////////////////////////////////////////////////////////////////////////
ULONG CMallocSpy::PreRealloc(void* pUsersBuffer, ULONG cbRequest,
void** ppNewRequest, BOOL fSpyed)
{
ASSERT(pUsersBuffer && ppNewRequest);

//If this was alloced under IMallocSpy we need to adjust
//the size stored in the header
if(fSpyed)
{
//Find the start
*ppNewRequest = HEADER_OFFSET(pUsersBuffer);

//Store the new desired size
m_cbRequest = cbRequest;

//Return the total size, including extra
return (m_cbRequest + HEADERSIZE + FOOTERSIZE);
}

//else
*ppNewRequest = pUsersBuffer;
return cbRequest;
}


/////////////////////////////////////////////////////////////////////////////
// void* CMallocSpy::PostRealloc
//
/////////////////////////////////////////////////////////////////////////////
void* CMallocSpy::PostRealloc(void* pHeader, BOOL fSpyed)
{
//If this buffer was alloced under IMallocSpy
if(fSpyed)
{
//HeadSigniture should still be intact
if(HEAD_SIGNITURE(pHeader) != HEADSIGN)
TRACE(L"-- IMallocSpy HeadSigniture Corrupted! - 0x%08x, ID=%08lu, %lu bytes\n", USERS_OFFSET(pHeader), BUFFER_ID(pHeader), BUFFER_LENGTH(pHeader));

//ID should still be intact

//Place the new Size in the HEADER
BUFFER_LENGTH(pHeader) = m_cbRequest;

//Need to place the tail signiture again,
//since it will be over written by the realloc
TAIL_SIGNITURE(pHeader) = TAILSIGN;

//Return the actual "user" buffer
return USERS_OFFSET(pHeader);
}

//else
return pHeader;
}


/////////////////////////////////////////////////////////////////////////////
// void* CMallocSpy::PreGetSize
//
/////////////////////////////////////////////////////////////////////////////
void* CMallocSpy::PreGetSize(void* pUsersBuffer, BOOL fSpyed)
{
if (fSpyed)
return HEADER_OFFSET(pUsersBuffer);

return pUsersBuffer;
}



/////////////////////////////////////////////////////////////////////////////
// ULONG CMallocSpy::PostGetSize
//
/////////////////////////////////////////////////////////////////////////////
ULONG CMallocSpy::PostGetSize(ULONG cbActual, BOOL fSpyed)
{
if (fSpyed)
return cbActual - HEADERSIZE - FOOTERSIZE;

return cbActual;
}




/////////////////////////////////////////////////////////////////////////////
// void* CMallocSpy::PreDidAlloc
//
/////////////////////////////////////////////////////////////////////////////
void* CMallocSpy::PreDidAlloc(void* pUsersBuffer, BOOL fSpyed)
{
if (fSpyed)
return HEADER_OFFSET(pUsersBuffer);

return pUsersBuffer;
}


/////////////////////////////////////////////////////////////////////////////
// BOOL CMallocSpy::PostDidAlloc
//
/////////////////////////////////////////////////////////////////////////////
BOOL CMallocSpy::PostDidAlloc(void* pRequest, BOOL fSpyed, BOOL fActual)
{
return fActual;
}



/////////////////////////////////////////////////////////////////////////////
// void CMallocSpy::PreHeapMinimize
//
/////////////////////////////////////////////////////////////////////////////
void CMallocSpy::PreHeapMinimize()
{
// We don't do anything here
return;
}


/////////////////////////////////////////////////////////////////////////////
// void CMallocSpy::PostHeapMinimize
//
/////////////////////////////////////////////////////////////////////////////
void CMallocSpy::PostHeapMinimize()
{
// We don't do anything here
return;
}

/////////////////////////////////////////////////////////////////////////////
// Resgistration
//
/////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
void MallocSpyRegister(CMallocSpy** ppCMallocSpy)
{
ASSERT(ppCMallocSpy);

//Allocate Interface
*ppCMallocSpy = new CMallocSpy(); //Constructor AddRef's

//Regisiter Interface
CoRegisterMallocSpy(*ppCMallocSpy); // Does an AddRef on Object
}

void MallocSpyUnRegister(CMallocSpy* pCMallocSpy)
{
ASSERT(pCMallocSpy);
CoRevokeMallocSpy(); //Does a Release on Object
}

void MallocSpyDump(CMallocSpy* pCMallocSpy)
{
ASSERT(pCMallocSpy);
pCMallocSpy->DumpLeaks();
}

#else
void MallocSpyRegister(CMallocSpy** ppCMallocSpy) {return; };
void MallocSpyUnRegister(CMallocSpy* pCMallocSpy) {return; };
void MallocSpyDump(CMallocSpy* pCMallocSpy) { return; };
#endif //DEBUG