Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MemoryDebug.h « ZenLib « ZenLib « thirdparty « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6111dcd055da1c6a4022a6d8186a1b5cbb8f43bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
 *
 *  Use of this source code is governed by a zlib-style license that can
 *  be found in the License.txt file in the root of the source tree.
 */

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// MemoryDebug
//
// Provide "new" and "delete" overloadings to be able to detect memory leaks
// Based on http://loulou.developpez.com/tutoriels/moteur3d/partie1/ 2.2.1
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//---------------------------------------------------------------------------
#ifndef ZenMemoryDebugH
#define ZenMemoryDebugH
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#if defined(ZENLIB_DEBUG)
//---------------------------------------------------------------------------
#include "ZenLib/Conf.h"
#include <fstream>
#include <map>
#include <stack>
#include <string>
//---------------------------------------------------------------------------

namespace ZenLib
{

//***************************************************************************
// Class
//***************************************************************************

class MemoryDebug
{
public :
    ~MemoryDebug();
    static MemoryDebug& Instance();

    void* Allocate(std::size_t Size, const char* File, int Line, bool Array);
    void  Free(void* Ptr, bool Array);
    void  NextDelete(const char*, int Line); //Sauvegarde les infos sur la désallocation courante

    void ReportLeaks();

private :
    MemoryDebug();
    struct TBlock
    {
        std::size_t Size;  // Taille allouée
        std::string File;  // Fichier contenant l'allocation
        int         Line;  // Ligne de l'allocation
        bool        Array; // Est-ce un objet ou un tableau ?
    };
    typedef std::map<void*, TBlock> TBlockMap;

    TBlockMap          m_Blocks;      // Blocs de mémoire alloués
    std::stack<TBlock> m_DeleteStack; // Pile dont le sommet contient la ligne et le fichier de la prochaine désallocation
};

} //NameSpace

//***************************************************************************
// operator overloadings
//***************************************************************************

inline void* operator new(std::size_t Size, const char* File, int Line)
{
    return ZenLib::MemoryDebug::Instance().Allocate(Size, File, Line, false);
}
inline void* operator new[](std::size_t Size, const char* File, int Line)
{
    return ZenLib::MemoryDebug::Instance().Allocate(Size, File, Line, true);
}

inline void operator delete(void* Ptr)
{
    ZenLib::MemoryDebug::Instance().Free(Ptr, false);
}

inline void operator delete[](void* Ptr)
{
    ZenLib::MemoryDebug::Instance().Free(Ptr, true);
}

#if !defined(__BORLANDC__) // Borland does not support overloaded delete
inline void operator delete(void* Ptr, const char* File, int Line)
{
    ZenLib::MemoryDebug::Instance().NextDelete(File, Line);
    ZenLib::MemoryDebug::Instance().Free(Ptr, false);
}

inline void operator delete[](void* Ptr, const char* File, int Line)
{
    ZenLib::MemoryDebug::Instance().NextDelete(File, Line);
    ZenLib::MemoryDebug::Instance().Free(Ptr, true);
}
#endif

#if !defined(__MINGW32__) //TODO: Does not work on MinGW, don't know why
#ifndef new
    #define new         new(__FILE__, __LINE__)
#endif
#ifndef delete
    #define delete      ZenLib::MemoryDebug::Instance().NextDelete(__FILE__, __LINE__), delete
#endif
#endif // __MINGW32__

#endif // defined(ZENLIB_DEBUG)

#endif // ZenMemoryDebugH