heap: add a unit test for malloc(0) and slightly optimize heap_caps_malloc_prefer

This commit is contained in:
Omar Chebib 2022-08-08 15:39:25 +08:00
parent 8b3f916d1a
commit 372ab1468b
2 changed files with 23 additions and 1 deletions

View File

@ -259,7 +259,7 @@ IRAM_ATTR void *heap_caps_malloc_prefer( size_t size, size_t num, ... )
while (num--) {
caps = va_arg( argp, uint32_t );
r = heap_caps_malloc_base( size, caps );
if (r != NULL) {
if (r != NULL || size == 0) {
break;
}
}

View File

@ -132,3 +132,25 @@ TEST_CASE("malloc(0) should return a NULL pointer", "[heap]")
p = malloc(0);
TEST_ASSERT(p == NULL);
}
static bool failure_occured = false;
static void test_alloc_failure_callback(size_t size, uint32_t caps, const char * function_name)
{
failure_occured = true;
}
TEST_CASE("malloc/calloc(0) should not call failure callback", "[heap]")
{
void* ptr = NULL;
esp_err_t ret = heap_caps_register_failed_alloc_callback(test_alloc_failure_callback);
TEST_ASSERT(ret == ESP_OK);
ptr = malloc(0);
TEST_ASSERT_NULL(ptr);
/* Check that our callback was NOT called */
TEST_ASSERT_FALSE(failure_occured);
/* Do the same thing for calloc */
ptr = calloc(0, 0);
TEST_ASSERT_NULL(ptr);
TEST_ASSERT_FALSE(failure_occured);
}