@@ -84,6 +84,19 @@ extern uint32_t UMM_MALLOC_CFG_HEAP_SIZE;
84
84
* CRT0 init.
85
85
*/
86
86
87
+ #if 0 // Must be zero at release
88
+ #warning "Macro NON_NULL_CONTEXT_ASSERT() is active!"
89
+ /*
90
+ * Keep for future debug/maintenance of umm_malloc. Not needed in a
91
+ * regular/debug build. Call paths that use NON_NULL_CONTEXT_ASSERT logically
92
+ * guard against returning NULL. This macro double-checks that assumption during
93
+ * development.
94
+ */
95
+ #define NON_NULL_CONTEXT_ASSERT() assert((NULL != _context))
96
+ #else
97
+ #define NON_NULL_CONTEXT_ASSERT () (void )0
98
+ #endif
99
+
87
100
#include " umm_local.h" // target-dependent supplemental
88
101
89
102
/* ------------------------------------------------------------------------- */
@@ -213,21 +226,41 @@ int umm_get_heap_stack_index(void) {
213
226
* realloc or free since you may not be in the right heap to handle it.
214
227
*
215
228
*/
216
- static bool test_ptr_context (size_t which, void *ptr) {
229
+ static bool test_ptr_context (const size_t which, const void *const ptr) {
217
230
return
218
231
heap_context[which].heap &&
219
232
ptr >= (void *)heap_context[which].heap &&
220
233
ptr < heap_context[which].heap_end ;
221
234
}
222
235
223
- static umm_heap_context_t *umm_get_ptr_context (void *ptr) {
236
+ /*
237
+ * Find Heap context by allocation address - may return NULL
238
+ */
239
+ umm_heap_context_t *_umm_get_ptr_context (const void *const ptr) {
224
240
for (size_t i = 0 ; i < UMM_NUM_HEAPS; i++) {
225
241
if (test_ptr_context (i, ptr)) {
226
242
return umm_get_heap_by_id (i);
227
243
}
228
244
}
229
245
230
- panic ();
246
+ return NULL ;
247
+ }
248
+
249
+ /*
250
+ * Find Heap context by allocation address - must either succeed or abort
251
+ */
252
+ static umm_heap_context_t *umm_get_ptr_context (const void *const ptr) {
253
+ umm_heap_context_t *const _context = _umm_get_ptr_context (ptr);
254
+ if (_context) {
255
+ return _context;
256
+ }
257
+
258
+ [[maybe_unused]] uintptr_t sketch_ptr = (uintptr_t )ptr;
259
+ #if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE)
260
+ sketch_ptr += sizeof (UMM_POISONED_BLOCK_LEN_TYPE) + UMM_POISON_SIZE_BEFORE;
261
+ #endif
262
+ DBGLOG_ERROR (" \n Pointer %p is not a Heap address.\n " , (void *)sketch_ptr);
263
+ abort ();
231
264
return NULL ;
232
265
}
233
266
@@ -579,10 +612,7 @@ static void umm_free_core(umm_heap_context_t *_context, void *ptr) {
579
612
580
613
uint16_t c;
581
614
582
- if (NULL == _context) {
583
- panic ();
584
- return ;
585
- }
615
+ NON_NULL_CONTEXT_ASSERT ();
586
616
587
617
STATS__FREE_REQUEST (id_free);
588
618
/*
@@ -672,12 +702,9 @@ static void *umm_malloc_core(umm_heap_context_t *_context, size_t size) {
672
702
673
703
uint16_t cf;
674
704
675
- STATS__ALLOC_REQUEST (id_malloc, size );
705
+ NON_NULL_CONTEXT_ASSERT ( );
676
706
677
- if (NULL == _context) {
678
- panic ();
679
- return NULL ;
680
- }
707
+ STATS__ALLOC_REQUEST (id_malloc, size);
681
708
682
709
blocks = umm_blocks (size);
683
710
@@ -925,10 +952,7 @@ void *umm_realloc(void *ptr, size_t size) {
925
952
926
953
/* Need to be in the heap in which this block lives */
927
954
umm_heap_context_t *_context = umm_get_ptr_context (ptr);
928
- if (NULL == _context) {
929
- panic ();
930
- return NULL ;
931
- }
955
+ NON_NULL_CONTEXT_ASSERT ();
932
956
933
957
if (0 == size) {
934
958
DBGLOG_DEBUG (" realloc to 0 size, just free the block\n " );
0 commit comments