问题描述
KMALLOC 是只分配页面大小的内存还是可以分配更少的内存?kmalloc 可以分配的大小是多少?我在哪里可以找到它的描述,因为我到处看它并没有真正说明它分配了多少内存?我想知道的是 KMALLOC 分配的实际大小是多少.它是否分配 2 的幂的大小?它是否只是从准备好的缓存中找到空闲对象?
Does KMALLOC allocates only in page size memory or it can allocate less ?What are the sizes that the kmalloc can allocate ?Where can I find description of it, as everywhere I looked it doesn't really say how much memory it allocates ?What I want to know is what are the actual sizes that KMALLOC allocates.Does it allocate size of power of 2 ?Does it just find free objects from the cache that is ready ?
推荐答案
我的理解是这样的:内核是在处理系统的物理内存,只有page-sized chunk才能使用;因此,当您调用 kmalloc()
时,您只会获得某些预定义的、固定大小的字节数组.
My understanding is as follows: the kernel is dealing with the physical memory of the system, which is available only in page-sized chunks; thus when you call kmalloc()
you are going to get only certain predefined, fixed-size byte arrays.
返回的实际内存取决于系统的架构,但 kmalloc 可以处理的最小分配高达 32 或 64 字节.您将从对 kmalloc()
的调用中返回至少 与您要求的内存一样多(通常更多).通常您不会获得超过 128 KB(同样取决于架构)
The actual memory you get back is dependent on the system's architecture, but the smallest allocation that kmalloc can handle is as big as 32 or 64 bytes. You will get back from a call to kmalloc()
at least as much memory as you asked for (usually more). Typically you will not get more than 128 KB (again, architecture dependent)
要获取系统的页面大小(以字节为单位),您可以执行以下命令:
To get the page size (in bytes) of your system you can execute the command:
getconf PAGESIZE
或
getconf PAGE_SIZE
关于最大页面大小的信息在/usr/src/linux/include/linux/slab.h
This information on max page size is in /usr/src/linux/include/linux/slab.h
是的,页面大小通常是 2 的幂,但同样,你不会得到你所要求的,但会更多一点.
And yes, the page sizes are generally powers of 2, but again, you're not going to get exactly what you ask for, but a little bit more.
你可以使用一些这样的代码:
You can use some code like this:
void * stuff;
stuff = kmalloc(1,GFP_KERNEL);
printk("I got: %zu bytes of memory
", ksize(stuff));
kfree(stuff);
显示实际分配的内存量:
To show the actual amount of memory allocated:
[90144.702588] I got: 32 bytes of memory
这篇关于KMALLOC 大小分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!