Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
244 chris 1
#ifndef __SEXYMEMMGR_H__
2
#define __SEXYMEMMGR_H__
3
 
4
//////////////////////////////////////////////////////////////////////////
5
//                                              HOW TO USE THIS FILE
6
//
7
//                      In the desired .CPP file (NOT header file), AFTER ALL of your
8
//      #include declarations, do a #include "memmgr.h" or whatever you renamed
9
//      this file to. It's very important that you do it only in the .cpp and
10
//      after every other include file, otherwise it won't compile.  The memory leaks
11
//  will appear in a file called mem_leaks.txt and they will also be printed out
12
//  in the output window when the program exits.
13
//
14
//////////////////////////////////////////////////////////////////////////
15
 
16
 
17
#include <list>
18
#include <stdio.h>
19
#include <stdlib.h>
20
 
21
extern void SexyDumpUnfreed();
22
 
23
#if defined(SEXY_MEMTRACE) && !defined(RELEASEFINAL)
24
 
25
/************************************************************************/
26
/* DO NOT CALL THESE TWO METHODS DIRECTLY                                                               */
27
/************************************************************************/
28
void SexyMemAddTrack(void* addr,  int asize,  const char *fname, int lnum);
29
void SexyMemRemoveTrack(void *addr);
30
 
31
 
32
//Replacement for the standard "new" operator, records size of allocation and 
33
//the file/line number it was on
34
inline void* __cdecl operator new(unsigned int size, const char* file, int line)
35
{
36
        void* ptr = (void*)malloc(size);
37
        SexyMemAddTrack(ptr, size, file, line);
38
        return(ptr);
39
}
40
 
41
//Same as above, but for arrays
42
inline void* __cdecl operator new[](unsigned int size, const char* file, int line)
43
{
44
        void* ptr = (void*)malloc(size);
45
        SexyMemAddTrack(ptr, size, file, line);
46
        return(ptr);
47
}
48
 
49
 
50
// These single argument new operators allow vc6 apps to compile without errors
51
inline void* __cdecl operator new(unsigned int size)
52
{
53
        void* ptr = (void*)malloc(size);
54
        return(ptr);
55
}
56
 
57
inline void* __cdecl operator new[](unsigned int size)
58
{
59
        void* ptr = (void*)malloc(size);
60
        return(ptr);
61
}
62
 
63
 
64
//custom delete operators
65
inline void __cdecl operator delete(void* p)
66
{
67
        SexyMemRemoveTrack(p);
68
        free(p);
69
}
70
 
71
inline void __cdecl operator delete[](void* p)
72
{
73
        SexyMemRemoveTrack(p);
74
        free(p);
75
}
76
 
77
//needed in case in the constructor of the class we're newing, it throws an exception
78
inline void __cdecl operator delete(void* pMem, const char *file, int line)
79
{
80
        free(pMem);
81
}
82
 
83
inline void __cdecl operator delete[](void* pMem, const char *file, int line)
84
{
85
        free(pMem);
86
}
87
 
88
 
89
#define DEBUG_NEW new(__FILE__, __LINE__)
90
#define new DEBUG_NEW
91
 
92
 
93
#endif // SEXY_MEMTRACE
94
 
95
 
96
#endif