Help with paging / mem mngt

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
astrocrep

Help with paging / mem mngt

Post by astrocrep »

My main() call initmemmanger() this prepares the mem code... next I test it by two calls

Code: Select all

 char *buffer1;
 char *buffer2;

 InitMemoryManager();

 AllocPages(3, &buffer1);
 AllocPages(10, &buffer2);

 vid.printf("Address of *Buffer1: %x\n",*buffer1);
 vid.printf("Address of *Buffer2: %x\n",*buffer2);
 vid.printf("Address of &Buffer1: %x\n",&buffer1);
 vid.printf("Address of &Buffer2: %x\n",&buffer2);
Being Im still learning... I am trying various methods...

Originally I had AllocPages return a long that was assigned in a manor similar to

Code: Select all

buffer1 = (char *) AllocPages(3);
Here is the code for my AllocPages procedure

Code: Select all

long AllocPages(int size, char **s)
{
 int pages = size;
 int i=0;
 int count=0;
 int t=0;
 vid.printf("Attempting to Allocate Ram for %d pages...\n",pages);
 if (pages > TotalFreePages) return 0;           /* Chunk to Big to Allocate */
 vid.printf("Enter Search Routine...\n");
 for (i=0;i < TotalPages;i++)                    /* Loop through the Page Bit Map */
 {
  if (MemBitMap[i] == 0)                         /* If Bit map index is 0 */
   {
    for (count = 0; count < pages; count++)      /* Enter an Inner loop to find Consecutive Pages */
     if (MemBitMap[i+count] != 0) break;         /* if this page isn't free stop checking */
    vid.printf("MemBitMap[i] = 0 @ i =%d\n",i); 
    vid.printf("Count = %d\n",count);
    if (count == pages)                          /* After the loop, are the pages free equal to what we are looking for? */
    {
     for (t=0; t<=count; t++)                    /* Lets Declare this chain of Pages Allocated */
     {
      MemBitMap[i+t] = 1;                        /* Set it to Non-Zero*/ 
     }
     TotalFreePages -= count;                    /* Adjust Number of Free Pages after alloc */
     vid.printf("Return Value = %d(%x)\n",i*4096,i*4096);
     s = (char **)(i*4096);
     return 1;                                   /* Return the Location of the Starting Page */
    }
   }
 }
 return 0;
}
In theory... it works... the "return value" is exactly what I expect it to be. So my question is... why doesn't my assignment work?

Thanks for the help,
Rich P.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Help with paging / mem mngt

Post by Pype.Clicker »

and what about returning the pointer rather than having a pointer-to-pointer providen as an argument ?
astrocrep

Re:Help with paging / mem mngt

Post by astrocrep »

Nope, here is the print out I am getting from main()

The printf lines can be seen above here is the total output

Code: Select all

Welcome To The Ukon Operating System.
 - Version v0.0.1, Build [0009] Alpha.
 - Release 1.
Kernel Name: C-Nex

Please Wait While the System Loads...
Bringup Memory Manger Done!
 - Total Free Pages: 640
 - Total Ram Free: 2560 Kbytes
System Has Completed Loading...
Attempting to Allocate Ram for 3 pages...
Enter Search Routine...
MemBitMap[i] = 0 @ i =384
Count = 3
Return Value = 1572864(180000)
Attempting to Allocate Ram for 10 pages...
Enter Search Routine...
MemBitMap[i] = 0 @ i =388
Count = 10
Return Value = 1589248(184000)
Address of *Buffer1: 0
Address of *Buffer2: 0
Address of &Buffer1: 67e9c
Address of &Buffer2: 67e98
My Free ram pool starts at 1.5mb so I know that the MemMan is finding the right spot based on the index value... I just get get a char * to point the the right address. The addres is i*4096 where i is the index in the page table.

Thanks for the help,

Rich P.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Help with paging / mem mngt

Post by Candy »

astrocrep wrote:

Code: Select all

     s = (char **)(i*4096);
In theory... it works... the "return value" is exactly what I expect it to be. So my question is... why doesn't my assignment work?
It does work. It's just not the assignment you think it is :)

Learn more about pointers.

PS: You must be a student... You left out the most obvious optimization :)
astrocrep

Re:Help with paging / mem mngt

Post by astrocrep »

Student Yes... but this isn't for a class... I am a senior at the New Jersey Institute of Technology... and the only class I had remotely similar to Operating System Programming was a thoery class... and my major is even Info Tech. LOL I WISH this was for a class!!

Anyway Back to the problem... I just through together that code to make a simple memory manager.... the actually mm system only supports 4mb ram. Ive been in OS Coding for a while... spent like 4 years in the realmode world... messed with mmurtl for about 2 years on and off... and now I am here... doing the pmode thing on my own!

My Pointer skills are ruster but correct me if I am wrong but isn't the following true?

to creat a pointer...

Code: Select all

char *ptr;
to assign a value to a ptr

Code: Select all

char *ptr;
ptr = (char *) 0xb80000;
Now that should be a ptr to video memory...

What is my code doing different... Ive tried like 5 different methods to assign the value...
passing by refrence

Code: Select all

AllocPages(3,&ptr);
I tried having AllocPages Return a long

Code: Select all

ptr = (char*) AllocPages(3);
I've even tried having AllocPages return a char *

Code: Select all

ptr = AllocPages(3);
I know that the actually address value is right... it just won't assign...

Ohh and Candy... what optimization am I missing, please help :)?

Thanks
Rich P.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Help with paging / mem mngt

Post by Candy »

astrocrep wrote: My Pointer skills are ruster but correct me if I am wrong but isn't the following true?

to creat a pointer...

Code: Select all

char *ptr;
to assign a value to a ptr

Code: Select all

char *ptr;
ptr = (char *) 0xb80000;
That's true.
Now that should be a ptr to video memory...
Not counting non-vga video cards, monochrome video cards, graphical video modes, paging and segmentation, and if you rip off a zero at the end, it is.
What is my code doing different... Ive tried like 5 different methods to assign the value...
passing by refrence

Code: Select all

AllocPages(3,&ptr);
I tried having AllocPages Return a long

Code: Select all

ptr = (char*) AllocPages(3);
I've even tried having AllocPages return a char *

Code: Select all

ptr = AllocPages(3);
Ok, I'm feeling sorry for you now ;)

Code: Select all

*pointer = (char *)value;
assigns to the content of the char ** (which is your pointer). If you do what you do now, you reassign the temporary pointer-to-pointer that's passed to you on the stack, which would have helped you get to the pointer. You missed a dereference.

PS: the char *-return (or better, void *) would be the best way. Returning a long might work but might not.
Ohh and Candy... what optimization am I missing, please help :)?
If you search through a string for a stretch of zeroes, and you hit a one before you meet your quotum, starting the search again 1 bit later is NOT going to find you a stretch of zeroes quickly. Skip ahead to the 1 and search from there.

Grtz, Candy
astrocrep

Re:Help with paging / mem mngt

Post by astrocrep »

Yea I realized it after I wrote it... I just wanted to get it working before I started messing around with it.

Anyway So should like work?

Code: Select all

void *CheatAlloc()
{
 i = 100;
 return (void *) i * 4096
}

void main()
{
 char *ptr;
 ptr = (char*) CheatAlloc();
}
Now ptr should be pointing to (100*4096)

What do you think?

Thanks for the help,

Rich P.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Help with paging / mem mngt

Post by Candy »

astrocrep wrote: Anyway So should like work?

Code: Select all

void *CheatAlloc()
{
 i = 100;
 return (void *) i * 4096
}

void main()
{
 char *ptr;
 ptr = (char*) CheatAlloc();
}
if you place a semicolon at the end of the return line, probably.
astrocrep

Re:Help with paging / mem mngt

Post by astrocrep »

Welp... unfortunately... it doesn't... Could this be a problem relating to the way my page directory is setup ? or perhaps my GDT or such...

Code: Select all

void InitMemoryManager()
{
 unsigned long *page_directory   = (unsigned long *) 0x132000; //1mb + 200k Mark
 unsigned long *page_table      = (unsigned long *) 0x133000; //1mb +204k the page table comes right after the page directory

 unsigned long address=0; // holds the physical address of where a page is
 unsigned int i;

 // map the first 4MB of memory
 /*4bytes * 1024 itterations = 4k... the lenght of the page directory*/
 for(i=0; i<1024; i++)
  {
   page_table[i] = address | 3; // attribute set to: supervisor level, read/write, present(011 in binary)
   address += 4096; // 4096 = 4kb
  };

   // fill the first entry of the page directory
 page_directory[0] = (long )page_table; // attribute set to: supervisor level, read/write, present(011 in binary)
 page_directory[0] = page_directory[0] | 3;
   // fill the rest of the page directory
 for(i=1; i<1024; i++)
  {
   page_directory[i] = 0 | 2; // attribute set to: supervisor level, read/write, not present(010 in binary)
  };

 write_cr3((long)page_directory); // put that page directory address into CR3
 write_cr0(read_cr0() | 0x80000000); // set the paging bit in CR0 to 1
 TotalPages = 1024; /*Only support 4mb ram*/
 for (i=0;i<384;i++)
  MemBitMap[i] = 255; /*Allocated to the OS*/
 for (i=384;i<1024;i++)
  MemBitMap[i] = 0;   /*Page is Currently Free*/
 TotalFreePages = TotalPages - 384;
 vid.printf(" Done!\n");
}
And as of right now this is the allocpage routine

Code: Select all

void *AllocPages(int size)
{
 int pages = size;
 int i=0;
 int count=0;
 int t=0;
 vid.printf("Attempting to Allocate Ram for %d pages...\n",pages);
 if (pages > TotalFreePages) return 0;           /* Chunk to Big to Allocate */
 vid.printf("Enter Search Routine...\n");
 for (i=0;i < TotalPages;i++)                    /* Loop through the Page Bit Map */
 {
  if (MemBitMap[i] == 0)                         /* If Bit map index is 0 */
   {
    for (count = 0; count < pages; count++)      /* Enter an Inner loop to find Consecutive Pages */
     if (MemBitMap[i+count] != 0) break;         /* if this page isn't free stop checking */
    vid.printf("MemBitMap[i] = 0 @ i =%d\n",i); 
    vid.printf("Count = %d\n",count);
    if (count == pages)                          /* After the loop, are the pages free equal to what we are looking for? */
    {
     for (t=0; t<=count; t++)                    /* Lets Declare this chain of Pages Allocated */
     {
      MemBitMap[i+t] = 1;                        /* Set it to Non-Zero*/ 
     }
     TotalFreePages -= count;                    /* Adjust Number of Free Pages after alloc */
     vid.printf("Return Value = %d(%x)\n",i*4096,i*4096);
     return (char *)(i*4096);                                   /* Return the Location of the Starting Page */
    }
    i += count;
   }
 }
 return 0;
}
and in my main function...
I have:

Code: Select all

void main()
{
 char *buffer1;
 InitMemoryManager();
 buffer1 = (char *) AllocPages(3);
 }
Like I said AllocPages is finding the write page address to return... but when I

Code: Select all

vid.printf("Buffer1 = %x",&buffer1);
I always get 67e9c...

What am I doing wrong?

Thanks for the help.

Rich P.
BI lazy

Re:Help with paging / mem mngt

Post by BI lazy »

Code: Select all

uchar_t *buffer="teststrings are teststrings";
printf("content of place buffer points to: %d",buffer);
... should do what you wanna achieve.

the &-operator usually returns the adress of the variable it precedes. Pointers already hold adresses. so no need for the & operator.

stay safe & hth & ccw.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Help with paging / mem mngt

Post by Pype.Clicker »

Like I said AllocPages is finding the write page address to return... but when I

vid.printf("Buffer1 = %x",&buffer1);

I always get 67e9c...

What am I doing wrong?
The printf is wrong... you have something you just allocated, right. let's call that memory region "X". What you returned is the address of X, which has the type of a pointer. Let's call that "address of X" Y. Now you stored Y in buffer1 variable, that means the *value* of buffer1 is the *address* of X.

But what you print with &buffer1 is the *address* of the variable that holds the address of X ... follow me ?
and when you print *buffer1, you print the value of X, not its address either...

If you want to print Y, you must use buffer1, not &buffer1 or *buffer1 ...

Pick up a pencil and a blank sheet and sketch all this with boxes and arrays, it will make more sense ...
I can just show you the door ... it's up to you to open it and enter
astrocrep

Re:Help with paging / mem mngt

Post by astrocrep »

I don't think it was my printf that was wrong... just my understanding of it... the whole pointer of a pointer was quiet true. When I told printf to print out the %d of &buffer1 it was taking a pointer of a pointer... so simply printf'ingi the %d of buffer1 works perfectly... and as such I have written a nice and spiffy memory manager that uses paging (max 4mb thou).

Just wanted to say thanks to all those who help ;)

Rich P.
BI lazy

Re:Help with paging / mem mngt

Post by BI lazy »

so you've worked it out. That's good! :)
Post Reply