forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.cpp
382 lines (327 loc) · 10.8 KB
/
heap.cpp
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* heap.c - overrides of SDK heap handling functions
* Copyright (c) 2016 Ivan Grokhotkov. All rights reserved.
* This file is distributed under MIT license.
*/
#include <stdlib.h>
#include "umm_malloc/umm_malloc.h"
extern "C" size_t umm_umul_sat(const size_t a, const size_t b);;
// Need FORCE_ALWAYS_INLINE to put HeapSelect class constructor/deconstructor in IRAM
#define FORCE_ALWAYS_INLINE_HEAP_SELECT
#include "umm_malloc/umm_heap_select.h"
#include <c_types.h>
#include <sys/reent.h>
#include <user_interface.h>
extern "C" {
#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE)
#define UMM_MALLOC(s) umm_poison_malloc(s)
#define UMM_CALLOC(n,s) umm_poison_calloc(n,s)
#define UMM_REALLOC_FL(p,s,f,l) umm_poison_realloc_fl(p,s,f,l)
#define UMM_FREE_FL(p,f,l) umm_poison_free_fl(p,f,l)
#define STATIC_ALWAYS_INLINE
#undef realloc
#undef free
#elif defined(DEBUG_ESP_OOM) || defined(UMM_INTEGRITY_CHECK)
#define UMM_MALLOC(s) umm_malloc(s)
#define UMM_CALLOC(n,s) umm_calloc(n,s)
#define UMM_REALLOC_FL(p,s,f,l) umm_realloc(p,s)
#define UMM_FREE_FL(p,f,l) umm_free(p)
#define STATIC_ALWAYS_INLINE
#undef realloc
#undef free
#else // ! UMM_POISON_CHECK && ! DEBUG_ESP_OOM
#define UMM_MALLOC(s) malloc(s)
#define UMM_CALLOC(n,s) calloc(n,s)
#define UMM_REALLOC_FL(p,s,f,l) realloc(p,s)
#define UMM_FREE_FL(p,f,l) free(p)
// STATIC_ALWAYS_INLINE only applies to the non-debug build path,
// it must not be enabled on the debug build path.
#define STATIC_ALWAYS_INLINE static ALWAYS_INLINE
#endif
#if defined(UMM_POISON_CHECK)
#define POISON_CHECK__ABORT() \
do { \
if ( ! POISON_CHECK() ) \
abort(); \
} while(0)
#define POISON_CHECK__PANIC_FL(file, line) \
do { \
if ( ! POISON_CHECK() ) \
__panic_func(file, line, ""); \
} while(0)
#else // No full heap poison checking.
#define POISON_CHECK__ABORT() do {} while(0)
#define POISON_CHECK__PANIC_FL(file, line) do { (void)file; (void)line; } while(0)
#endif
// Debugging helper, last allocation which returned NULL
void *umm_last_fail_alloc_addr = NULL;
int umm_last_fail_alloc_size = 0;
#if defined(DEBUG_ESP_OOM)
const char *umm_last_fail_alloc_file = NULL;
int umm_last_fail_alloc_line = 0;
#endif
#ifdef UMM_INTEGRITY_CHECK
#define INTEGRITY_CHECK__ABORT() \
do { \
if ( ! INTEGRITY_CHECK() ) \
abort(); \
} while(0)
#define INTEGRITY_CHECK__PANIC_FL(file, line) \
do { \
if ( ! INTEGRITY_CHECK() ) \
__panic_func(file, line, ""); \
} while(0)
#else // ! UMM_INTEGRITY_CHECK
#define INTEGRITY_CHECK__ABORT() do {} while(0)
#define INTEGRITY_CHECK__PANIC_FL(file, line) do { (void)file; (void)line; } while(0)
#endif // UMM_INTEGRITY_CHECK
#if defined(DEBUG_ESP_OOM)
#define PTR_CHECK__LOG_LAST_FAIL_FL(p, s, f, l) \
if(0 != (s) && 0 == p)\
{\
umm_last_fail_alloc_addr = __builtin_return_address(0);\
umm_last_fail_alloc_size = s;\
umm_last_fail_alloc_file = f;\
umm_last_fail_alloc_line = l;\
}
#define PTR_CHECK__LOG_LAST_FAIL(p, s) \
if(0 != (s) && 0 == p)\
{\
umm_last_fail_alloc_addr = __builtin_return_address(0);\
umm_last_fail_alloc_size = s;\
umm_last_fail_alloc_file = NULL;\
umm_last_fail_alloc_line = 0;\
}
#else
#define PTR_CHECK__LOG_LAST_FAIL_FL(p, s, f, l) \
(void)f;\
(void)l;\
if(0 != (s) && 0 == p)\
{\
umm_last_fail_alloc_addr = __builtin_return_address(0);\
umm_last_fail_alloc_size = s;\
}
#define PTR_CHECK__LOG_LAST_FAIL(p, s) \
if(0 != (s) && 0 == p)\
{\
umm_last_fail_alloc_addr = __builtin_return_address(0);\
umm_last_fail_alloc_size = s;\
}
#endif
void* _malloc_r(struct _reent* unused, size_t size)
{
(void) unused;
void *ret = malloc(size);
PTR_CHECK__LOG_LAST_FAIL(ret, size);
return ret;
}
void _free_r(struct _reent* unused, void* ptr)
{
(void) unused;
free(ptr);
}
void* _realloc_r(struct _reent* unused, void* ptr, size_t size)
{
(void) unused;
void *ret = realloc(ptr, size);
PTR_CHECK__LOG_LAST_FAIL(ret, size);
return ret;
}
void* _calloc_r(struct _reent* unused, size_t count, size_t size)
{
(void) unused;
void *ret = calloc(count, size);
PTR_CHECK__LOG_LAST_FAIL(ret, umm_umul_sat(count, size));
return ret;
}
#ifdef DEBUG_ESP_OOM
#undef malloc
#undef calloc
#undef realloc
#define DEBUG_HEAP_PRINTF ets_uart_printf
void IRAM_ATTR print_loc(size_t size, const char* file, int line)
{
(void)size;
(void)line;
if (system_get_os_print()) {
DEBUG_HEAP_PRINTF(":oom(%d)@", (int)size);
bool inISR = ETS_INTR_WITHINISR();
if (NULL == file || (inISR && (uint32_t)file >= 0x40200000)) {
DEBUG_HEAP_PRINTF("File: %p", file);
} else if (!inISR && (uint32_t)file >= 0x40200000) {
char buf[strlen_P(file) + 1];
strcpy_P(buf, file);
DEBUG_HEAP_PRINTF(buf);
} else {
DEBUG_HEAP_PRINTF(file);
}
DEBUG_HEAP_PRINTF(":%d\n", line);
}
}
void IRAM_ATTR print_oom_size(size_t size)
{
(void)size;
if (system_get_os_print()) {
DEBUG_HEAP_PRINTF(":oom(%d)@?\n", (int)size);
}
}
#define OOM_CHECK__PRINT_OOM(p, s) if ((s) && !(p)) print_oom_size(s)
#define OOM_CHECK__PRINT_LOC(p, s, f, l) if ((s) && !(p)) print_loc(s, f, l)
#else // ! DEBUG_ESP_OOM
#if 1
//C - to be discussed - is this what you want?
//C Skip OOM logging of last fail for malloc/... and pvPort... .
//C It cost 64 more bytes of IRAM to turn on. And was not previously enabled.
#undef PTR_CHECK__LOG_LAST_FAIL_FL
#define PTR_CHECK__LOG_LAST_FAIL_FL(p, s, f, l)
#undef PTR_CHECK__LOG_LAST_FAIL
#define PTR_CHECK__LOG_LAST_FAIL(p, s)
#endif
#define OOM_CHECK__PRINT_OOM(p, s)
#define OOM_CHECK__PRINT_LOC(p, s, f, l)
#endif
#if defined(DEBUG_ESP_OOM) || defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK)
/*
The thinking behind the ordering of Integrity Check, Full Poison Check, and
the specific *alloc function.
1. Integrity Check - verifies the heap management information is not corrupt.
This allows any other testing, that walks the heap, to run safely.
2. Place Full Poison Check before or after a specific *alloc function?
a. After, when the *alloc function operates on an existing allocation.
b. Before, when the *alloc function creates a new, not modified, allocation.
In a free() or realloc() call, the focus is on their allocation. It is
checked 1st and reported on 1ST if an error exists. Full Poison Check is
done after.
For malloc(), calloc(), and zalloc() Full Poison Check is done 1st since
these functions do not modify an existing allocation.
*/
void* IRAM_ATTR malloc(size_t size)
{
INTEGRITY_CHECK__ABORT();
POISON_CHECK__ABORT();
void* ret = UMM_MALLOC(size);
PTR_CHECK__LOG_LAST_FAIL(ret, size);
OOM_CHECK__PRINT_OOM(ret, size);
return ret;
}
void* IRAM_ATTR calloc(size_t count, size_t size)
{
INTEGRITY_CHECK__ABORT();
POISON_CHECK__ABORT();
void* ret = UMM_CALLOC(count, size);
#if defined(DEBUG_ESP_OOM)
size_t total_size = umm_umul_sat(count, size);// For logging purposes
#endif
PTR_CHECK__LOG_LAST_FAIL(ret, total_size);
OOM_CHECK__PRINT_OOM(ret, total_size);
return ret;
}
void* IRAM_ATTR realloc(void* ptr, size_t size)
{
INTEGRITY_CHECK__ABORT();
void* ret = UMM_REALLOC_FL(ptr, size, NULL, 0);
POISON_CHECK__ABORT();
PTR_CHECK__LOG_LAST_FAIL(ret, size);
OOM_CHECK__PRINT_OOM(ret, size);
return ret;
}
void IRAM_ATTR free(void* p)
{
INTEGRITY_CHECK__ABORT();
UMM_FREE_FL(p, NULL, 0);
POISON_CHECK__ABORT();
}
#endif
STATIC_ALWAYS_INLINE
void* IRAM_ATTR heap_pvPortMalloc(size_t size, const char* file, int line)
{
INTEGRITY_CHECK__PANIC_FL(file, line);
POISON_CHECK__PANIC_FL(file, line);
void* ret = UMM_MALLOC(size);
PTR_CHECK__LOG_LAST_FAIL_FL(ret, size, file, line);
OOM_CHECK__PRINT_LOC(ret, size, file, line);
return ret;
}
STATIC_ALWAYS_INLINE
void* IRAM_ATTR heap_pvPortCalloc(size_t count, size_t size, const char* file, int line)
{
INTEGRITY_CHECK__PANIC_FL(file, line);
POISON_CHECK__PANIC_FL(file, line);
void* ret = UMM_CALLOC(count, size);
#if defined(DEBUG_ESP_OOM)
size_t total_size = umm_umul_sat(count, size);
#endif
PTR_CHECK__LOG_LAST_FAIL_FL(ret, total_size, file, line);
OOM_CHECK__PRINT_LOC(ret, total_size, file, line);
return ret;
}
STATIC_ALWAYS_INLINE
void* IRAM_ATTR heap_pvPortRealloc(void *ptr, size_t size, const char* file, int line)
{
INTEGRITY_CHECK__PANIC_FL(file, line);
void* ret = UMM_REALLOC_FL(ptr, size, file, line);
POISON_CHECK__PANIC_FL(file, line);
PTR_CHECK__LOG_LAST_FAIL_FL(ret, size, file, line);
OOM_CHECK__PRINT_LOC(ret, size, file, line);
return ret;
}
STATIC_ALWAYS_INLINE
void* IRAM_ATTR heap_pvPortZalloc(size_t size, const char* file, int line)
{
INTEGRITY_CHECK__PANIC_FL(file, line);
POISON_CHECK__PANIC_FL(file, line);
void* ret = UMM_CALLOC(1, size);
PTR_CHECK__LOG_LAST_FAIL_FL(ret, size, file, line);
OOM_CHECK__PRINT_LOC(ret, size, file, line);
return ret;
}
STATIC_ALWAYS_INLINE
void IRAM_ATTR heap_vPortFree(void *ptr, const char* file, int line)
{
INTEGRITY_CHECK__PANIC_FL(file, line);
UMM_FREE_FL(ptr, file, line);
POISON_CHECK__PANIC_FL(file, line);
}
size_t IRAM_ATTR xPortWantedSizeAlign(size_t size)
{
return (size + 3) & ~((size_t) 3);
}
void system_show_malloc(void)
{
HeapSelectDram ephemeral;
umm_info(NULL, true);
}
/*
NONOS SDK and lwIP do not handle IRAM heap well. Since they also use portable
malloc calls pvPortMalloc, ... we can leverage that for this solution.
Force pvPortMalloc, ... APIs to serve DRAM only.
*/
void* IRAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
{
HeapSelectDram ephemeral;
return heap_pvPortMalloc(size, file, line);;
}
void* IRAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line)
{
HeapSelectDram ephemeral;
return heap_pvPortCalloc(count, size, file, line);
}
void* IRAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line)
{
HeapSelectDram ephemeral;
return heap_pvPortRealloc(ptr, size, file, line);
}
void* IRAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
{
HeapSelectDram ephemeral;
return heap_pvPortZalloc(size, file, line);
}
void IRAM_ATTR vPortFree(void *ptr, const char* file, int line)
{
#if defined(DEBUG_ESP_OOM) || defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK)
// This is only needed for debug checks to ensure they are performed in
// correct context. umm_malloc free internally determines the correct heap.
HeapSelectDram ephemeral;
#endif
return heap_vPortFree(ptr, file, line);
}
};