kmalloc() 函数如何工作?
我正在阅读Jamesm的内核开发教程,并在解释如何设置分页内存管理机制的“章节”中,显示了Kmalloc()函数,应该分配一些内存。
这是功能:
u32int kmalloc(u32int sz) {
u32int tmp = placement_address;
placement_address += sz;
return tmp;
}
我有点困惑。 此功能如何分配内存?
它返回分配的Chunck应该在哪里的地址,但我真的不明白实际分配的工作原理。
I am reading the JamesM's kernel development tutorial and in the "chapter" that explain how to setup the paging memory management mechanism, he shows the kmalloc() function, that should allocate some memory.
This is the function :
u32int kmalloc(u32int sz) {
u32int tmp = placement_address;
placement_address += sz;
return tmp;
}
I am a little bit confused about.
How does this function allocate memory?
It returns the address where the allocated chunck should be, but I really don't understand how the actual allocation works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是最简单的内存分配机制。
placement_address
在启动时初始化,指向某个大的、连续的内存块的开头。kmalloc
根据请求分配连续的块。我认为问题出在对世界“分配”的理解上。在这里,它以该术语的基本含义使用:它告诉调用者所请求的块在哪里,并注意它不再可用,因此不会再次返回。而已。没有释放,没有 OOM,没有边界,所有与物理内存的交互(如果发生的话)都是在启动时完成的。
想象一下售票员的一卷可撕票< /a>:您说“请 sz 门票”,因此它们从卷的当前末尾 (
placement_address
) 开始,并推进其“结束”sz
位置(placement_address += sz
) 并处理上一个“end”(return tmp
)。撕掉纸质门票在这里没有同等意义。您想知道“卷是如何打印的”,但“分配”并不意味着打印,而只是意味着将其分成几份处理。This is the simplest memory allocation mechanism.
placement_address
is initialized when booting to point to a start of some big, contiguous memory block. Thiskmalloc
assigns sequential chunks of it as requested.I think the problem comes from understanding of the world "allocate". Here it's used in the very basic meaning of the term: it tells the caller where requested chunk is and takes note that it's no longer available, so it won't be returned again. Nothing more. There is no deallocation, no OOM, no boundaries and all interaction with physical memory (if it happened) was done once at startup.
Think of a ticket seller with a roll of tear-off tickets: you say "sz tickets please", so they start at the current end of the roll (
placement_address
), they advance their "end"sz
places (placement_address += sz
) and handle you the previous "end" (return tmp
). The tearing away of paper tickets has no equivalent here. You're wondering on "how was the roll printed", but "allocation" doesn't mean printing, it only means handling it away in pieces.在我看来,
u32int
是一种保存数字的类型,该数字又引用内存地址。placement_address
也是一个u32int
,我猜它是已经分配的大内存块的起始地址。当您请求分配大小为sz
的空间时,您将获得当前的placement_address
,它是指向某些预分配内存的指针。然后,我们将指针前进所请求的大小,因此下次调用该函数时,我们会再次返回placement_address
,但它已前进,因此它使用不同的内存。请注意,这不会产生分段错误 - 如果您分配 2 个字节,然后写入第 3 个字节,从技术上讲,它已分配,但只是分配给程序的另一部分。我在下面的代码中写了一个简化
It looks to me like
u32int
is a type that holds a number which in turn references a memory address.placement_address
is also au32int
, which I would guess is the address of the start of a large memory block that has been allocated already. When you request an allocation of sizesz
, you are given the currentplacement_address
, which is a pointer to some pre-allocated memory. We then advance the pointer by the size requested, so next time the function is called we returnplacement_address
again, but it has been advanced so it uses different memory.Note that this doesn't create segmentation faults - if you allocate 2 bytes and then write on to the 3rd, it is technically allocated but just to another part of the program. I've written out a simplification in code below