Physical Memory Manager Method

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
Warrior

Physical Memory Manager Method

Post by Warrior »

Hey I got to thinking (uh oh ;)) about my PMM and I had an idea of how to implement a seemingly easy one

- Have a "stack" of free addresses starting at the beggining of the kernel heap
- Have a variable holding the end of the kernel heap

Since my low level PMM only gives out 4KB Pages of phsyical memory it just becomes a process of checking if the pointer is past the end of the heap and if not incrementing it and returning the address. (Which will act as the pool malloc reads from when dishing out smaller portions for allocation)

Freeing is simply decrementing this counter and returning

To better state my idea, consider the following code:

Code: Select all

unsigned long *pmm_stack;
unsigned long stack_end; 

void pmm_init()
{
   *pmm_stack = 0xKernelHeapAddrHere
   stack_end = 0xEndOfKernelHeapHere

   return;
}

unsigned long pmm_alloc()  // Returns a 4KB page
{
   unsigned long retval;
   
   if (*pmm_stack > stack_end) {
      return -1; // Out of Memory
   }

   *pmm_stack += 4096 // 4KB
   
   retval = *pmm_stack;
   return retval;
}

void pmm_free()
{
   *pmm_stack -= 4096 // 4KB
   
   return;
}
Any ideas or comments?

Thanks,
Nelson
AR

Re:Physical Memory Manager Method

Post by AR »

Is that actually a physical manager, or just a virtual allocator? The obvious problem with that as a physical manager is that it can't handle gaps between RAM chunks.

About that actual code example as well, where are you initalising pmm_stack, it looks like you're using it as a NULL pointer?
Warrior

Re:Physical Memory Manager Method

Post by Warrior »

Well my thinking was, you give it somewhere to start and however far it is since the beggining is what it has allocated and every time you de-allocate just subtract from what you need, I chose this method to get rid of the need of storing data such as the size of the allocated memory and that would be the "pool" malloc() reads from and if it's allocated it returns a successful allocation (by returning the size requested in malloc())

I am faced with several other problems as far as I can see:

- Allocating 4KB for something that might be a few bytes is REALLY wasteful (maybe not, since it's free'd -- well most of the time)

- If the requested size is > than 4KB
I think I can just divide it by 4KB and find how many times I need to call pmm_alloc() and THEN return

- No idea how I would handle realloc() at this point, maybe I just need to think more

Anyhow about the unitialized pointer, yes I'll set it to something..:)

But yea, I know this looks like a vmm allocator that gives out pages from a pool, I think this can work.

About the gaps between ram chunks, I can see that problem coming up when someone free's one allocation but not another, then allocates again...I wouldn't know how to solve this in any either memory allocation techniques, suggestions?

Thanks for the input so far,
Nelson
Warrior

Re:Physical Memory Manager Method

Post by Warrior »

Sorry for the double post but something hit me:

I think I can do something like this

Code: Select all

unsigned int lastSize;

somePtr = 10;
lastSize = 10;
*somePtr += somePtr;
somePtr = 20;
lastSize = 20;
*somePtr += somePtr;
*somePtr -= lastSize;
printf("%d", somePtr);
lastSize = somePtr;
*somePtr -= lastSize;
printf("%d", somePtr);
Which would give me to store the size in the variable and print it out when needed for free();

The above should print:
[pre]
10
20
[/pre]
DruG5t0r3

Re:Physical Memory Manager Method

Post by DruG5t0r3 »

I use a stack based physical memory manager too, and what I do is that I do a for loop for every 4kb page until physical memory size and determine if it's usable or not by using the bios memory map. If it's available, I push it in the stack...if it's not, well I don't push it at all. So Whenever I need a page I just pop it out of there.
Warrior

Re:Physical Memory Manager Method

Post by Warrior »

Wouldn't setting out a pool at a physical location to act as the kernel's heap involve less checking against the bios memory map which afaik, can have holes?
DruG5t0r3

Re:Physical Memory Manager Method

Post by DruG5t0r3 »

I never heard of memory holes in the 1mb-2mb area. Thats where my stack lies, but anyway, I guess it's a risk to take. It's kinda like allocating memory without a memory manager...you gotta close your eyes and poke at some places and hope no one hits you back...
Warrior

Re:Physical Memory Manager Method

Post by Warrior »

Hmm..I think I have a rewrite of my above code

Code: Select all

unsigned long *pmm_stack;
unsigned long stack_end;
static bool first_time = true; // First time allocating? 
static unsigned int lastSize; // Last size of allocation

void pmm_init() 
{
???pmm_stack = 0; // Size allocated
???*pmm_stack = 0xKernelHeapAddrHere
???stack_end = 0xEndOfKernelHeapHere;
}

int pmm_alloc(int size) 
{
???if (*pmm_stack > stack_end) {
??????return -1; // Out of memory
???}
???
???if (first_time == true) {
??????first_time = false;
??????pmm_stack = size;
??????lastSize = size;
??????*pmm_stack += pmm_stack;
??????
??????return size;
???}

???*pmm_stack += size;
???pmm_stack = size;
???
???return size;
}

void pmm_free()
{
     pmm_stack = 0;
???*pmm_stack -= lastSize;

???return;
}
As you can see, it implements the idea I came up with earlier.
Post Reply