Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
429 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
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
//
13
//    emBIOS is distributed in the hope that it will be useful,
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
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
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
 
56
void* realloc(void* ptr, size_t size)
57
{
58
    mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
59
    size_t oldsize = tlsf_block_size(ptr);
60
    struct scheduler_thread* owner = *((struct scheduler_thread**)(ptr + size - 4));
61
    ptr = tlsf_realloc(global_mallocpool, ptr, 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
 
71
void reownalloc(void* ptr, struct scheduler_thread* owner)
72
{
73
    mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
74
    size_t size = tlsf_block_size(ptr);
75
    *((struct scheduler_thread**)(ptr + size - 4)) = owner;
76
    mutex_unlock(&malloc_mutex);
77
}
78
 
79
void free(void* ptr)
80
{
81
    mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
82
    tlsf_free(global_mallocpool, ptr);
83
    mutex_unlock(&malloc_mutex);
84
}
85
 
86
void free_if_thread(void* ptr, size_t size, int used, void* owner)
87
{
88
    if (*((void**)(ptr + size - 4)) == owner)
89
        tlsf_free(global_mallocpool, ptr);
90
}
91
 
92
void free_all_of_thread(struct scheduler_thread* owner)
93
{
94
    mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
95
    tlsf_walk_heap(global_mallocpool, free_if_thread, owner);
96
    mutex_unlock(&malloc_mutex);
97
}
98
 
565 theseven 99
void malloc_walk(void (*walker), void* user)
100
{
101
    mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
102
    tlsf_walk_heap(global_mallocpool, walker, user);
103
    mutex_unlock(&malloc_mutex);
104
}
105
 
429 theseven 106
void malloc_init()
107
{
108
    mutex_init(&malloc_mutex);
436 theseven 109
    global_mallocpool = tlsf_create(&_poolstart, &_poolend - &_poolstart);
429 theseven 110
}