forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.cpp
More file actions
328 lines (256 loc) · 8.14 KB
/
Memory.cpp
File metadata and controls
328 lines (256 loc) · 8.14 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Core/Memory.h>
#include <MicroOcpp/Debug.h>
#if MO_OVERRIDE_ALLOCATION && MO_ENABLE_HEAP_PROFILER
#include <map>
namespace MicroOcpp {
namespace Memory {
struct MemBlockInfo {
void* tagger_ptr = nullptr;
std::string tag;
size_t size = 0;
MemBlockInfo(void* ptr, const char *tag, size_t size) : size{size} {
updateTag(ptr, tag);
}
void updateTag(void* ptr, const char *tag);
};
std::map<void*,MemBlockInfo> memBlocks; //key: memory address of malloc'd block
struct MemTagInfo {
size_t current_size = 0;
size_t max_size = 0;
MemTagInfo(size_t size) {
operator+=(size);
}
void operator+=(size_t size) {
current_size += size;
max_size = std::max(max_size, current_size);
}
void operator-=(size_t size) {
if (size > current_size) {
MO_DBG_ERR("captured size does not fit");
//return; let it happen for now
}
current_size -= size;
}
void reset() {
max_size = current_size;
}
};
std::map<std::string,MemTagInfo> memTags;
size_t memTotal, memTotalMax;
void MemBlockInfo::updateTag(void* ptr, const char *tag) {
if (!tag) {
return;
}
if (tagger_ptr == nullptr || ptr < tagger_ptr) {
MO_DBG_VERBOSE("update tag from %s to %s, ptr from %p to %p", this->tag.c_str(), tag, tagger_ptr, ptr);
auto tagInfo = memTags.find(this->tag);
if (tagInfo != memTags.end()) {
tagInfo->second -= size;
}
tagInfo = memTags.find(tag);
if (tagInfo != memTags.end()) {
tagInfo->second += size;
} else {
memTags.emplace(tag, size);
}
tagger_ptr = ptr;
this->tag = tag;
}
}
} //namespace Memory
} //namespace MicroOcpp
#endif //MO_OVERRIDE_ALLOCATION && MO_ENABLE_HEAP_PROFILER
#if MO_OVERRIDE_ALLOCATION
namespace MicroOcpp {
namespace Memory {
void* (*malloc_override)(size_t);
void (*free_override)(void*);
}
}
using namespace MicroOcpp::Memory;
void mo_mem_set_malloc_free(void* (*malloc_override)(size_t), void (*free_override)(void*)) {
MicroOcpp::Memory::malloc_override = malloc_override;
MicroOcpp::Memory::free_override = free_override;
}
void *mo_mem_malloc(const char *tag, size_t size) {
MO_DBG_VERBOSE("malloc %zu B (%s)", size, tag ? tag : "unspecified");
void *ptr;
if (malloc_override) {
ptr = malloc_override(size);
} else {
ptr = malloc(size);
}
#if MO_ENABLE_HEAP_PROFILER
if (ptr) {
memBlocks.emplace(ptr, MemBlockInfo(ptr, tag, size));
memTotal += size;
memTotalMax = std::max(memTotalMax, memTotal);
}
#endif
return ptr;
}
void mo_mem_free(void* ptr) {
MO_DBG_VERBOSE("free");
#if MO_ENABLE_HEAP_PROFILER
if (ptr) {
auto blockInfo = memBlocks.find(ptr);
if (blockInfo != memBlocks.end()) {
auto tagInfo = memTags.find(blockInfo->second.tag);
if (tagInfo != memTags.end()) {
tagInfo->second -= blockInfo->second.size;
}
memTotal -= blockInfo->second.size;
}
if (blockInfo != memBlocks.end()) {
memBlocks.erase(blockInfo);
}
}
#endif
if (free_override) {
free_override(ptr);
} else {
free(ptr);
}
}
#endif //MO_OVERRIDE_ALLOCATION
#if MO_OVERRIDE_ALLOCATION && MO_ENABLE_HEAP_PROFILER
void mo_mem_deinit() {
memBlocks.clear();
memTags.clear();
}
void mo_mem_reset() {
MO_DBG_DEBUG("Reset all maximum values to current values");
for (auto tagInfo = (memTags).begin(); tagInfo != memTags.end(); ++tagInfo) {
tagInfo->second.reset();
}
memTotalMax = memTotal;
}
void mo_mem_set_tag(void *ptr, const char *tag) {
MO_DBG_VERBOSE("set tag (%s)", tag ? tag : "unspecified");
if (!tag) {
return;
}
bool hasTagged = false;
if (tag) {
auto foundBlock = memBlocks.upper_bound(ptr);
if (foundBlock != memBlocks.begin()) {
--foundBlock;
}
if (foundBlock != memBlocks.end() &&
(unsigned char*)ptr - (unsigned char*)foundBlock->first < (std::ptrdiff_t)foundBlock->second.size) {
foundBlock->second.updateTag(ptr, tag);
hasTagged = true;
}
}
if (!hasTagged) {
MO_DBG_VERBOSE("memory area doesn't apply");
}
}
void mo_mem_print_stats() {
MO_CONSOLE_PRINTF("\n *** Heap usage statistics ***\n");
size_t size = 0;
size_t untagged = 0, untagged_size = 0;
for (const auto& heapEntry : memBlocks) {
size += heapEntry.second.size;
#if MO_DBG_LEVEL >= MO_DL_VERBOSE
{
MO_CONSOLE_PRINTF("@%p - %zu B (%s)\n", heapEntry.first, heapEntry.second.size, heapEntry.second.tag.c_str());
}
#endif
if (heapEntry.second.tag.empty()) {
untagged ++;
untagged_size += heapEntry.second.size;
}
}
std::map<std::string, size_t> tags;
for (const auto& heapEntry : memBlocks) {
auto foundTag = tags.find(heapEntry.second.tag);
if (foundTag != tags.end()) {
foundTag->second += heapEntry.second.size;
} else {
tags.emplace(heapEntry.second.tag, heapEntry.second.size);
}
}
size_t size_control = 0;
for (const auto& tag : tags) {
size_control += tag.second;
#if MO_DBG_LEVEL >= MO_DL_VERBOSE
{
MO_CONSOLE_PRINTF("%s - %zu B\n", tag.first.c_str(), tag.second);
}
#endif
}
size_t size_control2 = 0;
for (const auto& tag : memTags) {
size_control2 += tag.second.current_size;
MO_CONSOLE_PRINTF("%s - %zu B (max. %zu B)\n", tag.first.c_str(), tag.second.current_size, tag.second.max_size);
}
MO_CONSOLE_PRINTF(" *** Summary ***\nBlocks: %zu\nTags: %zu\nCurrent usage: %zu B\nMaximum usage: %zu B\n", memBlocks.size(), memTags.size(), memTotal, memTotalMax);
#if MO_DBG_LEVEL >= MO_DL_DEBUG
{
MO_CONSOLE_PRINTF(" *** Debug information ***\nTotal blocks (control value 1): %zu B\nTags (control value): %zu\nTotal tagged (control value 2): %zu B\nTotal tagged (control value 3): %zu B\nUntagged: %zu\nTotal untagged: %zu B\n", size, tags.size(), size_control, size_control2, untagged, untagged_size);
}
#endif
}
int mo_mem_write_stats_json(char *buf, size_t size) {
DynamicJsonDocument doc {size * 2};
doc["total_current"] = memTotal;
doc["total_max"] = memTotalMax;
doc["total_blocks"] = memBlocks.size();
JsonArray by_tag = doc.createNestedArray("by_tag");
for (const auto& tag : memTags) {
JsonObject entry = by_tag.createNestedObject();
entry["tag"] = tag.first.c_str();
entry["current"] = tag.second.current_size;
entry["max"] = tag.second.max_size;
}
size_t untagged = 0, untagged_size = 0;
for (const auto& heapEntry : memBlocks) {
if (heapEntry.second.tag.empty()) {
untagged ++;
untagged_size += heapEntry.second.size;
}
}
doc["untagged_blocks"] = untagged;
doc["untagged_size"] = untagged_size;
if (doc.overflowed()) {
MO_DBG_ERR("exceeded JSON capacity");
return -1;
}
return (int)serializeJson(doc, buf, size);
}
#endif //MO_OVERRIDE_ALLOCATION && MO_ENABLE_HEAP_PROFILER
namespace MicroOcpp {
String makeString(const char *tag, const char *val) {
#if MO_OVERRIDE_ALLOCATION
if (val) {
return String(val, Allocator<char>(tag));
} else {
return String(Allocator<char>(tag));
}
#else
if (val) {
return String(val);
} else {
return String();
}
#endif
}
JsonDoc initJsonDoc(const char *tag, size_t capacity) {
#if MO_OVERRIDE_ALLOCATION
return JsonDoc(capacity, ArduinoJsonAllocator(tag));
#else
return JsonDoc(capacity);
#endif
}
std::unique_ptr<JsonDoc> makeJsonDoc(const char *tag, size_t capacity) {
#if MO_OVERRIDE_ALLOCATION
return std::unique_ptr<JsonDoc>(new JsonDoc(capacity, ArduinoJsonAllocator(tag)));
#else
return std::unique_ptr<JsonDoc>(new JsonDoc(capacity));
#endif
}
}