Memory manager

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.
Busybox
Posts: 8
Joined: Sat Sep 17, 2016 11:14 pm

Re: Memory manager

Post by Busybox »

MichaelFarthing wrote:
Busybox wrote:Should i divide by 4 or 4096?
mem = RAM size in KB.

Code: Select all

bitmap_size = ((mem - kernel_end) / 4096) / 8;
Well I'm tempted to say "both".
There was some ambiguity in the original that you are quoting. (mem-kernel_end) was intended to be measured in pages not bytes.

If you have any understanding of what is going on you should be able to work it out. It should also be fairly obvious that your bitmap size is not going to be a quarter of your available memory

That divide by 4 though? What was that for? Well, I'm sorry, I am not going to give you the answer. Look at the code you have been given. Look at the rival code also suggested by someone else (this gives a clue). Work out what they are both doing and what the differences are.
Blood sweat and tears are needed.

Am I being cruel? As my parents said to me when I was a small childe, 'We're being cruel to be kind'.
I divide by 4 because the block size is 4 KB .
User avatar
MichaelFarthing
Member
Member
Posts: 167
Joined: Thu Mar 10, 2016 7:35 am
Location: Lancaster, England, Disunited Kingdom

Re: Memory manager

Post by MichaelFarthing »

Busybox wrote: I divide by 4 because the block size is 4 KB .
Ah Davy boy I just canna do it! [Robert Louis Stevenson - Kidnapped (from memory -may not be spot on)]

You will see that in the example quoted you also divided by 8. That was because each bit represented 1 page and there were 8 bits per byte
It was also necessary to divide by 4 because the coding did 4 bytes at a time. This was because 4 bytes are easily put into one CPU register.
I shouldn't have told you: you needed to work it out..
Look, I regularly (like every other day) battle over something for hours at a time without even thinking of asking for help. That's why I can now do things that a year ago I could not.
Busybox
Posts: 8
Joined: Sat Sep 17, 2016 11:14 pm

Re: Memory manager

Post by Busybox »

MichaelFarthing wrote:
Busybox wrote: I divide by 4 because the block size is 4 KB .
Ah Davy boy I just canna do it! [Robert Louis Stevenson - Kidnapped (from memory -may not be spot on)]

You will see that in the example quoted you also divided by 8. That was because each bit represented 1 page and there were 8 bits per byte
It was also necessary to divide by 4 because the coding did 4 bytes at a time. This was because 4 bytes are easily put into one CPU register.
I shouldn't have told you: you needed to work it out..
Look, I regularly (like every other day) battle over something for hours at a time without even thinking of asking for help. That's why I can now do things that a year ago I could not.
Thanks! I worked it out and understood:
If the memory is given in KB:
((mem - kernel_end) // Calculate free memory
/ 4) // Get size in pages (1 page = 4 KB)
/ 8; // 1 bit represent a page in bitmap array.
else If the memory is given in bytes:
((mem - kernel_end)
/ 4096) // 1 page = 4 KB(4096 bytes)
/ 8;

In both cases bmp_sizes are the same.
Do I understand correctly?
User avatar
Ycep
Member
Member
Posts: 401
Joined: Mon Dec 28, 2015 11:11 am

Re: Memory manager

Post by Ycep »

Busybox wrote:Thanks for the theory!

Can i do something like this?

Code: Select all

uint32_t* pmm_allocn(uint32_t size)
{
	if(!size) return 0;
	uint32_t* first = pmm_alloc();
	if(size > 1)
		for(; size > 1; size--)
			pmm_alloc();

	return first;
}
Nah, not.
When you allocate more blocks, you must be sure that you allocated that blocks in row.
If you, for example, want to allocate three blocks, you need to allocate three bits in bitmap in a row, so you fill it from this:
0101 0101 0100 0111 (Before)
To this:
0101 0101 0111 1111 (Correct)
Your code would probably do this:
1111 1101 0100 0111 (Wrong)
You need to search for three zeros in row for allocating three blocks, if you want to alloc 3 blocks.
User avatar
MichaelFarthing
Member
Member
Posts: 167
Joined: Thu Mar 10, 2016 7:35 am
Location: Lancaster, England, Disunited Kingdom

Re: Memory manager

Post by MichaelFarthing »

Busybox wrote:
Thanks! I worked it out and understood:
If the memory is given in KB:
((mem - kernel_end) // Calculate free memory
/ 4) // Get size in pages (1 page = 4 KB)
/ 8; // 1 bit represent a page in bitmap array.
else If the memory is given in bytes:
((mem - kernel_end)
/ 4096) // 1 page = 4 KB(4096 bytes)
/ 8;

In both cases bmp_sizes are the same.
Do I understand correctly?
Well when you coded it did it work?
[Of course, you haven't coded it]

May I draw your attention to this sentence:

"It was also necessary to divide by 4 because the coding did 4 bytes at a time. This was because 4 bytes are easily put into one CPU register."
To be quite explicit, the 4 has nothing to do with 4096 being 4 * 1024.
Did you understand this sentence? If not,try to.
Busybox
Posts: 8
Joined: Sat Sep 17, 2016 11:14 pm

Re: Memory manager

Post by Busybox »

MichaelFarthing wrote:
Busybox wrote:
Thanks! I worked it out and understood:
If the memory is given in KB:
((mem - kernel_end) // Calculate free memory
/ 4) // Get size in pages (1 page = 4 KB)
/ 8; // 1 bit represent a page in bitmap array.
else If the memory is given in bytes:
((mem - kernel_end)
/ 4096) // 1 page = 4 KB(4096 bytes)
/ 8;

In both cases bmp_sizes are the same.
Do I understand correctly?
Well when you coded it did it work?
[Of course, you haven't coded it]

May I draw your attention to this sentence:

"It was also necessary to divide by 4 because the coding did 4 bytes at a time. This was because 4 bytes are easily put into one CPU register."
To be quite explicit, the 4 has nothing to do with 4096 being 4 * 1024.
Did you understand this sentence? If not,try to.
That means, the correct calculation should look like this:
((mem - kernel_end) / 4 / 4) / 8
User avatar
MichaelFarthing
Member
Member
Posts: 167
Joined: Thu Mar 10, 2016 7:35 am
Location: Lancaster, England, Disunited Kingdom

Re: Memory manager

Post by MichaelFarthing »

Busybox wrote:
That means, the correct calculation should look like this:
((mem - kernel_end) / 4 / 4) / 8
Let us know what happens when you try running that on a machine.
Busybox
Posts: 8
Joined: Sat Sep 17, 2016 11:14 pm

Re: Memory manager

Post by Busybox »

MichaelFarthing wrote:
Busybox wrote:
That means, the correct calculation should look like this:
((mem - kernel_end) / 4 / 4) / 8
Let us know what happens when you try running that on a machine.
Nothing unusual.
Image
Post Reply