Index: emcore/trunk/malloc.c |
— | — | @@ -26,7 +26,7 @@ |
27 | 27 | #include "libc/tlsf/tlsf.h"
|
28 | 28 |
|
29 | 29 |
|
30 | | -extern char _poolstart; // These aren't ints at all, but gcc complains about void types being
|
| 30 | +extern char _poolstart; // These aren't chars at all, but gcc complains about void types being
|
31 | 31 | extern char _poolend; // used here, and we only need the address, so just make it happy...
|
32 | 32 |
|
33 | 33 | struct mutex malloc_mutex;
|
— | — | @@ -37,8 +37,12 @@ |
38 | 38 | {
|
39 | 39 | mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
40 | 40 | void* ptr = tlsf_malloc(global_mallocpool, size + 4);
|
41 | | - size = tlsf_block_size(ptr);
|
42 | | - *((struct scheduler_thread**)(ptr + size - 4)) = current_thread;
|
| 41 | + if (ptr)
|
| 42 | + {
|
| 43 | + size = tlsf_block_size(ptr);
|
| 44 | + *((struct scheduler_thread**)(ptr + size - 4)) = current_thread;
|
| 45 | + }
|
| 46 | + DEBUGF("malloc(%08X) => %08X (thread: %08X)", size, ptr, current_thread);
|
43 | 47 | mutex_unlock(&malloc_mutex);
|
44 | 48 | return ptr;
|
45 | 49 | }
|
— | — | @@ -47,8 +51,12 @@ |
48 | 52 | {
|
49 | 53 | mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
50 | 54 | void* ptr = tlsf_memalign(global_mallocpool, align, size + 4);
|
51 | | - size = tlsf_block_size(ptr);
|
52 | | - *((struct scheduler_thread**)(ptr + size - 4)) = current_thread;
|
| 55 | + if (ptr)
|
| 56 | + {
|
| 57 | + size = tlsf_block_size(ptr);
|
| 58 | + *((struct scheduler_thread**)(ptr + size - 4)) = current_thread;
|
| 59 | + }
|
| 60 | + DEBUGF("memalign(%X, %08X) => %08X (thread: %08X)", align, size, ptr, current_thread);
|
53 | 61 | mutex_unlock(&malloc_mutex);
|
54 | 62 | return ptr;
|
55 | 63 | }
|
— | — | @@ -58,14 +66,16 @@ |
59 | 67 | mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
60 | 68 | size_t oldsize = tlsf_block_size(ptr);
|
61 | 69 | struct scheduler_thread* owner = *((struct scheduler_thread**)(ptr + oldsize - 4));
|
62 | | - ptr = tlsf_realign(global_mallocpool, ptr, align, size + 4);
|
63 | | - if (ptr)
|
| 70 | + void* ptr_new = tlsf_realign(global_mallocpool, ptr, align, size + 4);
|
| 71 | + if (ptr_new)
|
64 | 72 | {
|
65 | | - size = tlsf_block_size(ptr);
|
66 | | - *((struct scheduler_thread**)(ptr + size - 4)) = owner;
|
| 73 | + size = tlsf_block_size(ptr_new);
|
| 74 | + *((struct scheduler_thread**)(ptr_new + size - 4)) = owner;
|
67 | 75 | }
|
| 76 | + DEBUGF("realign(%08X, %X, %08X) => %08X (old size: %08X, owner: %08X, thread: %08X)",
|
| 77 | + ptr, align, size, ptr_new, owner, current_thread);
|
68 | 78 | mutex_unlock(&malloc_mutex);
|
69 | | - return ptr;
|
| 79 | + return ptr_new;
|
70 | 80 | }
|
71 | 81 |
|
72 | 82 | void* realloc(void* ptr, size_t size)
|
— | — | @@ -77,6 +87,8 @@ |
78 | 88 | {
|
79 | 89 | mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
80 | 90 | size_t size = tlsf_block_size(ptr);
|
| 91 | + DEBUGF("reownalloc(%08X, %08X) (size: %08X, old owner: %08X, thread: %08X)",
|
| 92 | + ptr, size, owner, *((struct scheduler_thread**)(ptr + size - 4)), current_thread);
|
81 | 93 | *((struct scheduler_thread**)(ptr + size - 4)) = owner;
|
82 | 94 | mutex_unlock(&malloc_mutex);
|
83 | 95 | }
|
— | — | @@ -84,6 +96,9 @@ |
85 | 97 | void free(void* ptr)
|
86 | 98 | {
|
87 | 99 | mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
| 100 | + size_t size = tlsf_block_size(ptr);
|
| 101 | + DEBUGF("free(%08X) (size: %08X, owner: %08X, thread: %08X)", ptr, size,
|
| 102 | + *((struct scheduler_thread**)(ptr + size - 4)), current_thread);
|
88 | 103 | tlsf_free(global_mallocpool, ptr);
|
89 | 104 | mutex_unlock(&malloc_mutex);
|
90 | 105 | }
|
— | — | @@ -97,6 +112,7 @@ |
98 | 113 | void free_all_of_thread(struct scheduler_thread* owner)
|
99 | 114 | {
|
100 | 115 | mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
|
| 116 | + DEBUGF("free_all_of_thread(%08X) (thread: %08X)", owner, current_thread);
|
101 | 117 | tlsf_walk_heap(global_mallocpool, free_if_thread, owner);
|
102 | 118 | mutex_unlock(&malloc_mutex);
|
103 | 119 | }
|