1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2014-08-03     bernard      Add file header
9  * 2021-11-13     Meco Man     implement no-heap warning
10  */
11 
12 #include <rtthread.h>
13 #include <stddef.h>
14 
15 #ifndef RT_USING_HEAP
16 #define DBG_TAG    "armlibc.syscall.mem"
17 #define DBG_LVL    DBG_INFO
18 #include <rtdbg.h>
19 
20 #define _NO_HEAP_ERROR()  do{LOG_E("Please enable RT_USING_HEAP");\
21                              RT_ASSERT(0);\
22                             }while(0)
23 #endif /* RT_USING_HEAP */
24 
25 #ifdef __CC_ARM
26     /* avoid the heap and heap-using library functions supplied by arm */
27     #pragma import(__use_no_heap)
28 #endif /* __CC_ARM */
29 
malloc(size_t n)30 void *malloc(size_t n)
31 {
32 #ifdef RT_USING_HEAP
33     return rt_malloc(n);
34 #else
35     _NO_HEAP_ERROR();
36     return RT_NULL;
37 #endif
38 }
39 RTM_EXPORT(malloc);
40 
realloc(void * rmem,size_t newsize)41 void *realloc(void *rmem, size_t newsize)
42 {
43 #ifdef RT_USING_HEAP
44     return rt_realloc(rmem, newsize);
45 #else
46     _NO_HEAP_ERROR();
47     return RT_NULL;
48 #endif
49 }
50 RTM_EXPORT(realloc);
51 
calloc(size_t nelem,size_t elsize)52 void *calloc(size_t nelem, size_t elsize)
53 {
54 #ifdef RT_USING_HEAP
55     return rt_calloc(nelem, elsize);
56 #else
57     _NO_HEAP_ERROR();
58     return RT_NULL;
59 #endif
60 }
61 RTM_EXPORT(calloc);
62 
free(void * rmem)63 void free(void *rmem)
64 {
65 #ifdef RT_USING_HEAP
66     rt_free(rmem);
67 #else
68     _NO_HEAP_ERROR();
69 #endif
70 }
71 RTM_EXPORT(free);
72