1
0

heap_malloc.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <rtthread.h>
  2. #include "tc_comm.h"
  3. /*
  4. * This is an example for heap malloc
  5. */
  6. static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
  7. {
  8. while (len)
  9. {
  10. if (*ptr != value) return RT_FALSE;
  11. ptr ++;
  12. len --;
  13. }
  14. return RT_TRUE;
  15. }
  16. static void heap_malloc_init()
  17. {
  18. rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
  19. ptr1 = rt_malloc(1);
  20. ptr2 = rt_malloc(13);
  21. ptr3 = rt_malloc(31);
  22. ptr4 = rt_malloc(127);
  23. ptr5 = rt_malloc(0);
  24. memset(ptr1, 1, 1);
  25. memset(ptr2, 2, 13);
  26. memset(ptr3, 3, 31);
  27. memset(ptr4, 4, 127);
  28. if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
  29. if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
  30. if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
  31. if (mem_check(ptr4, 4, 127) != RT_FALSE) goto _failed;
  32. rt_free(ptr4);
  33. rt_free(ptr3);
  34. rt_free(ptr3);
  35. rt_free(ptr1);
  36. if (ptr5 != RT_NULL)
  37. {
  38. rt_free(ptr5);
  39. }
  40. tc_done(TC_STAT_PASSED);
  41. _failed:
  42. tc_done(TC_STAT_FAILED);
  43. }
  44. #ifdef RT_USING_TC
  45. int _tc_heap_malloc()
  46. {
  47. heap_malloc_init();
  48. return 0;
  49. }
  50. FINSH_FUNCTION_EXPORT(_tc_heap_malloc, a heap malloc test);
  51. #else
  52. int rt_application_init()
  53. {
  54. heap_malloc_init();
  55. return 0;
  56. }
  57. #endif