Free addresses stack use

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
Xcp
Posts: 19
Joined: Wed Oct 17, 2018 9:56 am

Free addresses stack use

Post by Xcp »

Once I pushed all the free addresses to the stack, I wondered how to push a just free address. That is, when I want to push an address do I have to test if it is usable from the memory map? I think not. I just have to increment and decrement the stack pointer to manage it because of the addresses which remains effectively there. Then to scan the stack I pass from the stack pointer pStack to the end of the kernel, is it OK?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Free addresses stack use

Post by AJ »

Hi,

More information needed.

From what I gather, you are building a physical page allocation stack. You have presumable found enough space for it in the system memory map so that there is enough space for it even if every page is free.

Given the above, I'm not quite sure what the question is, but you do not need to check the system memory map each time you free a page - just when you build the initial stack.

Cheers,
Adam
Xcp
Posts: 19
Joined: Wed Oct 17, 2018 9:56 am

Re: Free addresses stack use

Post by Xcp »

Yes. I've pushed the free usable addresses came from the GRUB memory map to the stack at the end of the kernel (knew it from the "flag" end set in the linker), but then a doubt has came up to me. I wondered how to manage it.

To pop an address from the stack I should decrement the stack pointer (in my case *pStack), but I'm not physically popping off it and overwriting the memory location, so it should be possible to re-push it simply by incrementing pStack. Am I right? I intend, when I pop an address I theoretically have to cancel the content and then decrement the stack pointer, but so when I push an address I don't know whether an address is usable or not. Thus I thought that I could not cancel the popped off address and, when I want to push an address, I don't push a specific one but I push the last popped address. But the problem came up anyway when I want to allocate a specific address, how should I check if it is usable or not?

Another thing is to scan the stack to test if an address is free (to allocate a specific address for example). should it be enough to scan from pStack down to the end of the kernel (where the stack has started)?
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Free addresses stack use

Post by JAAman »

Xcp wrote: To pop an address from the stack I should decrement the stack pointer (in my case *pStack), but I'm not physically popping off it and overwriting the memory location, so it should be possible to re-push it simply by incrementing pStack. Am I right?
wrong

physical addresses are rarely ever freed in the same (reverse) order they are allocated in, so yes, you must place the value back onto the stack (overwriting whatever was at that address before -- because what was there before was probably a different address)
I intend, when I pop an address I theoretically have to cancel the content and then decrement the stack pointer, but so when I push an address I don't know whether an address is usable or not. Thus I thought that I could not cancel the popped off address and, when I want to push an address, I don't push a specific one but I push the last popped address. But the problem came up anyway when I want to allocate a specific address, how should I check if it is usable or not?
if you are freeing non-existant pages, then you have a very serious problem in your code, and this check will not help you

basically, you will never free a page unless you were using that page, and if you were using that page, then you know it does exist, therefore, there is no need to check the memory map to see if that page really exists, because if it didn't exist you wouldn't be freeing it (and if you were freeing pages incorrectly, checking the memory map wouldn't help, because you could be freeing the wrong address but still a valid address)
Post Reply