//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * メモリチェッカ実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Core/Debug/MemoryChecker.h"
#include "Core/Debug/DebugOutput.h"

// デバッグ時のみ機能します。
#ifdef _DEBUG

namespace Lamp{

// 初期化フラグ
bool MemoryChecker::initialized_ = false;

//------------------------------------------------------------------------------
// メモリチェッカ初期化
void MemoryChecker::initialize(){
	if(initialized_){ return; }
	// プログラム終了時にメモリリークチェック。
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	// ワーニング、エラー、アサートをデバッグ出力に出す。
	_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
	_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
	_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
	initialized_ = true;
}
//------------------------------------------------------------------------------
// ステートの取得
MemoryChecker::State MemoryChecker::getState(){
	State state;
	_CrtMemCheckpoint(&state.state);
	return state;
}
//------------------------------------------------------------------------------
// 詳細ダンプ
void MemoryChecker::dumpDetail(){
	_CrtMemDumpAllObjectsSince(NULL);
}
//------------------------------------------------------------------------------
// 詳細の差分ダンプ
void MemoryChecker::dumpDetail(const State& oldState){
	_CrtMemDumpAllObjectsSince(&oldState.state);
}
//------------------------------------------------------------------------------
// 統計のダンプ
void MemoryChecker::dumpStatistics(){
	State state;
	_CrtMemCheckpoint(&state.state);
	_CrtMemDumpStatistics(&state.state);
}
//------------------------------------------------------------------------------
// 統計の差分ダンプ
void MemoryChecker::dumpStatistics(const State& oldState){
	State newState, diffState;
	_CrtMemCheckpoint(&newState.state);
	_CrtMemDifference(&diffState.state, &oldState.state, &newState.state);
	_CrtMemDumpStatistics(&diffState.state);
}
//------------------------------------------------------------------------------
// メモリリークチェック
bool MemoryChecker::leakCheck(const State& oldState){
	State newState, diffState;
	_CrtMemCheckpoint(&newState.state);
	if(_CrtMemDifference(&diffState.state, &oldState.state, &newState.state)){
		DebugOutThickLine();
		DebugOut("!!! メモリリーク !!!\n");
		DebugOutLine();
		_CrtMemDumpAllObjectsSince(&diffState.state);
		DebugOutLine();
		_CrtMemDumpStatistics(&diffState.state);
		DebugOutLine();
		DebugOut("!!! メモリリーク !!!\n");
		DebugOutThickLine();
		return true;
	}
	return false;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
#endif // End of _DEBUG
//------------------------------------------------------------------------------
