欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 明星 > memblock内存分配器

memblock内存分配器

2025/2/25 5:06:22 来源:https://blog.csdn.net/wwwlyj123321/article/details/143835726  浏览:    关键词:memblock内存分配器

一、简述

memblock 是 Linux 内核中的一个内存管理子系统,主要用于在系统启动早期阶段管理物理内存。它在内核初始化期间负责管理内存,直到更复杂的内存管理子系统(如伙伴系统)接管。

二、工作原理

初始化:在内核启动时,memblock 被初始化,扫描系统的物理内存布局,并将所有内存标记为可用。
内存保留:在内核初始化过程中,某些内存区域需要被保留(例如,内核自身占用的内存、设备内存等),这些区域会被标记为保留。
内存分配:提供简单的接口,如 memblock_alloc(),用于在可用内存中分配内存块。
内存释放:虽然 memblock 提供了内存释放的功能,但在大多数情况下,内存一旦分配就不会被释放,直到内核初始化完成。


三、 数据结构

memblock 使用几个关键的数据结构来管理内存:

  1. memblock_region:表示一个内存区域,包含起始地址、大小和属性(如是否可用)。
  2. memblock_type:用于区分不同类型的内存区域,如可用内存(memory)和保留内存(reserved)。
  3. memblock:全局变量。包含两个 memblock_type 实例,分别用于管理可用内存和保留内存。

memblock.h - include/linux/memblock.h - Linux source code v5.4.285 - Bootlin Elixir Cross Referencer

/*** struct memblock_region - represents a memory region* @base: physical address of the region* @size: size of the region* @flags: memory region attributes* @nid: NUMA node id*/
struct memblock_region {phys_addr_t base; //区域的起始物理地址phys_addr_t size; // 区域的大小enum memblock_flags flags;// 标志位,描述区域属性
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAPint nid;
#endif
};/*** struct memblock_type - collection of memory regions of certain type* @cnt: number of regions* @max: size of the allocated array* @total_size: size of all regions* @regions: array of regions* @name: the memory type symbolic name*/
struct memblock_type {unsigned long cnt; // 当前区域的数量unsigned long max; // 最大区域数量phys_addr_t total_size; //所有区域的总大小struct memblock_region *regions; // 指向区域数组char *name;  //区域名称
};/*** struct memblock - memblock allocator metadata* @bottom_up: is bottom up direction?* @current_limit: physical address of the current allocation limit* @memory: usabe memory regions* @reserved: reserved memory regions* @physmem: all physical memory*/
struct memblock {bool bottom_up;  /* is bottom up direction? */phys_addr_t current_limit;struct memblock_type memory;struct memblock_type reserved;
#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAPstruct memblock_type physmem;
#endif
};extern struct memblock memblock;

 

4. 接口函数
memblock_add():添加一个新的内存区域到 memblock。
memblock_reserve():保留一个内存区域,防止其被分配。
memblock_alloc():分配内存块。
memblock_free():释放内存块(通常不常用)。
 

ref:

memblock 内存分配器原理和代码分析 - 泰晓科技 

Linux内存都去哪了:(1)分析memblock在启动过程中对内存的影响 - ArnoldLu - 博客园

https://zhuanlan.zhihu.com/p/355035376

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词