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 / 3413
3412  |  3414
Subject: 
Question about mm.C in BrickOS
Newsgroups: 
lugnet.robotics.rcx.legos
Date: 
Wed, 13 Aug 2003 14:00:32 GMT
Viewed: 
3340 times
  
Hey

My Senior Preoject where I update BrickOS is slowly taking shape (I am actually
trying to do some now, but time is very limited).  This project I am editing the
Memory Managment Alg used by BrickOS.  the first one I am going to add is Next
Fit, it seems to be the easiest to add since it is the same as First Fit except
instead of starting from MM_Start when looking for free block, I need to start
at ptr++ (where ptr is the the last free block found).

It has been a while since I looked at this code or done any programming in any
langauge other that VB.NET (for work).  My OS coding background is becoming very
blurry and I dont understand the a whole block of code very well in mm.c.  Of
the block I understand some statements and conditions but not all, if possible
could someone please hlep me and explain what the block is doing?

Here is the malloc function in mm.c (the block is below after the equal signs):


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

  size=(size+1)>>1;       // only multiples of 2

#ifdef CONF_TM
  ENTER_KERNEL_CRITICAL_SECTION();
#endif
  ptr=mm_first_free;

  while(ptr>=&mm_start) {
    if(*(ptr++)==MM_FREE) {     // free block?
#ifdef CONF_TM
      mm_try_join(ptr);   // unite with later blocks
#endif
      if(*ptr>=size) {    // big enough?
        *(ptr-1)=(size_t)ctid;  // set owner

              // split this block?
        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;
        }

              // was it the first free one?
        if(ptr==mm_first_free+1)
          mm_update_first_free(ptr+*ptr+1);

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

    ptr+=(*ptr)+1;        // find next block.
  }

#ifdef CONF_TM
  LEAVE_KERNEL_CRITICAL_SECTION();
#endif
  return NULL;
}

============
1      if(*ptr>=size) {    // big enough?
2        *(ptr-1)=(size_t)ctid;  // set owner
3
4              // split this block?
5        if((*ptr-size)>=MM_SPLIT_THRESH) {
6          next=ptr+size+1;
7          *(next++)=MM_FREE;
8          *(next)=*ptr-size-MM_HEADER_SIZE;
9          mm_try_join(next);
10
11          *ptr=size;
12        }
===========

I know what the jist of the block does but I have a foggy idea of what lines 7
and 8 do.  Any help with those 2 lines and a summary of the block would be
great.  Thanks

Mike



Message has 2 Replies:
  Re: Question about mm.C in BrickOS
 
(...) <snip> (...) I've never looked at brickOS code, but here is my take: By the time you hit line 2, you've found a free block that is big enough. Line 2 sets the owner of the block. Line 5 decides if the block is oversized enough to split. Line 6 (...) (21 years ago, 13-Aug-03, to lugnet.robotics.rcx.legos)
  Re: Question about mm.C in BrickOS
 
Hi, (...) Ptr points to the length byte of the first free block (...) *(ptr-1) actually is MM_FREE and is now set to the callers id. (...) Now we can do two things: We could grab all memory we found in this block or we could allocate only the memory (...) (21 years ago, 13-Aug-03, to lugnet.robotics.rcx.legos)

7 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