mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
heap: add a unit test for malloc(0) and slightly optimize heap_caps_malloc_prefer
This commit is contained in:
parent
b43e777e0a
commit
6906c34319
@ -267,7 +267,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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user