1 #include <rtthread.h> 2 #include "tc_comm.h" 3 4 /* 5 * This is an example for heap malloc 6 */ 7 8 static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len) 9 { 10 while (len) 11 { 12 if (*ptr != value) return RT_FALSE; 13 14 ptr ++; 15 len --; 16 } 17 18 return RT_TRUE; 19 } 20 21 static void heap_realloc_init() 22 { 23 rt_uint8_t res = TC_STAT_PASSED; 24 rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5; 25 26 ptr1 = rt_malloc(1); 27 ptr2 = rt_malloc(13); 28 ptr3 = rt_malloc(31); 29 ptr4 = rt_malloc(127); 30 ptr5 = rt_malloc(0); 31 32 memset(ptr1, 1, 1); 33 memset(ptr2, 2, 13); 34 memset(ptr3, 3, 31); 35 memset(ptr4, 4, 127); 36 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 58 ptr1 = rt_realloc(ptr1, 13); 59 ptr2 = rt_realloc(ptr2, 31); 60 ptr3 = rt_realloc(ptr3, 127); 61 ptr4 = rt_realloc(ptr4, 1); 62 ptr5 = rt_realloc(ptr5, 0); 63 if (ptr5) 64 { 65 rt_kprintf("realloc(ptr, 0) should return NULL\n"); 66 res = TC_STAT_FAILED; 67 } 68 69 if (mem_check(ptr1, 1, 1) == RT_FALSE) 70 res = TC_STAT_FAILED; 71 if (mem_check(ptr2, 2, 13) == RT_FALSE) 72 res = TC_STAT_FAILED; 73 if (mem_check(ptr3, 3, 31) == RT_FALSE) 74 res = TC_STAT_FAILED; 75 if (mem_check(ptr4, 4, 1) == RT_FALSE) 76 res = TC_STAT_FAILED; 77 78 _free: 79 rt_free(ptr4); 80 rt_free(ptr3); 81 rt_free(ptr2); 82 rt_free(ptr1); 83 84 tc_done(res); 85 } 86 87 #ifdef RT_USING_TC 88 int _tc_heap_realloc() 89 { 90 heap_realloc_init(); 91 92 return 0; 93 } 94 FINSH_FUNCTION_EXPORT(_tc_heap_realloc, a heap re-malloc test); 95 #else 96 int rt_application_init() 97 { 98 heap_realloc_init(); 99 100 return 0; 101 } 102 #endif 103