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 core with upstream umm_malloc #6438

Merged
merged 40 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
88a32c8
WIP: Initial overlay of upstream version of umm_malloc over
mhightower83 Aug 20, 2019
b508642
WIP: upstream version of umm_malloc customized for Arduino ESP8266 Core
mhightower83 Aug 20, 2019
d7363e0
Resoved Travis CI - issue
mhightower83 Aug 23, 2019
57b65ab
Corrected #ifdef's for UMM_STATS/UMM_STATS_FULL
mhightower83 Aug 27, 2019
da47a79
Merge branch 'master' into WIP-upstream-umm_malloc
d-a-v Aug 29, 2019
71b10fd
Corrected UMM_REALLOC_DEFRAG option to be more like the original umm_…
mhightower83 Sep 3, 2019
4612235
Merge branch 'WIP-upstream-umm_malloc' of github.com:mhightower83/Ard…
mhightower83 Sep 3, 2019
79fe67f
Updated UMM_POISON_CHECK_LITE - Expanded the poison check on the current
mhightower83 Sep 5, 2019
5ded134
Updated printing in heap.cpp to behave the way os_printf did.
mhightower83 Sep 8, 2019
2a1923e
Merged latest, 2019-09-07, from upstream umm_malloc
mhightower83 Sep 8, 2019
f68a64f
Resolved issue where OOM selected w/ Debug port: "Disabled" caused hang.
mhightower83 Sep 8, 2019
9fcf27b
Fixed realloc OOM bug introduced due to confusing code, added comment…
mhightower83 Sep 9, 2019
ad03e16
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Sep 11, 2019
f438e02
Updated to track PRs for various print definitions. Cleaned up comments
mhightower83 Sep 27, 2019
919b122
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Sep 28, 2019
35c6868
Added missing "#ifdef __cplusplus"
mhightower83 Sep 28, 2019
47563cd
Reorganize local enhancements
mhightower83 Sep 29, 2019
2a1b722
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Sep 30, 2019
5560f6b
Print notice message for the need of -DUMM_INFO_PRINT
mhightower83 Sep 30, 2019
c4a46bc
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Sep 30, 2019
0b89ce7
This updates the heap management library, umm_malloc, to the current …
mhightower83 Sep 30, 2019
7e859c5
This updates the heap management library, umm_malloc, to the current …
mhightower83 Sep 30, 2019
2d26ccb
Updated upstream TODO
mhightower83 Oct 1, 2019
eed4b99
Merge branch 'WIP-upstream-umm_malloc' of github.com:mhightower83/Ard…
mhightower83 Oct 1, 2019
966812a
Changes on printing to comply with new understanding of SPI bus avail…
mhightower83 Oct 1, 2019
3669bf5
Removed no longer needed files.
mhightower83 Oct 1, 2019
be66ae7
OOM build option now saves file and line number for post mortem repor…
mhightower83 Oct 2, 2019
917e723
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Oct 2, 2019
503def2
Merge branch 'WIP-upstream-umm_malloc' of github.com:mhightower83/Ard…
mhightower83 Oct 2, 2019
3b80577
Missed change.
mhightower83 Oct 5, 2019
2507ced
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Oct 10, 2019
46b61fb
Consolated upstream integration comments into Notes.h. This helps
mhightower83 Oct 10, 2019
e9401c7
Comment updates.
mhightower83 Oct 10, 2019
648c028
Notes update.
mhightower83 Oct 10, 2019
3475ba2
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Oct 17, 2019
435b8a6
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Oct 23, 2019
eb5a7ca
Changes to heap.cpp -
mhightower83 Oct 24, 2019
3c77efe
Corrected Full Poison Check's placement and documented the
mhightower83 Oct 24, 2019
9da7eb8
Merge branch 'master' into WIP-upstream-umm_malloc
mhightower83 Oct 28, 2019
0eaa613
Fixed typos
mhightower83 Oct 29, 2019
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
5 changes: 4 additions & 1 deletion cores/esp8266/umm_malloc/umm_info.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if defined(BUILD_UMM_MALLOC_C)

#ifdef UMM_INFO

/* ----------------------------------------------------------------------------
Expand Down Expand Up @@ -36,6 +37,7 @@ void *umm_info( void *ptr, int force ) {
*/
memset( &ummHeapInfo, 0, sizeof( ummHeapInfo ) );

DBGLOG_FORCE( force, "\n" );
mhightower83 marked this conversation as resolved.
Show resolved Hide resolved
DBGLOG_FORCE( force, "+----------+-------+--------+--------+-------+--------+--------+\n" );
DBGLOG_FORCE( force, "|0x%08lx|B %5d|NB %5d|PB %5d|Z %5d|NF %5d|PF %5d|\n",
(unsigned long)(&UMM_BLOCK(blockNo)),
Expand Down Expand Up @@ -204,4 +206,5 @@ size_t umm_free_heap_size_lw( void ) {
return (size_t)ummStats.free_blocks * sizeof(umm_block);
}
#endif
#endif

#endif // defined(BUILD_UMM_MALLOC_C)
4 changes: 3 additions & 1 deletion cores/esp8266/umm_malloc/umm_integrity.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if defined(BUILD_UMM_MALLOC_C)
/* integrity check (UMM_INTEGRITY_CHECK) {{{ */
#if defined(UMM_INTEGRITY_CHECK) && defined(BUILD_UMM_MALLOC_C)
#if defined(UMM_INTEGRITY_CHECK)
/*
* Perform integrity check of the whole heap data. Returns 1 in case of
* success, 0 otherwise.
Expand Down Expand Up @@ -130,3 +131,4 @@ int umm_integrity_check(void) {

#endif
/* }}} */
#endif // defined(BUILD_UMM_MALLOC_C)
151 changes: 88 additions & 63 deletions cores/esp8266/umm_malloc/umm_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
* - Move integrity and poison checking to separate file
* R.Hempel 2017-12-29 - Fix bug in realloc when requesting a new block that
* results in OOM error - see Issue 11
* R.Hempel 2019-09-07 - Separate the malloc() and free() functionality into
* wrappers that use critical section protection macros
* and static core functions that assume they are
* running in a protected con text. Thanks @devyte
* ----------------------------------------------------------------------------
*/

Expand Down Expand Up @@ -367,22 +371,15 @@ void umm_init( void ) {
}
}

/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------
* Must be called only from within critical sections guarded by
* UMM_CRITICAL_ENTRY() and UMM_CRITICAL_EXIT().
*/

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

unsigned short int c;

/* If we're being asked to free a NULL pointer, well that's just silly! */

if( (void *)0 == ptr ) {
DBGLOG_DEBUG( "free a null pointer -> do nothing\n" );
STATS__NULL_FREE_REQUEST(id_free);

return;
}

STATS__FREE_REQUEST(id_free);
/*
* FIXME: At some point it might be a good idea to add a check to make sure
Expand All @@ -399,9 +396,6 @@ void umm_free( void *ptr ) {

DBGLOG_DEBUG( "Freeing block %6d\n", c );

/* Protect the critical section... */
UMM_CRITICAL_ENTRY(id_free);

/* Update stats Free Block count */
STATS__FREE_BLOCKS_UPDATE(UMM_NBLOCK(c) - c);

Expand Down Expand Up @@ -431,50 +425,53 @@ void umm_free( void *ptr ) {

UMM_NBLOCK(c) |= UMM_FREELIST_MASK;
}

/* Release the critical section... */
UMM_CRITICAL_EXIT(id_free);
}

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

void *umm_malloc( size_t size ) {
UMM_CRITICAL_DECL(id_malloc);
void umm_free( void *ptr ) {
UMM_CRITICAL_DECL(id_free);

unsigned short int blocks;
unsigned short int blockSize = 0;
if (umm_heap == NULL) {
umm_init();
}

unsigned short int bestSize;
unsigned short int bestBlock;
/* If we're being asked to free a NULL pointer, well that's just silly! */

unsigned short int cf;
if( (void *)0 == ptr ) {
DBGLOG_DEBUG( "free a null pointer -> do nothing\n" );
STATS__NULL_FREE_REQUEST(id_free);

if (umm_heap == NULL) {
umm_init();
return;
}

/*
* the very first thing we do is figure out if we're being asked to allocate
* a size of 0 - and if we are we'll simply return a null pointer. if not
* then reduce the size by 1 byte so that the subsequent calculations on
* the number of blocks to allocate are easier...
*/
/* Free the memory withing a protected critical section */

UMM_CRITICAL_ENTRY(id_free);

if( 0 == size ) {
DBGLOG_DEBUG( "malloc a block of 0 bytes -> do nothing\n" );
STATS__ZERO_ALLOC_REQUEST(id_malloc, size);
umm_free_core( ptr );

return( (void *)NULL );
}
UMM_CRITICAL_EXIT(id_free);
}

/* ------------------------------------------------------------------------
* Must be called only from within critical sections guarded by
* UMM_CRITICAL_ENTRY() and UMM_CRITICAL_EXIT().
*/

static void *umm_malloc_core( size_t size ) {
unsigned short int blocks;
unsigned short int blockSize = 0;

unsigned short int bestSize;
unsigned short int bestBlock;

unsigned short int cf;

STATS__ALLOC_REQUEST(id_malloc, size);

blocks = umm_blocks( size );

/* Protect the critical section... */
UMM_CRITICAL_ENTRY(id_malloc);

/*
* Now we can scan through the free list until we find a space that's big
* enough to hold the number of blocks we need.
Expand Down Expand Up @@ -566,16 +563,46 @@ void *umm_malloc( size_t size ) {

DBGLOG_DEBUG( "Can't allocate %5d blocks\n", blocks );

/* Release the critical section... */
UMM_CRITICAL_EXIT(id_malloc);

return( (void *)NULL );
}

/* Release the critical section... */
return( (void *)&UMM_DATA(cf) );
}

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

void *umm_malloc( size_t size ) {
UMM_CRITICAL_DECL(id_malloc);

void *ptr = NULL;

if (umm_heap == NULL) {
umm_init();
}

/*
* the very first thing we do is figure out if we're being asked to allocate
* a size of 0 - and if we are we'll simply return a null pointer. if not
* then reduce the size by 1 byte so that the subsequent calculations on
* the number of blocks to allocate are easier...
*/

if( 0 == size ) {
DBGLOG_DEBUG( "malloc a block of 0 bytes -> do nothing\n" );
STATS__ZERO_ALLOC_REQUEST(id_malloc, size);

return( ptr );
}

/* Allocate the memory withing a protected critical section */

UMM_CRITICAL_ENTRY(id_malloc);

ptr = umm_malloc_core( size );

UMM_CRITICAL_EXIT(id_malloc);

return( (void *)&UMM_DATA(cf) );
return( ptr );
}

/* ------------------------------------------------------------------------ */
Expand Down Expand Up @@ -724,7 +751,7 @@ void *umm_realloc( void *ptr, size_t size ) {
#ifdef UMM_LIGHTWEIGHT_CPU
if ((prevBlockSize + blockSize + nextBlockSize) > blocks) {
umm_split_block( c, blocks, 0 );
umm_free( (void *)&UMM_DATA(c+blocks) );
umm_free_core( (void *)&UMM_DATA(c+blocks) );
}
STATS__FREE_BLOCKS_ISR_MIN();
blockSize = blocks;
Expand All @@ -736,19 +763,18 @@ void *umm_realloc( void *ptr, size_t size ) {
ptr = (void *)&UMM_DATA(c);
UMM_CRITICAL_RESUME(id_realloc);
} else {
UMM_CRITICAL_SUSPEND(id_realloc);
DBGLOG_DEBUG( "realloc a completely new block %i\n", blocks );
void *oldptr = ptr;
if( (ptr = umm_malloc( size )) ) {
if( (ptr = umm_malloc_core( size )) ) {
DBGLOG_DEBUG( "realloc %i to a bigger block %i, copy, and free the old\n", blockSize, blocks );
UMM_CRITICAL_SUSPEND(id_realloc);
memcpy( ptr, oldptr, curSize );
umm_free( oldptr );
blockSize = blocks;
UMM_CRITICAL_RESUME(id_realloc);
umm_free_core( oldptr );
blockSize = blocks;
} else {
DBGLOG_DEBUG( "realloc %i to a bigger block %i failed - return NULL and leave the old block!\n", blockSize, blocks );
/* This space intentionally left blnk */
UMM_CRITICAL_RESUME(id_realloc);
STATS__OOM_UPDATE();
}
}
Expand Down Expand Up @@ -794,7 +820,7 @@ void *umm_realloc( void *ptr, size_t size ) {
#ifdef UMM_LIGHTWEIGHT_CPU
if (blockSize > blocks) {
umm_split_block( c, blocks, 0 );
umm_free( (void *)&UMM_DATA(c+blocks) );
umm_free_core( (void *)&UMM_DATA(c+blocks) );
}
STATS__FREE_BLOCKS_ISR_MIN();
blockSize = blocks;
Expand All @@ -813,19 +839,18 @@ void *umm_realloc( void *ptr, size_t size ) {
STATS__FREE_BLOCKS_UPDATE(-nextBlockSize);
blockSize += nextBlockSize;
} else { // 4
UMM_CRITICAL_SUSPEND(id_realloc);
DBGLOG_DEBUG( "realloc a completely new block %d\n", blocks );
void *oldptr = ptr;
if( (ptr = umm_malloc( size )) ) {
if( (ptr = umm_malloc_core( size )) ) {
DBGLOG_DEBUG( "realloc %d to a bigger block %d, copy, and free the old\n", blockSize, blocks );
UMM_CRITICAL_SUSPEND(id_realloc);
memcpy( ptr, oldptr, curSize );
umm_free( oldptr);
blockSize = blocks;
UMM_CRITICAL_RESUME(id_realloc);
umm_free_core( oldptr);
blockSize = blocks;
} else {
DBGLOG_DEBUG( "realloc %d to a bigger block %d failed - return NULL and leave the old block!\n", blockSize, blocks );
/* This space intentionally left blnk */
UMM_CRITICAL_RESUME(id_realloc);
STATS__OOM_UPDATE();
}
}
Expand All @@ -836,15 +861,15 @@ void *umm_realloc( void *ptr, size_t size ) {
DBGLOG_DEBUG( "realloc the same or smaller size block - %d, do nothing\n", blocks );
/* This space intentionally left blank */
} else {
UMM_CRITICAL_SUSPEND(id_realloc);
DBGLOG_DEBUG( "realloc a completely new block %d\n", blocks );
void *oldptr = ptr;
if( (ptr = umm_malloc( size )) ) {
if( (ptr = umm_malloc_core( size )) ) {
DBGLOG_DEBUG( "realloc %d to a bigger block %d, copy, and free the old\n", blockSize, blocks );
UMM_CRITICAL_SUSPEND(id_realloc);
memcpy( ptr, oldptr, curSize );
umm_free( oldptr );
blockSize = blocks;
UMM_CRITICAL_RESUME(id_realloc);
umm_free_core( oldptr );
blockSize = blocks;
} else {
DBGLOG_DEBUG( "realloc %d to a bigger block %d failed - return NULL and leave the old block!\n", blockSize, blocks );
/* This space intentionally left blnk */
Expand All @@ -860,7 +885,7 @@ void *umm_realloc( void *ptr, size_t size ) {
if (blockSize > blocks ) {
DBGLOG_DEBUG( "split and free %d blocks from %d\n", blocks, blockSize );
umm_split_block( c, blocks, 0 );
umm_free( (void *)&UMM_DATA(c+blocks) );
umm_free_core( (void *)&UMM_DATA(c+blocks) );
}

STATS__FREE_BLOCKS_MIN();
Expand Down
45 changes: 32 additions & 13 deletions cores/esp8266/umm_malloc/umm_malloc_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@ extern "C" {
* ----------------------------------------------------------------------------
*/

#ifdef TEST_BUILD
extern char test_umm_heap[];
#endif

#ifdef TEST_BUILD
/* Start addresses and the size of the heap */
#define UMM_MALLOC_CFG_HEAP_ADDR (test_umm_heap)
#define UMM_MALLOC_CFG_HEAP_SIZE 0x10000
#else
/* Start addresses and the size of the heap */
extern char _heap_start[];
#define UMM_MALLOC_CFG_HEAP_ADDR ((uint32_t)&_heap_start[0])
#define UMM_MALLOC_CFG_HEAP_SIZE ((size_t)(0x3fffc000 - UMM_MALLOC_CFG_HEAP_ADDR))
#endif

/* A couple of macros to make packing structures less compiler dependent */

Expand Down Expand Up @@ -336,17 +345,29 @@ static inline void _critical_exit(UMM_TIME_STAT *p, uint32_t *saved_ps) {
* called from within umm_malloc()
*/

#if defined(UMM_CRITICAL_METRICS)
#define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag
#define UMM_CRITICAL_ENTRY(tag)_critical_entry(&time_stats.tag, &_saved_ps_##tag)
#define UMM_CRITICAL_EXIT(tag) _critical_exit(&time_stats.tag, &_saved_ps_##tag)

#else // ! UMM_CRITICAL_METRICS
// This method preserves the intlevel on entry and restores the
// original intlevel at exit.
#define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag
#define UMM_CRITICAL_ENTRY(tag) _saved_ps_##tag = xt_rsil(DEFAULT_CRITICAL_SECTION_INTLEVEL)
#define UMM_CRITICAL_EXIT(tag) xt_wsr_ps(_saved_ps_##tag)
#ifdef TEST_BUILD
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
#if defined(UMM_CRITICAL_METRICS)
#define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag
#define UMM_CRITICAL_ENTRY(tag)_critical_entry(&time_stats.tag, &_saved_ps_##tag)
#define UMM_CRITICAL_EXIT(tag) _critical_exit(&time_stats.tag, &_saved_ps_##tag)

#else // ! UMM_CRITICAL_METRICS
// This method preserves the intlevel on entry and restores the
// original intlevel at exit.
#define UMM_CRITICAL_DECL(tag) uint32_t _saved_ps_##tag
#define UMM_CRITICAL_ENTRY(tag) _saved_ps_##tag = xt_rsil(DEFAULT_CRITICAL_SECTION_INTLEVEL)
#define UMM_CRITICAL_EXIT(tag) xt_wsr_ps(_saved_ps_##tag)
#endif
#endif

/*
Expand Down Expand Up @@ -376,11 +397,9 @@ static inline void _critical_exit(UMM_TIME_STAT *p, uint32_t *saved_ps) {
#ifdef UMM_LIGHTWEIGHT_CPU
#define UMM_CRITICAL_SUSPEND(tag) UMM_CRITICAL_EXIT(tag)
#define UMM_CRITICAL_RESUME(tag) UMM_CRITICAL_ENTRY(tag)
#define UMM_NEED_LOCK_LW_CPU true
#else
#define UMM_CRITICAL_SUSPEND(tag) do {} while(0)
#define UMM_CRITICAL_RESUME(tag) do {} while(0)
#define UMM_NEED_LOCK_LW_CPU true
#endif

/*
Expand Down
6 changes: 4 additions & 2 deletions cores/esp8266/umm_malloc/umm_poison.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* poisoning (UMM_POISON_CHECK) {{{ */
#if defined(BUILD_UMM_MALLOC_C)

/* poisoning (UMM_POISON_CHECK) {{{ */
#if defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE)
#define POISON_BYTE (0xa5)

Expand Down Expand Up @@ -236,4 +237,5 @@ int umm_poison_check(void) {
/* ------------------------------------------------------------------------ */

#endif
#endif

#endif // defined(BUILD_UMM_MALLOC_C)