简体   繁体   English

关于memory分配,C malloc/calloc依赖Linux mmap/malloc还是相反?

[英]About memory allocation, does C malloc/calloc depends on Linux mmap/malloc or the opposite?

As far as I know, C has the following functions, eg: malloc , calloc , realloc , to allocate memory. And the linux kernel also has the following functions, eg: malloc , mmap , kmalloc , vmalloc ... to allocate memory. As far as I know, C has the following functions, eg: malloc , calloc , realloc , to allocate memory. And the linux kernel also has the following functions, eg: malloc , mmap , kmalloc , vmalloc ... to allocate memory.

I want to know which is the lowest function. If you say, "Linux kernel is the lowest function, your C program must allocate memory with Linux kernel", then how does the Linux kernel allocate it's own memory? I want to know which is the lowest function. If you say, "Linux kernel is the lowest function, your C program must allocate memory with Linux kernel", then how does the Linux kernel allocate it's own memory?

Or if you say, "Linux kernel is the lowest function.", then when I write a C program and run in the Linux system, to allocate memory, I should through the system call.或者你说,“Linux kernel是最低的function”,那我写一个C程序运行在Linux系统时,分配memory,我应该通过系统调用。

Hope to have an answer.希望得到答复。

On the linux OS, the C functions malloc , calloc , realloc used by user mode programs are implemented in the C library and handle pages of memory mapped in the process address space using the mmap system call.在linux操作系统上,用户态程序使用的C函数malloccallocrealloc在C库中实现,并使用mmap系统调用处理映射到进程地址空间的memory页面。 mmap associates pages of virtual memory with addresses in the process address space. mmap将虚拟 memory 的页面与进程地址空间中的地址相关联。 When the process then accesses these addresses, actual RAM is mapped by the kernel to this virtual space.当进程访问这些地址时,实际 RAM 由 kernel 映射到这个虚拟空间。 Not every call to malloc maps memory pages, only those for which not enough space was already requested from the system.并非每次调用malloc映射 memory 页,只有那些没有从系统请求足够空间的页。

In the kernel space, a similar process takes place but the caller can require that the RAM be mapped immediately.在 kernel 空间中,发生类似的过程,但调用者可以要求立即映射 RAM。

I want to know which is the lowest function.我想知道最低的是function。

The user-level malloc function calls brk or malloc (depending on the library used and depending on the Linux version).用户级malloc function调用brkmalloc (取决于使用的库,取决于Linux版本)。

... how does the Linux kernel allocate it's own memory? ... Linux kernel 如何分配它自己的 memory?

On a system without MMU this is easy:在没有 MMU 的系统上,这很容易:

Let's say we have system with 8 MB RAM and we know that the address of the RAM is 0x20000000 to 0x20800000.假设我们的系统有 8 MB RAM,我们知道 RAM 的地址是 0x20000000 到 0x20800000。

The kernel contains an array that contains information about which pages are in use. kernel 包含一个数组,其中包含有关正在使用哪些页面的信息。 Let's say the size of a "page" is 0x1000 (this is the page size in x86 systems with MMU).假设“页面”的大小为 0x1000(这是带有 MMU 的 x86 系统中的页面大小)。

In old Linux versions the array was named mem_map .在旧的 Linux 版本中,数组被命名为mem_map Each element in the array corresponds to one memory "page".数组中的每个元素对应一个 memory “页面”。 It is zero if the page is free.如果页面空闲则为零。

When the system is started, the kernel itself initializes this array (writes the initial values in the array).系统启动时,kernel自己初始化这个数组(将初始值写入数组)。

If the kernel needs one page of memory, it searches for an element in the array whose value is 0. Let's say mem_map[0x123] is 0. The kernel now sets mem_map[0x123]=1;如果 kernel 需要 memory 的一页,它会在数组中搜索值为 0 的元素。假设mem_map[0x123]为 0。kernel 现在设置mem_map[0x123]=1; . . mem_map[0x123] corresponds to the address 0x20123000 , so the kernel "allocated" some memory at address 0x20123000 . mem_map[0x123]对应于地址0x20123000 ,因此 kernel 在地址 0x20123000 处“分配”了一些0x20123000

If the kernel wants to "free" some memory at address 0x20234000 , it simply sets mem_map[0x234]=0;如果 kernel 想要在地址0x20234000处“释放”一些 memory,它只需设置mem_map[0x234]=0; . .

On a system with MMU, it is a bit more complicated but the principle is the same.在有 MMU 的系统上,稍微复杂一点,但原理是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM