test_heap.c 943 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. * 2020-10-17 Meco Man translate to English comment
  10. */
  11. /*
  12. * Demo: heap
  13. *
  14. * This demo demonstrates allocating memory from the heap
  15. *
  16. */
  17. #include <FreeRTOS.h>
  18. // Set to 1 if using heap_5.c
  19. #define USE_HEAP_5 0
  20. #if USE_HEAP_5 == 1
  21. rt_uint8_t heap[512];
  22. HeapRegion_t heap_regions[] =
  23. {
  24. {heap, 512},
  25. {NULL, 0}
  26. };
  27. #endif
  28. int heap_sample(void)
  29. {
  30. #if USE_HEAP_5 == 1
  31. vPortDefineHeapRegions(heap_regions);
  32. #endif
  33. rt_uint8_t *ptr = pvPortMalloc(128);
  34. if (ptr == RT_NULL)
  35. {
  36. rt_kprintf("Memory allocation failed.\n");
  37. return -1;
  38. }
  39. for (rt_uint8_t i = 0; i < 128; i++)
  40. {
  41. ptr[i] = i;
  42. }
  43. vPortFree(ptr);
  44. return 0;
  45. }
  46. MSH_CMD_EXPORT(heap_sample, heap sample);