To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.legosOpen lugnet.robotics.rcx.legos in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / legOS / 3536
3535  |  3537
Subject: 
My problems with adding a Best Fit algorithm for memory management to kernel/mm.c
Newsgroups: 
lugnet.robotics.rcx.legos
Date: 
Sat, 6 Dec 2003 02:35:52 GMT
Viewed: 
4135 times
  
Well, my senior project is almost due (Monday) and I am having problems with one
of the last algorithms to be added.

My senior project is adding the 3 other algorithms to the memory management
facitlity (mm.c), the algorithsm are best, worst, and next fit (if you do not
know the algorithms reply and i will explain then to you).

here is my malloc() function for Best Fit:

==========================================
void *malloc(size_t size)
{
      size_t *ptr,*next, bestSize, *mm_best_free;

      size=(size+1)>>1;

   #ifdef CONF_TM
   ENTER_KERNEL_CRITICAL_SECTION();
   #endif

      ptr=mm_first_free;
      mm_best_free=ptr;
      bestSize=0xFFFF;

      mm_best_free++;

      while(ptr>=mm_first_free)
      {
         if(*(ptr++)==MM_FREE)
         {
         #ifdef CONF_TM
         mm_try_join(ptr);
         #endif

            if(*ptr>=size)
            {
               if((*mm_best_free)>(*ptr))    <------- look here
               {
                  mm_best_free=ptr;
                  bestSize=*(ptr);
               }
            }
         }
         ptr+=(*ptr)+1;
      }
      if(bestSize!=0xFFFF)
      {
         ptr=mm_best_free;
         *(ptr-1)=(size_t)ctid;

         if((*ptr-size)>=MM_SPLIT_THRESH)
         {
            next=ptr+size+1;
            *(next++)=MM_FREE;
            *(next)=*ptr-size-MM_HEADER_SIZE;
            mm_try_join(next);

            *ptr=size;
         }
         if(ptr==mm_first_free+1)
            mm_update_first_free(ptr+*ptr+1);

      #ifdef CONF_TM
      LEAVE_KERNEL_CRITICAL_SECTION();
      #endif

         return (void*) (mm_best_free+1); //or (ptr+1)
      }
   #ifdef CONF_TM
   LEAVE_KERNEL_CRITICAL_SECTION();
   #endif

      return NULL;
   }
==========================================


Mistakenly, I forgot the put the '*' before mm_best_free on the noted line above
(marked with '<-------'), and the OS compiled and ran fine, but did not provide
the 100% correct results, that is becuase that line compared the address of the
location pointed by mm_best_free to the value in the location pointed to by ptr
(which is always true becuase the HEX address are always going to be greater
than the size requested).  When I noticed the problem I added the '*' and now
for some reason the OS freezes after it downloads (it compiles fine).

Does anyone have any idea what is going wrong? and what can be done to maybe
solve the problem?  I have reviewed the code with 2 fellow classmates (who are
also using BrickOS) and they agree with me when I say the algorithm is correctly
written.

Thanks for your help.



Message has 2 Replies:
  Re: My problems with adding a Best Fit algorithm for memory management to kernel/mm.c
 
Nevermind, I figured out the error after I posted and reviewed my code again. Thanks anyway... (21 years ago, 6-Dec-03, to lugnet.robotics.rcx.legos)
  Re: My problems with adding a Best Fit algorithm for memory management to kernel/mm.c
 
Well, I have another problem - this time with Worst Fit. It may be becuase I have been looking at this code for so much time over the past few months, but I have encountered this problem with my implementation and my mind is boggled (as well as (...) (21 years ago, 7-Dec-03, to lugnet.robotics.rcx.legos)

14 Messages in This Thread:



Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR