Nuttx之mm_extend
声明:此处代码分析,来源与 nuttx 12.8.0版本。
/***************************************************************************** Name: mm_extend** Description:* Extend a heap region by add a block of (virtually) contiguous memory* to the end of the heap.*****************************************************************************/void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size,int region)
{FAR struct mm_allocnode_s *oldnode;FAR struct mm_allocnode_s *newnode;uintptr_t blockstart;uintptr_t blockend;/* Make sure that we were passed valid parameters */DEBUGASSERT(heap && mem);
#if CONFIG_MM_REGIONS > 1DEBUGASSERT(size >= MIN_EXTEND && region >= 0 &®ion < heap->mm_nregions);
#elseDEBUGASSERT(size >= MIN_EXTEND && region == 0);
#endif/* Make sure that the memory region are properly aligned */blockstart = (uintptr_t)mem;blockend = blockstart + size;DEBUGASSERT(MM_ALIGN_UP(blockstart) == blockstart);DEBUGASSERT(MM_ALIGN_DOWN(blockend) == blockend);/* Take the memory manager mutex */DEBUGVERIFY(mm_lock(heap));/* Get the terminal node in the old heap. The block to extend must* immediately follow this node.*/oldnode = heap->mm_heapend[region];DEBUGASSERT((uintptr_t)oldnode + MM_SIZEOF_ALLOCNODE == blockstart);/* The size of the old node now extends to the new terminal node.* This is the old size (MM_SIZEOF_ALLOCNODE) plus the size of* the block (size) minus the size of the new terminal node* (MM_SIZEOF_ALLOCNODE) or simply:*/oldnode->size = size | (oldnode->size & MM_MASK_BIT);/* The old node should already be marked as allocated */DEBUGASSERT(MM_NODE_IS_ALLOC(oldnode));/* Get and initialize the new terminal node in the heap */newnode = (FAR struct mm_allocnode_s *)(blockend - MM_SIZEOF_ALLOCNODE);newnode->size = MM_SIZEOF_ALLOCNODE | MM_ALLOC_BIT;heap->mm_heapend[region] = newnode;/* Finally, increase the total heap size accordingly */heap->mm_heapsize += size;mm_unlock(heap);/* Finally "free" the new block of memory where the old terminal node was* located.*/mm_free(heap, mem);
}
显然,此函数用于扩展堆空间。首先对函数的参数进行合理性检验自然属于基本操作。对于size的检验标准为size>=MIN_EXTEND,那么,问题来了,为何这样的size就是合理的呢?首先我们先来看看MIN_EXTEND的定义。
#define MIN_EXTEND (2 * MM_SIZEOF_ALLOCNODE)
可以看的出来,size大小的空间至少能容纳得下两个struct mm_allocnode_s。众所周知,堆是由struct mm_heap_s来进行管理的。mm_heap_s的成员mm_heapend成员用来标记堆空间的结束,这就解释了MIN_EXTEND中的一个struct mm_allocnode_s结构的来源。我们的函数功能是扩展堆空间,那么,扩展的空间必然是空闲的。对堆空间的扩展实质上就是增加struct mm_heap_s 的mm_nodelist的成员。struct mm_freenode_s用于空闲空间的管理。且,struct mm_freenode_s结构是存在于每个空闲空间的开头。这就解释了MIN_EXTEND中的第二个struct mm_allocnode_s结构的来源。有人说了,struct mm_freenode_s与struct mm_allocnode_s相差两个struct mm_freenode_s这指针的大小,确实,按照严格意义来说,应该修改MIN_EXTEND的定义如下。
#define MIN_EXTEND (sizeof(struct mm_freenode_s) + MM_SIZEOF_ALLOCNODE)
接下来的一个问题是,为什么会有如下代码。
oldnode->size = size | (oldnode->size & MM_MASK_BIT);
我的意思是,oldnode是属于struct mm_allocnode_s这个类型的,根据mm_addregion我们知道,对于新加入堆的空间struct mm_allocnode_s的size成员变量表示大小的部分,其值一般是MM_SIZEOF_ALLOCNODE,如下。
void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,size_t heapsize)
{......heap->mm_heapstart[idx]->size = MM_SIZEOF_ALLOCNODE | MM_ALLOC_BIT;......heap->mm_heapend[idx]->size = MM_SIZEOF_ALLOCNODE | MM_ALLOC_BIT |MM_PREVFREE_BIT;......
}
对于此处oldnode->size,其原因是,此时的oldnode,在mm_free间接调用mm_malloc_size时,mm_malloc_size做了强制类型转换。这也印证了我们之前说的,struct mm_freenode_s结构是存在于每个空闲空间的开头。
size_t mm_malloc_size(FAR struct mm_heap_s *heap, FAR void *mem)
{......node = (FAR struct mm_freenode_s *)((FAR char *)mem - MM_SIZEOF_ALLOCNODE);......
}
那么,struct mm_freenode_s的size成员是表示的同oldnode->size = size 相同的意思吗?即 struct mm_freenode_s的size成员表示的是sizeof(struct mm_freenode_s)+用户可用空间大小吗?显然是的。同样,可以从mm_addregion中就能看出来。
void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,size_t heapsize)
{FAR struct mm_freenode_s *node;......node = (FAR struct mm_freenode_s *)(heapbase + MM_SIZEOF_ALLOCNODE);DEBUGASSERT((((uintptr_t)node + MM_SIZEOF_ALLOCNODE) % MM_ALIGN) == 0);node->size = heapsize - 2 * MM_SIZEOF_ALLOCNODE;......
}
mm_addregion对struct mm_allocnode_s的size成员的赋值有一定的特殊性,那么,此size表示的是那段空间呢?更一般的来说,此size表示的是sizeof(struct mm_allocnode_s)+用户实际使用空间大小,何以见得呢?我们可以通过函数mm_malloc就能看得出来。
FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
{FAR struct mm_freenode_s *node;......alignsize = MM_ALIGN_UP(size + MM_ALLOCNODE_OVERHEAD);......node->size = alignsize | (node->size & MM_MASK_BIT);......ret = (FAR void *)((FAR char *)node + MM_SIZEOF_ALLOCNODE);......}