Hi,
blubber wrote:
I just started coding my own os (ofcourse), i've got paging working, so I wanted to start at a kernel level memory manager. But I was wondering two things:
1) What shoudl a (k)malloc function return? (The linear address off the page to use)?
2) How does the user level c library interface with the memory manager?
Normally(?) there's several different "memory managers":
- a "physical memory manager", which keeps track of available physical memory pages.
- a "linear memory manager", which maps physical memory pages into linear memory, handles swapping pages to/from disk, memory mapped files, allocation on demand, etc.
- "kmalloc", which manages linear address ranges for the kernel
- "malloc", which manages linear address ranges for user space
1) "kmalloc" and "malloc" would return the linear address of the allocated address range (or null if there isn't enough space left). In some OS's kmalloc and malloc also allocate physical pages and map them into the allocate address range, but in others this is done by the page fault handler the first time each page in the allocated address range is accessed (allocation on demand). In this case "kmalloc" and "malloc" do not allocate memory, but rather they allocate areas in the linear address space.
2) The user level C library interface (malloc) might call a kernel API function to map whole pages into the linear address space, or it might not need to call the kernel API at all (if the entire user space is using allocation on demand). Alternatively it might call a kernel API function to enable allocation on demand for a range of pages.
Please note that there's lots of different ways of doing each type of "memory management"...
Cheers,
Brendan