freemyipod r812 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r811‎ | r812 | r813 >
Date:22:36, 22 November 2011
Author:theseven
Status:new
Tags:
Comment:
emCORE: Add some missing malloc memory checks and some debug output
Modified paths:
  • /emcore/trunk/malloc.c (modified) (history)

Diff [purge]

Index: emcore/trunk/malloc.c
@@ -26,7 +26,7 @@
2727 #include "libc/tlsf/tlsf.h"
2828
2929
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
3131 extern char _poolend; // used here, and we only need the address, so just make it happy...
3232
3333 struct mutex malloc_mutex;
@@ -37,8 +37,12 @@
3838 {
3939 mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
4040 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);
4347 mutex_unlock(&malloc_mutex);
4448 return ptr;
4549 }
@@ -47,8 +51,12 @@
4852 {
4953 mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
5054 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);
5361 mutex_unlock(&malloc_mutex);
5462 return ptr;
5563 }
@@ -58,14 +66,16 @@
5967 mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
6068 size_t oldsize = tlsf_block_size(ptr);
6169 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)
6472 {
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;
6775 }
 76+ DEBUGF("realign(%08X, %X, %08X) => %08X (old size: %08X, owner: %08X, thread: %08X)",
 77+ ptr, align, size, ptr_new, owner, current_thread);
6878 mutex_unlock(&malloc_mutex);
69 - return ptr;
 79+ return ptr_new;
7080 }
7181
7282 void* realloc(void* ptr, size_t size)
@@ -77,6 +87,8 @@
7888 {
7989 mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
8090 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);
8193 *((struct scheduler_thread**)(ptr + size - 4)) = owner;
8294 mutex_unlock(&malloc_mutex);
8395 }
@@ -84,6 +96,9 @@
8597 void free(void* ptr)
8698 {
8799 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);
88103 tlsf_free(global_mallocpool, ptr);
89104 mutex_unlock(&malloc_mutex);
90105 }
@@ -97,6 +112,7 @@
98113 void free_all_of_thread(struct scheduler_thread* owner)
99114 {
100115 mutex_lock(&malloc_mutex, TIMEOUT_BLOCK);
 116+ DEBUGF("free_all_of_thread(%08X) (thread: %08X)", owner, current_thread);
101117 tlsf_walk_heap(global_mallocpool, free_if_thread, owner);
102118 mutex_unlock(&malloc_mutex);
103119 }