User | Revision | Line number | New contents of line |
ganlikun |
0:06036f8bee2d
|
1
|
/* mbed Microcontroller Library
|
ganlikun |
0:06036f8bee2d
|
2
|
* Copyright (c) 2006-2016 ARM Limited
|
ganlikun |
0:06036f8bee2d
|
3
|
*
|
ganlikun |
0:06036f8bee2d
|
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
ganlikun |
0:06036f8bee2d
|
5
|
* you may not use this file except in compliance with the License.
|
ganlikun |
0:06036f8bee2d
|
6
|
* You may obtain a copy of the License at
|
ganlikun |
0:06036f8bee2d
|
7
|
*
|
ganlikun |
0:06036f8bee2d
|
8
|
* http://www.apache.org/licenses/LICENSE-2.0
|
ganlikun |
0:06036f8bee2d
|
9
|
*
|
ganlikun |
0:06036f8bee2d
|
10
|
* Unless required by applicable law or agreed to in writing, software
|
ganlikun |
0:06036f8bee2d
|
11
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
ganlikun |
0:06036f8bee2d
|
12
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
ganlikun |
0:06036f8bee2d
|
13
|
* See the License for the specific language governing permissions and
|
ganlikun |
0:06036f8bee2d
|
14
|
* limitations under the License.
|
ganlikun |
0:06036f8bee2d
|
15
|
*/
|
ganlikun |
0:06036f8bee2d
|
16
|
|
ganlikun |
0:06036f8bee2d
|
17
|
#include "platform/mbed_mem_trace.h"
|
ganlikun |
0:06036f8bee2d
|
18
|
#include "platform/mbed_stats.h"
|
ganlikun |
0:06036f8bee2d
|
19
|
#include "platform/mbed_toolchain.h"
|
ganlikun |
0:06036f8bee2d
|
20
|
#include "platform/SingletonPtr.h"
|
ganlikun |
0:06036f8bee2d
|
21
|
#include "platform/PlatformMutex.h"
|
ganlikun |
0:06036f8bee2d
|
22
|
#include <stddef.h>
|
ganlikun |
0:06036f8bee2d
|
23
|
#include <stdio.h>
|
ganlikun |
0:06036f8bee2d
|
24
|
#include <string.h>
|
ganlikun |
0:06036f8bee2d
|
25
|
#include <stdlib.h>
|
ganlikun |
0:06036f8bee2d
|
26
|
|
ganlikun |
0:06036f8bee2d
|
27
|
/* There are two memory tracers in mbed OS:
|
ganlikun |
0:06036f8bee2d
|
28
|
|
ganlikun |
0:06036f8bee2d
|
29
|
- the first can be used to detect the maximum heap usage at runtime. It is
|
ganlikun |
0:06036f8bee2d
|
30
|
activated by defining the MBED_HEAP_STATS_ENABLED macro.
|
ganlikun |
0:06036f8bee2d
|
31
|
- the second can be used to trace each memory call by automatically invoking
|
ganlikun |
0:06036f8bee2d
|
32
|
a callback on each memory operation (see hal/api/mbed_mem_trace.h). It is
|
ganlikun |
0:06036f8bee2d
|
33
|
activated by defining the MBED_MEM_TRACING_ENABLED macro.
|
ganlikun |
0:06036f8bee2d
|
34
|
|
ganlikun |
0:06036f8bee2d
|
35
|
Both tracers can be activated and deactivated in any combination. If both tracers
|
ganlikun |
0:06036f8bee2d
|
36
|
are active, the second one (MBED_MEM_TRACING_ENABLED) will trace the first one's
|
ganlikun |
0:06036f8bee2d
|
37
|
(MBED_HEAP_STATS_ENABLED) memory calls.*/
|
ganlikun |
0:06036f8bee2d
|
38
|
|
ganlikun |
0:06036f8bee2d
|
39
|
/******************************************************************************/
|
ganlikun |
0:06036f8bee2d
|
40
|
/* Implementation of the runtime max heap usage checker */
|
ganlikun |
0:06036f8bee2d
|
41
|
/******************************************************************************/
|
ganlikun |
0:06036f8bee2d
|
42
|
|
ganlikun |
0:06036f8bee2d
|
43
|
/* Size must be a multiple of 8 to keep alignment */
|
ganlikun |
0:06036f8bee2d
|
44
|
typedef struct {
|
ganlikun |
0:06036f8bee2d
|
45
|
uint32_t size;
|
ganlikun |
0:06036f8bee2d
|
46
|
uint32_t pad;
|
ganlikun |
0:06036f8bee2d
|
47
|
} alloc_info_t;
|
ganlikun |
0:06036f8bee2d
|
48
|
|
ganlikun |
0:06036f8bee2d
|
49
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
50
|
static SingletonPtr<PlatformMutex> mem_trace_mutex;
|
ganlikun |
0:06036f8bee2d
|
51
|
#endif
|
ganlikun |
0:06036f8bee2d
|
52
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
53
|
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
|
ganlikun |
0:06036f8bee2d
|
54
|
static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0};
|
ganlikun |
0:06036f8bee2d
|
55
|
#endif
|
ganlikun |
0:06036f8bee2d
|
56
|
|
ganlikun |
0:06036f8bee2d
|
57
|
void mbed_stats_heap_get(mbed_stats_heap_t *stats)
|
ganlikun |
0:06036f8bee2d
|
58
|
{
|
ganlikun |
0:06036f8bee2d
|
59
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
60
|
extern uint32_t mbed_heap_size;
|
ganlikun |
0:06036f8bee2d
|
61
|
heap_stats.reserved_size = mbed_heap_size;
|
ganlikun |
0:06036f8bee2d
|
62
|
|
ganlikun |
0:06036f8bee2d
|
63
|
malloc_stats_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
64
|
memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
|
ganlikun |
0:06036f8bee2d
|
65
|
malloc_stats_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
66
|
#else
|
ganlikun |
0:06036f8bee2d
|
67
|
memset(stats, 0, sizeof(mbed_stats_heap_t));
|
ganlikun |
0:06036f8bee2d
|
68
|
#endif
|
ganlikun |
0:06036f8bee2d
|
69
|
}
|
ganlikun |
0:06036f8bee2d
|
70
|
|
ganlikun |
0:06036f8bee2d
|
71
|
/******************************************************************************/
|
ganlikun |
0:06036f8bee2d
|
72
|
/* GCC memory allocation wrappers */
|
ganlikun |
0:06036f8bee2d
|
73
|
/******************************************************************************/
|
ganlikun |
0:06036f8bee2d
|
74
|
|
ganlikun |
0:06036f8bee2d
|
75
|
#if defined(TOOLCHAIN_GCC)
|
ganlikun |
0:06036f8bee2d
|
76
|
|
ganlikun |
0:06036f8bee2d
|
77
|
#ifdef FEATURE_UVISOR
|
ganlikun |
0:06036f8bee2d
|
78
|
#include "uvisor-lib/uvisor-lib.h"
|
ganlikun |
0:06036f8bee2d
|
79
|
#endif/* FEATURE_UVISOR */
|
ganlikun |
0:06036f8bee2d
|
80
|
|
ganlikun |
0:06036f8bee2d
|
81
|
extern "C" {
|
ganlikun |
0:06036f8bee2d
|
82
|
void * __real__malloc_r(struct _reent * r, size_t size);
|
ganlikun |
0:06036f8bee2d
|
83
|
void * __real__memalign_r(struct _reent * r, size_t alignment, size_t bytes);
|
ganlikun |
0:06036f8bee2d
|
84
|
void * __real__realloc_r(struct _reent * r, void * ptr, size_t size);
|
ganlikun |
0:06036f8bee2d
|
85
|
void __real__free_r(struct _reent * r, void * ptr);
|
ganlikun |
0:06036f8bee2d
|
86
|
void* __real__calloc_r(struct _reent * r, size_t nmemb, size_t size);
|
ganlikun |
0:06036f8bee2d
|
87
|
}
|
ganlikun |
0:06036f8bee2d
|
88
|
|
ganlikun |
0:06036f8bee2d
|
89
|
// TODO: memory tracing doesn't work with uVisor enabled.
|
ganlikun |
0:06036f8bee2d
|
90
|
#if !defined(FEATURE_UVISOR)
|
ganlikun |
0:06036f8bee2d
|
91
|
|
ganlikun |
0:06036f8bee2d
|
92
|
extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
|
ganlikun |
0:06036f8bee2d
|
93
|
void *ptr = NULL;
|
ganlikun |
0:06036f8bee2d
|
94
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
95
|
malloc_stats_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
96
|
alloc_info_t *alloc_info = (alloc_info_t*)__real__malloc_r(r, size + sizeof(alloc_info_t));
|
ganlikun |
0:06036f8bee2d
|
97
|
if (alloc_info != NULL) {
|
ganlikun |
0:06036f8bee2d
|
98
|
alloc_info->size = size;
|
ganlikun |
0:06036f8bee2d
|
99
|
ptr = (void*)(alloc_info + 1);
|
ganlikun |
0:06036f8bee2d
|
100
|
heap_stats.current_size += size;
|
ganlikun |
0:06036f8bee2d
|
101
|
heap_stats.total_size += size;
|
ganlikun |
0:06036f8bee2d
|
102
|
heap_stats.alloc_cnt += 1;
|
ganlikun |
0:06036f8bee2d
|
103
|
if (heap_stats.current_size > heap_stats.max_size) {
|
ganlikun |
0:06036f8bee2d
|
104
|
heap_stats.max_size = heap_stats.current_size;
|
ganlikun |
0:06036f8bee2d
|
105
|
}
|
ganlikun |
0:06036f8bee2d
|
106
|
} else {
|
ganlikun |
0:06036f8bee2d
|
107
|
heap_stats.alloc_fail_cnt += 1;
|
ganlikun |
0:06036f8bee2d
|
108
|
}
|
ganlikun |
0:06036f8bee2d
|
109
|
malloc_stats_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
110
|
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
111
|
ptr = __real__malloc_r(r, size);
|
ganlikun |
0:06036f8bee2d
|
112
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
113
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
114
|
mem_trace_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
115
|
mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
|
ganlikun |
0:06036f8bee2d
|
116
|
mem_trace_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
117
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
118
|
return ptr;
|
ganlikun |
0:06036f8bee2d
|
119
|
}
|
ganlikun |
0:06036f8bee2d
|
120
|
|
ganlikun |
0:06036f8bee2d
|
121
|
extern "C" void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size) {
|
ganlikun |
0:06036f8bee2d
|
122
|
void *new_ptr = NULL;
|
ganlikun |
0:06036f8bee2d
|
123
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
124
|
// Implement realloc_r with malloc and free.
|
ganlikun |
0:06036f8bee2d
|
125
|
// The function realloc_r can't be used here directly since
|
ganlikun |
0:06036f8bee2d
|
126
|
// it can call into __wrap__malloc_r (returns ptr + 4) or
|
ganlikun |
0:06036f8bee2d
|
127
|
// resize memory directly (returns ptr + 0).
|
ganlikun |
0:06036f8bee2d
|
128
|
|
ganlikun |
0:06036f8bee2d
|
129
|
// Note - no lock needed since malloc and free are thread safe
|
ganlikun |
0:06036f8bee2d
|
130
|
|
ganlikun |
0:06036f8bee2d
|
131
|
// Get old size
|
ganlikun |
0:06036f8bee2d
|
132
|
uint32_t old_size = 0;
|
ganlikun |
0:06036f8bee2d
|
133
|
if (ptr != NULL) {
|
ganlikun |
0:06036f8bee2d
|
134
|
alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
|
ganlikun |
0:06036f8bee2d
|
135
|
old_size = alloc_info->size;
|
ganlikun |
0:06036f8bee2d
|
136
|
}
|
ganlikun |
0:06036f8bee2d
|
137
|
|
ganlikun |
0:06036f8bee2d
|
138
|
// Allocate space
|
ganlikun |
0:06036f8bee2d
|
139
|
if (size != 0) {
|
ganlikun |
0:06036f8bee2d
|
140
|
new_ptr = malloc(size);
|
ganlikun |
0:06036f8bee2d
|
141
|
}
|
ganlikun |
0:06036f8bee2d
|
142
|
|
ganlikun |
0:06036f8bee2d
|
143
|
// If the new buffer has been allocated copy the data to it
|
ganlikun |
0:06036f8bee2d
|
144
|
// and free the old buffer
|
ganlikun |
0:06036f8bee2d
|
145
|
if (new_ptr != NULL) {
|
ganlikun |
0:06036f8bee2d
|
146
|
uint32_t copy_size = (old_size < size) ? old_size : size;
|
ganlikun |
0:06036f8bee2d
|
147
|
memcpy(new_ptr, (void*)ptr, copy_size);
|
ganlikun |
0:06036f8bee2d
|
148
|
free(ptr);
|
ganlikun |
0:06036f8bee2d
|
149
|
}
|
ganlikun |
0:06036f8bee2d
|
150
|
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
151
|
new_ptr = __real__realloc_r(r, ptr, size);
|
ganlikun |
0:06036f8bee2d
|
152
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
153
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
154
|
mem_trace_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
155
|
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
|
ganlikun |
0:06036f8bee2d
|
156
|
mem_trace_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
157
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
158
|
return new_ptr;
|
ganlikun |
0:06036f8bee2d
|
159
|
}
|
ganlikun |
0:06036f8bee2d
|
160
|
|
ganlikun |
0:06036f8bee2d
|
161
|
extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
|
ganlikun |
0:06036f8bee2d
|
162
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
163
|
malloc_stats_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
164
|
alloc_info_t *alloc_info = NULL;
|
ganlikun |
0:06036f8bee2d
|
165
|
if (ptr != NULL) {
|
ganlikun |
0:06036f8bee2d
|
166
|
alloc_info = ((alloc_info_t*)ptr) - 1;
|
ganlikun |
0:06036f8bee2d
|
167
|
heap_stats.current_size -= alloc_info->size;
|
ganlikun |
0:06036f8bee2d
|
168
|
heap_stats.alloc_cnt -= 1;
|
ganlikun |
0:06036f8bee2d
|
169
|
}
|
ganlikun |
0:06036f8bee2d
|
170
|
__real__free_r(r, (void*)alloc_info);
|
ganlikun |
0:06036f8bee2d
|
171
|
malloc_stats_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
172
|
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
173
|
__real__free_r(r, ptr);
|
ganlikun |
0:06036f8bee2d
|
174
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
175
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
176
|
mem_trace_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
177
|
mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
|
ganlikun |
0:06036f8bee2d
|
178
|
mem_trace_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
179
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
180
|
}
|
ganlikun |
0:06036f8bee2d
|
181
|
|
ganlikun |
0:06036f8bee2d
|
182
|
extern "C" void * __wrap__calloc_r(struct _reent * r, size_t nmemb, size_t size) {
|
ganlikun |
0:06036f8bee2d
|
183
|
void *ptr = NULL;
|
ganlikun |
0:06036f8bee2d
|
184
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
185
|
// Note - no lock needed since malloc is thread safe
|
ganlikun |
0:06036f8bee2d
|
186
|
|
ganlikun |
0:06036f8bee2d
|
187
|
ptr = malloc(nmemb * size);
|
ganlikun |
0:06036f8bee2d
|
188
|
if (ptr != NULL) {
|
ganlikun |
0:06036f8bee2d
|
189
|
memset(ptr, 0, nmemb * size);
|
ganlikun |
0:06036f8bee2d
|
190
|
}
|
ganlikun |
0:06036f8bee2d
|
191
|
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
192
|
ptr = __real__calloc_r(r, nmemb, size);
|
ganlikun |
0:06036f8bee2d
|
193
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
194
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
195
|
mem_trace_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
196
|
mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
|
ganlikun |
0:06036f8bee2d
|
197
|
mem_trace_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
198
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
199
|
return ptr;
|
ganlikun |
0:06036f8bee2d
|
200
|
}
|
ganlikun |
0:06036f8bee2d
|
201
|
|
ganlikun |
0:06036f8bee2d
|
202
|
extern "C" void * __wrap__memalign_r(struct _reent * r, size_t alignment, size_t bytes) {
|
ganlikun |
0:06036f8bee2d
|
203
|
return __real__memalign_r(r, alignment, bytes);
|
ganlikun |
0:06036f8bee2d
|
204
|
}
|
ganlikun |
0:06036f8bee2d
|
205
|
|
ganlikun |
0:06036f8bee2d
|
206
|
#endif // if !defined(FEATURE_UVISOR)
|
ganlikun |
0:06036f8bee2d
|
207
|
|
ganlikun |
0:06036f8bee2d
|
208
|
|
ganlikun |
0:06036f8bee2d
|
209
|
/******************************************************************************/
|
ganlikun |
0:06036f8bee2d
|
210
|
/* ARMCC / IAR memory allocation wrappers */
|
ganlikun |
0:06036f8bee2d
|
211
|
/******************************************************************************/
|
ganlikun |
0:06036f8bee2d
|
212
|
|
ganlikun |
0:06036f8bee2d
|
213
|
#elif defined(TOOLCHAIN_ARM) || defined(__ICCARM__)
|
ganlikun |
0:06036f8bee2d
|
214
|
|
ganlikun |
0:06036f8bee2d
|
215
|
#if defined(TOOLCHAIN_ARM)
|
ganlikun |
0:06036f8bee2d
|
216
|
#define SUPER_MALLOC $Super$$malloc
|
ganlikun |
0:06036f8bee2d
|
217
|
#define SUB_MALLOC $Sub$$malloc
|
ganlikun |
0:06036f8bee2d
|
218
|
#define SUPER_REALLOC $Super$$realloc
|
ganlikun |
0:06036f8bee2d
|
219
|
#define SUB_REALLOC $Sub$$realloc
|
ganlikun |
0:06036f8bee2d
|
220
|
#define SUPER_CALLOC $Super$$calloc
|
ganlikun |
0:06036f8bee2d
|
221
|
#define SUB_CALLOC $Sub$$calloc
|
ganlikun |
0:06036f8bee2d
|
222
|
#define SUPER_FREE $Super$$free
|
ganlikun |
0:06036f8bee2d
|
223
|
#define SUB_FREE $Sub$$free
|
ganlikun |
0:06036f8bee2d
|
224
|
#elif defined(__ICCARM__)
|
ganlikun |
0:06036f8bee2d
|
225
|
#define SUPER_MALLOC $Super$$__iar_dlmalloc
|
ganlikun |
0:06036f8bee2d
|
226
|
#define SUB_MALLOC $Sub$$__iar_dlmalloc
|
ganlikun |
0:06036f8bee2d
|
227
|
#define SUPER_REALLOC $Super$$__iar_dlrealloc
|
ganlikun |
0:06036f8bee2d
|
228
|
#define SUB_REALLOC $Sub$$__iar_dlrealloc
|
ganlikun |
0:06036f8bee2d
|
229
|
#define SUPER_CALLOC $Super$$__iar_dlcalloc
|
ganlikun |
0:06036f8bee2d
|
230
|
#define SUB_CALLOC $Sub$$__iar_dlcalloc
|
ganlikun |
0:06036f8bee2d
|
231
|
#define SUPER_FREE $Super$$__iar_dlfree
|
ganlikun |
0:06036f8bee2d
|
232
|
#define SUB_FREE $Sub$$__iar_dlfree
|
ganlikun |
0:06036f8bee2d
|
233
|
#endif
|
ganlikun |
0:06036f8bee2d
|
234
|
|
ganlikun |
0:06036f8bee2d
|
235
|
/* Enable hooking of memory function only if tracing is also enabled */
|
ganlikun |
0:06036f8bee2d
|
236
|
#if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
|
ganlikun |
0:06036f8bee2d
|
237
|
|
ganlikun |
0:06036f8bee2d
|
238
|
extern "C" {
|
ganlikun |
0:06036f8bee2d
|
239
|
void *SUPER_MALLOC(size_t size);
|
ganlikun |
0:06036f8bee2d
|
240
|
void *SUPER_REALLOC(void *ptr, size_t size);
|
ganlikun |
0:06036f8bee2d
|
241
|
void *SUPER_CALLOC(size_t nmemb, size_t size);
|
ganlikun |
0:06036f8bee2d
|
242
|
void SUPER_FREE(void *ptr);
|
ganlikun |
0:06036f8bee2d
|
243
|
}
|
ganlikun |
0:06036f8bee2d
|
244
|
|
ganlikun |
0:06036f8bee2d
|
245
|
extern "C" void* SUB_MALLOC(size_t size) {
|
ganlikun |
0:06036f8bee2d
|
246
|
void *ptr = NULL;
|
ganlikun |
0:06036f8bee2d
|
247
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
248
|
malloc_stats_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
249
|
alloc_info_t *alloc_info = (alloc_info_t*)SUPER_MALLOC(size + sizeof(alloc_info_t));
|
ganlikun |
0:06036f8bee2d
|
250
|
if (alloc_info != NULL) {
|
ganlikun |
0:06036f8bee2d
|
251
|
alloc_info->size = size;
|
ganlikun |
0:06036f8bee2d
|
252
|
ptr = (void*)(alloc_info + 1);
|
ganlikun |
0:06036f8bee2d
|
253
|
heap_stats.current_size += size;
|
ganlikun |
0:06036f8bee2d
|
254
|
heap_stats.total_size += size;
|
ganlikun |
0:06036f8bee2d
|
255
|
heap_stats.alloc_cnt += 1;
|
ganlikun |
0:06036f8bee2d
|
256
|
if (heap_stats.current_size > heap_stats.max_size) {
|
ganlikun |
0:06036f8bee2d
|
257
|
heap_stats.max_size = heap_stats.current_size;
|
ganlikun |
0:06036f8bee2d
|
258
|
}
|
ganlikun |
0:06036f8bee2d
|
259
|
} else {
|
ganlikun |
0:06036f8bee2d
|
260
|
heap_stats.alloc_fail_cnt += 1;
|
ganlikun |
0:06036f8bee2d
|
261
|
}
|
ganlikun |
0:06036f8bee2d
|
262
|
malloc_stats_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
263
|
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
264
|
ptr = SUPER_MALLOC(size);
|
ganlikun |
0:06036f8bee2d
|
265
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
266
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
267
|
mem_trace_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
268
|
mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
|
ganlikun |
0:06036f8bee2d
|
269
|
mem_trace_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
270
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
271
|
return ptr;
|
ganlikun |
0:06036f8bee2d
|
272
|
}
|
ganlikun |
0:06036f8bee2d
|
273
|
|
ganlikun |
0:06036f8bee2d
|
274
|
extern "C" void* SUB_REALLOC(void *ptr, size_t size) {
|
ganlikun |
0:06036f8bee2d
|
275
|
void *new_ptr = NULL;
|
ganlikun |
0:06036f8bee2d
|
276
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
277
|
// Note - no lock needed since malloc and free are thread safe
|
ganlikun |
0:06036f8bee2d
|
278
|
|
ganlikun |
0:06036f8bee2d
|
279
|
// Get old size
|
ganlikun |
0:06036f8bee2d
|
280
|
uint32_t old_size = 0;
|
ganlikun |
0:06036f8bee2d
|
281
|
if (ptr != NULL) {
|
ganlikun |
0:06036f8bee2d
|
282
|
alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
|
ganlikun |
0:06036f8bee2d
|
283
|
old_size = alloc_info->size;
|
ganlikun |
0:06036f8bee2d
|
284
|
}
|
ganlikun |
0:06036f8bee2d
|
285
|
|
ganlikun |
0:06036f8bee2d
|
286
|
// Allocate space
|
ganlikun |
0:06036f8bee2d
|
287
|
if (size != 0) {
|
ganlikun |
0:06036f8bee2d
|
288
|
new_ptr = malloc(size);
|
ganlikun |
0:06036f8bee2d
|
289
|
}
|
ganlikun |
0:06036f8bee2d
|
290
|
|
ganlikun |
0:06036f8bee2d
|
291
|
// If the new buffer has been allocated copy the data to it
|
ganlikun |
0:06036f8bee2d
|
292
|
// and free the old buffer
|
ganlikun |
0:06036f8bee2d
|
293
|
if (new_ptr != NULL) {
|
ganlikun |
0:06036f8bee2d
|
294
|
uint32_t copy_size = (old_size < size) ? old_size : size;
|
ganlikun |
0:06036f8bee2d
|
295
|
memcpy(new_ptr, (void*)ptr, copy_size);
|
ganlikun |
0:06036f8bee2d
|
296
|
free(ptr);
|
ganlikun |
0:06036f8bee2d
|
297
|
}
|
ganlikun |
0:06036f8bee2d
|
298
|
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
299
|
new_ptr = SUPER_REALLOC(ptr, size);
|
ganlikun |
0:06036f8bee2d
|
300
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
301
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
302
|
mem_trace_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
303
|
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
|
ganlikun |
0:06036f8bee2d
|
304
|
mem_trace_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
305
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
306
|
return new_ptr;
|
ganlikun |
0:06036f8bee2d
|
307
|
}
|
ganlikun |
0:06036f8bee2d
|
308
|
|
ganlikun |
0:06036f8bee2d
|
309
|
extern "C" void *SUB_CALLOC(size_t nmemb, size_t size) {
|
ganlikun |
0:06036f8bee2d
|
310
|
void *ptr = NULL;
|
ganlikun |
0:06036f8bee2d
|
311
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
312
|
// Note - no lock needed since malloc is thread safe
|
ganlikun |
0:06036f8bee2d
|
313
|
ptr = malloc(nmemb * size);
|
ganlikun |
0:06036f8bee2d
|
314
|
if (ptr != NULL) {
|
ganlikun |
0:06036f8bee2d
|
315
|
memset(ptr, 0, nmemb * size);
|
ganlikun |
0:06036f8bee2d
|
316
|
}
|
ganlikun |
0:06036f8bee2d
|
317
|
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
318
|
ptr = SUPER_CALLOC(nmemb, size);
|
ganlikun |
0:06036f8bee2d
|
319
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
320
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
321
|
mem_trace_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
322
|
mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
|
ganlikun |
0:06036f8bee2d
|
323
|
mem_trace_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
324
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
325
|
return ptr;
|
ganlikun |
0:06036f8bee2d
|
326
|
}
|
ganlikun |
0:06036f8bee2d
|
327
|
|
ganlikun |
0:06036f8bee2d
|
328
|
extern "C" void SUB_FREE(void *ptr) {
|
ganlikun |
0:06036f8bee2d
|
329
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
330
|
malloc_stats_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
331
|
alloc_info_t *alloc_info = NULL;
|
ganlikun |
0:06036f8bee2d
|
332
|
if (ptr != NULL) {
|
ganlikun |
0:06036f8bee2d
|
333
|
alloc_info = ((alloc_info_t*)ptr) - 1;
|
ganlikun |
0:06036f8bee2d
|
334
|
heap_stats.current_size -= alloc_info->size;
|
ganlikun |
0:06036f8bee2d
|
335
|
heap_stats.alloc_cnt -= 1;
|
ganlikun |
0:06036f8bee2d
|
336
|
}
|
ganlikun |
0:06036f8bee2d
|
337
|
SUPER_FREE((void*)alloc_info);
|
ganlikun |
0:06036f8bee2d
|
338
|
malloc_stats_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
339
|
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
340
|
SUPER_FREE(ptr);
|
ganlikun |
0:06036f8bee2d
|
341
|
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
342
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
343
|
mem_trace_mutex->lock();
|
ganlikun |
0:06036f8bee2d
|
344
|
mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
|
ganlikun |
0:06036f8bee2d
|
345
|
mem_trace_mutex->unlock();
|
ganlikun |
0:06036f8bee2d
|
346
|
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
347
|
}
|
ganlikun |
0:06036f8bee2d
|
348
|
|
ganlikun |
0:06036f8bee2d
|
349
|
#endif // #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
|
ganlikun |
0:06036f8bee2d
|
350
|
|
ganlikun |
0:06036f8bee2d
|
351
|
/******************************************************************************/
|
ganlikun |
0:06036f8bee2d
|
352
|
/* Allocation wrappers for other toolchains are not supported yet */
|
ganlikun |
0:06036f8bee2d
|
353
|
/******************************************************************************/
|
ganlikun |
0:06036f8bee2d
|
354
|
|
ganlikun |
0:06036f8bee2d
|
355
|
#else
|
ganlikun |
0:06036f8bee2d
|
356
|
|
ganlikun |
0:06036f8bee2d
|
357
|
#ifdef MBED_MEM_TRACING_ENABLED
|
ganlikun |
0:06036f8bee2d
|
358
|
#error Memory tracing is not supported with the current toolchain.
|
ganlikun |
0:06036f8bee2d
|
359
|
#endif
|
ganlikun |
0:06036f8bee2d
|
360
|
|
ganlikun |
0:06036f8bee2d
|
361
|
#ifdef MBED_HEAP_STATS_ENABLED
|
ganlikun |
0:06036f8bee2d
|
362
|
#error Heap statistics are not supported with the current toolchain.
|
ganlikun |
0:06036f8bee2d
|
363
|
#endif
|
ganlikun |
0:06036f8bee2d
|
364
|
|
ganlikun |
0:06036f8bee2d
|
365
|
#endif // #if defined(TOOLCHAIN_GCC)
|
ganlikun |
0:06036f8bee2d
|
366
|
|