test_runtime_heap_reg.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Tests for registering new heap memory at runtime
  3. */
  4. #include <stdio.h>
  5. #include "unity.h"
  6. #include "esp_heap_caps_init.h"
  7. #include "esp_system.h"
  8. #include <stdlib.h>
  9. /* NOTE: This is not a well-formed unit test, it leaks memory */
  10. TEST_CASE("Allocate new heap at runtime", "[heap][ignore]")
  11. {
  12. const size_t BUF_SZ = 1000;
  13. const size_t HEAP_OVERHEAD_MAX = 200;
  14. void *buffer = malloc(BUF_SZ);
  15. TEST_ASSERT_NOT_NULL(buffer);
  16. uint32_t before_free = esp_get_free_heap_size();
  17. TEST_ESP_OK( heap_caps_add_region((intptr_t)buffer, (intptr_t)buffer + BUF_SZ) );
  18. uint32_t after_free = esp_get_free_heap_size();
  19. printf("Before %u after %u\n", before_free, after_free);
  20. /* allow for some 'heap overhead' from accounting structures */
  21. TEST_ASSERT(after_free >= before_free + BUF_SZ - HEAP_OVERHEAD_MAX);
  22. }
  23. /* NOTE: This is not a well-formed unit test, it leaks memory and
  24. may fail if run twice in a row without a reset.
  25. */
  26. TEST_CASE("Allocate new heap with new capability", "[heap][ignore]")
  27. {
  28. const size_t BUF_SZ = 100;
  29. #ifdef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  30. const size_t ALLOC_SZ = 32;
  31. #else
  32. const size_t ALLOC_SZ = 64; // More than half of BUF_SZ
  33. #endif
  34. const uint32_t MALLOC_CAP_INVENTED = (1 << 30); /* this must be unused in esp_heap_caps.h */
  35. /* no memory exists to provide this capability */
  36. TEST_ASSERT_NULL( heap_caps_malloc(ALLOC_SZ, MALLOC_CAP_INVENTED) );
  37. void *buffer = malloc(BUF_SZ);
  38. TEST_ASSERT_NOT_NULL(buffer);
  39. uint32_t caps[SOC_MEMORY_TYPE_NO_PRIOS] = { MALLOC_CAP_INVENTED };
  40. TEST_ESP_OK( heap_caps_add_region_with_caps(caps, (intptr_t)buffer, (intptr_t)buffer + BUF_SZ) );
  41. /* ta-da, it's now possible! */
  42. TEST_ASSERT_NOT_NULL( heap_caps_malloc(ALLOC_SZ, MALLOC_CAP_INVENTED) );
  43. }
  44. /* NOTE: This is not a well-formed unit test.
  45. * If run twice without a reset, it will failed.
  46. */
  47. TEST_CASE("Add .bss memory to heap region runtime", "[heap][ignore]")
  48. {
  49. #define BUF_SZ 1000
  50. #define HEAP_OVERHEAD_MAX 200
  51. static uint8_t s_buffer[BUF_SZ];
  52. printf("s_buffer start %08x end %08x\n", (intptr_t)s_buffer, (intptr_t)s_buffer + BUF_SZ);
  53. uint32_t before_free = esp_get_free_heap_size();
  54. TEST_ESP_OK( heap_caps_add_region((intptr_t)s_buffer, (intptr_t)s_buffer + BUF_SZ) );
  55. uint32_t after_free = esp_get_free_heap_size();
  56. printf("Before %u after %u\n", before_free, after_free);
  57. /* allow for some 'heap overhead' from accounting structures */
  58. TEST_ASSERT(after_free >= before_free + BUF_SZ - HEAP_OVERHEAD_MAX);
  59. /* Twice add must be failed */
  60. TEST_ASSERT( (heap_caps_add_region((intptr_t)s_buffer, (intptr_t)s_buffer + BUF_SZ) != ESP_OK) );
  61. }