Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UMM_CRITICAL #55

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/umm_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ void compute_fragmentation_metric(void)
ummHeapInfo.fragmentation_metric = 100 - (((uint32_t)(sqrtf(ummHeapInfo.freeBlocksSquared)) * 100) / (ummHeapInfo.freeBlocks));
}
}

void *umm_info(void *ptr, bool force) {
UMM_CRITICAL_DECL(id_info);

uint16_t blockNo = 0;

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
30 changes: 21 additions & 9 deletions src/umm_malloc_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,18 @@ 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()
Expand All @@ -254,19 +262,23 @@ extern int umm_fragmentation_metric(void);
#ifdef UMM_MAX_CRITICAL_DEPTH_CHECK
extern int umm_critical_depth;
extern int umm_max_critical_depth;
#define UMM_CRITICAL_ENTRY() { \
#define UMM_CRITICAL_DECL(tag)
#define UMM_CRITICAL_ENTRY(tag) { \
++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--)
}
#define UMM_CRITICAL_EXIT(tag) (umm_critical_depth--)
#else
#ifndef UMM_CRITICAL_DECL
#define UMM_CRITICAL_DECL(tag)
#endif
#ifndef UMM_CRITICAL_ENTRY
#define 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