1
0

heap_malloc.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. *
  8. */
  9. #include <rtthread.h>
  10. #include "tc_comm.h"
  11. /*
  12. * This is an example for heap malloc
  13. */
  14. static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
  15. {
  16. while (len)
  17. {
  18. if (*ptr != value)
  19. return RT_FALSE;
  20. ptr ++;
  21. len --;
  22. }
  23. return RT_TRUE;
  24. }
  25. static void heap_malloc_init()
  26. {
  27. rt_uint8_t res = TC_STAT_PASSED;
  28. rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
  29. ptr1 = rt_malloc(1);
  30. ptr2 = rt_malloc(13);
  31. ptr3 = rt_malloc(31);
  32. ptr4 = rt_malloc(127);
  33. ptr5 = rt_malloc(0);
  34. memset(ptr1, 1, 1);
  35. memset(ptr2, 2, 13);
  36. memset(ptr3, 3, 31);
  37. memset(ptr4, 4, 127);
  38. if (mem_check(ptr1, 1, 1) == RT_FALSE)
  39. res = TC_STAT_FAILED;
  40. if (mem_check(ptr2, 2, 13) == RT_FALSE)
  41. res = TC_STAT_FAILED;
  42. if (mem_check(ptr3, 3, 31) == RT_FALSE)
  43. res = TC_STAT_FAILED;
  44. if (mem_check(ptr4, 4, 127) == RT_FALSE)
  45. res = TC_STAT_FAILED;
  46. rt_free(ptr4);
  47. rt_free(ptr3);
  48. rt_free(ptr2);
  49. rt_free(ptr1);
  50. if (ptr5 != RT_NULL)
  51. {
  52. rt_free(ptr5);
  53. }
  54. tc_done(res);
  55. }
  56. #ifdef RT_USING_TC
  57. int _tc_heap_malloc()
  58. {
  59. heap_malloc_init();
  60. return 0;
  61. }
  62. FINSH_FUNCTION_EXPORT(_tc_heap_malloc, a heap malloc test);
  63. #else
  64. int rt_application_init()
  65. {
  66. heap_malloc_init();
  67. return 0;
  68. }
  69. #endif