Page 1 of 1

Memory management and paging question

Posted: Tue Sep 30, 2008 2:37 am
by finarfin
I'm writing the memory manager in paging environment for my os.
And i have a question.

In every tutorial that i read, the allocation returns alway a 4k page.
Now i wonder: if i have an allocation requst of 10bytes, memory menager return the pointer to a 4k page?
Or maybe there's a way to manage a page for multiple allocation?

Thanks ^_^
(And sorry for my bad english)

Re: Memory managemnt and paging question

Posted: Tue Sep 30, 2008 2:56 am
by System123
It is probably possible but each page will need to be split into smaller pages and then u will need more page tables which u will have to handle yourself as the MMU wont know what you are doing. The next problem you have is that your data wont be page aligned. The extra effort and code that will be needed just makes the whole thing very inefficient and not worth the effort. You shouldn normally be writting 10bytes in an allocation anyway

removed duplicate posts - Combuster

Re: Memory managemnt and paging question

Posted: Tue Sep 30, 2008 3:06 am
by egos
finarfin wrote:Now i wonder: if i have an allocation requst of 10bytes, memory menager return the pointer to a 4k page?
Yes, it will. You can use application-level memory manager for the thinner control of granularity.

I'm sorry for my bad english too :)

Re: Memory managemnt and paging question

Posted: Tue Sep 30, 2008 3:09 am
by xiphias
Usually the kernel allocates pages to the heap of a process, see sbrk in linux. The process then manage that new memory with malloc/free.
When malloc(10) is called and there is no free memory on the heap, it increases the heap (maybe with one or two pages) and puts that chunk of
memory into a linked list of free memory (adds a link-header to that chunk of memory). It will then split that chunk into two chunks (each with their own header),
one which is 10 bytes (or more) and return the address to that chunk (right after the header) and at the same time remove it from the free list. When free is called the chunk is inserted into the free-list again. The size of the chunk can be stored in the header of that chunk.

Re: Memory managemnt and paging question

Posted: Tue Sep 30, 2008 3:17 am
by Combuster
You are making the mistake that there are several layers of memory management in an OS at work.

The memory allocator (i.e. malloc() and friends) deal with memory on the application level. They hand out small pieces of memory to their host for them to use. When they run out of memory, they'll ask one level up to get more memory (i.e. the virtual memory manager), then cuts it again in chunks to mimimise wastage.

The virtual memory manager deals with how memory appears to the application. Since paging has a granularity of 4K, the virtual memory manager's smallest allocation for a program is of that size. It asks for a free page of physical memory, then maps it into the user's address space, giving the level below an extra multiple of 4K to work with.

Then you have the physical memory manager. This unit maintains which areas of physical memory are free, and which are occupied. It is called by the virtual memory manager when it wants to hand out memory to other applications. Since the virtual memory manager will only ask for multiples of 4K, the physical memory manager is generally written to only return multiples of 4K.

Hence if you ask the physical memory manager for 10 bytes, you're talking to the wrong guy.

Re: Memory managemnt and paging question

Posted: Tue Sep 30, 2008 5:36 am
by finarfin
Combuster wrote:You are making the mistake that there are several layers of memory management in an OS at work.

The memory allocator (i.e. malloc() and friends) deal with memory on the application level. They hand out small pieces of memory to their host for them to use. When they run out of memory, they'll ask one level up to get more memory (i.e. the virtual memory manager), then cuts it again in chunks to mimimise wastage.
Then, malloc work at app. level, and how does it works?
For example:
[*]The program asks for some space with malloc
[*]The app. memory manager seek for space into the pages assigned to it. If it found the required space it return theaddress to the requester, else it asks To the virtual memory manager for a new page (if there isn't space into other pages)
[*]The VMM gives to the malloc a page of 4k.
[*]At this point, i suppose that the malloc, take from the page the required space, and following mallocs use that page until it has space available (i think that i need another manager, in order to grant a deallocation of that space).

Is this correct?

Re: Memory managemnt and paging question

Posted: Tue Sep 30, 2008 6:47 am
by AJ
finarfin wrote: (i think that i need another manager, in order to grant a deallocation of that space).

Is this correct?
What you outline seems about right except that deallocation can be handled by the application-level memory manager. If the heap contracts enough, you can then make a call to the kernel again to return that page to the page store.

For an excellent look at memory management, see the tutorials at http://www.jamesmolloy.co.uk .

Cheers,
Adam

Re: Memory managemnt and paging question

Posted: Wed Oct 01, 2008 3:58 am
by jal
Combuster wrote:mimimise
That's such a cute word :)


JAL

Re: Memory managemnt and paging question

Posted: Wed Oct 01, 2008 5:51 am
by Combuster
Thanks :D