/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbHeapBase
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<18/08/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

//#define xbHEAPINTERNALS
#include "xbHeapLow.h"

#ifdef _xbALLOCDEBUG_SPACEINIT
char	xbDebugHeap_SpaceUninit = (const char)0xCA;
char	xbDebugHeap_SpaceFreed = (const char)0xFA;
#endif

xbBaseHeap::xbBaseHeap(const char * pszDbgName)
{
#ifdef _xbALLOCDEBUG
	m_cAllocOp = 0;
#endif
	resetAllocInfo();

	xbStrncpy(m_szDbgName,pszDbgName?pszDbgName:"No name",sizeof(m_szDbgName)-1);
	m_szDbgName[sizeof(m_szDbgName)-1] = 0;
	m_pCleanerFct = NULL;
	m_pCleanerFctOwner = NULL;
}

void xbBaseHeap::SetName(const char * pszDbgName,...)
{
	va_list ap;
	va_start(ap,pszDbgName);
	xbVsnprintf(m_szDbgName,sizeof(m_szDbgName)-1,pszDbgName,ap);
	va_end(ap);
	m_szDbgName[sizeof(m_szDbgName)-1] = 0;
}

bool xbBaseHeap::RequestCleanerToFreeBytes(size_t cByteRequired)
{
	if (Compact()) return true;

	if ((m_pCleanerFct==NULL) || !m_pCleanerFctOwner 
		|| !(m_pCleanerFctOwner->*m_pCleanerFct)((xbint)cByteRequired)) return false;

	return true;
}

xbBaseHeap::~xbBaseHeap()
{
}

#ifdef _xbALLOCDEBUG
void xbBaseHeap::addAllocDebugBreak(uint allocId)
{
//	if (allocId == 4573 || allocId == 3671)
//		::DebugBreak();
}

#define DBGFILE_CMAX 512
long g_cDebugFile = 0;
char g_debugFile[DBGFILE_CMAX][DBGFILE_SZMAX];
const char *_xbDebugFileEntry(const char *pszFile)
{
	const char *ptr = strrchr(pszFile,xbPath_SLASHch);
	if (!ptr)
		ptr = pszFile;
	else
		ptr++;
//	if (ptr[0] == 'v' && ptr[1] == 'b')
//		return pszFile;
retryFind:
	long cDebugFile = g_cDebugFile;
	if (cDebugFile > DBGFILE_CMAX)
		g_cDebugFile = cDebugFile = DBGFILE_CMAX;
	for(int i = 0; i < cDebugFile; i++)
	{
		if (xbStrnicmp(g_debugFile[i],ptr,DBGFILE_SZMAX-1)==0)
			return g_debugFile[i];
	}

	if (cDebugFile != g_cDebugFile)
		goto retryFind;
	if (cDebugFile >= DBGFILE_CMAX)
		return NULL;
	cDebugFile = g_cDebugFile++;
	if (cDebugFile >= DBGFILE_CMAX)
	{
		g_cDebugFile = DBGFILE_CMAX;
		return NULL;
	}
	strncpy(g_debugFile[cDebugFile],ptr,DBGFILE_SZMAX-1);
	g_debugFile[cDebugFile][DBGFILE_SZMAX-1]=0;
	return g_debugFile[cDebugFile];
}

#endif
