/*<<<***************************************************************************
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: xbODheapLow
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<02/06/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#ifdef xbOS_MSWIN
#include "xbODheapLow.h"
#define xbHEAPINTERNALS
#include "../../xbHeap.h"

////////////////////////////////////////////////////////////////////////////////////////////
// virtual heap
////////////////////////////////////////////////////////////////////////////////////////////
ulong xbllVirtualHeap::GetGranularity()
{
	if (m_cByteGran)
		return m_cByteGran;

	SYSTEM_INFO sysInfo;
	::GetSystemInfo(&sysInfo);

	m_cByteGran = sysInfo.dwAllocationGranularity;
	if (!m_cByteGran)
		m_cByteGran = 4096;

	return m_cByteGran;
}

xbllVirtualHeap::xbllVirtualHeap(bool /*bMT*/,xbCtorMode /*CtorMode*/)
{
	m_cByteGran = 0;
}

xbllVirtualHeap::~xbllVirtualHeap()
{
}

void	xbllVirtualHeap::Create()
{
}

void	xbllVirtualHeap::Destroy()
{
}

void *xbllVirtualHeap::Alloc(size_t cByte,xbOpMode opmode)
{
//	xbRdbgAssert((cByte % GetGranularity()) == 0);
	void *ptr = ::VirtualAlloc(NULL,cByte,MEM_COMMIT/*|MEM_RESET*/,PAGE_READWRITE);
	if (!ptr && opmode == xbOp_Required) 
		xbThrowEsysLowFmt(xbHR_LAST,"xbllVirtualHeap()::Alloc(cByte=%ld) : VirtualAlloc failure",cByte);

	return ptr;
}

void *xbllVirtualHeap::ReAlloc(void *ptr,size_t cByte,size_t cByteOld,xbOpMode opmode)
{
//	xbRdbgAssert((cByte % GetGranularity()) == 0);
//	xbRdbgAssert((cByteOld % GetGranularity()) == 0);
	void *ptrNew = ::VirtualAlloc(NULL,cByte,MEM_COMMIT/*|MEM_RESET*/,PAGE_READWRITE);
	if (!ptrNew)
	{
		if (opmode == xbOp_Required) 
			xbThrowEsysLowFmt(xbHR_LAST,"xbllVirtualHeap()::Alloc(cByte=%ld) : VirtualAlloc failure",cByte);
		return NULL;
	}
	if (cByte < cByteOld)
		cByteOld = cByte;
	::memcpy(ptrNew,ptr,cByteOld);
	eProtect(Free(ptr););
	return ptrNew;
}

void xbllVirtualHeap::Free(void *ptr)
{
	if (!xbMustBe(ptr)) return;

	if (!::VirtualFree(ptr,0,MEM_RELEASE))
		xbThrowEsysLow(xbHR_LAST,"xbllVirtualHeap::Free() : VirtualFree failed");
}

bool xbllVirtualHeap::Vlock(void *ptr,size_t cByte)
{
//	xbRdbgAssert((cByte % GetGranularity()) == 0);
	return ::VirtualLock(ptr,cByte)!=0;
}

bool xbllVirtualHeap::Vunlock(void *ptr,size_t cByte)
{
//	xbRdbgAssert((cByte % GetGranularity()) == 0);
	bool bVunlock = ::VirtualUnlock(ptr,cByte)!=0;
	xbRdbgAssert(bVunlock);
	return bVunlock;
}

bool xbllVirtualHeap::Compact()
{
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////
// win32 system heap
////////////////////////////////////////////////////////////////////////////////////////////
xbllSysHeap::xbllSysHeap(bool bMT,xbCtorMode CtorMode)
{
	m_hHeap = NULL;
	m_bMT = bMT;
//	m_cAlloc = 0;
	if (CtorMode == xbCtor_CallCreate) Create();
}

void xbllSysHeap::Create()
{
	if (m_hHeap) return;

//	m_cAlloc = 0;
	if ((m_hHeap = ::HeapCreate(0,0x1000,0)) == NULL)
		xbThrowEsysLow(xbHR_LAST,"xbllSysHeap()::Create() : heap creation failure !");
}

void xbllSysHeap::Destroy()
{
	if (!m_hHeap) return;
/*#ifdef _xbHEAPDEBUG
	if (m_cAlloc!=0)
	{
		xbDbgHeapOutput("xbllSysHeap()::~xbllSysHeap() : %ld allocations not freed !",
			m_cAlloc);
	}
#endif*/
	if (!::HeapDestroy(m_hHeap))
		xbDbgHeapOutput("xbllSysHeap()::Destroy() : heap destruction failure");
	m_hHeap = NULL;
//	m_cAlloc = 0;
}

void *xbllSysHeap::Alloc(size_t cByte,xbOpMode opmode)
{
	if (!m_hHeap) 
		xbThrowEsysLow("xbllSysHeap()::Alloc() : heap not created");
	if (cByte <= 0) 
		xbThrowEsysLowFmt("xbllSysHeap()::Alloc(cByte=%ld) : size to allocate is invalid",cByte);
	void *ret = ::HeapAlloc(m_hHeap,m_bMT?0:HEAP_NO_SERIALIZE,cByte);
/*	if (ret) m_cAlloc++;
	else*/ if (!ret && opmode == xbOp_Required) 
		xbThrowEsysLowFmt(xbHR_LAST,"xbllSysHeap()::Alloc(cByte=%ld) : HeapAlloc failure",cByte);
	return ret;
}

void *xbllSysHeap::ReAlloc(void *ptr,size_t cByte,xbOpMode opmode)
{
	if (!m_hHeap) 
		xbThrowEsysLow("xbllSysHeap()::ReAlloc() : heap not created");
	if (cByte <= 0) 
		xbThrowEsysLowFmt("xbllSysHeap()::ReAlloc(cByte=%ld) : size to allocate is invalid",cByte);
	if (!ptr)
		return Alloc(cByte);

	void *ret = ::HeapReAlloc(m_hHeap,m_bMT?0:HEAP_NO_SERIALIZE,ptr,cByte);
	if (!ret && opmode == xbOp_Required) 
		xbThrowEsysLowFmt(xbHR_LAST,"xbllSysHeap()::ReAlloc(cByte=%ld) : HeapReAlloc failure",cByte);
	return ret;
}

void xbllSysHeap::Free(void *ptr)
{
	if (!m_hHeap) 
		xbThrowEsysLow("xbllSysHeap()::Free() : heap not created");
	if (!xbMustBe(ptr)) return;
	if (!::HeapFree(m_hHeap,m_bMT?0:HEAP_NO_SERIALIZE,ptr))
		xbDbgHeapOutput("xbllSysHeap()::Free(%08lX) : HeapFree failure",ptr);
//	xbMustBe(--m_cAlloc >= 0);
}

xbllSysHeap::~xbllSysHeap()
{
	eProtect(Destroy());
}

bool xbllSysHeap::Compact()
{
	return ::HeapCompact(m_hHeap,m_bMT?0:HEAP_NO_SERIALIZE) != 0;
}


bool xbllCrtHeap::Compact()
{
	return ::HeapCompact(GetProcessHeap(),0) != 0;
}

#endif
