Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

MemoryChecker.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * メモリチェッカヘッダ
00022  * @author Junpee
00023  */
00024 
00025 #ifndef MEMORY_CHECKER_H_
00026 #define MEMORY_CHECKER_H_
00027 
00028 //------------------------------------------------------------------------------
00029 // デバッグ時
00030 #ifdef _DEBUG
00031 
00032 /// デバッグ版メモリアロケータを有効にする。
00033 #include <memory>
00034 #define _CRTDBG_MAP_ALLOC
00035 #include <crtdbg.h>
00036 
00037 /// newにデバッグ情報を付加する。
00038 #define new  ::new(_NORMAL_BLOCK, __FILE__, __LINE__)
00039 
00040 namespace Lamp{
00041 
00042 /**
00043  * メモリチェッカ
00044  *
00045  * メモリリークを検出する為のクラスです。<br>
00046  * システム初期化時に MemoryChecker::initialize()を呼んで下さい。
00047  * <pre>
00048  * サンプルコード
00049  *
00050  * // 初期化
00051  * MemoryChecker::initialize();
00052  *
00053  * // メモリの詳細な情報をダンプ
00054  * MemoryChecker::dumpDetail();
00055  * // メモリの統計情報をダンプ
00056  * MemoryChecker::dumpStatistics();
00057  *
00058  * // 現在のメモリステートを取得
00059  * MemoryChecker::State state = MemoryChecker::getState();
00060  *
00061  * // メモリを使用する処理
00062  * allocateMemoryMethod();
00063  *
00064  * // allocateMemoryMethod()で使用したメモリの詳細な情報をダンプ
00065  * MemoryChecker::dumpDetail(state);
00066  * // allocateMemoryMethod()で使用したメモリの統計情報をダンプ
00067  * MemoryChecker::dumpStatistics(state);
00068  *
00069  * // メモリを解放する処理
00070  * freeMemoryMethod();
00071  *
00072  * // freeMemoryMethodでちゃんとメモリが解放されているかチェックする
00073  * MemoryChecker::leakCheck(state);
00074  * </pre>
00075  */
00076 //------------------------------------------------------------------------------
00077 class MemoryChecker{
00078 public:
00079     /**
00080      * メモリ初期化
00081      *
00082      * システムの初期化時に一度だけ呼んで下さい。
00083      */
00084     static void initialize();
00085 
00086     /**
00087      * メモリステート
00088      *
00089      * メモリの状態をクラス内部に保存します。
00090      */
00091     class State{
00092     friend class MemoryChecker;
00093     private:
00094         _CrtMemState state;
00095     };
00096 
00097     /**
00098      * メモリステートの取得
00099      * @return メモリステート
00100      */
00101     static State getState();
00102 
00103     /**
00104      * 詳細ダンプ
00105      *
00106      * メモリの詳細な情報ををデバッグ出力にダンプします。
00107      */
00108     static void dumpDetail();
00109 
00110     /**
00111      * 詳細の差分ダンプ
00112      *
00113      * メモリ差分の詳細な情報ををデバッグ出力にダンプします。
00114      * @param oldState 差分を取る以前のステート
00115      */
00116     static void dumpDetail(const State& oldState);
00117 
00118     /**
00119      * 統計のダンプ
00120      *
00121      * メモリの統計情報をデバッグ出力にダンプします。
00122      */
00123     static void dumpStatistics();
00124 
00125     /**
00126      * 統計の差分ダンプ
00127      *
00128      * メモリ差分の統計情報をデバッグ出力にダンプします。
00129      * @param oldState 差分を取る以前のステート
00130      */
00131     static void dumpStatistics(const State& oldState);
00132 
00133     /**
00134      * メモリリークチェック
00135      *
00136      * メモリリークをチェックします。
00137      * メモリ使用量が同じ場合はリークを検出できません。
00138      * メモリ使用量が同じでリークを検出するにはdumpDetailを使用してください。
00139      * @param oldState 差分を取る以前のステート
00140      @ @return trueならメモリリークが発生している。
00141      */
00142     static bool leakCheck(const State& oldState);
00143 
00144 private:
00145     // コンストラクタ隠蔽
00146     MemoryChecker(){}
00147 
00148     // 初期化フラグ
00149     static bool initialized_;
00150 };
00151 
00152 } // End of namespace Lamp
00153 
00154 //------------------------------------------------------------------------------
00155 // 非デバッグ時
00156 #else // ! _DEBUG
00157 
00158 // デバッグを切った時にコンパイルエラーが多発する状態への対策
00159 #include <memory>
00160 
00161 namespace Lamp{
00162 
00163 /// メモリ
00164 class MemoryChecker{
00165 public:
00166     /// メモリ初期化ダミー
00167     inline static void initialize(){}
00168 
00169     /// メモリステート
00170     class State{};
00171 
00172     /// ステートの取得ダミー
00173     inline static State getState(){
00174         State state;
00175         return state;
00176     }
00177 
00178     /// 詳細ダンプダミー
00179     inline static void dumpDetail(){}
00180 
00181     /// 詳細の差分ダンプダミー
00182     inline static void dumpDetail(State oldState){}
00183 
00184     /// 統計のダンプダミー
00185     inline static void dumpStatistics(){}
00186 
00187     /// 統計の差分ダンプダミー
00188     inline static void dumpStatistics(State oldState){}
00189 
00190     /// メモリリークチェックダミー
00191     inline static bool leakCheck(State oldState){ return false; }
00192 
00193 private:
00194     // コンストラクタ隠蔽
00195     MemoryChecker(){}
00196 };
00197 
00198 } // End of namespace Lamp
00199 
00200 #endif // End of _DEBUG
00201 
00202 //------------------------------------------------------------------------------
00203 #endif // End of MEMORY_CHECKER_H_
00204 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:32 2005 for Lamp by doxygen 1.3.2