当前位置:网站首页>RT thread migration to s5p4418 (III): static memory pool management
RT thread migration to s5p4418 (III): static memory pool management
2022-06-30 06:40:00 【Mnnk】
The memory heap manager can allocate memory blocks of any size , Very flexible and convenient . But it also has obvious disadvantages : First, the efficiency of distribution is not high , In every distribution , To find the free memory block ; Second, it is easy to generate memory fragmentation . To improve the efficiency of memory allocation , And avoid memory fragmentation ,RT-Thread Provides another memory management method : Memory pool (Memory Pool).
A memory pool is a way of allocating memory , Used to allocate a large number of small memory blocks of the same size , It can greatly speed up the speed of memory allocation and release , And try to avoid memory fragmentation . Besides ,RT-Thread The memory pool of Support thread suspend function , When there are no free memory blocks in the memory pool , The application thread will be suspended , Until there are new memory blocks available in the memory pool , Then wake up the suspended application thread .
Memory pool control block :
struct rt_mempool
{
struct rt_object parent;
void *start_address; /* Memory pool data area start address */
rt_size_t size; /* Memory pool data area size */
rt_size_t block_size; /* Memory block size */
rt_uint8_t *block_list; /* Memory block list */
/* The maximum number of memory blocks that can be held in the memory pool data area */
rt_size_t block_total_count;
/* Number of free memory blocks in the memory pool */
rt_size_t block_free_count;
/* List of threads suspended due to unavailability of memory blocks */
rt_list_t suspend_thread;
/* Number of threads suspended due to unavailability of memory blocks */
rt_size_t suspend_thread_count;
};
typedef struct rt_mempool* rt_mp_t;
Initialization function
rt_err_t rt_mp_init(rt_mp_t mp, const char *name, void *start, rt_size_t size, rt_size_t block_size){
rt_ubase_t offset;
rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name); // Object initialization
mp->start_address = start;
size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
mp->size = size;
block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
mp->block_size = block_size;
mp->block_free_count = size / (block_size + sizeof(rt_ubase_t *));
mp->block_total_count = mp->block_free_count;
rt_list_init(&(mp->suspend_thread));
mp->suspend_thread_count = 0;
/* Form a single linked list */
for(offset = 0; offset < mp->block_total_count - 1; offset++){
*(rt_ubase_t **)((rt_uint8_t *)start + (block_size + sizeof(rt_ubase_t *)) * offset) =
(rt_ubase_t *)((rt_uint8_t *)start + (block_size + sizeof(rt_ubase_t *)) * (offset + 1));
}
*(rt_ubase_t **)((rt_uint8_t *)start + (block_size + sizeof(rt_ubase_t *)) * offset) = RT_NULL;
mp->block_list = (rt_uint8_t *)start;
return RT_EOK;
}
Memory distribution after initialization :
Apply for memory block
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time){
rt_thread_t thread;
rt_uint8_t *block_ptr;
rt_ubase_t tmp;
rt_uint32_t before_sleep = 0;
thread = rt_thread_self();
tmp = rt_hw_interrupt_disable();
while(mp->block_free_count == 0){
if(time == 0){
// Direct return without waiting
thread->error = -RT_ETIMEOUT;
rt_hw_interrupt_enable(tmp);
return RT_NULL;
}
thread->error = RT_EOK;
rt_thread_suspend(thread); // Pending threads waiting
rt_list_insert_before(&(mp->suspend_thread), &(thread->tlist));
mp->suspend_thread_count++;
if(time > 0){
before_sleep = rt_tick_get();
rt_timer_control(&thread->thread_timer, RT_TIMER_CTRL_SET_TIME, &time);
rt_timer_start(&thread->thread_timer); // Start timer
}
rt_hw_interrupt_enable(tmp);
rt_schedule(); // Dispatch
/* Timeout or active thread recovery */
if(thread->error != RT_EOK){
return RT_NULL;
}
if(time > 0){
time = rt_tick_get() - before_sleep; // Unallocated memory block , And continue to wait without timeout
if(time < 0){
time = 0;
}
}
tmp = rt_hw_interrupt_disable();
}
/* This indicates that there are free blocks */
block_ptr = mp->block_list;
mp->block_list = *(rt_uint8_t **)block_ptr; // .block_list Always point to the next free block
*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
mp->block_free_count--;
rt_hw_interrupt_enable(tmp);
return (void *)(block_ptr + sizeof(rt_uint8_t *));
}
If you apply for two pieces of memory . Distribution of memory :
Memory free
void rt_mp_free(void *block){
rt_uint8_t *block_ptr;
rt_mp_t mp;
rt_thread_t thread;
rt_ubase_t tmp;
RT_ASSERT(block != RT_NULL);
block_ptr = (rt_uint8_t *)((rt_uint32_t)block - sizeof(rt_uint8_t *));
mp = (rt_mp_t)(*(rt_uint8_t **)block_ptr); // Get the memory pool control block
tmp = rt_hw_interrupt_disable();
*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp->block_list; // .next
mp->block_list = block_ptr; // This block becomes the first free block in the linked list
mp->block_free_count++;
if(mp->suspend_thread_count > 0){
// Determine whether there are threads waiting to be allocated
thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
rt_thread_resume(thread);
thread->error = RT_EOK;
mp->suspend_thread_count--;
rt_hw_interrupt_enable(tmp);
rt_schedule();
return ;
}
rt_hw_interrupt_enable(tmp);
}
Release the two pieces of memory requested above , The linked list returns to its original state .
For dynamically created memory pools , Just one more step to apply for memory .
Engineering documents
Key 1 apply , Key 2 Release
边栏推荐
- Simple example of class template
- HuaWei满级大牛首次分享出这份598页网络协议全彩手册
- ES6 array
- Four ways to create multithreads
- Why does the verification code not refresh when clicked
- C语言:练习题三
- RT thread Kernel Implementation (IV): multi priority
- Rhcsa day 3
- RT thread Kernel Implementation (II): critical area, object container
- Arrangement of in-depth learning materials
猜你喜欢

Keil - the "trace HW not present" appears during download debugging

File Transfer Protocol,FTP文件共享服务器

Px4 control mode summary

Fastapi learning Day1

Use and principle of completionservice (source code analysis)

HuaWei满级大牛首次分享出这份598页网络协议全彩手册

Introduction to neural networks

1.8 - 多级存储

1.2 (supplementary)

1.9 - 存储器的分类
随机推荐
Simple example of class template
A complete performance test process
ETL为什么经常变成ELT甚至LET?
Gazebo installation, uninstall and upgrade
1.4 - fixed and floating point numbers
Idea run SQL file
1.2(补充)
1.8 - multi level storage
Zibll sub theme v6.4.1wordpress open source download_ Crack the original / use it directly / no tutorial required
Subnet division and subnet summary
Notes of the first week of 2021 Chengdu Arts and Sciences cloud computing intensive training class
File Transfer Protocol,FTP文件共享服务器
Analysis of startup process of gazebo multi computer simulation
DHCP operation
1.8 - 多级存储
Collections tool class (V)
Rhcsa day 1
1.9 - Cache
Fastapi learning Day1
图像处理7-图像增强