mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
fix malloc(0) and heap_caps_alloc_failed()
Don't call heap_caps_alloc_failed() for malloc(0) and calloc(0), because it is not an error. Improve handling of malloc(0) and calloc(0). Merges https://github.com/espressif/esp-idf/pull/9517
This commit is contained in:
parent
05575cc246
commit
b43e777e0a
@ -70,9 +70,9 @@ static void heap_caps_alloc_failed(size_t requested_size, uint32_t caps, const c
|
||||
alloc_failed_callback(requested_size, caps, function_name);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS
|
||||
#ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS
|
||||
esp_system_abort("Memory allocation failed");
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
esp_err_t heap_caps_register_failed_alloc_callback(esp_alloc_failed_hook_t callback)
|
||||
@ -100,6 +100,10 @@ IRAM_ATTR static void *heap_caps_malloc_base( size_t size, uint32_t caps)
|
||||
{
|
||||
void *ret = NULL;
|
||||
|
||||
if (size == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size > HEAP_SIZE_MAX) {
|
||||
// Avoids int overflow when adding small numbers to size, or
|
||||
// calculating 'end' from start+size, by limiting 'size' to the possible range
|
||||
@ -169,7 +173,7 @@ IRAM_ATTR void *heap_caps_malloc( size_t size, uint32_t caps){
|
||||
|
||||
void* ptr = heap_caps_malloc_base(size, caps);
|
||||
|
||||
if (!ptr){
|
||||
if (!ptr && size > 0){
|
||||
heap_caps_alloc_failed(size, caps, __func__);
|
||||
}
|
||||
|
||||
@ -204,13 +208,13 @@ IRAM_ATTR void *heap_caps_malloc_default( size_t size )
|
||||
} else {
|
||||
r=heap_caps_malloc_base( size, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM );
|
||||
}
|
||||
if (r==NULL) {
|
||||
if (r==NULL && size > 0) {
|
||||
//try again while being less picky
|
||||
r=heap_caps_malloc_base( size, MALLOC_CAP_DEFAULT );
|
||||
}
|
||||
|
||||
// allocation failure?
|
||||
if (r==NULL){
|
||||
if (r==NULL && size > 0){
|
||||
heap_caps_alloc_failed(size, MALLOC_CAP_DEFAULT, __func__);
|
||||
}
|
||||
|
||||
@ -267,7 +271,7 @@ IRAM_ATTR void *heap_caps_malloc_prefer( size_t size, size_t num, ... )
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (r == NULL){
|
||||
if (r == NULL && size > 0){
|
||||
heap_caps_alloc_failed(size, caps, __func__);
|
||||
}
|
||||
va_end( argp );
|
||||
@ -290,7 +294,7 @@ IRAM_ATTR void *heap_caps_realloc_prefer( void *ptr, size_t size, size_t num, ..
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (r == NULL){
|
||||
if (r == NULL && size > 0){
|
||||
heap_caps_alloc_failed(size, caps, __func__);
|
||||
}
|
||||
va_end( argp );
|
||||
@ -309,11 +313,11 @@ IRAM_ATTR void *heap_caps_calloc_prefer( size_t n, size_t size, size_t num, ...
|
||||
while (num--) {
|
||||
caps = va_arg( argp, uint32_t );
|
||||
r = heap_caps_calloc_base( n, size, caps );
|
||||
if (r != NULL){
|
||||
if (r != NULL || size == 0){
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (r == NULL){
|
||||
if (r == NULL && size > 0){
|
||||
heap_caps_alloc_failed(size, caps, __func__);
|
||||
}
|
||||
va_end( argp );
|
||||
@ -470,7 +474,7 @@ IRAM_ATTR void *heap_caps_calloc( size_t n, size_t size, uint32_t caps)
|
||||
{
|
||||
void* ptr = heap_caps_calloc_base(n, size, caps);
|
||||
|
||||
if (!ptr){
|
||||
if (!ptr && size > 0){
|
||||
heap_caps_alloc_failed(size, caps, __func__);
|
||||
}
|
||||
|
||||
@ -631,6 +635,10 @@ IRAM_ATTR void *heap_caps_aligned_alloc(size_t alignment, size_t size, uint32_t
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size > HEAP_SIZE_MAX) {
|
||||
// Avoids int overflow when adding small numbers to size, or
|
||||
// calculating 'end' from start+size, by limiting 'size' to the possible range
|
||||
|
Loading…
Reference in New Issue
Block a user