From 8dbcab1c4ae107a1f8b352b23dd3d09ecd4ba56b Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Mon, 21 Mar 2011 07:40:09 +0000 Subject: [PATCH] update more debug information. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1338 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- src/mem.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/mem.c b/src/mem.c index 4afc7ebd96..b928430628 100644 --- a/src/mem.c +++ b/src/mem.c @@ -233,7 +233,7 @@ void *rt_malloc(rt_size_t size) rt_kprintf("malloc size %d, but align to %d\n", size, RT_ALIGN(size, RT_ALIGN_SIZE)); else rt_kprintf("malloc size %d\n", size); -#endif +#endif /* alignment size */ size = RT_ALIGN(size, RT_ALIGN_SIZE); @@ -327,7 +327,9 @@ void *rt_malloc(rt_size_t size) RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0); #ifdef RT_MEM_DEBUG - rt_kprintf("allocate memory at 0x%x\n", (rt_uint32_t)((rt_uint8_t*)mem + SIZEOF_STRUCT_MEM)); + rt_kprintf("allocate memory at 0x%x, size: %d\n", + (rt_uint32_t)((rt_uint8_t*)mem + SIZEOF_STRUCT_MEM), + (rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr))); #endif #ifdef RT_USING_HOOK @@ -467,37 +469,39 @@ void rt_free(void *rmem) if (rmem == RT_NULL) return; RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE-1)) == 0); + RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr && + (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end); #ifdef RT_USING_HOOK if (rt_free_hook != RT_NULL) rt_free_hook(rmem); -#endif - -#ifdef RT_MEM_DEBUG - rt_kprintf("release memory 0x%x\n", (rt_uint32_t)rmem); -#endif - - /* protect the heap from concurrent access */ - rt_sem_take(&heap_sem, RT_WAITING_FOREVER); - - RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr && - (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end); +#endif if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr || (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end) { #ifdef RT_MEM_DEBUG rt_kprintf("illegal memory\n"); #endif - /* illegal memory */ - rt_sem_release(&heap_sem); return; } /* Get the corresponding struct heap_mem ... */ mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM); + +#ifdef RT_MEM_DEBUG + rt_kprintf("release memory 0x%x, size: %d\n", + (rt_uint32_t)rmem, + (rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr))); +#endif + + /* protect the heap from concurrent access */ + rt_sem_take(&heap_sem, RT_WAITING_FOREVER); + /* ... which has to be in a used state ... */ - RT_ASSERT(mem->used); + RT_ASSERT(mem->used); + RT_ASSERT(mem->magic != HEAP_MAGIC); /* ... and is now unused. */ - mem->used = 0; + mem->used = 0; + mem->magic = 0; if (mem < lfree) {