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
 
29
extern int _initstart;   // These aren't ints at all, but gcc complains about void types being
30
extern int _sdramstart;  // used here, and we only need the address, so just make it happy...
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);
62
    size = tlsf_block_size(ptr);
63
    *((struct scheduler_thread**)(ptr + size - 4)) = owner;
64
    mutex_unlock(&malloc_mutex);
65
    return ptr;
66
}
67
 
68
void reownalloc(void* ptr, struct scheduler_thread* owner)
69
{
70
    mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
71
    size_t size = tlsf_block_size(ptr);
72
    *((struct scheduler_thread**)(ptr + size - 4)) = owner;
73
    mutex_unlock(&malloc_mutex);
74
}
75
 
76
void free(void* ptr)
77
{
78
    mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
79
    tlsf_free(global_mallocpool, ptr);
80
    mutex_unlock(&malloc_mutex);
81
}
82
 
83
void free_if_thread(void* ptr, size_t size, int used, void* owner)
84
{
85
    if (*((void**)(ptr + size - 4)) == owner)
86
        tlsf_free(global_mallocpool, ptr);
87
}
88
 
89
void free_all_of_thread(struct scheduler_thread* owner)
90
{
91
    mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
92
    tlsf_walk_heap(global_mallocpool, free_if_thread, owner);
93
    mutex_unlock(&malloc_mutex);
94
}
95
 
96
void malloc_init()
97
{
98
    mutex_init(&malloc_mutex);
99
    global_mallocpool = tlsf_create(&_initstart, _sdramstart - _initstart);
100
}