fixed coding style in src/mem.c
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2523 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
55f1cb72b6
commit
07ea5e8fec
565
src/mem.c
565
src/mem.c
|
@ -74,7 +74,7 @@ static void (*rt_free_hook)(void *ptr);
|
|||
*/
|
||||
void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
|
||||
{
|
||||
rt_malloc_hook = hook;
|
||||
rt_malloc_hook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -85,7 +85,7 @@ void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
|
|||
*/
|
||||
void rt_free_sethook(void (*hook)(void *ptr))
|
||||
{
|
||||
rt_free_hook = hook;
|
||||
rt_free_hook = hook;
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
|
@ -95,11 +95,11 @@ void rt_free_sethook(void (*hook)(void *ptr))
|
|||
#define HEAP_MAGIC 0x1ea0
|
||||
struct heap_mem
|
||||
{
|
||||
/* magic and used flag */
|
||||
rt_uint16_t magic;
|
||||
rt_uint16_t used;
|
||||
/* magic and used flag */
|
||||
rt_uint16_t magic;
|
||||
rt_uint16_t used;
|
||||
|
||||
rt_size_t next, prev;
|
||||
rt_size_t next, prev;
|
||||
};
|
||||
|
||||
/** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */
|
||||
|
@ -123,38 +123,42 @@ static rt_size_t used_mem, max_mem;
|
|||
|
||||
static void plug_holes(struct heap_mem *mem)
|
||||
{
|
||||
struct heap_mem *nmem;
|
||||
struct heap_mem *pmem;
|
||||
struct heap_mem *nmem;
|
||||
struct heap_mem *pmem;
|
||||
|
||||
RT_ASSERT((rt_uint8_t *)mem >= heap_ptr);
|
||||
RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end);
|
||||
RT_ASSERT(mem->used == 0);
|
||||
RT_ASSERT((rt_uint8_t *)mem >= heap_ptr);
|
||||
RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end);
|
||||
RT_ASSERT(mem->used == 0);
|
||||
|
||||
/* plug hole forward */
|
||||
nmem = (struct heap_mem *)&heap_ptr[mem->next];
|
||||
if (mem != nmem && nmem->used == 0 && (rt_uint8_t *)nmem != (rt_uint8_t *)heap_end)
|
||||
{
|
||||
/* if mem->next is unused and not end of heap_ptr, combine mem and mem->next */
|
||||
if (lfree == nmem)
|
||||
{
|
||||
lfree = mem;
|
||||
}
|
||||
mem->next = nmem->next;
|
||||
((struct heap_mem *)&heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - heap_ptr;
|
||||
}
|
||||
/* plug hole forward */
|
||||
nmem = (struct heap_mem *)&heap_ptr[mem->next];
|
||||
if (mem != nmem &&
|
||||
nmem->used == 0 &&
|
||||
(rt_uint8_t *)nmem != (rt_uint8_t *)heap_end)
|
||||
{
|
||||
/* if mem->next is unused and not end of heap_ptr,
|
||||
* combine mem and mem->next
|
||||
*/
|
||||
if (lfree == nmem)
|
||||
{
|
||||
lfree = mem;
|
||||
}
|
||||
mem->next = nmem->next;
|
||||
((struct heap_mem *)&heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - heap_ptr;
|
||||
}
|
||||
|
||||
/* plug hole backward */
|
||||
pmem = (struct heap_mem *)&heap_ptr[mem->prev];
|
||||
if (pmem != mem && pmem->used == 0)
|
||||
{
|
||||
/* if mem->prev is unused, combine mem and mem->prev */
|
||||
if (lfree == mem)
|
||||
{
|
||||
lfree = pmem;
|
||||
}
|
||||
pmem->next = mem->next;
|
||||
((struct heap_mem *)&heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - heap_ptr;
|
||||
}
|
||||
/* plug hole backward */
|
||||
pmem = (struct heap_mem *)&heap_ptr[mem->prev];
|
||||
if (pmem != mem && pmem->used == 0)
|
||||
{
|
||||
/* if mem->prev is unused, combine mem and mem->prev */
|
||||
if (lfree == mem)
|
||||
{
|
||||
lfree = pmem;
|
||||
}
|
||||
pmem->next = mem->next;
|
||||
((struct heap_mem *)&heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - heap_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -167,50 +171,51 @@ static void plug_holes(struct heap_mem *mem)
|
|||
*/
|
||||
void rt_system_heap_init(void *begin_addr, void *end_addr)
|
||||
{
|
||||
struct heap_mem *mem;
|
||||
rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);
|
||||
rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE);
|
||||
struct heap_mem *mem;
|
||||
rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);
|
||||
rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE);
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* alignment addr */
|
||||
if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
|
||||
((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))
|
||||
{
|
||||
/* calculate the aligned memory size */
|
||||
mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n", (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
|
||||
|
||||
return;
|
||||
}
|
||||
/* alignment addr */
|
||||
if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
|
||||
((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))
|
||||
{
|
||||
/* calculate the aligned memory size */
|
||||
mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",
|
||||
(rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
|
||||
|
||||
/* point to begin address of heap */
|
||||
heap_ptr = (rt_uint8_t *)begin_align;
|
||||
return;
|
||||
}
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
|
||||
/* point to begin address of heap */
|
||||
heap_ptr = (rt_uint8_t *)begin_align;
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
|
||||
(rt_uint32_t)heap_ptr, mem_size_aligned));
|
||||
|
||||
/* initialize the start of the heap */
|
||||
mem = (struct heap_mem *)heap_ptr;
|
||||
mem->magic= HEAP_MAGIC;
|
||||
mem->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
mem->prev = 0;
|
||||
mem->used = 0;
|
||||
/* initialize the start of the heap */
|
||||
mem = (struct heap_mem *)heap_ptr;
|
||||
mem->magic = HEAP_MAGIC;
|
||||
mem->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
mem->prev = 0;
|
||||
mem->used = 0;
|
||||
|
||||
/* initialize the end of the heap */
|
||||
heap_end = (struct heap_mem *)&heap_ptr[mem->next];
|
||||
heap_end->magic= HEAP_MAGIC;
|
||||
heap_end->used = 1;
|
||||
heap_end->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
heap_end->prev = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
/* initialize the end of the heap */
|
||||
heap_end = (struct heap_mem *)&heap_ptr[mem->next];
|
||||
heap_end->magic = HEAP_MAGIC;
|
||||
heap_end->used = 1;
|
||||
heap_end->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
heap_end->prev = mem_size_aligned + SIZEOF_STRUCT_MEM;
|
||||
|
||||
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 */
|
||||
lfree = (struct heap_mem *)heap_ptr;
|
||||
/* initialize the lowest-free pointer to the start of the heap */
|
||||
lfree = (struct heap_mem *)heap_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -228,129 +233,132 @@ void rt_system_heap_init(void *begin_addr, void *end_addr)
|
|||
*/
|
||||
void *rt_malloc(rt_size_t size)
|
||||
{
|
||||
rt_size_t ptr, ptr2;
|
||||
struct heap_mem *mem, *mem2;
|
||||
rt_size_t ptr, ptr2;
|
||||
struct heap_mem *mem, *mem2;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
if (size == 0)
|
||||
return RT_NULL;
|
||||
if (size == 0)
|
||||
return RT_NULL;
|
||||
|
||||
if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",
|
||||
if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",
|
||||
size, RT_ALIGN(size, RT_ALIGN_SIZE)));
|
||||
else
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d\n", size));
|
||||
else
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d\n", size));
|
||||
|
||||
/* alignment size */
|
||||
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
||||
/* alignment size */
|
||||
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
||||
|
||||
if (size > mem_size_aligned)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));
|
||||
if (size > mem_size_aligned)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/* every data block must be at least MIN_SIZE_ALIGNED long */
|
||||
if (size < MIN_SIZE_ALIGNED)
|
||||
size = MIN_SIZE_ALIGNED;
|
||||
/* every data block must be at least MIN_SIZE_ALIGNED long */
|
||||
if (size < MIN_SIZE_ALIGNED)
|
||||
size = MIN_SIZE_ALIGNED;
|
||||
|
||||
/* take memory semaphore */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
/* take memory semaphore */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
for (ptr = (rt_uint8_t *)lfree - heap_ptr; ptr < mem_size_aligned - size;
|
||||
ptr = ((struct heap_mem *)&heap_ptr[ptr])->next)
|
||||
{
|
||||
mem = (struct heap_mem *)&heap_ptr[ptr];
|
||||
for (ptr = (rt_uint8_t *)lfree - heap_ptr;
|
||||
ptr < mem_size_aligned - size;
|
||||
ptr = ((struct heap_mem *)&heap_ptr[ptr])->next)
|
||||
{
|
||||
mem = (struct heap_mem *)&heap_ptr[ptr];
|
||||
|
||||
if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
|
||||
{
|
||||
/* mem is not used and at least perfect fit is possible:
|
||||
* mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
|
||||
if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
|
||||
{
|
||||
/* mem is not used and at least perfect fit is possible:
|
||||
* mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
|
||||
|
||||
if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >= (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
|
||||
{
|
||||
/* (in addition to the above, we test if another struct heap_mem (SIZEOF_STRUCT_MEM) containing
|
||||
* at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
|
||||
* -> split large block, create empty remainder,
|
||||
* remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
|
||||
* mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
|
||||
* struct heap_mem would fit in but no data between mem2 and mem2->next
|
||||
* @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
|
||||
* region that couldn't hold data, but when mem->next gets freed,
|
||||
* the 2 regions would be combined, resulting in more free memory
|
||||
*/
|
||||
ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
|
||||
if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=
|
||||
(size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
|
||||
{
|
||||
/* (in addition to the above, we test if another struct heap_mem (SIZEOF_STRUCT_MEM) containing
|
||||
* at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
|
||||
* -> split large block, create empty remainder,
|
||||
* remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
|
||||
* mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
|
||||
* struct heap_mem would fit in but no data between mem2 and mem2->next
|
||||
* @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
|
||||
* region that couldn't hold data, but when mem->next gets freed,
|
||||
* the 2 regions would be combined, resulting in more free memory
|
||||
*/
|
||||
ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
|
||||
|
||||
/* create mem2 struct */
|
||||
mem2 = (struct heap_mem *)&heap_ptr[ptr2];
|
||||
mem2->used = 0;
|
||||
mem2->next = mem->next;
|
||||
mem2->prev = ptr;
|
||||
/* create mem2 struct */
|
||||
mem2 = (struct heap_mem *)&heap_ptr[ptr2];
|
||||
mem2->used = 0;
|
||||
mem2->next = mem->next;
|
||||
mem2->prev = ptr;
|
||||
|
||||
/* and insert it between mem and mem->next */
|
||||
mem->next = ptr2;
|
||||
mem->used = 1;
|
||||
/* and insert it between mem and mem->next */
|
||||
mem->next = ptr2;
|
||||
mem->used = 1;
|
||||
|
||||
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
|
||||
{
|
||||
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
|
||||
}
|
||||
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
|
||||
{
|
||||
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
|
||||
}
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem += (size + SIZEOF_STRUCT_MEM);
|
||||
if (max_mem < used_mem)
|
||||
max_mem = used_mem;
|
||||
used_mem += (size + SIZEOF_STRUCT_MEM);
|
||||
if (max_mem < used_mem)
|
||||
max_mem = used_mem;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
/* (a mem2 struct does no fit into the user data space of mem and mem->next will always
|
||||
* be used at this point: if not we have 2 unused structs in a row, plug_holes should have
|
||||
* take care of this).
|
||||
* -> near fit or excact fit: do not split, no mem2 creation
|
||||
* also can't move mem->next directly behind mem, since mem->next
|
||||
* will always be used at this point!
|
||||
*/
|
||||
mem->used = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* (a mem2 struct does no fit into the user data space of mem and mem->next will always
|
||||
* be used at this point: if not we have 2 unused structs in a row, plug_holes should have
|
||||
* take care of this).
|
||||
* -> near fit or excact fit: do not split, no mem2 creation
|
||||
* also can't move mem->next directly behind mem, since mem->next
|
||||
* will always be used at this point!
|
||||
*/
|
||||
mem->used = 1;
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);
|
||||
if (max_mem < used_mem)
|
||||
max_mem = used_mem;
|
||||
used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);
|
||||
if (max_mem < used_mem)
|
||||
max_mem = used_mem;
|
||||
#endif
|
||||
}
|
||||
/* set memory block magic */
|
||||
mem->magic = HEAP_MAGIC;
|
||||
}
|
||||
/* set memory block magic */
|
||||
mem->magic = HEAP_MAGIC;
|
||||
|
||||
if (mem == lfree)
|
||||
{
|
||||
/* Find next free block after mem and update lowest free pointer */
|
||||
while (lfree->used && lfree != heap_end)
|
||||
lfree = (struct heap_mem *)&heap_ptr[lfree->next];
|
||||
if (mem == lfree)
|
||||
{
|
||||
/* Find next free block after mem and update lowest free pointer */
|
||||
while (lfree->used && lfree != heap_end)
|
||||
lfree = (struct heap_mem *)&heap_ptr[lfree->next];
|
||||
|
||||
RT_ASSERT(((lfree == heap_end) || (!lfree->used)));
|
||||
}
|
||||
RT_ASSERT(((lfree == heap_end) || (!lfree->used)));
|
||||
}
|
||||
|
||||
rt_sem_release(&heap_sem);
|
||||
RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);
|
||||
RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
|
||||
RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0);
|
||||
rt_sem_release(&heap_sem);
|
||||
RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);
|
||||
RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
|
||||
RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM,
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM,
|
||||
("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))));
|
||||
|
||||
RT_OBJECT_HOOK_CALL(rt_malloc_hook, (((void*)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));
|
||||
|
||||
/* return the memory data except mem struct */
|
||||
return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
|
||||
}
|
||||
}
|
||||
RT_OBJECT_HOOK_CALL(rt_malloc_hook,
|
||||
(((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));
|
||||
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return RT_NULL;
|
||||
/* return the memory data except mem struct */
|
||||
return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(rt_malloc);
|
||||
|
||||
|
@ -364,85 +372,85 @@ RTM_EXPORT(rt_malloc);
|
|||
*/
|
||||
void *rt_realloc(void *rmem, rt_size_t newsize)
|
||||
{
|
||||
rt_size_t size;
|
||||
rt_size_t ptr, ptr2;
|
||||
struct heap_mem *mem, *mem2;
|
||||
void *nmem;
|
||||
rt_size_t size;
|
||||
rt_size_t ptr, ptr2;
|
||||
struct heap_mem *mem, *mem2;
|
||||
void *nmem;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* alignment size */
|
||||
newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
|
||||
if (newsize > mem_size_aligned)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));
|
||||
/* alignment size */
|
||||
newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
|
||||
if (newsize > mem_size_aligned)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/* allocate a new memory block */
|
||||
if (rmem == RT_NULL)
|
||||
return rt_malloc(newsize);
|
||||
/* allocate a new memory block */
|
||||
if (rmem == RT_NULL)
|
||||
return rt_malloc(newsize);
|
||||
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
|
||||
if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
|
||||
(rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
|
||||
{
|
||||
/* illegal memory */
|
||||
rt_sem_release(&heap_sem);
|
||||
if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
|
||||
(rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
|
||||
{
|
||||
/* illegal memory */
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return rmem;
|
||||
}
|
||||
return rmem;
|
||||
}
|
||||
|
||||
mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
|
||||
mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
|
||||
|
||||
ptr = (rt_uint8_t *)mem - heap_ptr;
|
||||
size = mem->next - ptr - SIZEOF_STRUCT_MEM;
|
||||
if (size == newsize)
|
||||
{
|
||||
/* the size is the same as */
|
||||
rt_sem_release(&heap_sem);
|
||||
ptr = (rt_uint8_t *)mem - heap_ptr;
|
||||
size = mem->next - ptr - SIZEOF_STRUCT_MEM;
|
||||
if (size == newsize)
|
||||
{
|
||||
/* the size is the same as */
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return rmem;
|
||||
}
|
||||
return rmem;
|
||||
}
|
||||
|
||||
if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
|
||||
{
|
||||
/* split memory block */
|
||||
if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
|
||||
{
|
||||
/* split memory block */
|
||||
#ifdef RT_MEM_STATS
|
||||
used_mem -= (size - newsize);
|
||||
used_mem -= (size - newsize);
|
||||
#endif
|
||||
|
||||
ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
|
||||
mem2 = (struct heap_mem *)&heap_ptr[ptr2];
|
||||
mem2->magic= HEAP_MAGIC;
|
||||
mem2->used = 0;
|
||||
mem2->next = mem->next;
|
||||
mem2->prev = ptr;
|
||||
mem->next = ptr2;
|
||||
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
|
||||
{
|
||||
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
|
||||
}
|
||||
ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
|
||||
mem2 = (struct heap_mem *)&heap_ptr[ptr2];
|
||||
mem2->magic= HEAP_MAGIC;
|
||||
mem2->used = 0;
|
||||
mem2->next = mem->next;
|
||||
mem2->prev = ptr;
|
||||
mem->next = ptr2;
|
||||
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
|
||||
{
|
||||
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
|
||||
}
|
||||
|
||||
plug_holes(mem2);
|
||||
plug_holes(mem2);
|
||||
|
||||
rt_sem_release(&heap_sem);
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
return rmem;
|
||||
}
|
||||
rt_sem_release(&heap_sem);
|
||||
return rmem;
|
||||
}
|
||||
rt_sem_release(&heap_sem);
|
||||
|
||||
/* expand memory */
|
||||
nmem = rt_malloc(newsize);
|
||||
if (nmem != RT_NULL) /* check memory */
|
||||
{
|
||||
rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
|
||||
rt_free(rmem);
|
||||
}
|
||||
/* expand memory */
|
||||
nmem = rt_malloc(newsize);
|
||||
if (nmem != RT_NULL) /* check memory */
|
||||
{
|
||||
rt_memcpy(nmem, rmem, size < newsize ? size : newsize);
|
||||
rt_free(rmem);
|
||||
}
|
||||
|
||||
return nmem;
|
||||
return nmem;
|
||||
}
|
||||
RTM_EXPORT(rt_realloc);
|
||||
|
||||
|
@ -460,98 +468,105 @@ RTM_EXPORT(rt_realloc);
|
|||
*/
|
||||
void *rt_calloc(rt_size_t count, rt_size_t size)
|
||||
{
|
||||
void *p;
|
||||
void *p;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
/* allocate 'count' objects of size 'size' */
|
||||
p = rt_malloc(count * size);
|
||||
/* allocate 'count' objects of size 'size' */
|
||||
p = rt_malloc(count * size);
|
||||
|
||||
/* zero the memory */
|
||||
if (p)
|
||||
rt_memset(p, 0, count * size);
|
||||
/* zero the memory */
|
||||
if (p)
|
||||
rt_memset(p, 0, count * size);
|
||||
|
||||
return p;
|
||||
return p;
|
||||
}
|
||||
RTM_EXPORT(rt_calloc);
|
||||
|
||||
/**
|
||||
* This function will release the previously allocated memory block by rt_malloc.
|
||||
* The released memory block is taken back to system heap.
|
||||
* This function will release the previously allocated memory block by
|
||||
* rt_malloc. The released memory block is taken back to system heap.
|
||||
*
|
||||
* @param rmem the address of memory which will be released
|
||||
*/
|
||||
void rt_free(void *rmem)
|
||||
{
|
||||
struct heap_mem *mem;
|
||||
struct heap_mem *mem;
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
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);
|
||||
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_OBJECT_HOOK_CALL(rt_free_hook, (rmem));
|
||||
RT_OBJECT_HOOK_CALL(rt_free_hook, (rmem));
|
||||
|
||||
if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr || (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n"));
|
||||
if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||
|
||||
(rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
|
||||
{
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n"));
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get the corresponding struct heap_mem ... */
|
||||
mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
|
||||
/* Get the corresponding struct heap_mem ... */
|
||||
mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
|
||||
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM,
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEM,
|
||||
("release memory 0x%x, size: %d\n",
|
||||
(rt_uint32_t)rmem,
|
||||
(rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
|
||||
|
||||
|
||||
/* protect the heap from concurrent access */
|
||||
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
|
||||
/* 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->magic == HEAP_MAGIC);
|
||||
/* ... and is now unused. */
|
||||
mem->used = 0;
|
||||
mem->magic = 0;
|
||||
/* ... which has to be in a used state ... */
|
||||
RT_ASSERT(mem->used);
|
||||
RT_ASSERT(mem->magic == HEAP_MAGIC);
|
||||
/* ... and is now unused. */
|
||||
mem->used = 0;
|
||||
mem->magic = 0;
|
||||
|
||||
if (mem < lfree)
|
||||
{
|
||||
/* the newly freed struct is now the lowest */
|
||||
lfree = mem;
|
||||
}
|
||||
if (mem < lfree)
|
||||
{
|
||||
/* the newly freed struct is now the lowest */
|
||||
lfree = mem;
|
||||
}
|
||||
|
||||
#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
|
||||
|
||||
/* finally, see if prev or next are free also */
|
||||
plug_holes(mem);
|
||||
rt_sem_release(&heap_sem);
|
||||
/* finally, see if prev or next are free also */
|
||||
plug_holes(mem);
|
||||
rt_sem_release(&heap_sem);
|
||||
}
|
||||
RTM_EXPORT(rt_free);
|
||||
|
||||
#ifdef RT_MEM_STATS
|
||||
void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used)
|
||||
void rt_memory_info(rt_uint32_t *total,
|
||||
rt_uint32_t *used,
|
||||
rt_uint32_t *max_used)
|
||||
{
|
||||
if (total != RT_NULL) *total = mem_size_aligned;
|
||||
if (used != RT_NULL) *used = used_mem;
|
||||
if (max_used != RT_NULL) *max_used = max_mem;
|
||||
if (total != RT_NULL)
|
||||
*total = mem_size_aligned;
|
||||
if (used != RT_NULL)
|
||||
*used = used_mem;
|
||||
if (max_used != RT_NULL)
|
||||
*max_used = max_mem;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
|
||||
void list_mem(void)
|
||||
{
|
||||
rt_kprintf("total memory: %d\n", mem_size_aligned);
|
||||
rt_kprintf("used memory : %d\n", used_mem);
|
||||
rt_kprintf("maximum allocated memory: %d\n", max_mem);
|
||||
rt_kprintf("total memory: %d\n", mem_size_aligned);
|
||||
rt_kprintf("used memory : %d\n", used_mem);
|
||||
rt_kprintf("maximum allocated memory: %d\n", max_mem);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_mem, list memory usage information)
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue