Help me on 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
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Help me on Memory management

Post by digo_rp »

Hi all, I?m trying to learn O.S Dev, but, I got a big problem, as my english isn?t enough to understand all tutorial that I have been reading from internet and the complex of Operating System, I can?t understand some times " almost all the times :D " some simples ways to program.
for now I need to try to implement a Memory management, so at this case we have to ways: bitmap and linked list, right ?

I would like to have some example " don?t need to be exactly working " just some simples examples on how it works " I mean how to code it "

could anyone help me please.
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: Help me on Memory management

Post by carbonBased »

Both of these techniques are good for page allocators. In other words, a large granularity allocater the usually allocates blocks only of one size -- the size imposed by the paging hardware (4kb (or 4mb) on x86 CPUs).

A bitmap allocate creates a bitmap that contains 1 bit for each page on the system. If the bit is set, the page is allocated, otherwise it's free.

A linked list allocator would contain a list containing pointers to all pages. When a page is allocated, it's removed from the free list (and potentially also added to an allocated list).

A faster alternative to a linked list, in this case, would be a stack. When you want to allocate a page, simply pop it off the free stack, and push it onto the allocated stack. If these two stacks grow in opposite directions they can even be made to occupy the same ammount of memory and allocating a page could be as fast as incrementing/decrementing a pointer/index into this memory stack:

Code: Select all

  -----------------------
 |                      |
 | Free Memory Pages    |
 |                      |
 |                      |
 | ---------------------| <--- boundary index/pointer
 |                      |
 | Allocated Memory Pgs |
 |                      |
 |                      |
 |                      |
 |                      |
 |                      |
 |                      |
 -----------------------
--Jeff
SpooK
Member
Member
Posts: 260
Joined: Sun Jun 18, 2006 7:21 pm

Post by SpooK »

Just to note, carbonBased gave you an "ideal" example. In a real system, memory is not necessarily freed in the same way it is allocated.

You can count on the "free stack" working as indicated, but the MM has to issue free pages back into the stack without breaking (out of order) it or making holes (like it would do in the example allocation stack).
User avatar
chase
Site Admin
Posts: 710
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

Not only that but some OSes will have different levels of a page being free. For instance, memory pages in Solaris used for file caching may get freed when they haven't been used in a while but not actually flushed from memory. This way the OS can reclaim the page instead of performing disk IO if a file needs to be read again. But other areas of memory like heap and stack regions of finished processes will never be reclaimed so their pages are more free and would get used first during memory allocation routines.

Also some OSes and hardware support using multiple memory page sizes at the same time. It can speed up applications that need to allocate lots of memory all the time(eg: Oracle).

As far as linked list vs bitmap...if you go the linked list approach you'll need to have a seperate linked list for every category of page(freed from process, freed from cache, used by process, used by cache, etc). That way your memory scanning and management code doesn't have to figure out or store how a page is being used. You're already going to have an effective bitmap anyway with you page address traslation tables.
Post Reply