Page 1 of 1

Stack based memory manager

Posted: Thu Sep 10, 2009 11:51 am
by skwee
Hi, I had a chat in IRC about Physical memory manager. For now my PMM implemented as bitmap of N blocks, where each block represents a region of 4096bytes of memory.
The suggestion on IRC was to implement a stack based allocation. It takes more RAM but faster in looking for free blocks.
I would like to get more information on this topic. If someone can reference me to a tutorial or some design document, or even give me hint about what to search in google (cause searches like "stack based pmm", "stack based memory allocation/manager" dont give enough relevant results), it will be awesome!

Thanks a lot!

Re: Stack based memory manager

Posted: Thu Sep 10, 2009 12:08 pm
by Mark139
Not sure about tutorials or example code. But, my pmm is stack based and it may need to use more memory when little memory is needed as it hands out blocks the pmm uses less.

If you are using 4k pages then one page can hold 1024 32bit references to 4m of memory. As you hand out blocks you can reduce the blocks you are using to manage the stack. When you are down to the last 4k of memory left you can hand it over. The pmm is now managing zero ram so it's stack can be zero in size. When the first 4k block is returned that can become your first 1024 entries!.

I really like the elegance of this design.

Re: Stack based memory manager

Posted: Thu Sep 10, 2009 12:32 pm
by gravaera
Were you REALLY expecting random documentation to be simply floating around google about writing a Paged Memory Manager? Honestly: are you aware that the enterprise you are attempting is NOT commonly done, and there WILL NOT be abundant documentation? Have you THOUGHT about what you're doing?

This question, firstly is in the wrong section. It should be in Design and Theory. Second, it shows that you do not think. Why on earth would you search on google for "Stack based PMM"? If you were thinking, would you not lookup "stack algorithm"? Or "Stack implementation"?

More importantly, I don't think you should be trying to do an OS if you don't even understand something like a stack. Here: let me show you how to google for THE RIGHT information, and how to reason things out, such that you may not even have to google!!

"I have to implement a Memory Manager for my operating system. Oh boy. I have no idea how! Okay, I'll go on IRC and harass people instead of trying to sit down with a pen and paper and sketch it out."

Several hours later. "Okay. The folks on the IRC suggested that I should use a stack to menage my memory. The first thing I need to consider is that a normal stack has considerable overhead. I'd have to have pointers to the next item in the list. This is inconvenient. I should use an array of contiguous 32 bit integers to store the stack items.

This brings up the first question: How can I have a dynamically growing and shrinking stack, without a Dynamic Memory Allocator? Problem.

Okay: Maybe I can make an assumption: Assume that I can use the first 127 pages (minus the 1st page with the IVT) in Low Mem (you should be able to, theoretically), to store a mini-stack, and just push the addresses of the first, say, 16 MB worth of pages onto this temporary stack.

Now I can write some functions to manipulate this insufficient stack which I will use only at boot time, until Dynamic Mem is set up."

About two hours later (If so many), "Done writing some stack manipulation functions. The temporary stack is of a fixed size, and I use a pointer to the last item, and an algorithm similar to the CPU stack to manage it.

Now, In order to know how large the REAL stack should be, I need to find out how much memory is on the system. Thank god I can check the wiki for this. I've decided to use the Multiboot Data Memory Map for now, until I can do better. The first thing I need to do is parse the multiboot data, and find out how many pages are in memory. I should store this total in a variable and keep it safe.

Now I need to find out how large the stack CAN grow, and find out therefore, how many physical pages I need to reserve for the stack. Got it. So now I can use my nice temporary algorithms and my temporary stack, and allocate some space for my REAL stack.

Problem: I need to make sure that the physical pages that I use for the REAL stack don't go into the REAL stack, so that they won't be allocated by the memory manager. This should be simple enough.

Now, since I have used my temporary algorithm to allocate the exacly number of pages necessary for my REAL stack, I can start just filling up the real stack. When the real stack is populated, I now have a physical Memory Manager base. WOW. That wasn't so hard, was it?"

Amazing =D> You wouldn't even have had to check google.

Unless of course your REAL problem REALLY is that you don't know what a stack is...in which case...You do not have the required knowledge to build an OS. Simple. In either case, this question SHOULD NOT have been asked.

Re: Stack based memory manager

Posted: Thu Sep 10, 2009 12:53 pm
by skwee
Mark139
Thanks for sharing your opinion.

gravaera
wow I dont even know where to start....
Were you REALLY expecting random documentation to be simply floating around google about writing a Paged Memory Manager?
Actually I do. As documentation on how to write bitmap based memory manager exists in many exemplars.
Honestly: are you aware that the enterprise you are attempting is NOT commonly done, and there WILL NOT be abundant documentation?
See my comment to previous quote.
If you were thinking, would you not lookup "stack algorithm"? Or "Stack implementation"?
No I would not, cause I know how to implement stack.

I think that you just don't understand the difference between implementing stack and using it in different purposes..
When one wants to implement a big numbers using stack would he need to google "How to implement stack?" Wont it be more smarter to search something like "how it implement big numbers using stack?" (A very primitive example, cause I cant think of other right now).
Okay, I'll go on IRC and harass people...
I didn't harass anybody, its just came out of a conversation.
..instead of trying to sit down with a pen and paper and sketch it out.
To sketch what? stack?
Simple. In either case, this question SHOULD NOT have been asked.
I'm happy that you know everything with just sketching stuff out, but I don't understand the concept behind stack based memory manager, so I asked it here in hope for somebody to help me. If you are not in mood to help you don't have too.

Re: Stack based memory manager

Posted: Thu Sep 10, 2009 12:59 pm
by gravaera
Edit: Nvm. I already answered his question too, anyway. Look: Good luck in the rest of your OS if implementing a stack is giving you so much trouble.

Re: Stack based memory manager

Posted: Thu Sep 10, 2009 1:03 pm
by skwee
gravaera wrote:Edit: Nvm. I already answered his question too, anyway. Look: Good luck in the rest of your OS if implementing a stack is giving you so much trouble.
Thank you!