当前位置: 首页 > news >正文

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 &&region < 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);......}

http://www.xdnf.cn/news/991801.html

相关文章:

  • Python数据类型大全:整型、浮点、字符串与布尔值
  • Codeforces 1029 Div3(ABCDE)
  • Windows10下利用VS2019编译JpegLib
  • seo优化新利器:AI如何让内容批量生成与排名提升双管齐下?
  • Gremlin创建schema(包括实体和关系)
  • 【质数】埃氏筛法、线性筛法(欧拉筛法)
  • 【Linux系统编程】System V
  • Java锁机制对决:ReadWriteLock vs StampedLock
  • 从0到1落地一个RAG智能客服系统
  • ConcurrentHashMap详解:原理、实现与并发控制
  • 专访伦敦发展促进署CEO:在AI与黄仁勋之外,伦敦为何给泡泡玛特和比亚迪留了C位?
  • MySQL优化器
  • 3.3.1_2 检错编码(循环冗余校验码)
  • 【完整源码+数据集+部署教程】安检爆炸物检测系统源码和数据集:改进yolo11-REPVGGOREPA
  • 接口测试之文件上传
  • 【完整源码+数据集+部署教程】石材实例分割系统源码和数据集:改进yolo11-CA-HSFPN
  • 【Docker】快速入门与项目部署实战
  • Haclon例程1-<剃须刀片检测程序详解>
  • < 买了个麻烦 (二) 618 京东云--轻量服务器 > “可以为您申请全额退订呢。“ 工单记录:可以“全额退款“
  • linux引导过程与服务控制
  • nginx ./nginx -s reload 不生效
  • 2024-2030年中国轨道交通智能运维市场全景分析与战略前瞻
  • 永磁同步电机无速度算法--基于稳态卡尔曼滤波器SSEKF的滑模观测器
  • shell 中的 expect工具
  • AI 赋能 Java 开发:从通宵达旦到高效交付的蜕变之路
  • 如何“调优”我们自身的人体系统?
  • 以太网MDI信号PCB EMC设计要点
  • mysql 8.0引入递归cte以支持层级数据查询
  • 【Dv3Admin】系统视图操作日志API文件解析
  • 大模型呼叫系统——重塑学校招生问答,提升服务效能