I divide by 4 because the block size is 4 KB .MichaelFarthing wrote:Well I'm tempted to say "both".Busybox wrote:Should i divide by 4 or 4096?
mem = RAM size in KB.Code: Select all
bitmap_size = ((mem - kernel_end) / 4096) / 8;
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'.
Memory manager
Re: Memory manager
- MichaelFarthing
- Member
- Posts: 167
- Joined: Thu Mar 10, 2016 7:35 am
- Location: Lancaster, England, Disunited Kingdom
Re: Memory manager
Ah Davy boy I just canna do it! [Robert Louis Stevenson - Kidnapped (from memory -may not be spot on)]Busybox wrote: I divide by 4 because the block size is 4 KB .
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.
Re: Memory manager
Thanks! I worked it out and understood:MichaelFarthing wrote:Ah Davy boy I just canna do it! [Robert Louis Stevenson - Kidnapped (from memory -may not be spot on)]Busybox wrote: I divide by 4 because the block size is 4 KB .
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.
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?
Re: Memory manager
Nah, not.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; }
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.
- MichaelFarthing
- Member
- Posts: 167
- Joined: Thu Mar 10, 2016 7:35 am
- Location: Lancaster, England, Disunited Kingdom
Re: Memory manager
Well when you coded it did it work?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?
[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.
Re: Memory manager
That means, the correct calculation should look like this:MichaelFarthing wrote:Well when you coded it did it work?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?
[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.
((mem - kernel_end) / 4 / 4) / 8
- MichaelFarthing
- Member
- Posts: 167
- Joined: Thu Mar 10, 2016 7:35 am
- Location: Lancaster, England, Disunited Kingdom
Re: Memory manager
Let us know what happens when you try running that on a machine.Busybox wrote:
That means, the correct calculation should look like this:
((mem - kernel_end) / 4 / 4) / 8
Re: Memory manager
Nothing unusual.MichaelFarthing wrote:Let us know what happens when you try running that on a machine.Busybox wrote:
That means, the correct calculation should look like this:
((mem - kernel_end) / 4 / 4) / 8