Memory management and paging question

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
User avatar
finarfin
Member
Member
Posts: 106
Joined: Fri Feb 23, 2007 1:41 am
Location: Italy & Ireland
Contact:

Memory management and paging question

Post 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)
Last edited by finarfin on Thu Oct 02, 2008 1:34 am, edited 1 time in total.
Elen síla lúmenn' omentielvo
- DreamOS64 - My latest attempt with osdev: https://github.com/dreamos82/Dreamos64
- Osdev Notes - My notes about osdeving! https://github.com/dreamos82/Osdev-Notes
- My old Os Project: https://github.com/dreamos82/DreamOs
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Memory managemnt and paging question

Post 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
Gizmic OS
Currently - Busy with FAT12 driver and VFS
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Memory managemnt and paging question

Post 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 :)
If you have seen bad English in my words, tell me what's wrong, please.
xiphias
Posts: 7
Joined: Mon Jul 23, 2007 5:45 am
Location: Sweden

Re: Memory managemnt and paging question

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Memory managemnt and paging question

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
finarfin
Member
Member
Posts: 106
Joined: Fri Feb 23, 2007 1:41 am
Location: Italy & Ireland
Contact:

Re: Memory managemnt and paging question

Post 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?
Elen síla lúmenn' omentielvo
- DreamOS64 - My latest attempt with osdev: https://github.com/dreamos82/Dreamos64
- Osdev Notes - My notes about osdeving! https://github.com/dreamos82/Osdev-Notes
- My old Os Project: https://github.com/dreamos82/DreamOs
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Memory managemnt and paging question

Post 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
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Memory managemnt and paging question

Post by jal »

Combuster wrote:mimimise
That's such a cute word :)


JAL
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Memory managemnt and paging question

Post by Combuster »

Thanks :D
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply