heap_realloc.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_realloc_init()
  17. {
  18. rt_uint8_t res = TC_STAT_PASSED;
  19. rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
  20. ptr1 = rt_malloc(1);
  21. ptr2 = rt_malloc(13);
  22. ptr3 = rt_malloc(31);
  23. ptr4 = rt_malloc(127);
  24. ptr5 = rt_malloc(0);
  25. memset(ptr1, 1, 1);
  26. memset(ptr2, 2, 13);
  27. memset(ptr3, 3, 31);
  28. memset(ptr4, 4, 127);
  29. if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
  30. if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
  31. if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
  32. if (mem_check(ptr4, 4, 127) != RT_FALSE) goto _failed;
  33. ptr1 = rt_realloc(ptr1, 13);
  34. ptr2 = rt_realloc(ptr2, 31);
  35. ptr3 = rt_realloc(ptr3, 127);
  36. ptr4 = rt_realloc(ptr4, 1);
  37. ptr5 = rt_realloc(ptr5, 0);
  38. if (ptr5)
  39. {
  40. rt_kprintf("realloc(ptr, 0) should return NULL\n");
  41. res = TC_STAT_FAILED;
  42. }
  43. if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
  44. if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
  45. if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
  46. if (mem_check(ptr4, 4, 1) != RT_FALSE) goto _failed;
  47. rt_free(ptr4);
  48. rt_free(ptr3);
  49. rt_free(ptr3);
  50. rt_free(ptr1);
  51. tc_done(res);
  52. }
  53. #ifdef RT_USING_TC
  54. int _tc_heap_realloc()
  55. {
  56. heap_realloc_init();
  57. return 0;
  58. }
  59. FINSH_FUNCTION_EXPORT(_tc_heap_realloc, a heap re-malloc test);
  60. #else
  61. int rt_application_init()
  62. {
  63. heap_realloc_init();
  64. return 0;
  65. }
  66. #endif