-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathmemorystat.h
183 lines (146 loc) · 3.64 KB
/
memorystat.h
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#pragma once
#include "config.h"
#if USE_MEMORYSTAT
#ifdef __cplusplus
#include <new>
#include <cstdlib>
#include <cstring>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__GNUC__)
#include <malloc.h>
#endif
struct MemoryStat {
size_t mallocCount;
size_t reallocCount;
size_t freeCount;
size_t currentSize;
size_t peakSize;
};
class Memory {
public:
static Memory& Instance() {
static Memory memory;
return memory;
}
void* Malloc(size_t size) {
void* p = malloc(size);
MallocStat(GetMallocSize(p));
return p;
}
void* Realloc(void* ptr, size_t size) {
if (ptr)
FreeStat(GetMallocSize(ptr));
void *p = realloc(ptr, size);
ReallocStat(GetMallocSize(p));
return p;
}
void Free(void* ptr) {
if (ptr) {
FreeStat(GetMallocSize(ptr));
free(ptr);
}
}
const MemoryStat& GetStat() { return *stat_; }
MemoryStat* SetStat(MemoryStat* stat) {
MemoryStat* old = stat_;
stat_ = stat;
return old;
}
void MallocStat(size_t size) {
stat_->currentSize += size;
if (stat_->peakSize < stat_->currentSize)
stat_->peakSize = stat_->currentSize;
stat_->mallocCount++;
}
void ReallocStat(size_t size) {
stat_->currentSize += size;
if (stat_->peakSize < stat_->currentSize)
stat_->peakSize = stat_->currentSize;
stat_->reallocCount++;
}
void FreeStat(size_t size) {
stat_->currentSize -= size;
stat_->freeCount++;
}
private:
size_t GetMallocSize(void* ptr) {
#if defined(_MSC_VER)
return _msize(ptr);
#elif defined(__APPLE__)
return malloc_size(ptr);
#elif defined(__GNUC__)
return malloc_usable_size(ptr);
#else
#error Must implement Memory::GetMallocSize() in memorystat.h
#endif
}
Memory() : stat_(&global_) { memset(&global_, 0, sizeof(global_)); }
MemoryStat global_;
MemoryStat* stat_;
};
class MemoryStatScope {
public:
MemoryStatScope() : old_(Memory::Instance().SetStat(&local_)) {
memset(&local_, 0, sizeof(local_));
}
~MemoryStatScope() {
old_->mallocCount += local_.mallocCount;
old_->reallocCount += local_.reallocCount;
old_->freeCount += local_.freeCount;
old_->peakSize += local_.peakSize;
old_->currentSize += local_.currentSize;
Memory::Instance().SetStat(old_);
}
private:
MemoryStat local_;
MemoryStat* old_;
};
#define MEMORYSTAT_SCOPE() MemoryStatScope scope##__LINE__
#endif // __cplusplus
#ifdef __cplusplus
extern "C" {
#else
#include <stdlib.h>
#endif
extern void* MemoryStatMalloc(size_t size);
extern void* MemoryStatCalloc(size_t num, size_t size);
extern void* MemoryStatRealloc(void* ptr, size_t size);
extern void MemoryStatFree(void* ptr);
#ifdef __cplusplus
}
#endif
#define malloc MemoryStatMalloc
#define calloc MemoryStatCalloc
#define realloc MemoryStatRealloc
#define free MemoryStatFree
#ifdef __cplusplus
namespace std {
inline void* MemoryStatMalloc(size_t size) {
return ::MemoryStatMalloc(size);
}
inline void* MemoryStatRealloc(void* ptr, size_t size) {
return ::MemoryStatRealloc(ptr, size);
}
inline void MemoryStatFree(void* ptr) {
return ::MemoryStatFree(ptr);
}
};
#endif
#else // USE_MEMORYSTAT
#define MEMORYSTAT_SCOPE()
#endif
// Override strdup
#include <string.h>
#include <stdlib.h>
#ifdef strdup
#undef strdup
#endif
#define strdup StrDup
#ifdef __cplusplus
extern "C" {
#endif
char* StrDup(const char* src);
#ifdef __cplusplus
}
#endif