| 429 |
theseven |
1 |
//
|
|
|
2 |
//
|
|
|
3 |
// Copyright 2010 TheSeven
|
|
|
4 |
//
|
|
|
5 |
//
|
| 677 |
theseven |
6 |
// This file is part of emCORE.
|
| 429 |
theseven |
7 |
//
|
| 677 |
theseven |
8 |
// emCORE is free software: you can redistribute it and/or
|
| 429 |
theseven |
9 |
// modify it under the terms of the GNU General Public License as
|
|
|
10 |
// published by the Free Software Foundation, either version 2 of the
|
|
|
11 |
// License, or (at your option) any later version.
|
|
|
12 |
//
|
| 677 |
theseven |
13 |
// emCORE is distributed in the hope that it will be useful,
|
| 429 |
theseven |
14 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
16 |
// See the GNU General Public License for more details.
|
|
|
17 |
//
|
|
|
18 |
// You should have received a copy of the GNU General Public License along
|
| 677 |
theseven |
19 |
// with emCORE. If not, see <http://www.gnu.org/licenses/>.
|
| 429 |
theseven |
20 |
//
|
|
|
21 |
//
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
#include "global.h"
|
|
|
25 |
#include "malloc.h"
|
|
|
26 |
#include "libc/tlsf/tlsf.h"
|
|
|
27 |
|
|
|
28 |
|
| 565 |
theseven |
29 |
extern char _poolstart; // These aren't ints at all, but gcc complains about void types being
|
|
|
30 |
extern char _poolend; // used here, and we only need the address, so just make it happy...
|
| 429 |
theseven |
31 |
|
|
|
32 |
struct mutex malloc_mutex;
|
|
|
33 |
tlsf_pool global_mallocpool;
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
void* malloc(size_t size)
|
|
|
37 |
{
|
|
|
38 |
mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
|
|
39 |
void* ptr = tlsf_malloc(global_mallocpool, size + 4);
|
|
|
40 |
size = tlsf_block_size(ptr);
|
|
|
41 |
*((struct scheduler_thread**)(ptr + size - 4)) = current_thread;
|
|
|
42 |
mutex_unlock(&malloc_mutex);
|
|
|
43 |
return ptr;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
void* memalign(size_t align, size_t size)
|
|
|
47 |
{
|
|
|
48 |
mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
|
|
49 |
void* ptr = tlsf_memalign(global_mallocpool, align, size + 4);
|
|
|
50 |
size = tlsf_block_size(ptr);
|
|
|
51 |
*((struct scheduler_thread**)(ptr + size - 4)) = current_thread;
|
|
|
52 |
mutex_unlock(&malloc_mutex);
|
|
|
53 |
return ptr;
|
|
|
54 |
}
|
|
|
55 |
|
| 625 |
theseven |
56 |
void* realign(void* ptr, size_t align, size_t size)
|
| 429 |
theseven |
57 |
{
|
|
|
58 |
mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
|
|
59 |
size_t oldsize = tlsf_block_size(ptr);
|
| 571 |
theseven |
60 |
struct scheduler_thread* owner = *((struct scheduler_thread**)(ptr + oldsize - 4));
|
| 625 |
theseven |
61 |
ptr = tlsf_realign(global_mallocpool, ptr, align, size + 4);
|
| 570 |
theseven |
62 |
if (ptr)
|
|
|
63 |
{
|
|
|
64 |
size = tlsf_block_size(ptr);
|
|
|
65 |
*((struct scheduler_thread**)(ptr + size - 4)) = owner;
|
|
|
66 |
}
|
| 429 |
theseven |
67 |
mutex_unlock(&malloc_mutex);
|
|
|
68 |
return ptr;
|
|
|
69 |
}
|
|
|
70 |
|
| 625 |
theseven |
71 |
void* realloc(void* ptr, size_t size)
|
|
|
72 |
{
|
|
|
73 |
return realign(ptr, 4, size);
|
|
|
74 |
}
|
|
|
75 |
|
| 429 |
theseven |
76 |
void reownalloc(void* ptr, struct scheduler_thread* owner)
|
|
|
77 |
{
|
|
|
78 |
mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
|
|
79 |
size_t size = tlsf_block_size(ptr);
|
|
|
80 |
*((struct scheduler_thread**)(ptr + size - 4)) = owner;
|
|
|
81 |
mutex_unlock(&malloc_mutex);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
void free(void* ptr)
|
|
|
85 |
{
|
|
|
86 |
mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
|
|
87 |
tlsf_free(global_mallocpool, ptr);
|
|
|
88 |
mutex_unlock(&malloc_mutex);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
void free_if_thread(void* ptr, size_t size, int used, void* owner)
|
|
|
92 |
{
|
|
|
93 |
if (*((void**)(ptr + size - 4)) == owner)
|
|
|
94 |
tlsf_free(global_mallocpool, ptr);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
void free_all_of_thread(struct scheduler_thread* owner)
|
|
|
98 |
{
|
|
|
99 |
mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
|
|
100 |
tlsf_walk_heap(global_mallocpool, free_if_thread, owner);
|
|
|
101 |
mutex_unlock(&malloc_mutex);
|
|
|
102 |
}
|
|
|
103 |
|
| 565 |
theseven |
104 |
void malloc_walk(void (*walker), void* user)
|
|
|
105 |
{
|
|
|
106 |
mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
|
|
107 |
tlsf_walk_heap(global_mallocpool, walker, user);
|
|
|
108 |
mutex_unlock(&malloc_mutex);
|
|
|
109 |
}
|
|
|
110 |
|
| 429 |
theseven |
111 |
void malloc_init()
|
|
|
112 |
{
|
|
|
113 |
mutex_init(&malloc_mutex);
|
| 436 |
theseven |
114 |
global_mallocpool = tlsf_create(&_poolstart, &_poolend - &_poolstart);
|
| 429 |
theseven |
115 |
}
|