heap_realloc.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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) return RT_FALSE;
  19. ptr ++;
  20. len --;
  21. }
  22. return RT_TRUE;
  23. }
  24. static void heap_realloc_init()
  25. {
  26. rt_uint8_t res = TC_STAT_PASSED;
  27. rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
  28. ptr1 = rt_malloc(1);
  29. ptr2 = rt_malloc(13);
  30. ptr3 = rt_malloc(31);
  31. ptr4 = rt_malloc(127);
  32. ptr5 = rt_malloc(0);
  33. memset(ptr1, 1, 1);
  34. memset(ptr2, 2, 13);
  35. memset(ptr3, 3, 31);
  36. memset(ptr4, 4, 127);
  37. if (mem_check(ptr1, 1, 1) == RT_FALSE)
  38. {
  39. res = TC_STAT_FAILED;
  40. goto _free;
  41. }
  42. if (mem_check(ptr2, 2, 13) == RT_FALSE)
  43. {
  44. res = TC_STAT_FAILED;
  45. goto _free;
  46. }
  47. if (mem_check(ptr3, 3, 31) == RT_FALSE)
  48. {
  49. res = TC_STAT_FAILED;
  50. goto _free;
  51. }
  52. if (mem_check(ptr4, 4, 127) == RT_FALSE)
  53. {
  54. res = TC_STAT_FAILED;
  55. goto _free;
  56. }
  57. ptr1 = rt_realloc(ptr1, 13);
  58. ptr2 = rt_realloc(ptr2, 31);
  59. ptr3 = rt_realloc(ptr3, 127);
  60. ptr4 = rt_realloc(ptr4, 1);
  61. ptr5 = rt_realloc(ptr5, 0);
  62. if (ptr5)
  63. {
  64. rt_kprintf("realloc(ptr, 0) should return NULL\n");
  65. res = TC_STAT_FAILED;
  66. }
  67. if (mem_check(ptr1, 1, 1) == RT_FALSE)
  68. res = TC_STAT_FAILED;
  69. if (mem_check(ptr2, 2, 13) == RT_FALSE)
  70. res = TC_STAT_FAILED;
  71. if (mem_check(ptr3, 3, 31) == RT_FALSE)
  72. res = TC_STAT_FAILED;
  73. if (mem_check(ptr4, 4, 1) == RT_FALSE)
  74. res = TC_STAT_FAILED;
  75. _free:
  76. rt_free(ptr4);
  77. rt_free(ptr3);
  78. rt_free(ptr2);
  79. rt_free(ptr1);
  80. tc_done(res);
  81. }
  82. #ifdef RT_USING_TC
  83. int _tc_heap_realloc()
  84. {
  85. heap_realloc_init();
  86. return 0;
  87. }
  88. FINSH_FUNCTION_EXPORT(_tc_heap_realloc, a heap re-malloc test);
  89. #else
  90. int rt_application_init()
  91. {
  92. heap_realloc_init();
  93. return 0;
  94. }
  95. #endif