Browse Source

fix the magic issue in allocated memory block.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1344 bbd45198-f89e-11dd-88c7-29a3b14d5316
bernard.xiong 14 years ago
parent
commit
96c53b4957
1 changed files with 11 additions and 10 deletions
  1. 11 10
      src/mem.c

+ 11 - 10
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);
@@ -279,7 +279,6 @@ void *rt_malloc(rt_size_t size)
 
 				/* create mem2 struct */
 				mem2 = (struct heap_mem *)&heap_ptr[ptr2];
-				mem2->magic = HEAP_MAGIC;
 				mem2->used = 0;
 				mem2->next = mem->next;
 				mem2->prev = ptr;
@@ -312,6 +311,8 @@ void *rt_malloc(rt_size_t size)
 				if (max_mem < used_mem) max_mem = used_mem;
 #endif
 			}
+			/* set memory block magic */
+			mem->magic = HEAP_MAGIC;
 
 			if (mem == lfree)
 			{
@@ -327,8 +328,8 @@ 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, size: %d\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
 
@@ -470,11 +471,11 @@ 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);
+			  (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end);
 
 #ifdef RT_USING_HOOK
 	if (rt_free_hook != RT_NULL) rt_free_hook(rmem);
-#endif
+#endif
 
 	if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr || (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
 	{
@@ -488,8 +489,8 @@ void rt_free(void *rmem)
 	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_kprintf("release memory 0x%x, size: %d\n", 
+		(rt_uint32_t)rmem, 
 		(rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr)));
 #endif
 
@@ -497,10 +498,10 @@ void rt_free(void *rmem)
 	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)