xref: /nrf52832-nimble/rt-thread/examples/kernel/heap_malloc.c (revision 167494296f0543431a51b6b1b83e957045294e05)
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)
13             return RT_FALSE;
14         ptr ++;
15         len --;
16     }
17 
18     return RT_TRUE;
19 }
20 
21 static void heap_malloc_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         res = TC_STAT_FAILED;
39     if (mem_check(ptr2, 2, 13)  == RT_FALSE)
40         res = TC_STAT_FAILED;
41     if (mem_check(ptr3, 3, 31)  == RT_FALSE)
42         res = TC_STAT_FAILED;
43     if (mem_check(ptr4, 4, 127) == RT_FALSE)
44         res = TC_STAT_FAILED;
45 
46     rt_free(ptr4);
47     rt_free(ptr3);
48     rt_free(ptr2);
49     rt_free(ptr1);
50 
51     if (ptr5 != RT_NULL)
52     {
53         rt_free(ptr5);
54     }
55 
56     tc_done(res);
57 }
58 
59 #ifdef RT_USING_TC
60 int _tc_heap_malloc()
61 {
62     heap_malloc_init();
63 
64     return 0;
65 }
66 FINSH_FUNCTION_EXPORT(_tc_heap_malloc, a heap malloc test);
67 #else
68 int rt_application_init()
69 {
70     heap_malloc_init();
71 
72     return 0;
73 }
74 #endif
75