what is the kernel heap?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

what is the kernel heap?

Post by yemista »

Ok, this question is not as stupid as it sounds. I know a heap is a space of organized free memory that you can pull blocks out of, and know a little bit about heap management, so what my question really is, is why would your kernel have a heap? Also, how is using a heap different or better than implementing a paging system and just giving out pages of memory?
xyzzy
Member
Member
Posts: 391
Joined: Wed Jul 25, 2007 8:45 am
Libera.chat IRC: aejsmith
Location: London, UK
Contact:

Re: what is the kernel heap?

Post by xyzzy »

A kernel heap is usually used to implement malloc-style functions for the kernel, so that your kernel can dynamically allocate memory, for example to store structures describing processes/threads, etc. The kernel heap can be implemented on top of a physical memory (page) allocator - in my kernel, the malloc code calls into the kernel heap code, which then allocates pages and maps them into an area in the virtual address space reserved for the kernel heap.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: what is the kernel heap?

Post by yemista »

oh ok, so its used for cases when the kernel needs to create new records such as if it started another process? and its not used for things like loading in a driver or giving a program an address space?
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: what is the kernel heap?

Post by CodeCat »

Both. If a process needs to be created, it needs its own page directory and at least one page table, as well as other data structures describing that process. Allocating actual memory for the process comes as the binary is loaded into memory. You wouldn't normally kernel-malloc that memory, since malloc is meant only for 'end-user' memory, and is therefore restricted only for use within the kernel. A typical kernel malloc would be built on top of a memory allocator, which has the duty of allocating memory and assigning it to the target process one page at a time. Malloc then manages the memory allocated to the process (which could be the kernel, as it too is a 'process' in that sense, just one with special status that can't be terminated).
Post Reply