forked from RT-Thread-packages/tlsf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrt_tlsf.c
288 lines (246 loc) · 5.44 KB
/
rt_tlsf.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include <rtthread.h>
#include "rt_tlsf.h"
#if defined (RT_USING_USERHEAP) && defined (PKG_USING_TLSF)
static tlsf_t tlsf_ptr = 0;
static struct rt_semaphore heap_sem;
struct pool_list
{
pool_t pool_addr;
rt_slist_t list;
};
typedef struct pool_list *pool_list_t;
static pool_list_t pools_list;
#ifdef RT_USING_HOOK
static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
static void (*rt_free_hook)(void *ptr);
void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
{
rt_malloc_hook = hook;
}
void rt_free_sethook(void (*hook)(void *ptr))
{
rt_free_hook = hook;
}
#endif
rt_inline void tlsf_lock(void)
{
if (rt_thread_self())
{
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
}
}
rt_inline void tlsf_unlock(void)
{
if (rt_thread_self())
{
rt_sem_release(&heap_sem);
}
}
void rt_system_heap_init(void *begin_addr, void *end_addr)
{
size_t size;
if (begin_addr < end_addr)
{
size = (rt_uint32_t)end_addr - (rt_uint32_t)begin_addr;
}
else
{
return;
}
if (!tlsf_ptr)
{
tlsf_ptr = (tlsf_t)tlsf_create_with_pool(begin_addr, size);
rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_PRIO);
pools_list = rt_malloc(sizeof(struct pool_list));
rt_slist_init(&pools_list->list);
pools_list->pool_addr = tlsf_get_pool(tlsf_ptr);
}
else
{
RT_ASSERT(0);
}
}
void *rt_system_heap_add(void *begin_addr, void *end_addr)
{
pool_t tlsf_pool;
size_t size;
pool_list_t heap;
if (begin_addr < end_addr)
{
size = (rt_uint32_t)end_addr - (rt_uint32_t)begin_addr;
}
else
{
RT_ASSERT(0);
}
if (tlsf_ptr)
{
tlsf_pool = tlsf_add_pool(tlsf_ptr, begin_addr, size);
if (tlsf_pool != RT_NULL)
{
heap = rt_malloc(sizeof(struct pool_list));
rt_slist_append(&pools_list->list, &heap->list);
heap->pool_addr = tlsf_pool;
return heap;
}
}
return RT_NULL;
}
void rt_system_heap_remove(void *heap)
{
RT_ASSERT(heap != RT_NULL);
pool_list_t pool_node = (pool_list_t)heap;
pool_t pool = pool_node->pool_addr;
if (tlsf_ptr)
{
rt_slist_remove(&pools_list->list, &pool_node->list);
rt_free(pool_node);
tlsf_remove_pool(tlsf_ptr, pool);
}
}
#ifdef PKG_TLSF_USING_ALIGN
void *rt_malloc_align(rt_size_t size, rt_size_t align)
{
void *ptr = RT_NULL;
if (tlsf_ptr)
{
tlsf_lock();
ptr = tlsf_memalign(tlsf_ptr, align, size);
tlsf_unlock();
}
return ptr;
}
void rt_free_align(void *ptr)
{
rt_free(ptr);
}
#endif
void *rt_malloc(rt_size_t nbytes)
{
void *ptr = RT_NULL;
if (tlsf_ptr)
{
tlsf_lock();
ptr = tlsf_malloc(tlsf_ptr, nbytes);
RT_OBJECT_HOOK_CALL(rt_malloc_hook, ((void *)ptr, nbytes));
tlsf_unlock();
}
return ptr;
}
RTM_EXPORT(rt_malloc);
void rt_free(void *ptr)
{
if (tlsf_ptr)
{
tlsf_lock();
tlsf_free(tlsf_ptr, ptr);
RT_OBJECT_HOOK_CALL(rt_free_hook, (ptr));
tlsf_unlock();
}
}
RTM_EXPORT(rt_free);
void *rt_realloc(void *ptr, rt_size_t nbytes)
{
if (tlsf_ptr)
{
tlsf_lock();
ptr = tlsf_realloc(tlsf_ptr, ptr, nbytes);
tlsf_unlock();
}
return ptr;
}
RTM_EXPORT(rt_realloc);
void *rt_calloc(rt_size_t count, rt_size_t size)
{
void *ptr = RT_NULL;
rt_size_t total_size;
total_size = count * size;
ptr = rt_malloc(total_size);
if (ptr != RT_NULL)
{
/* clean memory */
rt_memset(ptr, 0, total_size);
}
return ptr;
}
RTM_EXPORT(rt_calloc);
static size_t used_mem = 0;
static size_t total_mem = 0;
static void mem_info(void *ptr, size_t size, int used, void *user)
{
if (used)
{
used_mem += size;
}
total_mem += size;
}
void rt_memory_info(rt_uint32_t *total,
rt_uint32_t *used,
rt_uint32_t *max_used)
{
used_mem = 0;
total_mem = 0;
tlsf_walk_pool(tlsf_get_pool(tlsf_ptr), mem_info, 0);
*total = total_mem;
*used = used_mem;
*max_used = used_mem;
}
void list_mem(void)
{
rt_uint8_t i = 0;
rt_uint8_t len = 0;
pool_list_t pool_node;
struct rt_slist_node *node;
node = &pools_list->list;
used_mem = 0;
total_mem = 0;
len = rt_slist_len(node) + 1;
for (i = 0; i < len; i++)
{
pool_node = rt_container_of(node, struct pool_list, list);
tlsf_walk_pool(pool_node->pool_addr, mem_info, 0);
node = node->next;
}
rt_kprintf("total memory: %d\n", total_mem);
rt_kprintf("used memory : %d\n", used_mem);
}
static _tlsftrace_dump(void *ptr, size_t size, int used)
{
rt_kprintf("[0x%08x - ", ptr);
if (size < 1024)
{
rt_kprintf("%5d", size);
}
else if (size < 1024 * 1024)
{
rt_kprintf("%4dK", size / 1024);
}
else
{
rt_kprintf("%4dM", size / (1024 * 1024));
}
if (used)
{
rt_kprintf("] used\n");
}
else
{
rt_kprintf("] free\n");
}
}
int tlsftrace(void)
{
rt_kprintf("\n--memory item information --\n");
tlsf_walk_pool(tlsf_get_pool(tlsf_ptr), _tlsftrace_dump, 0);
return 0;
}
MSH_CMD_EXPORT(tlsftrace, dump tlsf memory trace information);
#endif