Memory management

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
blubber

Memory management

Post by blubber »

Hi all,

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?

Greetings,
blubber
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Memory management

Post by Brendan »

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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply