Page 1 of 1

Memory questions

Posted: Thu Jul 07, 2022 1:14 pm
by viruss33
So I'm working on malloc function now, I implemented PMM and VMM and I want to clarify if I understand it all correctly. So the way I see it is PMM manages physical memory, so it has some structure to manage free blocks of memory. Then we have VMM, it manages virtual adresses, so also holds a structure to check which virtual addresses are free. VMM is responsible for mapping virtual to physical memory, so when someone asks for new page, it asks PMM for new blocks of memory, then it gets next free virtual address and maps them. Then we have heap which manages bytes within pages and with the malloc function it allocates bytes per demand. At initialization time it requests some pages from VMM to work with and when it runs out of memory it asks VMM for more. So heap also has to hold some structure for determining which regions of memory are free. And malloc does not prevent the user of the api to use more memory than allocated - the user must take care to use only the allocated memory. It all seems reasonable for me, except I don't see how virtual memory solves the fragmentation problem. Lets say I allocate 10 pages, then free page 2, 4 and 6. Now we have fragmented memory. I don't have userspace in my os yet, so I'm thinking in terms of kernel memory only.

Re: Memory questions

Posted: Thu Jul 07, 2022 1:39 pm
by Octocontrabass
viruss33 wrote:I don't see how virtual memory solves the fragmentation problem.
Paging solves physical memory fragmentation, not virtual memory fragmentation. When your VMM needs to map a contiguous block of virtual memory, it can satisfy that request with any physical pages - they don't have to be in a single contiguous block.