test_malloc_caps.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Tests for the capabilities-based memory allocator.
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include "unity.h"
  7. #include "esp_attr.h"
  8. #include "esp_heap_caps.h"
  9. #include "esp_spi_flash.h"
  10. #include <stdlib.h>
  11. #include <sys/param.h>
  12. TEST_CASE_ESP32("Capabilities allocator test", "[heap]")
  13. {
  14. char *m1, *m2[10];
  15. int x;
  16. size_t free8start, free32start, free8, free32;
  17. /* It's important we printf() something before we take the empty heap sizes,
  18. as the first printf() in a task allocates heap resources... */
  19. printf("Testing capabilities allocator...\n");
  20. free8start = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  21. free32start = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  22. printf("Free 8bit-capable memory (start): %dK, 32-bit capable memory %dK\n", free8start, free32start);
  23. TEST_ASSERT(free32start>free8start);
  24. printf("Allocating 10K of 8-bit capable RAM\n");
  25. m1= heap_caps_malloc(10*1024, MALLOC_CAP_8BIT);
  26. printf("--> %p\n", m1);
  27. free8 = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  28. free32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  29. printf("Free 8bit-capable memory (both reduced): %dK, 32-bit capable memory %dK\n", free8, free32);
  30. //Both should have gone down by 10K; 8bit capable ram is also 32-bit capable
  31. TEST_ASSERT(free8<(free8start-10*1024));
  32. TEST_ASSERT(free32<(free32start-10*1024));
  33. //Assume we got DRAM back
  34. TEST_ASSERT((((int)m1)&0xFF000000)==0x3F000000);
  35. free(m1);
  36. //The goal here is to allocate from IRAM. Since there is no external IRAM (yet)
  37. //the following gives size of IRAM-only (not D/IRAM) memory.
  38. size_t free_iram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL) -
  39. heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  40. size_t alloc32 = MIN(free_iram / 2, 10*1024) & (~3);
  41. printf("Freeing; allocating %u bytes of 32K-capable RAM\n", alloc32);
  42. m1 = heap_caps_malloc(alloc32, MALLOC_CAP_32BIT);
  43. printf("--> %p\n", m1);
  44. //Check that we got IRAM back
  45. TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
  46. free8 = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  47. free32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  48. printf("Free 8bit-capable memory (after 32-bit): %dK, 32-bit capable memory %dK\n", free8, free32);
  49. //Only 32-bit should have gone down by alloc32: 32-bit isn't necessarily 8bit capable
  50. TEST_ASSERT(free32<(free32start-alloc32));
  51. TEST_ASSERT(free8==free8start);
  52. free(m1);
  53. printf("Allocating impossible caps\n");
  54. m1= heap_caps_malloc(10*1024, MALLOC_CAP_8BIT|MALLOC_CAP_EXEC);
  55. printf("--> %p\n", m1);
  56. TEST_ASSERT(m1==NULL);
  57. printf("Testing changeover iram -> dram");
  58. // priorities will exhaust IRAM first, then start allocating from DRAM
  59. for (x=0; x<10; x++) {
  60. m2[x]= heap_caps_malloc(alloc32, MALLOC_CAP_32BIT);
  61. printf("--> %p\n", m2[x]);
  62. }
  63. TEST_ASSERT((((int)m2[0])&0xFF000000)==0x40000000);
  64. TEST_ASSERT((((int)m2[9])&0xFF000000)==0x3F000000);
  65. printf("Test if allocating executable code still gives IRAM, even with dedicated IRAM region depleted\n");
  66. // (the allocation should come from D/IRAM)
  67. free_iram = heap_caps_get_free_size(MALLOC_CAP_EXEC);
  68. m1= heap_caps_malloc(MIN(free_iram / 2, 10*1024), MALLOC_CAP_EXEC);
  69. printf("--> %p\n", m1);
  70. TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
  71. free(m1);
  72. for (x=0; x<10; x++) free(m2[x]);
  73. printf("Done.\n");
  74. }
  75. TEST_CASE("heap_caps metadata test", "[heap]")
  76. {
  77. /* need to print something as first printf allocates some heap */
  78. printf("heap_caps metadata test\n");
  79. heap_caps_print_heap_info(MALLOC_CAP_8BIT);
  80. heap_caps_print_heap_info(MALLOC_CAP_32BIT);
  81. multi_heap_info_t original;
  82. heap_caps_get_info(&original, MALLOC_CAP_8BIT);
  83. void *b = heap_caps_malloc(original.largest_free_block, MALLOC_CAP_8BIT);
  84. TEST_ASSERT_NOT_NULL(b);
  85. printf("After allocating %d bytes:\n", original.largest_free_block);
  86. heap_caps_print_heap_info(MALLOC_CAP_8BIT);
  87. multi_heap_info_t after;
  88. heap_caps_get_info(&after, MALLOC_CAP_8BIT);
  89. TEST_ASSERT(after.largest_free_block < original.largest_free_block);
  90. TEST_ASSERT(after.total_free_bytes < original.total_free_bytes);
  91. free(b);
  92. heap_caps_get_info(&after, MALLOC_CAP_8BIT);
  93. /* Allow some leeway here, because LWIP sometimes allocates up to 144 bytes in the background
  94. as part of timer management.
  95. */
  96. TEST_ASSERT_INT32_WITHIN(200, after.total_free_bytes, original.total_free_bytes);
  97. TEST_ASSERT_INT32_WITHIN(200, after.largest_free_block, original.largest_free_block);
  98. TEST_ASSERT(after.minimum_free_bytes < original.total_free_bytes);
  99. }
  100. /* Small function runs from IRAM to check that malloc/free/realloc
  101. all work OK when cache is disabled...
  102. */
  103. static IRAM_ATTR __attribute__((noinline)) bool iram_malloc_test(void)
  104. {
  105. spi_flash_guard_get()->start(); // Disables flash cache
  106. bool result = true;
  107. void *x = heap_caps_malloc(64, MALLOC_CAP_32BIT);
  108. result = result && (x != NULL);
  109. void *y = heap_caps_realloc(x, 32, MALLOC_CAP_32BIT);
  110. result = result && (y != NULL);
  111. heap_caps_free(y);
  112. spi_flash_guard_get()->end(); // Re-enables flash cache
  113. return result;
  114. }
  115. TEST_CASE("heap_caps_xxx functions work with flash cache disabled", "[heap]")
  116. {
  117. TEST_ASSERT( iram_malloc_test() );
  118. }