]> granicus.if.org Git - esp-idf/commitdiff
heap: fix unit test for the case when less than 10k of IRAM is available
authorIvan Grokhotkov <ivan@espressif.com>
Wed, 27 Jun 2018 06:46:20 +0000 (14:46 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 24 Jul 2018 06:56:40 +0000 (09:56 +0300)
components/heap/test/test_malloc_caps.c

index 0be5f6a6cac903f9be3a8304952360c408f2f0b4..f1db712b7a257f5b7d7621e218b51b873bc96fe0 100644 (file)
@@ -9,6 +9,7 @@
 #include "esp_heap_caps.h"
 #include "esp_spi_flash.h"
 #include <stdlib.h>
+#include <sys/param.h>
 
 TEST_CASE("Capabilities allocator test", "[heap]")
 {
@@ -38,18 +39,24 @@ TEST_CASE("Capabilities allocator test", "[heap]")
     TEST_ASSERT((((int)m1)&0xFF000000)==0x3F000000);
     free(m1);
 
-    printf("Freeing; allocating 10K of 32K-capable RAM\n");
-    m1 = heap_caps_malloc(10*1024, MALLOC_CAP_32BIT);
+    //The goal here is to allocate from IRAM. Since there is no external IRAM (yet)
+    //the following gives size of IRAM-only (not D/IRAM) memory.
+    size_t free_iram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL) -
+                           heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
+    size_t alloc32 = MIN(free_iram / 2, 10*1024);
+    printf("Freeing; allocating %u bytes of 32K-capable RAM\n", alloc32);
+    m1 = heap_caps_malloc(alloc32, MALLOC_CAP_32BIT);
     printf("--> %p\n", m1);
+    //Check that we got IRAM back
+    TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
     free8 = heap_caps_get_free_size(MALLOC_CAP_8BIT);
     free32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
     printf("Free 8bit-capable memory (after 32-bit): %dK, 32-bit capable memory %dK\n", free8, free32);
-    //Only 32-bit should have gone down by 10K: 32-bit isn't necessarily 8bit capable
-    TEST_ASSERT(free32<(free32start-10*1024));
+    //Only 32-bit should have gone down by alloc32: 32-bit isn't necessarily 8bit capable
+    TEST_ASSERT(free32<(free32start-alloc32));
     TEST_ASSERT(free8==free8start);
-    //Assume we got IRAM back
-    TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
     free(m1);
+
     printf("Allocating impossible caps\n");
     m1= heap_caps_malloc(10*1024, MALLOC_CAP_8BIT|MALLOC_CAP_EXEC);
     printf("--> %p\n", m1);
@@ -57,14 +64,15 @@ TEST_CASE("Capabilities allocator test", "[heap]")
     printf("Testing changeover iram -> dram");
     // priorities will exhaust IRAM first, then start allocating from DRAM
     for (x=0; x<10; x++) {
-        m2[x]= heap_caps_malloc(10*1024, MALLOC_CAP_32BIT);
+        m2[x]= heap_caps_malloc(alloc32, MALLOC_CAP_32BIT);
         printf("--> %p\n", m2[x]);
     }
     TEST_ASSERT((((int)m2[0])&0xFF000000)==0x40000000);
     TEST_ASSERT((((int)m2[9])&0xFF000000)==0x3F000000);
     printf("Test if allocating executable code still gives IRAM, even with dedicated IRAM region depleted\n");
     // (the allocation should come from D/IRAM)
-    m1= heap_caps_malloc(10*1024, MALLOC_CAP_EXEC);
+    free_iram = heap_caps_get_free_size(MALLOC_CAP_EXEC);
+    m1= heap_caps_malloc(MIN(free_iram / 2, 10*1024), MALLOC_CAP_EXEC);
     printf("--> %p\n", m1);
     TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
     free(m1);