heap_realloc.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 *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. ptr1 = rt_realloc(ptr1, 13);
  33. ptr2 = rt_realloc(ptr2, 31);
  34. ptr3 = rt_realloc(ptr3, 127);
  35. ptr4 = rt_realloc(ptr4, 1);
  36. ptr5 = rt_realloc(ptr5, 0);
  37. if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
  38. if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
  39. if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
  40. if (mem_check(ptr4, 4, 1) != RT_FALSE) goto _failed;
  41. rt_free(ptr4);
  42. rt_free(ptr3);
  43. rt_free(ptr3);
  44. rt_free(ptr1);
  45. if (ptr5 != RT_NULL)
  46. {
  47. rt_free(ptr5);
  48. }
  49. tc_done(TC_STAT_PASSED);
  50. _failed:
  51. tc_done(TC_STAT_FAILED);
  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