[RTT例程练习] 3.3 静态内存管理,内存池mempool

来源:互联网 发布:中美军事对峙知乎 编辑:程序博客网 时间:2024/05/16 04:54

内存池是一种静态的内存管理方法。它预先将一块固定连续的内存区域划分成几个大小不同的块。使用者申请时就将对应大小的内存块给他。这种方法的优点是不会有内存碎片,但不够灵活,适用于需要频繁存取的场合,例如buffer。

这个例子有两个线程。thread1不停分配内存块,但其中并没有使用delay() 来使自己挂起,所以thread2 由于优先级低于 thread1 而一直得不到运行。thread1 分配完所有内存块以后,又试着再取得一个内存块,由于内存块已经被分配完,所以thread1 会因为得不到资源而被挂起。此时thread2 开始运行,释放内存块,释放了一个以后,thread1 的等待条件满足而执行了一次,然后thread2 继续运行直至结束。

程序:

#include <rtthread.h>static rt_uint8_t *ptr[48];static rt_uint8_t mempool[4096];static struct rt_mempool mp;static rt_thread_t tid1 = RT_NULL;static rt_thread_t tid2 = RT_NULL;static void thread1_entry(void* parameter){    int i,j = 1;    char *block;    while(j--)    {        for (i = 0; i < 48; i++)        {            /* 申请内存块 */            rt_kprintf("allocate No.%d\n", i);            if (ptr[i] == RT_NULL)            {                ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER);            }        }        /* 继续申请一个内存块,因为已经没有内存块,线程应该被挂起 */        block = rt_mp_alloc(&mp, RT_WAITING_FOREVER);        rt_kprintf("allocate the block mem\n");        /* 释放这个内存块 */        rt_mp_free(block);        block = RT_NULL;    }}static void thread2_entry(void *parameter){    int i,j = 1;    while(j--)    {        rt_kprintf("try to release block\n");        for (i = 0 ; i < 48; i ++)        {            /* 释放所有分配成功的内存块 */            if (ptr[i] != RT_NULL)            {                rt_kprintf("release block %d\n", i);                rt_mp_free(ptr[i]);                ptr[i] = RT_NULL;            }        }        /* 休眠10个OS Tick */        rt_thread_delay(10);    }}int rt_application_init(){    int i;    for (i = 0; i < 48; i ++) ptr[i] = RT_NULL;    /* 初始化内存池对象 ,每块分配的大小为80,但是另外还有大小为4的控制头,所以实际大小为84*/    rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80);    /* 创建线程1 */    tid1 = rt_thread_create("t1",        thread1_entry, RT_NULL, 512, 8, 10);    if (tid1 != RT_NULL)        rt_thread_startup(tid1);    /* 创建线程2 */    tid2 = rt_thread_create("t2",    thread2_entry, RT_NULL, 512, 9, 10);    if (tid2 != RT_NULL)        rt_thread_startup(tid2);    return 0;}

结果:

allocate No.0allocate No.1allocate No.2allocate No.3allocate No.4allocate No.5allocate No.6allocate No.7allocate No.8allocate No.9allocate No.10allocate No.11allocate No.12allocate No.13allocate No.14allocate No.15allocate No.16allocate No.17allocate No.18allocate No.19allocate No.20allocate No.21allocate No.22allocate No.23allocate No.24allocate No.25allocate No.26allocate No.27allocate No.28allocate No.29allocate No.30allocate No.31allocate No.32allocate No.33allocate No.34allocate No.35allocate No.36allocate No.37allocate No.38allocate No.39allocate No.40allocate No.41allocate No.42allocate No.43allocate No.44allocate No.45allocate No.46allocate No.47try to release blockrelease block 0allocate the block memrelease block 1release block 2release block 3release block 4release block 5release block 6release block 7release block 8release block 9release block 10release block 11release block 12release block 13release block 14release block 15release block 16release block 17release block 18release block 19release block 20release block 21release block 22release block 23release block 24release block 25release block 26release block 27release block 28release block 29release block 30release block 31release block 32release block 33release block 34release block 35release block 36release block 37release block 38release block 39release block 40release block 41release block 42release block 43release block 44release block 45release block 46release block 47