Page 1 of 1

what is the kernel heap?

Posted: Thu Jan 15, 2009 1:37 pm
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?

Re: what is the kernel heap?

Posted: Thu Jan 15, 2009 2:03 pm
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.

Re: what is the kernel heap?

Posted: Thu Jan 15, 2009 2:06 pm
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?

Re: what is the kernel heap?

Posted: Thu Jan 15, 2009 3:23 pm
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).