[core] Fix reentrant malloc wrappers

This commit is contained in:
Kuba Szczodrzyński
2023-05-01 20:58:37 +02:00
parent d332315e7a
commit 8337ac121e

View File

@@ -44,10 +44,38 @@ void __wrap_free(void *ptr) {
vPortFree(ptr);
}
__attribute__((alias("__wrap_malloc"))) void *__wrap__malloc_r(void *reent, size_t size);
__attribute__((alias("__wrap_calloc"))) void *__wrap__calloc_r(void *reent, size_t num, size_t size);
__attribute__((alias("__wrap_realloc"))) void *__wrap__realloc_r(void *reent, void *ptr, size_t new_size);
__attribute__((alias("__wrap_free"))) void __wrap__free_r(void *reent, void *ptr);
// Mind the 'reent' parameter - do NOT define these as linker aliases!
void *__wrap__malloc_r(void *reent, size_t size) {
return pvPortMalloc(size);
}
void *__wrap__calloc_r(void *reent, size_t num, size_t size) {
void *ptr;
if (num == 0 || size == 0)
num = size = 1;
ptr = pvPortMalloc(num * size);
if (ptr)
memset(ptr, 0, num * size);
return ptr;
}
void *__wrap__realloc_r(void *reent, void *ptr, size_t new_size) {
#if LT_REMALLOC
void *nptr = pvPortMalloc(new_size);
if (nptr) {
memcpy(nptr, ptr, new_size);
vPortFree(ptr);
}
return nptr;
#else
return LT_REALLOC_FUNC(ptr, new_size);
#endif
}
void __wrap__free_r(void *reent, void *ptr) {
vPortFree(ptr);
}
#endif