1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2014-08-03 bernard Add file header
* 2021-11-13 Meco Man implement no-heap warning
*/
#include <rtthread.h>
#include <stddef.h>
#ifndef RT_USING_HEAP
#define DBG_TAG "armlibc.syscall.mem"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define _NO_HEAP_ERROR() do{LOG_E("Please enable RT_USING_HEAP");\
RT_ASSERT(0);\
}while(0)
#endif /* RT_USING_HEAP */
#ifdef __CC_ARM
/* avoid the heap and heap-using library functions supplied by arm */
#pragma import(__use_no_heap)
#endif /* __CC_ARM */
/**
* @brief Allocate memory block
*
* Allocates a block of size bytes of memory, returning a pointer to the
* beginning of the block. The content of the newly allocated block of
* memory is not initialized, remaining with indeterminate values.
*
* @param[in] n the size of the memory block, in bytes.
*
* @return On success, a pointer to the memory block allocated by the function.
* If the system is configured without heap (RT_USING_HEAP is not defined),
* the function will assert and return RT_NULL.
*
* @note The returned pointer is always suitably aligned for any built-in type.
*/
void *malloc(size_t n)
{
#ifdef RT_USING_HEAP
return rt_malloc(n);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}
RTM_EXPORT(malloc);
/**
* @brief Reallocate memory block
*
* Changes the size of the memory block pointed to by rmem.
* The function may move the memory block to a new location
* (whose address is returned by the function).
* The content of the memory block is preserved up to the
* lesser of the new and old sizes, even if the block is
* moved to a new location. If the new size is larger,
* the value of the newly allocated portion is indeterminate.
*
* @param[in,out] rmem pointer to a memory block previously allocated with
* malloc, calloc or realloc to be reallocated.
* If this is RT_NULL, a new block is allocated and
* a pointer to it is returned by the function.
* @param[in] newsize new size for the memory block, in bytes.
*
* @return A pointer to the reallocated memory block, which may be either
* the same as the rmem pointer or a new location.
* If the system is configured without heap (RT_USING_HEAP is not defined),
* the function will assert and return RT_NULL.
*/
void *realloc(void *rmem, size_t newsize)
{
#ifdef RT_USING_HEAP
return rt_realloc(rmem, newsize);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}
RTM_EXPORT(realloc);
/**
* @brief Allocate and zero-initialize array
*
* Allocates a block of memory for an array of nelem elements, each of them
* elsize bytes long, and initializes all its bits to zero.
* The effective result is the allocation of a zero-initialized memory block
* of (nelem*elsize) bytes.
*
* @param[in] nelem number of elements to allocate.
* @param[in] elsize size of each element.
*
* @return On success, a pointer to the memory block allocated by the function.
* If the system is configured without heap (RT_USING_HEAP is not defined),
* the function will assert and return RT_NULL.
*/
void *calloc(size_t nelem, size_t elsize)
{
#ifdef RT_USING_HEAP
return rt_calloc(nelem, elsize);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}
RTM_EXPORT(calloc);
/**
* @brief Deallocate memory block
*
* A block of memory previously allocated by a call to malloc, calloc or realloc
* is deallocated, making it available again for further allocations.
*
* @param[in] rmem pointer to a memory block previously allocated with malloc,
* calloc or realloc to be deallocated. If a null pointer is
* passed as argument, no action occurs.
*
* @note If the system is configured without heap (RT_USING_HEAP is not defined),
* the function will assert.
*/
void free(void *rmem)
{
#ifdef RT_USING_HEAP
rt_free(rmem);
#else
_NO_HEAP_ERROR();
#endif
}
RTM_EXPORT(free);
|