8000 Corrected UMM_REALLOC_DEFRAG option to be more like the original umm_… · esp8266/Arduino@71b10fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 71b10fd

Browse files
committed
Corrected UMM_REALLOC_DEFRAG option to be more like the original umm_realloc.
Updated some comments and changed method of defining _rom_putc1.
1 parent 57b65ab commit 71b10fd

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

cores/esp8266/umm_malloc/isr_safe_printf.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
code using the other port. Most of the ROM printf functions are not built
3232
to support this kind of usage. Things get especially dangerous when you
3333
try to use the ...external_printf stuff.
34-
* ets_vprintf - by it self is safe.
34+
* ets_vprintf - by itself is safe.
3535
* newlibc printf - not safe - lives in flash.
3636
* newlibc snprintf - not safe - lives in flash.
3737
* builtin putc1 print function - Is installed when you use
@@ -41,12 +41,16 @@
4141
* call uart_buff_switch at each printf call to reselect UART
4242
* use a stack buffer to hold a copy the PROGMEM string to print from.
4343
* use ets_vprintf for printing with putc1 function.
44+
* os_printf_plus looks interesting. It is in IRAM. If no heap is available it
45+
will use up to 64 bytes of stack space to copy a PROGMEM fmt for printing.
46+
Issues:
47+
* Printing is turned off by system_set_os_print
48+
* putc1 needs to be in IRAM - this is a uart.cpp issue
49+
* Need to force system_get_free_heap_size to return 0 during critical periods.
50+
* won't work for umm_info it prints over 64 characters.
51+
* along with umm_info there are other debug messages that exceed 64 characters.
4452
4553
Research TODO, Unknowns:
46-
* ets_printf_plus - is it safe?
47-
* check if it uses alloc? the old versions used malloc for PROGMEM, if
48-
that fails, they try to use the stack for PROGMEM.
49-
* confirmed it is in IRAM in SDK!
5054
* Is there a problem with the ROM "serial print functions"?
5155
Rtos SDK does not use them. igrr also didn't use them in ...postmortem.
5256
See "ets_printf" above.
@@ -72,17 +76,15 @@ int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2)
7276
//D int (*f)(int) ets_install_uart_printf(void);
7377
//D int (*_rom_putc1)(int) = (int (*)(int))0x40001dcc;
7478

75-
// ROM _putc1, ignores CRs and sends CR/LF for LF, newline.
79+
// Boot ROM _putc1, ignores CRs and sends CR/LF for LF, newline.
7680
// Always returns character sent.
77-
typedef int (*fptr_t)(int);
78-
fptr_t ets_install_uart_printf(void);
81+
typedef int (*fp_putc_t)(int);
82+
#define _rom_putc1 ((fp_putc_t)0x40001dcc)
83+
7984
void uart_buff_switch(uint8_t);
8085

8186
int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
8287
int ICACHE_RAM_ATTR _isr_safe_printf_P(const char *fmt, ...) {
83-
// This will replace, if any, the UART handler that was installed by uart.cpp
84-
// calls ets_install_putc1.
85-
fptr_t _rom_putc1 = ets_install_uart_printf();
8688
#ifdef DEBUG_ESP_PORT
8789
#define VALUE(x) __STRINGIFY(x)
8890
// Preprocessor and compiler together will optimize away the if.

cores/esp8266/umm_malloc/umm_malloc.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -650,14 +650,15 @@ void *umm_realloc( void *ptr, size_t size ) {
650650
* out the best strategy for the new allocation. The following strategy is
651651
* focused on defragging the heap:
652652
*
653-
* 1. If the new block is the same size or smaller than the current block do
654-
* nothing.
655-
* 2. If the prev is free and adding it to the current and next block
656-
* gives us enough memory, proceed, note that next block may not be
657-
* available.
653+
* 1. If the prev is free and adding it to the current, or current and next
654+
* block, gives us enough memory, proceed. Note, that next block may not
655+
* be available.
658656
* a. Remove the previous block from the free list, assimilate it.
659-
* b. If new block gives enough memory, copy to the new block.
657+
* b. If this new block gives enough memory, copy to the new block.
658+
* Note, this includes the case of same size or smaller block.
660659
* c. Else assimilate the next block, copy to the new block.
660+
* 2. If the new block is the same size or smaller than the current block do
661+
* nothing.
661662
* 3. If the next block is free and adding it to the current block gives us
662663
* enough memory, assimilate the next block.
663664
* 4. Otherwise try to allocate an entirely new block of memory. If the
@@ -668,10 +669,7 @@ void *umm_realloc( void *ptr, size_t size ) {
668669
* was not exact, then split the memory block so that we use only the
669670
* requested number of blocks and add what's left to the free list.
670671
*/
671-
if (blockSize >= blocks) { // 1
672-
DBGLOG_DEBUG( "realloc the same or smaller size block - %d, do nothing\n", blocks );
673-
/* This space intentionally left blank */
674-
} else if (prevBlockSize && (prevBlockSize + blockSize + nextBlockSize) >= blocks) { // 2
672+
if (prevBlockSize && (prevBlockSize + blockSize + nextBlockSize) >= blocks) { // 1
675673
umm_disconnect_from_free_list( UMM_PBLOCK(c) );
676674
c = umm_assimilate_down(c, 0);
677675
STATS__FREE_BLOCKS_UPDATE( - prevBlockSize );
@@ -697,6 +695,9 @@ void *umm_realloc( void *ptr, size_t size ) {
697695
memmove( (void *)&UMM_DATA(c), ptr, curSize );
698696
ptr = (void *)&UMM_DATA(c);
699697
UMM_CRITICAL_RESUME(id_realloc);
698+
} else if (blockSize >= blocks) { // 2
699+
DBGLOG_DEBUG( "realloc the same or smaller size block - %d, do nothing\n", blocks );
700+
/* This space intentionally left blank */
700701
} else if ((blockSize + nextBlockSize) >= blocks) { // 3
701702
DBGLOG_DEBUG( "realloc using next block - %d\n", blocks );
702703
umm_assimilate_up( c );

0 commit comments

Comments
 (0)
0