mm支持查看是谁申请的内存
This commit is contained in:
@@ -4,16 +4,13 @@
|
||||
#include "util.h"
|
||||
#include "assert.h"
|
||||
#include "spinlock.h"
|
||||
|
||||
#include "kobject.h"
|
||||
#define MAGIC_NUM 0xFEDCBA98 //!< mm的幻数
|
||||
#define MEM_HEAP_NAME 4 //!< 名字长度
|
||||
#define MEM_HEAP_NAME KOBJ_NAME_SIZE //!< 名字长度
|
||||
|
||||
typedef struct mem_heap
|
||||
{
|
||||
// union
|
||||
// {
|
||||
// char name[MEM_HEAP_NAME];
|
||||
// };
|
||||
char name[MEM_HEAP_NAME];
|
||||
struct mem_heap *next;
|
||||
struct mem_heap *prev;
|
||||
umword_t size; // 可用的大小
|
||||
|
||||
@@ -216,6 +216,10 @@ static inline thread_t *thread_get_current(void)
|
||||
|
||||
return th;
|
||||
}
|
||||
static inline const char *thread_current_get_name(void)
|
||||
{
|
||||
return kobject_get_name(&(thread_get_current()->kobj));
|
||||
}
|
||||
static inline int thread_fast_ipc_save(thread_t *th, task_t *tk, void *usp)
|
||||
{
|
||||
int ret;
|
||||
@@ -263,6 +267,7 @@ static inline int thread_fast_ipc_pop(thread_t *th, thread_fast_ipc_item_t *item
|
||||
return ret;
|
||||
}
|
||||
task_t *thread_get_current_task(void);
|
||||
const char *thread_get_current_task_name(void);
|
||||
task_t *thread_get_task(thread_t *th);
|
||||
task_t *thread_get_bind_task(thread_t *th);
|
||||
int thread_set_prio(thread_t *th, int prio);
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
*
|
||||
*/
|
||||
#include "mm.h"
|
||||
|
||||
#include "thread.h"
|
||||
#include "string.h"
|
||||
void mem_init(mem_t *_this)
|
||||
{
|
||||
spinlock_init(&_this->lock);
|
||||
@@ -80,9 +81,6 @@ void mem_free(mem_t *_this, void *mem)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// #if MEM_TRACE
|
||||
// mem_trace(_this);
|
||||
// #endif
|
||||
umword_t status = spinlock_lock(&_this->lock);
|
||||
m_mem = (struct mem_heap *)((ptr_t)mem - MEM_HEAP_STRUCT_SIZE);
|
||||
assert(m_mem->magic == MAGIC_NUM);
|
||||
@@ -92,11 +90,9 @@ void mem_free(mem_t *_this, void *mem)
|
||||
return;
|
||||
}
|
||||
m_mem->used = 0;
|
||||
m_mem->name[0] = '\0';
|
||||
mem_merge(_this, m_mem);
|
||||
spinlock_set(&_this->lock, status);
|
||||
// #if MEM_TRACE
|
||||
// mem_trace(_this);
|
||||
// #endif
|
||||
}
|
||||
/**
|
||||
* @brief 划分内存
|
||||
@@ -124,11 +120,14 @@ void *mem_split(mem_t *_this, void *mem, uint32_t size)
|
||||
r_mem->next = t_mem->next;
|
||||
r_mem->prev = t_mem;
|
||||
r_mem->magic = MAGIC_NUM;
|
||||
memcpy(r_mem->name, thread_get_current_task_name(), MEM_HEAP_NAME);
|
||||
|
||||
t_mem->next->prev = r_mem;
|
||||
t_mem->next = r_mem;
|
||||
t_mem->used = 1;
|
||||
t_mem->size = size - MEM_HEAP_STRUCT_SIZE;
|
||||
|
||||
memcpy(t_mem->name, thread_get_current_task_name(), MEM_HEAP_NAME);
|
||||
spinlock_set(&_this->lock, status);
|
||||
// mem_trace(_this);
|
||||
|
||||
@@ -207,15 +206,9 @@ void mem_free_align(mem_t *_this, void *f_mem)
|
||||
{
|
||||
real_mem = (umword_t *)(*(((umword_t *)(f_mem)) - 1));
|
||||
mem_free(_this, real_mem);
|
||||
// #if MEM_TRACE
|
||||
// mem_trace(_this);
|
||||
// #endif
|
||||
return;
|
||||
}
|
||||
mem_free(_this, f_mem);
|
||||
// #if MEM_TRACE
|
||||
// mem_trace(_this);
|
||||
// #endif
|
||||
}
|
||||
/**
|
||||
* @brief 申请内存
|
||||
@@ -247,15 +240,16 @@ void *mem_alloc(mem_t *_this, uint32_t size)
|
||||
mem->used = 1;
|
||||
mem->next->prev = mem_temp;
|
||||
mem->next = mem_temp;
|
||||
|
||||
memcpy(mem->name, thread_get_current_task_name(), MEM_HEAP_NAME);
|
||||
spinlock_set(&_this->lock, status);
|
||||
// mem_trace(_this);
|
||||
return (void *)((ptr_t)mem + MEM_HEAP_STRUCT_SIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
mem->used = 1;
|
||||
memcpy(mem->name, thread_get_current_task_name(), MEM_HEAP_NAME);
|
||||
spinlock_set(&_this->lock, status);
|
||||
// mem_trace(_this);
|
||||
return (void *)((ptr_t)mem + MEM_HEAP_STRUCT_SIZE);
|
||||
}
|
||||
}
|
||||
@@ -276,10 +270,7 @@ int mem_heap_add(mem_t *_this, void *mem, uint32_t size)
|
||||
|
||||
printk("total mem size:%d.\n", size);
|
||||
|
||||
// ((struct mem_heap *)mem)->name[0] = ' ';
|
||||
// ((struct mem_heap *)mem)->name[1] = ' ';
|
||||
// ((struct mem_heap *)mem)->name[2] = ' ';
|
||||
// ((struct mem_heap *)mem)->name[3] = ' ';
|
||||
((struct mem_heap *)mem)->name[0] = '\0';
|
||||
((struct mem_heap *)mem)->used = 0;
|
||||
umword_t status = spinlock_lock(&_this->lock);
|
||||
if (!_this->heap_start)
|
||||
@@ -386,7 +377,7 @@ void mem_trace(mem_t *_this)
|
||||
for (mem = _this->heap_start; mem != _this->heap_end; mem = mem->next)
|
||||
{
|
||||
assert(mem->magic == MAGIC_NUM);
|
||||
printk("%d [0x%x-] %dB\n", mem->used, mem, mem->size);
|
||||
printk("%d [0x%x-] %7dB %s\n", mem->used, mem, mem->size, mem->name);
|
||||
total += mem->size + MEM_HEAP_STRUCT_SIZE;
|
||||
}
|
||||
spinlock_set(&_this->lock, status);
|
||||
@@ -408,7 +399,7 @@ void mem_info(mem_t *_this, size_t *total, size_t *free)
|
||||
|
||||
for (mem = _this->heap_start; mem != _this->heap_end; mem = mem->next)
|
||||
{
|
||||
printk("%d [0x%x-] %dB\n", mem->used, mem, mem->size);
|
||||
printk("%d [0x%x-] %7dB %s\n", mem->used, mem, mem->size, mem->name);
|
||||
total_ += mem->size + MEM_HEAP_STRUCT_SIZE;
|
||||
if (mem->used == 0)
|
||||
{
|
||||
|
||||
@@ -121,7 +121,7 @@ static void sys_syscall(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t in_tag,
|
||||
|
||||
printk("================================mem:[%d]=============================\n", i);
|
||||
mm_info_raw(i, &item_total, &item_free);
|
||||
printk("total:0x%x free:0x%x\n", item_total, item_free);
|
||||
printk("total:%d free:%d\n", item_total, item_free);
|
||||
printk("=====================================================================\n\n", i);
|
||||
total += item_total;
|
||||
free += item_free;
|
||||
|
||||
@@ -1039,6 +1039,18 @@ task_t *thread_get_current_task(void)
|
||||
}
|
||||
return container_of(kobj, task_t, kobj);
|
||||
}
|
||||
const char *thread_get_current_task_name(void)
|
||||
{
|
||||
task_t *tk;
|
||||
|
||||
tk = thread_get_current_task();
|
||||
if (tk == NULL)
|
||||
{
|
||||
static const char tmp[KOBJ_NAME_SIZE] = "";
|
||||
return tmp;
|
||||
}
|
||||
return kobject_get_name(&tk->kobj);
|
||||
}
|
||||
task_t *thread_get_task(thread_t *th)
|
||||
{
|
||||
return container_of(th->task, task_t, kobj);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
# export TOOLCHAIN=/home/zhangzheng/toolchain/gcc-arm-10.3-2021.07-x86_64-arm-none-eabi/bin/
|
||||
# export TOOLCHAIN_LIB=/home/zhangzheng/toolchain/gcc-arm-10.3-2021.07-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp
|
||||
export TOOLCHAIN=/home/zhangzheng/toolchain/gcc-arm-10.3-2021.07-x86_64-arm-none-eabi/bin/
|
||||
export TOOLCHAIN_LIB=/home/zhangzheng/toolchain/gcc-arm-10.3-2021.07-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp
|
||||
|
||||
export TOOLCHAIN=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/
|
||||
export TOOLCHAIN_LIB=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp
|
||||
# export TOOLCHAIN=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/bin/
|
||||
# export TOOLCHAIN_LIB=/Users/zhangzheng/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp
|
||||
|
||||
# export TOOLCHAIN=/d/GNUArmEmbeddedToolchain/102021.10/bin/
|
||||
# export TOOLCHAIN_LIB=/d/GNUArmEmbeddedToolchain/102021.10/lib/gcc/arm-none-eabi/10.3.1/thumb/v7-m/nofp
|
||||
|
||||
Reference in New Issue
Block a user