rt-thread的小内存管理算法分析

rt-thread的小内存管理是rt-thread操作系统默认堆内存管理算法,是一种简单的内存分配算法,当有可用的内存的时候,会从中分割一块来作为分配的内存,而剩下的则返回到动态内存堆中.此算法采用了一个静态链表来实现的,其源码文件在根目录下的src目录下,包含mem.c和mem.h两个文件.

1 数据结构

小内存管理算法将内存看成是一个个内存块:

[cpp]  view plain  copy
  1. struct heap_mem  
  2. {  
  3.     /* magic and used flag */  
  4.     rt_uint16_t magic;     //如果此内存块被分配了,则置0x1ea0,以此标志此块内存是正常分配出来的,而不是非法指针  
  5.     rt_uint16_t used;      //0:未分配;1:已分配  
  6.   
  7.     rt_size_t next, prev;   //前一内存块,后一内存块  
  8. };  
此结构为一个内存块控制结构的定义.

2 初始化动态内存堆

[cpp]  view plain  copy
  1. /** 
  2.  * @ingroup SystemInit 
  3.  * 
  4.  * This function will init system heap 
  5.  * 
  6.  * @param begin_addr the beginning address of system page 
  7.  * @param end_addr the end address of system page 
  8.  */  
  9. void rt_system_heap_init(void *begin_addr, void *end_addr)  //函数传入用为初始化的起始地址和末尾地址,但这两个地址有可能并非对齐的  
  10. {  
  11.     struct heap_mem *mem;  
  12.     rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);     //得到对齐后的堆内存起始地址  
  13.     rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE);  //得到对齐后的堆内存末尾地址  
  14.   
  15.     RT_DEBUG_NOT_IN_INTERRUPT;          //确保此函数不是运行在中断例程内  
  16.   
  17.     /* alignment addr */  
  18.     if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&                 //确保可用动态堆内存大小至少大于可等于2个内存块控制结构大小  
  19.         ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))  
  20.     {  
  21.         /* calculate the aligned memory size */  
  22.         mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;   //计算出可用的真实可用作动态分配的内存大小  
  23.     }  
  24.     else  
  25.     {  
  26.         rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",  
  27.                    (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);  
  28.   
  29.         return;  
  30.     }  
  31.   
  32.     /* point to begin address of heap */  
  33.     heap_ptr = (rt_uint8_t *)begin_align;     //heap_ptr为静态全局变量,指用堆内存起始地址  
  34.   
  35.     RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",  
  36.                                 (rt_uint32_t)heap_ptr, mem_size_aligned));  
  37.   
  38.     /* initialize the start of the heap */  
  39.     mem        = (struct heap_mem *)heap_ptr;    //将动态堆内存首地址初始化为一个内存块,可用大小为全部可用大小  
  40.     mem->magic = HEAP_MAGIC;         //初始化为0x1ea0  
  41.     mem->next  = mem_size_aligned + SIZEOF_STRUCT_MEM;    //下一个指向动态堆内存最靠末尾的内存控制结构  
  42.     mem->prev  = 0;                 //无前一个内存块  
  43.     mem->used  = 0;            //初始化为未使用  
  44.   
  45.     /* initialize the end of the heap */  
  46.     heap_end        = (struct heap_mem *)&heap_ptr[mem->next];   //指向末尾内存块  
  47.     heap_end->magic = HEAP_MAGIC;  
  48.     heap_end->used  = 1;             //末尾内存块初始化为已使用  
  49.     heap_end->next  = mem_size_aligned + SIZEOF_STRUCT_MEM;   //下一个内存块指向自己  
  50.     heap_end->prev  = mem_size_aligned + SIZEOF_STRUCT_MEM;   //前一个内存块也指向自己  
  51.   
  52.     rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);    //初始化堆内存信号量为1  
  53.   
  54.     /* initialize the lowest-free pointer to the start of the heap */  
  55.     lfree = (struct heap_mem *)heap_ptr;     //lfree为空闲内存,为静态全局变量,初始化时指向动态堆内存首地址  
  56. }  

由上述代码可知,在初始化时,小内存管理算法通过传进来的起始地址和末尾地址将动态堆内存初始化为两个内存块,第一个内存块指向动态堆内存首地址,可用空间为整个可分配的内存(不包含两个内存控制块本身所占大小),此内存块下一指针指向末尾内存控制块.第二个内存块指向最末尾的一个内存控制块,可用空间大小为0,此内存块前一指针和后一指针都指向本身.


2 分配内存

2.1 malloc

[cpp]  view plain  copy
  1. /** 
  2.  * Allocate a block of memory with a minimum of 'size' bytes. 
  3.  * 
  4.  * @param size is the minimum size of the requested block in bytes. 
  5.  * 
  6.  * @return pointer to allocated memory or NULL if no free memory was found. 
  7.  */  
  8. void *rt_malloc(rt_size_t size)      //分配内存接口  
  9. {  
  10.     rt_size_t ptr, ptr2;  
  11.     struct heap_mem *mem, *mem2;  
  12.   
  13.     RT_DEBUG_NOT_IN_INTERRUPT;     //确保此函数不是在中断例程内执行  
  14.   
  15.     if (size == 0)           //如果指定分配的大小为0则直接返回NULL  
  16.         return RT_NULL;  
  17.   
  18.     if (size != RT_ALIGN(size, RT_ALIGN_SIZE))  
  19.         RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",  
  20.                                     size, RT_ALIGN(size, RT_ALIGN_SIZE)));  
  21.     else  
  22.         RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d\n", size));  
  23.   
  24.     /* alignment size */  
  25.     size = RT_ALIGN(size, RT_ALIGN_SIZE);      //传入的size不一定是对齐的数据民,因此,这里只取对齐后的大小  
  26.   
  27.     if (size > mem_size_aligned)         //如果指定大小比整个可用空间都要大,则直接返回NULL  
  28.     {  
  29.         RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));  
  30.   
  31.         return RT_NULL;  
  32.     }  
  33.   
  34.     /* every data block must be at least MIN_SIZE_ALIGNED long */  //如果指定大小不得小于MN_SIZE_ALIGNED,即12  
  35.     if (size < MIN_SIZE_ALIGNED)  
  36.         size = MIN_SIZE_ALIGNED;  
  37.   
  38.     /* take memory semaphore */  
  39.     rt_sem_take(&heap_sem, RT_WAITING_FOREVER);  //获得堆信号量,从第1章,已知初始化时已将此信号量初始为1,因此,若第一次是可以成功获取  
  40.   
  41.     for (ptr = (rt_uint8_t *)lfree - heap_ptr;    //扫描内存空闲链表  
  42.          ptr < mem_size_aligned - size;  
  43.          ptr = ((struct heap_mem *)&heap_ptr[ptr])->next)  
  44.     {  
  45.         mem = (struct heap_mem *)&heap_ptr[ptr];    //mem指向空闲节点  
  46.   
  47.         if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)   //如果当前内存块未分配且大小足够  
  48.         {  
  49.             /* mem is not used and at least perfect fit is possible: 
  50.              * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */  
  51.   
  52.             if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=    //如果当前内存块大小比需求还大,剩余空间还有可能分配另一内存块时  
  53.                 (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))  
  54.             {  
  55.                 /* (in addition to the above, we test if another struct heap_mem (SIZEOF_STRUCT_MEM) containing 
  56.                  * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem') 
  57.                  * -> split large block, create empty remainder, 
  58.                  * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if 
  59.                  * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size, 
  60.                  * struct heap_mem would fit in but no data between mem2 and mem2->next 
  61.                  * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty 
  62.                  *       region that couldn't hold data, but when mem->next gets freed, 
  63.                  *       the 2 regions would be combined, resulting in more free memory 
  64.                  */  
  65.                 ptr2 = ptr + SIZEOF_STRUCT_MEM + size;     //将此内存块分割成两块,ptr2指向后一块  
  66.   
  67.                 /* create mem2 struct */  
  68.                 mem2       = (struct heap_mem *)&heap_ptr[ptr2];  
  69.                 mem2->used = 0;  
  70.                 mem2->next = mem->next;  
  71.                 mem2->prev = ptr;  
  72.   
  73.                 /* and insert it between mem and mem->next */    //更新当前块的后指针指向分割后另一块内存块  
  74.                 mem->next = ptr2;  
  75.                 mem->used = 1;     //当前内存块被分配,标志置1  
  76.   
  77.                 if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)   //如果分割后的内存块不是末尾,则将其后指针指向自己  
  78.                 {  
  79.                     ((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;  
  80.                 }  
  81. #ifdef RT_MEM_STATS  
  82.                 used_mem += (size + SIZEOF_STRUCT_MEM);    //更新总共已分配的内存块大小,便于统计  
  83.                 if (max_mem < used_mem)  
  84.                     max_mem = used_mem;  
  85. #endif  
  86.             }  
  87.             else        //如果当前内存块只够本次分配时  
  88.             {  
  89.                 /* (a mem2 struct does no fit into the user data space of mem and mem->next will always 
  90.                  * be used at this point: if not we have 2 unused structs in a row, plug_holes should have 
  91.                  * take care of this). 
  92.                  * -> near fit or excact fit: do not split, no mem2 creation 
  93.                  * also can't move mem->next directly behind mem, since mem->next 
  94.                  * will always be used at this point! 
  95.                  */  
  96.                 mem->used = 1;   //置已分配标志  
  97. #ifdef RT_MEM_STATS  
  98.                 used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);  
  99.                 if (max_mem < used_mem)  
  100.                     max_mem = used_mem;  
  101. #endif  
  102.             }  
  103.             /* set memory block magic */  
  104.             mem->magic = HEAP_MAGIC;  
  105.   
  106.             if (mem == lfree)     //如果空闲内存链表头刚好指向当前内存块  
  107.             {  
  108.                 /* Find next free block after mem and update lowest free pointer */  
  109.                 while (lfree->used && lfree != heap_end)      //则更新空闲链表指向下一个空闲内存块  
  110.                     lfree = (struct heap_mem *)&heap_ptr[lfree->next];  
  111.   
  112.                 RT_ASSERT(((lfree == heap_end) || (!lfree->used)));   //空闲链表不能指向堆内存末尾或空闲链表指向的内存块有使用标志为已分配  
  113.             }  
  114.   
  115.             rt_sem_release(&heap_sem);  //释放信号量,保证另一分配函数可以运行  
  116.             RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);  
  117.             RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);  
  118.             RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0);  
  119.   
  120.             RT_DEBUG_LOG(RT_DEBUG_MEM,  
  121.                          ("allocate memory at 0x%x, size: %d\n",   
  122.                           (rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),  
  123.                           (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));  
  124.   
  125.             RT_OBJECT_HOOK_CALL(rt_malloc_hook,  
  126.                                 (((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));  
  127.   
  128.             /* return the memory data except mem struct */  
  129.             return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;    //返回当前内存块的可用内存地址,前面大小为SIZE_STRUCT_MEM的空间表示内存块控制结构所占空间,是不能拿来用的  
  130.         }  
  131.     }  
  132.   
  133.     rt_sem_release(&heap_sem);  
  134.   
  135.     return RT_NULL;  //所有空闲链表上的内存块都不符合,则返回NULL  
  136. }  

2.2 realloc

[cpp]  view plain  copy
  1. /** 
  2.  * This function will change the previously allocated memory block. 
  3.  * 
  4.  * @param rmem pointer to memory allocated by rt_malloc 
  5.  * @param newsize the required new size 
  6.  * 
  7.  * @return the changed memory block address 
  8.  */  
  9. void *rt_realloc(void *rmem, rt_size_t newsize)  
  10. {  
  11.     rt_size_t size;  
  12.     rt_size_t ptr, ptr2;  
  13.     struct heap_mem *mem, *mem2;  
  14.     void *nmem;  
  15.   
  16.     RT_DEBUG_NOT_IN_INTERRUPT; //确保此函数不是在中断例程中使用  
  17.   
  18.     /* alignment size */  
  19.     newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);//得到对齐后的重新分配的大小  
  20.     if (newsize > mem_size_aligned)//如果超过上限,则直接返回NULL  
  21.     {  
  22.         RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));  
  23.   
  24.         return RT_NULL;  
  25.     }  
  26.   
  27.     /* allocate a new memory block */  
  28.     if (rmem == RT_NULL)//如果传入的参数为空,则为分配好内存则返回  
  29.         return rt_malloc(newsize);  
  30.   
  31.     rt_sem_take(&heap_sem, RT_WAITING_FOREVER);//尝试获取信号量  
  32.   
  33.     if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||  //如果原先的内存指针不在合法范围内,则原样返回原先的内存指针  
  34.         (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)  
  35.     {  
  36.         /* illegal memory */  
  37.         rt_sem_release(&heap_sem);  
  38.   
  39.         return rmem;  
  40.     }  
  41.   
  42.     mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);//得到对应的内存块指针  
  43.   
  44.     ptr = (rt_uint8_t *)mem - heap_ptr;           //得到当前指针到内存堆首地址的偏移  
  45.     size = mem->next - ptr - SIZEOF_STRUCT_MEM; //得到当前内存块的总共可用空间  
  46.     if (size == newsize)                   //如果当前指针可用内存本身就等于重新需要分配的大小,则不必再重新分配,原样返回即可  
  47.     {  
  48.         /* the size is the same as */  
  49.         rt_sem_release(&heap_sem);  
  50.   
  51.         return rmem;  
  52.     }  
  53.   
  54.     if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size) //如果当前指针所指向的内存块本身就比重新要分配的大小还大,且还有至少一个MIN_SIZE的剩余则分割当前内存块  
  55.     {  
  56.         /* split memory block */  
  57. #ifdef RT_MEM_STATS  
  58.         used_mem -= (size - newsize); //刷新已使用的内存大小  
  59. #endif  
  60.   
  61.         ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;//ptr2指向分割后的第二块内存  
  62.         mem2 = (struct heap_mem *)&heap_ptr[ptr2];//转化为内存块,mem2即为第二块内存块  
  63.         mem2->magic= HEAP_MAGIC;//置maigc  
  64.         mem2->used = 0;//置未使用标志  
  65.         mem2->next = mem->next;//更新next  
  66.         mem2->prev = ptr;  
  67.         mem->next = ptr2;//当前内存块的下一块指向ptr2  
  68.         if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)//如果分割后的第二块内存块的下一指针不是指向内存堆最后一块  
  69.         {  
  70.             ((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;//则将下块内存块的前指针指向自己  
  71.         }  
  72.   
  73.         plug_holes(mem2);//将分割后的第二块内存尝试与左右内存块合并  
  74.   
  75.         rt_sem_release(&heap_sem);  
  76.   
  77.         return rmem;//原样返回指针  
  78.     }  
  79.     rt_sem_release(&heap_sem);  
  80.   
  81.     /* expand memory */  
  82.     nmem = rt_malloc(newsize);//如果当前内存块本身可用内存确实不足,则重新分配一块内存  
  83.     if (nmem != RT_NULL) /* check memory */  
  84.     {  
  85.         rt_memcpy(nmem, rmem, size < newsize ? size : newsize); //将原内存块的数据原样复制到新内存块中  
  86.         rt_free(rmem);//将原内存块释放  
  87.     }  
  88.   
  89.     return nmem;//返回新内存块  
  90. }  

需要注意的是,上述代码中,如果当前内存块可用内存比较充裕时,将分割成两块,后一块侵害出来后会尝试与前后内存块合并,这合并的代友后续章节将会介绍其过程.

2.3 calloc

[cpp]  view plain  copy
  1. /** 
  2.  * This function will contiguously allocate enough space for count objects 
  3.  * that are size bytes of memory each and returns a pointer to the allocated 
  4.  * memory. 
  5.  * 
  6.  * The allocated memory is filled with bytes of value zero. 
  7.  * 
  8.  * @param count number of objects to allocate 
  9.  * @param size size of the objects to allocate 
  10.  * 
  11.  * @return pointer to allocated memory / NULL pointer if there is an error 
  12.  */  
  13. void *rt_calloc(rt_size_t count, rt_size_t size)  
  14. {  
  15.     void *p;  
  16.   
  17.     RT_DEBUG_NOT_IN_INTERRUPT;//防止此函数在中断例程中使用  
  18.   
  19.     /* allocate 'count' objects of size 'size' */  
  20.     p = rt_malloc(count * size); //分配内存  
  21.   
  22.     /* zero the memory */  
  23.     if (p)  
  24.         rt_memset(p, 0, count * size);//初始化分配好的内存内容全部为0  
  25.   
  26.     return p;  
  27. }  

3 free

[cpp]  view plain  copy
  1. /** 
  2.  * This function will release the previously allocated memory block by 
  3.  * rt_malloc. The released memory block is taken back to system heap. 
  4.  * 
  5.  * @param rmem the address of memory which will be released 
  6.  */  
  7. void rt_free(void *rmem)  
  8. {  
  9.     struct heap_mem *mem;  
  10.   
  11.     RT_DEBUG_NOT_IN_INTERRUPT;//防止此函数在中断例程中使用  
  12.   
  13.     if (rmem == RT_NULL)  
  14.         return;  
  15.     RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE-1)) == 0);//断言输入参数是否对齐  
  16.     RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr && //断言输入参数是否在合法范围内  
  17.               (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end);  
  18.   
  19.     RT_OBJECT_HOOK_CALL(rt_free_hook, (rmem));//使用钩子函数  
  20.   
  21.     if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||   //如果输入参数范围不合法,则直接返回  
  22.         (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)  
  23.     {  
  24.         RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n"));  
  25.   
  26.         return;  
  27.     }  
  28.   
  29.     /* Get the corresponding struct heap_mem ... */  
  30.     mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);//获取对应内存块指针  
  31.   
  32.     RT_DEBUG_LOG(RT_DEBUG_MEM,  
  33.                  ("release memory 0x%x, size: %d\n",   
  34.                   (rt_uint32_t)rmem,   
  35.                   (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));  
  36.   
  37.   
  38.     /* protect the heap from concurrent access */  
  39.     rt_sem_take(&heap_sem, RT_WAITING_FOREVER);  
  40.   
  41.     /* ... which has to be in a used state ... */  
  42.     RT_ASSERT(mem->used); //断言当前内存块被使用  
  43.     RT_ASSERT(mem->magic == HEAP_MAGIC);//断言当前内存块是之前被分配出来的  
  44.     /* ... and is now unused. */  
  45.     mem->used  = 0;  
  46.     mem->magic = 0;  
  47.   
  48.     if (mem < lfree)//放入空闲链表  
  49.     {  
  50.         /* the newly freed struct is now the lowest */  
  51.         lfree = mem;  
  52.     }  
  53.   
  54. #ifdef RT_MEM_STATS  
  55.     used_mem -= (mem->next - ((rt_uint8_t*)mem - heap_ptr));//更新已使用的内存大小  
  56. #endif  
  57.   
  58.     /* finally, see if prev or next are free also */  
  59.     plug_holes(mem); //尝试合并内存  
  60.     rt_sem_release(&heap_sem);  
  61. }  

合并内存块的代码如下:

[cpp]  view plain  copy
  1. static void plug_holes(struct heap_mem *mem)  
  2. {  
  3.     struct heap_mem *nmem;  
  4.     struct heap_mem *pmem;  
  5.   
  6.     RT_ASSERT((rt_uint8_t *)mem >= heap_ptr);//断言当前输入参数有合法范围  
  7.     RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end);  
  8.     RT_ASSERT(mem->used == 0);//断言当前内存未使用  
  9.   
  10.     /* plug hole forward */     //其下代码是尝试向后合并  
  11.     nmem = (struct heap_mem *)&heap_ptr[mem->next];//指向下一内存块  
  12.     if (mem != nmem && //如果存在下一内存块,且同样未使用,并且下一内存块不是指向堆内存末尾  
  13.         nmem->used == 0 &&  
  14.         (rt_uint8_t *)nmem != (rt_uint8_t *)heap_end)  
  15.     {  
  16.         /* if mem->next is unused and not end of heap_ptr, 
  17.          * combine mem and mem->next 
  18.          */  
  19.         if (lfree == nmem)//如果空闲内存指向下一内存块  
  20.         {  
  21.             lfree = mem;//则将空闲内存指向当前内存块  
  22.         }  
  23.         mem->next = nmem->next;//合并  
  24.         ((struct heap_mem *)&heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - heap_ptr;  
  25.     }  
  26.   
  27.     /* plug hole backward */  //其下代码是尝试向前合并  
  28.     pmem = (struct heap_mem *)&heap_ptr[mem->prev];//指向前一内存块  
  29.     if (pmem != mem && pmem->used == 0)//如果存在前一内存块且未使用  
  30.     {  
  31.         /* if mem->prev is unused, combine mem and mem->prev */  
  32.         if (lfree == mem)//如果空闲内存指向前一内存,则将空闲内存指向它  
  33.         {  
  34.             lfree = pmem;  
  35.         }  
  36.         pmem->next = mem->next;//合并  
  37.         ((struct heap_mem *)&heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - heap_ptr;  
  38.     }  
  39. }  

至此,小内存算法的源码分析完了,从上述可知,小内存管理算法从整体上来讲,是将一片内存初始化为静态链表来实现的,初始化时只有两个内存块,第一块除了包含内存块控制块外,还包含待分配的空间,这个空间就是mem_size_aligned,它是指此算法可用来作分配的动态堆内存总大小,任何待分配的内存都不能比它还大,否则超时极限;第二块只包含内存控制块本身,不包含待分配的空间,它作为链表尾,且标志恒使用.此算法中还有一空闲指针,指向空闲的内存,初始化时指向第一内块.接下来就是分配内存了,分配内存时首先从空闲内存所指向的节点开始扫描,一旦扫描到大小满足的节点,则返回此节点,另当此节点所指向的空间足够大,大到还有足够空间分配另一空间时,则分割此节点指向的内存为两块,前一内存返回,后一内存放入空闲链表中; 当释放内存时,算法将检查待释放内存的前一内存块和后一内存块,如果为空闲则合并.

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值