summaryrefslogtreecommitdiff
path: root/src/memheap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/memheap.c')
-rw-r--r--src/memheap.c122
1 files changed, 60 insertions, 62 deletions
diff --git a/src/memheap.c b/src/memheap.c
index 0df9ed41ed..067523bb93 100644
--- a/src/memheap.c
+++ b/src/memheap.c
@@ -26,6 +26,14 @@
#ifdef RT_USING_MEMHEAP
+#define DBG_TAG "kernel.memheap"
+#ifdef RT_DEBUG_MEMHEAP
+#define DBG_LVL DBG_LOG
+#else
+#define DBG_LVL DBG_WARNING
+#endif /* defined (RT_DEBUG_MEM) */
+#include <rtdbg.h>
+
/* dynamic pool magic and mask */
#define RT_MEMHEAP_MAGIC 0x1ea01ea0
#define RT_MEMHEAP_MASK 0xFFFFFFFE
@@ -143,9 +151,8 @@ rt_err_t rt_memheap_init(struct rt_memheap *memheap,
rt_sem_init(&(memheap->lock), name, 1, RT_IPC_FLAG_PRIO);
memheap->locked = RT_FALSE;
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x\n",
- start_addr, size, &(memheap->free_header)));
+ LOG_D("memory heap: start addr 0x%08x, size %d, free list header 0x%08x",
+ start_addr, size, &(memheap->free_header));
return RT_EOK;
}
@@ -195,8 +202,8 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
if (size < RT_MEMHEAP_MINIALLOC)
size = RT_MEMHEAP_MINIALLOC;
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d on heap:%8.*s",
- size, RT_NAME_MAX, heap->parent.name));
+ LOG_D("allocate %d on heap:%8.*s",
+ size, RT_NAME_MAX, heap->parent.name);
if (size < heap->available_size)
{
@@ -242,12 +249,11 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
new_ptr = (struct rt_memheap_item *)
(((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
- header_ptr,
- header_ptr->next,
- header_ptr->prev,
- new_ptr));
+ LOG_D("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]",
+ header_ptr,
+ header_ptr->next,
+ header_ptr->prev,
+ new_ptr);
/* mark the new block as a memory block and freed. */
new_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
@@ -276,9 +282,9 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
new_ptr->prev_free = heap->free_list;
heap->free_list->next_free->prev_free = new_ptr;
heap->free_list->next_free = new_ptr;
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
- new_ptr->next_free,
- new_ptr->prev_free));
+ LOG_D("new ptr: next_free 0x%08x, prev_free 0x%08x",
+ new_ptr->next_free,
+ new_ptr->prev_free);
/* decrement the available byte count. */
heap->available_size = heap->available_size -
@@ -295,11 +301,10 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
heap->max_used_size = heap->pool_size - heap->available_size;
/* remove header_ptr from free list */
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x\n",
- header_ptr,
- header_ptr->next_free,
- header_ptr->prev_free));
+ LOG_D("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x",
+ header_ptr,
+ header_ptr->next_free,
+ header_ptr->prev_free);
header_ptr->next_free->prev_free = header_ptr->prev_free;
header_ptr->prev_free->next_free = header_ptr->next_free;
@@ -324,11 +329,10 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
}
/* Return a memory address to the caller. */
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("alloc mem: memory[0x%08x], heap[0x%08x], size: %d\n",
- (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
- header_ptr,
- size));
+ LOG_D("alloc mem: memory[0x%08x], heap[0x%08x], size: %d",
+ (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
+ header_ptr,
+ size);
return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE);
}
@@ -340,7 +344,7 @@ void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
}
}
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate memory: failed\n"));
+ LOG_D("allocate memory: failed");
/* Return the completion status. */
return RT_NULL;
@@ -436,22 +440,20 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
heap->max_used_size = heap->pool_size - heap->available_size;
/* remove next_ptr from free list */
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("remove block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x",
- next_ptr,
- next_ptr->next_free,
- next_ptr->prev_free));
+ LOG_D("remove block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x",
+ next_ptr,
+ next_ptr->next_free,
+ next_ptr->prev_free);
_remove_next_ptr(next_ptr);
/* build a new one on the right place */
next_ptr = (struct rt_memheap_item *)((char *)ptr + newsize);
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("new free block: block[0x%08x] nextm[0x%08x] prevm[0x%08x]",
- next_ptr,
- next_ptr->next,
- next_ptr->prev));
+ LOG_D("new free block: block[0x%08x] nextm[0x%08x] prevm[0x%08x]",
+ next_ptr,
+ next_ptr->next,
+ next_ptr->prev);
/* mark the new block as a memory block and freed. */
next_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
@@ -473,9 +475,9 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
next_ptr->prev_free = heap->free_list;
heap->free_list->next_free->prev_free = (struct rt_memheap_item *)next_ptr;
heap->free_list->next_free = (struct rt_memheap_item *)next_ptr;
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x",
- next_ptr->next_free,
- next_ptr->prev_free));
+ LOG_D("new ptr: next_free 0x%08x, prev_free 0x%08x",
+ next_ptr->next_free,
+ next_ptr->prev_free);
if (heap->locked == RT_FALSE)
{
/* release lock */
@@ -523,12 +525,11 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
new_ptr = (struct rt_memheap_item *)
(((rt_uint8_t *)header_ptr) + newsize + RT_MEMHEAP_SIZE);
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
- header_ptr,
- header_ptr->next,
- header_ptr->prev,
- new_ptr));
+ LOG_D("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]",
+ header_ptr,
+ header_ptr->next,
+ header_ptr->prev,
+ new_ptr);
/* mark the new block as a memory block and freed. */
new_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
@@ -554,9 +555,8 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
free_ptr = new_ptr->next;
heap->available_size = heap->available_size - MEMITEM_SIZE(free_ptr);
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
- header_ptr, header_ptr->next_free, header_ptr->prev_free));
+ LOG_D("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x",
+ header_ptr, header_ptr->next_free, header_ptr->prev_free);
free_ptr->next->prev = new_ptr;
new_ptr->next = free_ptr->next;
@@ -571,9 +571,9 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
new_ptr->prev_free = heap->free_list;
heap->free_list->next_free->prev_free = new_ptr;
heap->free_list->next_free = new_ptr;
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new free ptr: next_free 0x%08x, prev_free 0x%08x\n",
- new_ptr->next_free,
- new_ptr->prev_free));
+ LOG_D("new free ptr: next_free 0x%08x, prev_free 0x%08x",
+ new_ptr->next_free,
+ new_ptr->prev_free);
/* increment the available byte count. */
heap->available_size = heap->available_size + MEMITEM_SIZE(new_ptr);
@@ -611,15 +611,15 @@ void rt_memheap_free(void *ptr)
header_ptr = (struct rt_memheap_item *)
((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
- ptr, header_ptr));
+ LOG_D("free memory: memory[0x%08x], block[0x%08x]",
+ ptr, header_ptr);
/* check magic */
if (header_ptr->magic != (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED) ||
(header_ptr->next->magic & RT_MEMHEAP_MASK) != RT_MEMHEAP_MAGIC)
{
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("bad magic:0x%08x @ memheap\n",
- header_ptr->magic));
+ LOG_D("bad magic:0x%08x @ memheap",
+ header_ptr->magic);
RT_ASSERT(header_ptr->magic == (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED));
/* check whether this block of memory has been over-written. */
RT_ASSERT((header_ptr->next->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
@@ -651,8 +651,8 @@ void rt_memheap_free(void *ptr)
/* Determine if the block can be merged with the previous neighbor. */
if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
{
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
- header_ptr->prev));
+ LOG_D("merge: left node 0x%08x",
+ header_ptr->prev);
/* adjust the available number of bytes. */
heap->available_size += RT_MEMHEAP_SIZE;
@@ -676,9 +676,8 @@ void rt_memheap_free(void *ptr)
/* merge block with next neighbor. */
new_ptr = header_ptr->next;
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
- new_ptr, new_ptr->next_free, new_ptr->prev_free));
+ LOG_D("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x",
+ new_ptr, new_ptr->next_free, new_ptr->prev_free);
new_ptr->next->prev = header_ptr;
header_ptr->next = new_ptr->next;
@@ -708,9 +707,8 @@ void rt_memheap_free(void *ptr)
n->prev_free->next_free = header_ptr;
n->prev_free = header_ptr;
- RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
- ("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
- header_ptr->next_free, header_ptr->prev_free));
+ LOG_D("insert to free list: next_free 0x%08x, prev_free 0x%08x",
+ header_ptr->next_free, header_ptr->prev_free);
}
#ifdef RT_USING_MEMTRACE