Skip to content

malloc hooks #307

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

Closed
rayozzie opened this issue Aug 20, 2018 · 1 comment
Closed

malloc hooks #307

rayozzie opened this issue Aug 20, 2018 · 1 comment
Labels
bug 🐛 Something isn't working
Milestone

Comments

@rayozzie
Copy link

rayozzie commented Aug 20, 2018

I am trying to make progress in porting an esp32 arduino sketch to use stm32duino.

In the short term, my progress was impeded by a freertos memory management Issue stm32duino/STM32FreeRTOS#8.

As a temporary workaround because malloc()/realloc() were failing quite badly, I changed my sketch to use heap_4.c along with a statically-allocated memory buffer.

This worked quite well with one exception: my I2C I/O was failing.

After debugging it, I found that the i2c core driver was doing malloc/realloc() for its buffer management.

This is problematic for two reasons:

  • it is nonreentrant, in the case where freertos is operating.
  • It makes the stm32 arduino core dependent upon dynamically allocated memory, even in an environment in which the developer may wish to just use static

As a workaround, I added the code below to my sketch. I would suggest that the correct workaround, however, is to do something so that the STM32 FreeRTOS code can hook all core allocator requests and reroute them through its own appropriate per-port allocator.

#if configAPPLICATION_ALLOCATED_HEAP && !defined(ESP32DUINO)

// Define the static heap                                                                                               
uint8_t ucHeap[configTOTAL_HEAP_SIZE];

/* Prototypes for our hooks, decorated to make sure they are C bindings */
extern "C" {
extern void *malloc(size_t size);
extern void free(void *p);
extern void *realloc(void *p, size_t size);
}

void *malloc(size_t size) {
    return pvPortMalloc(size);
}

void free(void *p) {
    vPortFree(p);
}

void *realloc(void *p, size_t size) {
    void *pnew = pvPortMalloc(size);
    memcpy(pnew, p, size);
    vPortFree(p);
    return pnew;
}

#endif  // configAPPLICATION_ALLOCATED_HEAP                   
@fpistm
Copy link
Member

fpistm commented Sep 6, 2018

Hi @rayozzie
Sorry for the delay, I'm very busy since I came back.

Basically, I2C buffer allocation should be called the first time to allocate the 32 bytes buffer. It will be called again only if length is higher to 32.

malloc()/free() are used for new/delete (new.cpp)
realloc() in WString Class
I guess they also could bring some issues ?

Maybe your code could be added to STM32 FreeRTOS instead?

@fpistm fpistm added the on going Currently work on this label Sep 25, 2018
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Oct 4, 2018
Minimum stack size is defined in linker script:
_Min_Stack_Size = 0x400;; /* required amount of stack */

If more stack is requested, then user have to ensure that heap
and stack can fit in the SRAM.

Fix stm32duino#307

Signed-off-by: Frederic.Pillon <[email protected]>
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Oct 4, 2018
Minimum stack size is defined in linker script:
_Min_Stack_Size = 0x400;; /* required amount of stack */

If more stack is requested, then user have to ensure that heap
and stack can fit in the SRAM.

Fix stm32duino#307

Signed-off-by: Frederic.Pillon <[email protected]>
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Oct 4, 2018
Minimum stack size is defined in linker script:
_Min_Stack_Size = 0x400;; /* required amount of stack */

If more stack is requested, then user have to ensure that heap
and stack can fit in the SRAM.

Fix stm32duino#307

Signed-off-by: Frederic.Pillon <[email protected]>
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Oct 10, 2018
Minimum stack size is defined in linker script:
_Min_Stack_Size = 0x400;; /* required amount of stack */

If more stack is requested, then user have to ensure that heap
and stack can fit in the SRAM.

Fix stm32duino#307

Signed-off-by: Frederic.Pillon <[email protected]>
@fpistm fpistm added this to the 1.4.0 milestone Oct 12, 2018
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Oct 15, 2018
Minimum stack size is defined in linker script:
_Min_Stack_Size = 0x400;; /* required amount of stack */

If more stack is requested, then user have to ensure that heap
and stack can fit in the SRAM.

Fix stm32duino#307

Signed-off-by: Frederic.Pillon <[email protected]>
@fpistm fpistm added the bug 🐛 Something isn't working label Oct 16, 2018
@fpistm fpistm removed the on going Currently work on this label Oct 16, 2018
xC0000005 pushed a commit to xC0000005/Arduino_Core_STM32 that referenced this issue Nov 27, 2018
Minimum stack size is defined in linker script:
_Min_Stack_Size = 0x400;; /* required amount of stack */

If more stack is requested, then user have to ensure that heap
and stack can fit in the SRAM.

Fix stm32duino#307

Signed-off-by: Frederic.Pillon <[email protected]>
benwaffle pushed a commit to benwaffle/Arduino_Core_STM32 that referenced this issue Apr 10, 2019
Minimum stack size is defined in linker script:
_Min_Stack_Size = 0x400;; /* required amount of stack */

If more stack is requested, then user have to ensure that heap
and stack can fit in the SRAM.

Fix stm32duino#307

Signed-off-by: Frederic.Pillon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants