fix memory re-allocation issue
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@15 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
bd76439fd2
commit
74f893fc2e
57
src/mem.c
57
src/mem.c
|
@ -48,7 +48,7 @@
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
|
|
||||||
/* #define RT_MEM_DEBUG */
|
/* #define RT_MEM_DEBUG */
|
||||||
#define RT_MEM_STATS
|
#define RT_MEM_STATS
|
||||||
|
|
||||||
#if defined (RT_USING_HEAP) && defined (RT_USING_SMALL_MEM)
|
#if defined (RT_USING_HEAP) && defined (RT_USING_SMALL_MEM)
|
||||||
#ifdef RT_USING_HOOK
|
#ifdef RT_USING_HOOK
|
||||||
|
@ -109,11 +109,11 @@ static struct heap_mem *heap_end;
|
||||||
static struct heap_mem *lfree; /* pointer to the lowest free block */
|
static struct heap_mem *lfree; /* pointer to the lowest free block */
|
||||||
|
|
||||||
static struct rt_semaphore heap_sem;
|
static struct rt_semaphore heap_sem;
|
||||||
static rt_size_t mem_size_aligned;
|
static rt_size_t mem_size_aligned;
|
||||||
|
|
||||||
#ifdef RT_MEM_STATS
|
#ifdef RT_MEM_STATS
|
||||||
static rt_size_t used_mem, max_mem;
|
static rt_size_t used_mem, max_mem;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void plug_holes(struct heap_mem *mem)
|
static void plug_holes(struct heap_mem *mem)
|
||||||
{
|
{
|
||||||
|
@ -194,7 +194,7 @@ void rt_system_heap_init(void* begin_addr, void* end_addr)
|
||||||
rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);
|
rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);
|
||||||
|
|
||||||
/* initialize the lowest-free pointer to the start of the heap */
|
/* initialize the lowest-free pointer to the start of the heap */
|
||||||
lfree = (struct heap_mem *)heap_ptr;
|
lfree = (struct heap_mem *)heap_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -277,10 +277,10 @@ void *rt_malloc(rt_size_t size)
|
||||||
if (mem2->next != mem_size_aligned)
|
if (mem2->next != mem_size_aligned)
|
||||||
{
|
{
|
||||||
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
|
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
|
||||||
}
|
}
|
||||||
#ifdef RT_MEM_STATS
|
#ifdef RT_MEM_STATS
|
||||||
used_mem += (size + SIZEOF_STRUCT_MEM);
|
used_mem += (size + SIZEOF_STRUCT_MEM);
|
||||||
if (max_mem < used_mem) max_mem = used_mem;
|
if (max_mem < used_mem) max_mem = used_mem;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -292,10 +292,10 @@ void *rt_malloc(rt_size_t size)
|
||||||
* also can't move mem->next directly behind mem, since mem->next
|
* also can't move mem->next directly behind mem, since mem->next
|
||||||
* will always be used at this point!
|
* will always be used at this point!
|
||||||
*/
|
*/
|
||||||
mem->used = 1;
|
mem->used = 1;
|
||||||
#ifdef RT_MEM_STATS
|
#ifdef RT_MEM_STATS
|
||||||
used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);
|
used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);
|
||||||
if (max_mem < used_mem) max_mem = used_mem;
|
if (max_mem < used_mem) max_mem = used_mem;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,6 +342,7 @@ void *rt_realloc(void *rmem, rt_size_t newsize)
|
||||||
rt_size_t size;
|
rt_size_t size;
|
||||||
rt_size_t ptr, ptr2;
|
rt_size_t ptr, ptr2;
|
||||||
struct heap_mem *mem, *mem2;
|
struct heap_mem *mem, *mem2;
|
||||||
|
void* nmem;
|
||||||
|
|
||||||
/* alignment size */
|
/* alignment size */
|
||||||
newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
|
newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
|
||||||
|
@ -369,10 +370,10 @@ void *rt_realloc(void *rmem, rt_size_t newsize)
|
||||||
size = mem->next - ptr - SIZEOF_STRUCT_MEM;
|
size = mem->next - ptr - SIZEOF_STRUCT_MEM;
|
||||||
|
|
||||||
if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
|
if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
|
||||||
{
|
{
|
||||||
#if MEM_STATS
|
#if MEM_STATS
|
||||||
used_mem -= (size - newsize);
|
used_mem -= (size - newsize);
|
||||||
#endif /* MEM_STATS */
|
#endif /* MEM_STATS */
|
||||||
|
|
||||||
ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
|
ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
|
||||||
mem2 = (struct heap_mem *)&heap_ptr[ptr2];
|
mem2 = (struct heap_mem *)&heap_ptr[ptr2];
|
||||||
|
@ -394,12 +395,12 @@ void *rt_realloc(void *rmem, rt_size_t newsize)
|
||||||
rt_sem_release(&heap_sem);
|
rt_sem_release(&heap_sem);
|
||||||
|
|
||||||
/* expand memory */
|
/* expand memory */
|
||||||
mem2 = rt_malloc(newsize);
|
nmem = rt_malloc(newsize);
|
||||||
rt_memcpy(mem2, mem, size);
|
rt_memcpy(nmem, rmem, size);
|
||||||
|
|
||||||
rt_free(mem);
|
rt_free(rmem);
|
||||||
|
|
||||||
return mem2;
|
return nmem;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -475,16 +476,16 @@ void rt_free(void *rmem)
|
||||||
{
|
{
|
||||||
/* the newly freed struct is now the lowest */
|
/* the newly freed struct is now the lowest */
|
||||||
lfree = mem;
|
lfree = mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RT_MEM_STATS
|
#ifdef RT_MEM_STATS
|
||||||
used_mem -= (mem->next - ((rt_uint8_t*)mem - heap_ptr));
|
used_mem -= (mem->next - ((rt_uint8_t*)mem - heap_ptr));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* finally, see if prev or next are free also */
|
/* finally, see if prev or next are free also */
|
||||||
plug_holes(mem);
|
plug_holes(mem);
|
||||||
rt_sem_release(&heap_sem);
|
rt_sem_release(&heap_sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RT_MEM_STATS
|
#ifdef RT_MEM_STATS
|
||||||
#ifdef RT_USING_FINSH
|
#ifdef RT_USING_FINSH
|
||||||
|
@ -497,7 +498,7 @@ void list_mem()
|
||||||
}
|
}
|
||||||
FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
|
FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*@}*/
|
/*@}*/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue