//===========================================================================//
// File:	GlobalPointerManager.cpp                                        
// Project: Adept
// Contents: GlobalPointers	
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               
// --------  ---  -----------------------------------------------------------//
// 07/08/00  JSE  Initial Coding
//---------------------------------------------------------------------------//
// Copyright (C) 2000, Microsoft Corp.							             
// All Rights reserved worldwide                                            
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL              
//===========================================================================//

#include "AdeptHeaders.hpp"
#include "GlobalPointerManager.hpp"

GlobalPointers 
	*GlobalPointers::globalPointer = NULL;
int 
	*GlobalPointers::globalKey = NULL;


void Adept::AdeptGlobalPointerSecurityCheckStart()
{
	_asm 
	{
		nop
	}	
}

void GlobalPointers::InitializeClass()
{
	globalKey = new int;
	*globalKey = Random::GetInt();

	globalPointer = new GlobalPointers;
	
	for (int i = 0; i < 30; ++i)
	{
		globalPointer->m_pointerArray[i] = Cast_Pointer(GlobalPointers *, (0 ^ 0x85739531));
	}

	//globalPointer ^= *globalKey;
	globalPointer = reinterpret_cast<GlobalPointers *>( ((int)globalPointer ^ *globalKey));
	

}

void GlobalPointers::TerminateClass()
{
	//globalPointer ^= *globalKey;
	globalPointer = reinterpret_cast<GlobalPointers *>( ((int)globalPointer ^ *globalKey));

	delete globalPointer;
	delete globalKey;
}



void GlobalPointers::ClearPointer(int pointer_index)
{
	GlobalPointers *pointer = reinterpret_cast<GlobalPointers *>( ((int)globalPointer ^ *globalKey));
	pointer->m_pointerArray[pointer_index] = reinterpret_cast<GlobalPointers *>( (0 ^ 0x85739531));
}

void  GlobalPointers::AddGlobalPointer(void *add_pointer, int pointer_index)
{
	Verify(pointer_index < 30);
	//GlobalPointers *pointer = globalPointer ^ *globalKey;
	GlobalPointers *pointer = reinterpret_cast<GlobalPointers *>( ((int)globalPointer ^ *globalKey));

	pointer->m_pointerArray[pointer_index] = reinterpret_cast<void*>(((int)add_pointer ^ 0x85739531));

}

void GlobalPointers::MoveGlobals()
{
	GLOBAL_POINTER("Move");

	GlobalPointers *new_pointer = new GlobalPointers;
	//globalPointer ^= *globalKey;
	globalPointer = reinterpret_cast<GlobalPointers *>( ((int)globalPointer ^ *globalKey));


	for (int i = 0; i < 30; ++i)
	{
		new_pointer->m_pointerArray[i] = globalPointer->m_pointerArray[i];
	}

	delete globalPointer;
	delete globalKey;

	globalKey = new int;
	*globalKey = Random::GetInt();
	globalPointer = new_pointer;
	//globalPointer ^= *globalKey;
	globalPointer = reinterpret_cast<GlobalPointers *>( ((int)globalPointer ^ *globalKey));
	
}


void Adept::AdeptGlobalPointerSecurityCheckStop()
{
	_asm 
	{
		nop
	}	
}