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 / 3542
3541  |  3543
Subject: 
Re: 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 18:47:40 GMT
Viewed: 
3979 times
  
In lugnet.robotics.rcx.legos, Michael Martelli wrote:

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++; //this must point to the size portion of the header

      while(ptr>=mm_first_free) //&mm_start gives the same results
      {
         if(*(ptr++)==MM_FREE)
         {
         #ifdef CONF_TM
         mm_try_join(ptr);
         #endif

            if(*ptr>=size)
            {
               if(bestSize>(*ptr)) //THIS LINE!!!!
               {
                  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;
   }

BTW, you can delete these two lines of intialization (i.e. lines 12 & 15 in the
above function) as the intialized value gets clobbered on finding the first
suitable sized block:

12)  mm_best_free=ptr;
15)  mm_best_free++; //this must point to the size portion of the

If you never find a suitable block, mm_best_free isn't used for anything either.

Mark



Message has 1 Reply:
  Re: My problems with adding a Best Fit algorithm for memory management to kernel/mm.c
 
(...) Yeah, very true. Those lines where in there becuase of the error I originally had in my code, but since my code changed (and is correct) those lines are not needed). Thanks Mike (21 years ago, 6-Dec-03, to lugnet.robotics.rcx.legos)

Message is in Reply To:
  Re: My problems with adding a Best Fit algorithm for memory management to kernel/mm.c
 
(...) Sure thing sorry, the problem was that I had the wrong variable in one of the if statement, I copied it over wrong from my hand-written notes I made of the Alg ===...=== void *malloc(size_t size) { size_t *ptr,*next, bestSize, *mm_best_free; (...) (21 years ago, 6-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

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

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