kmalloc() 函数如何工作?

发布于 2025-01-17 08:56:44 字数 290 浏览 7 评论 0原文

我正在阅读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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2025-01-24 08:56:44

这是最简单的内存分配机制。 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. This kmalloc 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.

忆伤 2025-01-24 08:56:44

在我看来,u32int 是一种保存数字的类型,该数字又引用内存地址。 placement_address 也是一个u32int,我猜它是已经分配的大内存块的起始地址。当您请求分配大小为 sz 的空间时,您将获得当前的 placement_address,它是指向某些预分配内存的指针。然后,我们将指针前进所请求的大小,因此下次调用该函数时,我们会再次返回 placement_address,但它已前进,因此它使用不同的内存。

请注意,这不会产生分段错误 - 如果您分配 2 个字节,然后写入第 3 个字节,从技术上讲,它已分配,但只是分配给程序的另一部分。我在下面的代码中写了一个简化

u32int placement_address = malloc(40000); //allocate 40kb for kmalloc to hand out

u32int kmalloc(u32int sz) {
   u32int tmp = placement_address; //temporarily store the return pointer
   placement_address += sz; //advance pointer by memory used for next allocation
   return tmp; //return pointer somewhere in malloc'ed area
}

u32int item  = kmalloc(5);  //returns placement_address
u32int item2 = kmalloc(3);  //returns placement_address + 5 (see above line)
u32int item3 = kmalloc(40); //returns placement_address + 5 + 3 (see above 2 lines)

char valid = (*(item3+5)) //get the data in memory at placement_address + 48. With
                          //malloc, this should make a segmentation fault, but the
                          //memory is already allocated when using kmalloc. This
                          //can create some really difficult to find bugs if done
                          //accidentally!

It looks to me like u32int is a type that holds a number which in turn references a memory address. placement_address is also a u32int, 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 size sz, you are given the current placement_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 return placement_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

u32int placement_address = malloc(40000); //allocate 40kb for kmalloc to hand out

u32int kmalloc(u32int sz) {
   u32int tmp = placement_address; //temporarily store the return pointer
   placement_address += sz; //advance pointer by memory used for next allocation
   return tmp; //return pointer somewhere in malloc'ed area
}

u32int item  = kmalloc(5);  //returns placement_address
u32int item2 = kmalloc(3);  //returns placement_address + 5 (see above line)
u32int item3 = kmalloc(40); //returns placement_address + 5 + 3 (see above 2 lines)

char valid = (*(item3+5)) //get the data in memory at placement_address + 48. With
                          //malloc, this should make a segmentation fault, but the
                          //memory is already allocated when using kmalloc. This
                          //can create some really difficult to find bugs if done
                          //accidentally!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文