Skip to content

Commit

Permalink
[UMM-55] Update UMM_CRITICAL... macros to support id tags
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
mhightower83 authored and rhempel committed Dec 26, 2021
1 parent 1bc286e commit ed58a3d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 25 deletions.
8 changes: 5 additions & 3 deletions src/umm_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions src/umm_integrity.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* chain.
*/
bool umm_integrity_check(void) {
UMM_CRITICAL_DECL(id_integrity);
bool ok = true;
uint16_t prev;
uint16_t cur;
Expand All @@ -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);

Expand Down Expand Up @@ -119,6 +121,7 @@ bool umm_integrity_check(void) {
}

clean:
UMM_CRITICAL_EXIT(id_integrity);
if (!ok) {
UMM_HEAP_CORRUPTION_CB();
}
Expand Down
19 changes: 11 additions & 8 deletions src/umm_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -398,6 +398,7 @@ static void umm_free_core(void *ptr) {
/* ------------------------------------------------------------------------ */

void umm_free(void *ptr) {
UMM_CRITICAL_DECL(id_free);

UMM_CHECK_INITIALIZED();

Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -555,18 +557,19 @@ 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;
}

/* ------------------------------------------------------------------------ */

void *umm_realloc(void *ptr, size_t size) {
UMM_CRITICAL_DECL(id_realloc);

uint16_t blocks;
uint16_t blockSize;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
44 changes: 30 additions & 14 deletions src/umm_malloc_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions src/umm_poison.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -223,6 +227,7 @@ bool umm_poison_check(void) {

cur = UMM_NBLOCK(cur) & UMM_BLOCKNO_MASK;
}
UMM_CRITICAL_EXIT(id_poison);

return ok;
}
Expand Down

0 comments on commit ed58a3d

Please sign in to comment.