8
8
#include < c_types.h>
9
9
#include < sys/reent.h>
10
10
11
+
11
12
extern " C" {
12
13
14
+ #if defined( UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE)
15
+ #define __umm_malloc (s ) umm_poison_malloc(s)
16
+ #define __umm_calloc (n,s ) umm_poison_calloc(n,s)
17
+ #define __umm_realloc_fl (p,s,f,l ) umm_poison_realloc_fl(p,s,f,l)
18
+ #define __umm_free_fl (p,f,l ) umm_poison_free_fl(p,f,l)
19
+
20
+ #ifdef UMM_POISON_CHECK_LITE
21
+ #define POISON_CHECK__ABORT () do {} while (0 )
22
+ #define POISON_CHECK__PANIC_FL (file, line ) do { (void )file; (void )line; } while (0 )
23
+
24
+ #else // Full heap poison check at every malloc libary call.
25
+ #define POISON_CHECK__ABORT () \
26
+ do { \
27
+ if ( ! POISON_CHECK () ) \
28
+ abort (); \
29
+ } while (0 )
30
+
31
+ #define POISON_CHECK__PANIC_FL (file, line ) \
32
+ do { \
33
+ if ( ! POISON_CHECK () ) \
34
+ __panic_func (file, line, " " ); \
35
+ } while (0 )
36
+ #endif
37
+
38
+ #undef realloc
39
+ #undef free
40
+
41
+ #else // ! UMM_POISON_CHECK
42
+ #define __umm_malloc (s ) malloc(s)
43
+ #define __umm_calloc (n,s ) calloc(n,s)
44
+ #define __umm_realloc_fl (p,s,f,l ) realloc(p,s)
45
+ #define __umm_free_fl (p,f,l ) free(p)
46
+
47
+ #define POISON_CHECK__ABORT () do {} while (0 )
48
+ #define POISON_CHECK__PANIC_FL (file, line ) do { (void )file; (void )line; } while (0 )
49
+ #endif // UMM_POISON_CHECK
50
+
13
51
// Debugging helper, last allocation which returned NULL
14
52
void *umm_last_fail_alloc_addr = NULL ;
15
53
int umm_last_fail_alloc_size = 0 ;
16
54
55
+
56
+ #ifdef UMM_INTEGRITY_CHECK
57
+ #define INTEGRITY_CHECK__ABORT () \
58
+ do { \
59
+ if ( ! INTEGRITY_CHECK () ) \
60
+ abort (); \
61
+ } while (0 )
62
+
63
+ #define INTEGRITY_CHECK__PANIC_FL (file, line ) \
64
+ do { \
65
+ if ( ! INTEGRITY_CHECK () ) \
66
+ __panic_func (file, line, " " ); \
67
+ } while (0 )
68
+
69
+ #else // ! UMM_INTEGRITY_CHECK
70
+ #define INTEGRITY_CHECK__ABORT () do {} while (0 )
71
+ #define INTEGRITY_CHECK__PANIC_FL (file, line ) do { (void )file; (void )line; } while (0 )
72
+
73
+ #endif // UMM_INTEGRITY_CHECK
74
+
75
+ #define PTR_CHECK__LOG_LAST_FAIL (p, s, a ) \
76
+ if (0 != (s) && 0 == p)\
77
+ {\
78
+ umm_last_fail_alloc_addr = a;\
79
+ umm_last_fail_alloc_size = s;\
80
+ }
81
+
17
82
void * _malloc_r (struct _reent * unused, size_t size)
18
83
{
19
84
(void ) unused;
20
85
void *ret = malloc (size);
21
- if (0 != size && 0 == ret) {
22
- umm_last_fail_alloc_addr = __builtin_return_address (0 );
23
- umm_last_fail_alloc_size = size;
24
- }
86
+ PTR_CHECK__LOG_LAST_FAIL (ret, size, __builtin_return_address (0 ))
25
87
return ret;
26
88
}
27
89
28
90
void _free_r (struct _reent * unused, void * ptr)
29
91
{
30
92
(void ) unused;
31
- return free (ptr);
93
+ free (ptr);
32
94
}
33
95
34
96
void * _realloc_r (struct _reent * unused, void * ptr, size_t size)
35
97
{
36
98
(void ) unused;
37
99
void *ret = realloc (ptr, size);
38
- if (0 != size && 0 == ret) {
39
- umm_last_fail_alloc_addr = __builtin_return_address (0 );
40
- umm_last_fail_alloc_size = size;
41
- }
100
+ PTR_CHECK__LOG_LAST_FAIL (ret, size, __builtin_return_address (0 ))
42
101
return ret;
43
102
}
44
103
45
104
void * _calloc_r (struct _reent * unused, size_t count, size_t size)
46
105
{
47
106
(void ) unused;
48
107
void *ret = calloc (count, size);
49
- if (0 != (count * size) && 0 == ret) {
50
- umm_last_fail_alloc_addr = __builtin_return_address (0 );
51
- umm_last_fail_alloc_size = count * size;
52
- }
108
+ PTR_CHECK__LOG_LAST_FAIL (ret, count * size, __builtin_return_address (0 ))
53
109
return ret;
54
110
}
55
111
56
- void ICACHE_RAM_ATTR vPortFree (void *ptr, const char * file, int line)
57
- {
58
- (void ) file;
59
- (void ) line;
60
- free (ptr);
61
- }
62
-
63
- #ifdef DEBUG_ESP_OOM
64
-
65
- void * ICACHE_RAM_ATTR pvPortMalloc (size_t size, const char * file, int line)
66
- {
67
- return malloc_loc (size, file, line);
68
- }
69
-
70
- void * ICACHE_RAM_ATTR pvPortCalloc (size_t count, size_t size, const char * file, int line)
71
- {
72
- return calloc_loc (count, size, file, line);
73
- }
74
-
75
- void * ICACHE_RAM_ATTR pvPortRealloc (void *ptr, size_t size, const char * file, int line)
76
- {
77
- return realloc_loc (ptr, size, file, line);
78
- }
79
112
80
- void * ICACHE_RAM_ATTR pvPortZalloc (size_t size, const char * file, int line)
81
- {
82
- return calloc_loc (1 , size, file, line);
83
- }
113
+ #define PTR_CHECK__LOG_LAST_FAIL (p, s, a ) \
114
+ if (0 != (s) && 0 == p)\
115
+ {\
116
+ umm_last_fail_alloc_addr = a;\
117
+ umm_last_fail_alloc_size = s;\
118
+ }
84
119
120
+ #ifdef DEBUG_ESP_OOM
85
121
#undef malloc
86
122
#undef calloc
87
123
#undef realloc
@@ -90,96 +126,115 @@ static const char oom_fmt[] PROGMEM STORE_ATTR = ":oom(%d)@?\n";
90
126
static const char oom_fmt_1[] PROGMEM STORE_ATTR = " :oom(%d)@" ;
91
127
static const char oom_fmt_2[] PROGMEM STORE_ATTR = " :%d\n " ;
92
128
93
- void * malloc (size_t s )
129
+ void ICACHE_RAM_ATTR print_loc (size_t size, const char * file, int line )
94
130
{
95
- void * ret = umm_malloc (s);
96
- if (!ret)
97
- os_printf (oom_fmt, (int )s);
98
- return ret;
131
+ DBGLOG_FUNCTION_P (oom_fmt_1, (int )size);
132
+ DBGLOG_FUNCTION_P (file);
133
+ DBGLOG_FUNCTION_P (oom_fmt_2, line);
99
134
}
100
135
101
- void * calloc (size_t n, size_t s)
102
- {
103
- void * ret = umm_calloc (n, s);
104
- if (!ret)
105
- os_printf (oom_fmt, (int )s);
106
- return ret;
107
- }
136
+ #define OOM_CHECK__PRINT_OOM (p, f, s ) if (!p) DBGLOG_FUNCTION_P(f, s)
137
+ #define OOM_CHECK__PRINT_LOC (p, s, f, l ) if (!p) print_loc(s, f, l)
108
138
109
- void * realloc (void * p, size_t s)
110
- {
111
- void * ret = umm_realloc (p, s);
112
- if (!ret)
113
- os_printf (oom_fmt, (int )s);
114
- return ret;
115
- }
139
+ #else // ! DEBUG_ESP_OOM
140
+
141
+ #if 1
142
+ // C Skip OOM check of pvPort... . It cost 64 more bytes of IRAM to turn on.
143
+ // C Was not previously enabled.
144
+ #undef PTR_CHECK__LOG_LAST_FAIL
145
+ #define PTR_CHECK__LOG_LAST_FAIL (p, s, a )
146
+ #endif
116
147
117
- void print_loc (size_t s, const char * file, int line)
148
+ #define OOM_CHECK__PRINT_OOM (p, f, s )
149
+ #define OOM_CHECK__PRINT_LOC (p, s, f, l )
150
+ #endif
151
+
152
+ #if defined(DEBUG_ESP_OOM) || defined(UMM_CHECK_POISON) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK)
153
+
154
+ void * ICACHE_RAM_ATTR malloc (size_t size)
118
155
{
119
- os_printf (oom_fmt_1, (int )s);
120
- os_printf (file);
121
- os_printf (oom_fmt_2, line);
156
+ INTEGRITY_CHECK__ABORT ();
157
+ POISON_CHECK__ABORT ();
158
+ void * ret = __umm_malloc (size);
159
+ PTR_CHECK__LOG_LAST_FAIL (ret, size, __builtin_return_address (0 ));
160
+ OOM_CHECK__PRINT_OOM (ret, oom_fmt, (int )size);
161
+ return ret;
122
162
}
123
163
124
- void * malloc_loc (size_t s, const char * file, int line )
164
+ void * ICACHE_RAM_ATTR calloc (size_t count, size_t size )
125
165
{
126
- void * ret = umm_malloc (s);
127
- if (!ret)
128
- print_loc (s, file, line);
166
+ INTEGRITY_CHECK__ABORT ();
167
+ POISON_CHECK__ABORT ();
168
+ void * ret = __umm_calloc (count, size);
169
+ PTR_CHECK__LOG_LAST_FAIL (ret, count * size, __builtin_return_address (0 ));
170
+ OOM_CHECK__PRINT_OOM (ret, oom_fmt, (int )size);
129
171
return ret;
130
172
}
131
173
132
- void * calloc_loc ( size_t n , size_t s, const char * file, int line )
174
+ void * ICACHE_RAM_ATTR realloc ( void * ptr , size_t size )
133
175
{
134
- void * ret = umm_calloc (n, s);
135
- if (!ret)
136
- print_loc (s, file, line);
176
+ INTEGRITY_CHECK__ABORT ();
177
+ void * ret = __umm_realloc_fl (ptr, size, NULL , 0 );
178
+ POISON_CHECK__ABORT ();
179
+ PTR_CHECK__LOG_LAST_FAIL (ret, size, __builtin_return_address (0 ));
180
+ OOM_CHECK__PRINT_OOM (ret, oom_fmt, (int )size);
137
181
return ret;
138
182
}
139
183
140
- void * realloc_loc (void * p, size_t s, const char * file, int line )
184
+ void ICACHE_RAM_ATTR free (void * p)
141
185
{
142
- void * ret = umm_realloc (p, s);
143
- if (!ret)
144
- print_loc (s, file, line);
145
- return ret;
186
+ INTEGRITY_CHECK__ABORT ();
187
+ __umm_free_fl (p, NULL , 0 );
188
+ POISON_CHECK__ABORT ();
146
189
}
190
+ #endif
147
191
148
- #else
149
192
150
193
void * ICACHE_RAM_ATTR pvPortMalloc (size_t size, const char * file, int line)
151
194
{
152
- (void ) file;
153
- (void ) line;
154
- return malloc (size);
195
+ INTEGRITY_CHECK__PANIC_FL (file, line);
196
+ POISON_CHECK__PANIC_FL (file, line);
197
+ void * ret = __umm_malloc (size);
198
+ PTR_CHECK__LOG_LAST_FAIL (ret, size, __builtin_return_address (0 ));
199
+ OOM_CHECK__PRINT_LOC (ret, size, file, line);
200
+ return ret;
155
201
}
156
202
157
203
void * ICACHE_RAM_ATTR pvPortCalloc (size_t count, size_t size, const char * file, int line)
158
204
{
159
- (void ) file;
160
- (void ) line;
161
- return calloc (count, size);
205
+ INTEGRITY_CHECK__PANIC_FL (file, line);
206
+ POISON_CHECK__PANIC_FL (file, line);
207
+ void * ret = __umm_calloc (count, size);
208
+ PTR_CHECK__LOG_LAST_FAIL (ret, count * size, __builtin_return_address (0 ));
209
+ OOM_CHECK__PRINT_LOC (ret, size, file, line);
210
+ return ret;
162
211
}
163
212
164
213
void * ICACHE_RAM_ATTR pvPortRealloc (void *ptr, size_t size, const char * file, int line)
165
214
{
166
- (void ) file;
167
- (void ) line;
168
- return realloc (ptr, size);
215
+ INTEGRITY_CHECK__PANIC_FL (file, line);
216
+ void * ret = __umm_realloc_fl (ptr, size, file, line);
217
+ POISON_CHECK__PANIC_FL (file, line);
218
+ PTR_CHECK__LOG_LAST_FAIL (ret, size, __builtin_return_address (0 ));
219
+ OOM_CHECK__PRINT_LOC (ret, size, file, line);
220
+ return ret;
169
221
}
170
222
171
223
void * ICACHE_RAM_ATTR pvPortZalloc (size_t size, const char * file, int line)
172
224
{
173
- (void ) file;
174
- (void ) line;
175
- return calloc (1 , size);
225
+ INTEGRITY_CHECK__PANIC_FL (file, line);
226
+ POISON_CHECK__PANIC_FL (file, line);
227
+ void * ret = __umm_calloc (1 , size);
228
+ PTR_CHECK__LOG_LAST_FAIL (ret, size, __builtin_return_address (0 ));
229
+ OOM_CHECK__PRINT_LOC (ret, size, file, line);
230
+ return ret;
176
231
}
177
232
178
- #endif // !defined(DEBUG_ESP_OOM)
179
-
180
- size_t xPortGetFreeHeapSize (void )
233
+ void ICACHE_RAM_ATTR vPortFree (void *ptr, const char * file, int line)
181
234
{
182
- return umm_free_heap_size ();
235
+ INTEGRITY_CHECK__PANIC_FL (file, line);
236
+ POISON_CHECK__PANIC_FL (file, line);
237
+ __umm_free_fl (ptr, file, line);
183
238
}
184
239
185
240
size_t ICACHE_RAM_ATTR xPortWantedSizeAlign (size_t size)
0 commit comments