summaryrefslogtreecommitdiff
path: root/src/mem.c
diff options
context:
space:
mode:
authorbernard.xiong <[email protected]>2011-03-24 06:09:58 +0000
committerbernard.xiong <[email protected]>2011-03-24 06:09:58 +0000
commit96c53b49571adadd957df804f4f5e6698c14e3b3 (patch)
tree6dc18b297d418319cdef6d363e60a6831a25f8e9 /src/mem.c
parentbaf5e4143c93368f503a8cf7303e41f6efce1a75 (diff)
fix the magic issue in allocated memory block.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1344 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'src/mem.c')
-rw-r--r--src/mem.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/mem.c b/src/mem.c
index fc8df6f9b5..bc4dba4cc5 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);
@@ -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)