//-----------------------------------------------------------------------------
//	XMemFree
//
//	XBox Memory Allocator Override
//-----------------------------------------------------------------------------
VOID WINAPI XMemFree( PVOID pAddress, DWORD dwAllocAttributes )
{
	if ( !pAddress )
	{
		return;
	}

	XALLOC_ATTRIBUTES *pAttribs = (XALLOC_ATTRIBUTES *)&dwAllocAttributes;
	bool bPhysical = ( pAttribs->dwMemoryType == XALLOC_MEMTYPE_PHYSICAL );

	if ( !bPhysical && !pAttribs->dwHeapTracksAttributes && pAttribs->dwAllocatorId != eXALLOCAllocatorId_XUI )
	{
		switch ( pAttribs->dwAlignment )
		{
		case XALLOC_ALIGNMENT_4:
			return g_pMemAlloc->Free( pAddress );
		default:
			return MemAlloc_FreeAligned( pAddress );
		}
		return;
	}

	XMemAlloc_RegisterDeallocation( pAddress, dwAllocAttributes );

	XMemFreeDefault( pAddress, dwAllocAttributes );
}
void CLightingManager::Shutdown()
{
	Assert( m_hDeferredLights.Count() == 0 );
	materials->RemoveReleaseFunc( LightingResourceRelease );

#if DEFCFG_USE_SSE
	if( m_pSortDataX4 != NULL )
	{
		MemAlloc_FreeAligned( m_pSortDataX4 );
		m_pSortDataX4 = NULL;
	}
#endif
}
void CMemoryStack::Term()
{
	FreeAll();
	if ( m_pBase )
	{
#if defined(_WIN32)
		VirtualFree( m_pBase, 0, MEM_RELEASE );
#else
		MemAlloc_FreeAligned( m_pBase );
#endif
		m_pBase = NULL;
	}
}
void CLightingManager::AllocateSortDataBuffer()
{
	if( m_pSortDataX4 != NULL )
	{
		MemAlloc_FreeAligned( m_pSortDataX4 );
		m_pSortDataX4 = NULL;
	}

	if( m_hDeferredLights.Count() == 0 )
		return;

	const int iSortBufferDataSize = m_hDeferredLights.Count() % 4 == 0
			? m_hDeferredLights.Count() / 4 
			: ((m_hDeferredLights.Count() - (m_hDeferredLights.Count() % 4)) / 4) + 1;

	m_pSortDataX4 = reinterpret_cast<def_light_presortdatax4_t*>(
		MemAlloc_AllocAligned( iSortBufferDataSize * sizeof(def_light_presortdatax4_t),
		sizeof(fltx4) ));
}
FREE_CALL void __cdecl _aligned_free_base( void *ptr )
{
	MemAlloc_FreeAligned( ptr );
}