test_malloc_caps.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 "rom/ets_sys.h"
  8. #include "esp_heap_alloc_caps.h"
  9. #include <stdlib.h>
  10. TEST_CASE("Capabilities allocator test", "[esp32]")
  11. {
  12. char *m1, *m2[10];
  13. int x;
  14. size_t free8start, free32start, free8, free32;
  15. free8start=xPortGetFreeHeapSizeCaps(MALLOC_CAP_8BIT);
  16. free32start=xPortGetFreeHeapSizeCaps(MALLOC_CAP_32BIT);
  17. printf("Free 8bit-capable memory: %dK, 32-bit capable memory %dK\n", free8start, free32start);
  18. TEST_ASSERT(free32start>free8start);
  19. printf("Allocating 10K of 8-bit capable RAM\n");
  20. m1=pvPortMallocCaps(10*1024, MALLOC_CAP_8BIT);
  21. printf("--> %p\n", m1);
  22. free8=xPortGetFreeHeapSizeCaps(MALLOC_CAP_8BIT);
  23. free32=xPortGetFreeHeapSizeCaps(MALLOC_CAP_32BIT);
  24. printf("Free 8bit-capable memory: %dK, 32-bit capable memory %dK\n", free8, free32);
  25. //Both should have gone down by 10K; 8bit capable ram is also 32-bit capable
  26. TEST_ASSERT(free8<(free8start-10*1024));
  27. TEST_ASSERT(free32<(free32start-10*1024));
  28. //Assume we got DRAM back
  29. TEST_ASSERT((((int)m1)&0xFF000000)==0x3F000000);
  30. free(m1);
  31. printf("Freeing; allocating 10K of 32K-capable RAM\n");
  32. m1=pvPortMallocCaps(10*1024, MALLOC_CAP_32BIT);
  33. printf("--> %p\n", m1);
  34. free8=xPortGetFreeHeapSizeCaps(MALLOC_CAP_8BIT);
  35. free32=xPortGetFreeHeapSizeCaps(MALLOC_CAP_32BIT);
  36. printf("Free 8bit-capable memory: %dK, 32-bit capable memory %dK\n", free8, free32);
  37. //Only 32-bit should have gone down by 10K: 32-bit isn't necessarily 8bit capable
  38. TEST_ASSERT(free32<(free32start-10*1024));
  39. TEST_ASSERT(free8==free8start);
  40. //Assume we got IRAM back
  41. TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
  42. free(m1);
  43. printf("Allocating impossible caps\n");
  44. m1=pvPortMallocCaps(10*1024, MALLOC_CAP_8BIT|MALLOC_CAP_EXEC);
  45. printf("--> %p\n", m1);
  46. TEST_ASSERT(m1==NULL);
  47. printf("Testing changeover iram -> dram");
  48. for (x=0; x<10; x++) {
  49. m2[x]=pvPortMallocCaps(10*1024, MALLOC_CAP_32BIT);
  50. printf("--> %p\n", m2[x]);
  51. }
  52. TEST_ASSERT((((int)m2[0])&0xFF000000)==0x40000000);
  53. TEST_ASSERT((((int)m2[9])&0xFF000000)==0x3F000000);
  54. printf("Test if allocating executable code still gives IRAM, even with dedicated IRAM region depleted\n");
  55. m1=pvPortMallocCaps(10*1024, MALLOC_CAP_EXEC);
  56. printf("--> %p\n", m1);
  57. TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
  58. free(m1);
  59. for (x=0; x<10; x++) free(m2[x]);
  60. printf("Done.\n");
  61. }