Writing a memory manager: Difference between revisions
[unchecked revision] | [unchecked revision] |
Content deleted Content added
m Remove double periods |
|||
Line 8:
=== malloc() ===
Okay, the first step is to get out a pen and paper - I don't mean a keyboard and text editor since you can't draw diagrams (a stylus/tablet is okay with your favorite-graphics-program/OneNote/Windows-Journal)
Anyway, my point is
Remember this is your operating system! Plan it how you would like
The important thing is to start from an OVERALL view
Then write a basic structure for the header/bitmap
When you know how memory is stored, then you can go on to your allocation function. Close that text editor, we're not done with that pen yet :) Again, start with a top down view. What, exactly, does a memory allocator do?
Line 23:
# Return its address
Then break it up
How do we find a free block? (this is for a first fit method with a header, but for a basic buddy system all you need is some imagination)
Line 42:
*** Point to the next block and go back to step 2
Once you have found one, set it's used bit to used. If the space found is significantly larger than the space needed, then it is generally a good idea to split it up (cut it in half, then half again, or add another header right after the used space and update the pointers
=== free() ===
Don't program it yet! Now you need to write your free function
in your bitmap or which header is associated with the address you're given. With a header it's simply address - size of header. Set it's bit to free.
The next step is to scan through the memory and merge free blocks (e.g. if next pointer's header is free, search the next pointer's header, then the next, until you find a used block, then set the current block's next pointer to that used block, skipping over the free blocks - if any of that made sense :D).
Just keep breaking it into smaller, and smaller blocks
=== Testing ===
Only then, you can start coding. Once it's written, you should try it out in a simple sandbox environment
Don't copy someone else's code! You can copy their basic theory, but plan how you want it to work in YOUR OS on paper, and implement it YOUR way. That way, you know exactly how it works, and you can debug it and extend on it yourself.
|