From ed58a3d3681c93eb136c9710de1ef3d0fb85b5b3 Mon Sep 17 00:00:00 2001 From: M Hightower <27247790+mhightower83@users.noreply.github.com> Date: Mon, 10 May 2021 16:52:13 -0700 Subject: [PATCH] [UMM-55] Update UMM_CRITICAL... macros to support id tags - Added UMM_CRITICAL... to integrity and poison check. - Added UMM_CRITICAL_DECL macro to support defining and initializing synchronization objects. - Added an ID string to UMM_CRITICAL... macros to facilitate making unique names, etc. --- src/umm_info.c | 8 +++++--- src/umm_integrity.c | 3 +++ src/umm_malloc.c | 19 +++++++++++-------- src/umm_malloc_cfg.h | 44 ++++++++++++++++++++++++++++++-------------- src/umm_poison.c | 5 +++++ 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/umm_info.c b/src/umm_info.c index b49999a..a7066ca 100644 --- a/src/umm_info.c +++ b/src/umm_info.c @@ -44,10 +44,12 @@ void compute_fragmentation_metric(void) void *umm_info(void *ptr, bool force) { uint16_t blockNo = 0; + UMM_CRITICAL_DECL(id_info); + UMM_CHECK_INITIALIZED(); /* Protect the critical section... */ - UMM_CRITICAL_ENTRY(); + UMM_CRITICAL_ENTRY(id_info); /* * Clear out all of the entries in the ummHeapInfo structure before doing @@ -105,7 +107,7 @@ void *umm_info(void *ptr, bool force) { if (ptr == &UMM_BLOCK(blockNo)) { /* Release the critical section... */ - UMM_CRITICAL_EXIT(); + UMM_CRITICAL_EXIT(id_info); return ptr; } @@ -163,7 +165,7 @@ void *umm_info(void *ptr, bool force) { DBGLOG_FORCE(force, "+--------------------------------------------------------------+\n"); /* Release the critical section... */ - UMM_CRITICAL_EXIT(); + UMM_CRITICAL_EXIT(id_info); return NULL; } diff --git a/src/umm_integrity.c b/src/umm_integrity.c index d26931f..7f42b85 100644 --- a/src/umm_integrity.c +++ b/src/umm_integrity.c @@ -27,6 +27,7 @@ * chain. */ bool umm_integrity_check(void) { + UMM_CRITICAL_DECL(id_integrity); bool ok = true; uint16_t prev; uint16_t cur; @@ -35,6 +36,7 @@ bool umm_integrity_check(void) { /* Iterate through all free blocks */ prev = 0; + UMM_CRITICAL_ENTRY(id_integrity); while (1) { cur = UMM_NFREE(prev); @@ -119,6 +121,7 @@ bool umm_integrity_check(void) { } clean: + UMM_CRITICAL_EXIT(id_integrity); if (!ok) { UMM_HEAP_CORRUPTION_CB(); } diff --git a/src/umm_malloc.c b/src/umm_malloc.c index 2b04f34..ec55909 100644 --- a/src/umm_malloc.c +++ b/src/umm_malloc.c @@ -344,7 +344,7 @@ void umm_init(void) { /* ------------------------------------------------------------------------ * Must be called only from within critical sections guarded by - * UMM_CRITICAL_ENTRY() and UMM_CRITICAL_EXIT(). + * UMM_CRITICAL_ENTRY(id) and UMM_CRITICAL_EXIT(id). */ static void umm_free_core(void *ptr) { @@ -398,6 +398,7 @@ static void umm_free_core(void *ptr) { /* ------------------------------------------------------------------------ */ void umm_free(void *ptr) { + UMM_CRITICAL_DECL(id_free); UMM_CHECK_INITIALIZED(); @@ -411,16 +412,16 @@ void umm_free(void *ptr) { /* Free the memory withing a protected critical section */ - UMM_CRITICAL_ENTRY(); + UMM_CRITICAL_ENTRY(id_free); umm_free_core(ptr); - UMM_CRITICAL_EXIT(); + UMM_CRITICAL_EXIT(id_free); } /* ------------------------------------------------------------------------ * Must be called only from within critical sections guarded by - * UMM_CRITICAL_ENTRY() and UMM_CRITICAL_EXIT(). + * UMM_CRITICAL_ENTRY(id) and UMM_CRITICAL_EXIT(id). */ static void *umm_malloc_core(size_t size) { @@ -535,6 +536,7 @@ static void *umm_malloc_core(size_t size) { /* ------------------------------------------------------------------------ */ void *umm_malloc(size_t size) { + UMM_CRITICAL_DECL(id_malloc); void *ptr = NULL; @@ -555,11 +557,11 @@ void *umm_malloc(size_t size) { /* Allocate the memory withing a protected critical section */ - UMM_CRITICAL_ENTRY(); + UMM_CRITICAL_ENTRY(id_malloc); ptr = umm_malloc_core(size); - UMM_CRITICAL_EXIT(); + UMM_CRITICAL_EXIT(id_malloc); return ptr; } @@ -567,6 +569,7 @@ void *umm_malloc(size_t size) { /* ------------------------------------------------------------------------ */ void *umm_realloc(void *ptr, size_t size) { + UMM_CRITICAL_DECL(id_realloc); uint16_t blocks; uint16_t blockSize; @@ -631,7 +634,7 @@ void *umm_realloc(void *ptr, size_t size) { curSize = (blockSize * UMM_BLOCKSIZE) - (sizeof(((umm_block *)0)->header)); /* Protect the critical section... */ - UMM_CRITICAL_ENTRY(); + UMM_CRITICAL_ENTRY(id_realloc); /* Now figure out if the previous and/or next blocks are free as well as * their sizes - this will help us to minimize special code later when we @@ -748,7 +751,7 @@ void *umm_realloc(void *ptr, size_t size) { } /* Release the critical section... */ - UMM_CRITICAL_EXIT(); + UMM_CRITICAL_EXIT(id_realloc); return ptr; } diff --git a/src/umm_malloc_cfg.h b/src/umm_malloc_cfg.h index 1c273b5..f50ff3d 100644 --- a/src/umm_malloc_cfg.h +++ b/src/umm_malloc_cfg.h @@ -242,31 +242,47 @@ extern int umm_fragmentation_metric(void); #endif /* - * A couple of macros to make it easier to protect the memory allocator - * in a multitasking system. You should set these macros up to use whatever - * your system uses for this purpose. You can disable interrupts entirely, or - * just disable task switching - it's up to you + * Three macros to make it easier to protect the memory allocator in a + * multitasking system. You should set these macros up to use whatever your + * system uses for this purpose. You can disable interrupts entirely, or just + * disable task switching - it's up to you + * + * If needed, UMM_CRITICAL_DECL can be used to declare or initialize + * synchronization elements before their use. "tag" can be used to add context + * uniqueness to the declaration. + * exp. #define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag + * Another possible use for "tag", activity identifier when profiling time + * spent in UMM_CRITICAL. The "tag" values used are id_malloc, id_realloc, + * id_free, id_poison, id_integrity, and id_info. * * NOTE WELL that these macros MUST be allowed to nest, because umm_free() is * called from within umm_malloc() */ +#ifndef UMM_CRITICAL_DECL + #define UMM_CRITICAL_DECL(tag) +#endif + #ifdef UMM_MAX_CRITICAL_DEPTH_CHECK extern int umm_critical_depth; extern int umm_max_critical_depth; - #define UMM_CRITICAL_ENTRY() { \ - ++umm_critical_depth; \ - if (umm_critical_depth > umm_max_critical_depth) { \ - umm_max_critical_depth = umm_critical_depth; \ - } \ -} - #define UMM_CRITICAL_EXIT() (umm_critical_depth--) -#else #ifndef UMM_CRITICAL_ENTRY - #define UMM_CRITICAL_ENTRY() + #define UMM_CRITICAL_ENTRY(tag) { \ + ++umm_critical_depth; \ + if (umm_critical_depth > umm_max_critical_depth) { \ + umm_max_critical_depth = umm_critical_depth; \ + } \ + } + #endif + #ifndef UMM_CRITICAL_EXIT + #define UMM_CRITICAL_EXIT(tag) (umm_critical_depth--) + #endif +#else + #ifndef UMM_CRITICAL_ENTRY + #define UMM_CRITICAL_ENTRY(tag) #endif #ifndef UMM_CRITICAL_EXIT - #define UMM_CRITICAL_EXIT() + #define UMM_CRITICAL_EXIT(tag) #endif #endif diff --git a/src/umm_poison.c b/src/umm_poison.c index d565674..3e64591 100644 --- a/src/umm_poison.c +++ b/src/umm_poison.c @@ -204,11 +204,15 @@ void umm_poison_free(void *ptr) { */ bool umm_poison_check(void) { + UMM_CRITICAL_DECL(id_poison); + bool ok = true; unsigned short int cur; UMM_CHECK_INITIALIZED(); + UMM_CRITICAL_ENTRY(id_poison); + /* Now iterate through the blocks list */ cur = UMM_NBLOCK(0) & UMM_BLOCKNO_MASK; @@ -223,6 +227,7 @@ bool umm_poison_check(void) { cur = UMM_NBLOCK(cur) & UMM_BLOCKNO_MASK; } + UMM_CRITICAL_EXIT(id_poison); return ok; }