Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,318 people online right now. Registration is fast and FREE... Join Now!




Change of classification

 
Reply to this topicStart new topic

Change of classification

santi mavrop
27 May, 2008 - 06:27 PM
Post #1

New D.I.C Head
*

Joined: 27 May, 2008
Posts: 2


My Contributions
Hello,
with this code the list classified by address
CODE

/*===========================================================================*  
*                              free_mem                                     *  
*===========================================================================*/
PUBLIC void free_mem(base, clicks)                                              
phys_clicks base;               /* base address of block to free */            
phys_clicks clicks;             /* number of clicks to free */                  
{                                                                              
/* Return a block of free memory to the hole list.  The parameters tell where  
* the block starts in physical memory and how big it is.  The block is added  
* to the hole list.  If it is continuous with an existing hole on either end,  
* it is merged with the hole or holes.                                        
*/                                                                            
                                                                                
  register struct hole *hp, *new_ptr, *prev_ptr;                                
                                                                                
  if (clicks == 0) return;                                                      
  if ( (new_ptr = free_slots) == NIL_HOLE) panic("Hole table full", NO_NUM);    
  new_ptr->h_base = base;                                                      
  new_ptr->h_len = clicks;                                                      
  free_slots = new_ptr->h_next;                                                
  hp = hole_head;                                                              
                                                                                
  /* If this block's address is numerically less than the lowest hole currently
   * available, or if no holes are currently available, put this hole on the    
   * front of the hole list.                                                    
   */                                                                          
  if (hp == NIL_HOLE || base <= hp->h_base)
  {                                  
        /* Block to be freed goes on front of the hole list. */                
        new_ptr->h_next = hp;                                                  
        hole_head = new_ptr;                                                    
        merge(new_ptr);                                                        
        return;                                                                
  }                                                                            
                                                                                
  /* Block to be returned does not go on front of hole list. */                
  while (hp != NIL_HOLE && base > hp->h_base)
  {                                
        prev_ptr = hp;                                                          
        hp = hp->h_next;                                                        
  }                                                                            
                                                                                
  /* We found where it goes.  Insert block after 'prev_ptr'. */                
  new_ptr->h_next = prev_ptr->h_next;                                          
  prev_ptr->h_next = new_ptr;                                                  
  merge(prev_ptr);              /* sequence is 'prev_ptr', 'new_ptr', 'hp' */  
}                                                                              

/*===========================================================================*  
*                              merge                                        *  
*===========================================================================*/
PRIVATE void merge(hp)                                                          
register struct hole *hp;       /* ptr to hole to merge with its successors */  
{                                                                              
/* Check for continuous holes and merge any found.  Continuous holes can occur  
* when a block of memory is freed, and it happens to abut another hole on      
* either or both ends.  The pointer 'hp' points to the first of a series of    
* three holes that can potentially all be merged together.                    
*/                                                                            
                                                                                
  register struct hole *next_ptr;                                              
                                                                                
  /* If 'hp' points to the last hole, no merging is possible.  If it does not,  
   * try to absorb its successor into it and free the successor's table entry.  
   */                                                                          
  if ( (next_ptr = hp->h_next) == NIL_HOLE) return;                            
  if (hp->h_base + hp->h_len == next_ptr->h_base)
  {                            
        hp->h_len += next_ptr->h_len;   /* first one gets second one's mem */  
        del_slot(hp, next_ptr);                                                
  } else
  {                                                                      
        hp = next_ptr;                                                          
  }  
                                                                          
  /* If 'hp' now points to the last hole, return; otherwise, try to absorb its  
   * successor into it.                                                        
   */                                                                          
  if ( (next_ptr = hp->h_next) == NIL_HOLE) return;                            
  if (hp->h_base + hp->h_len == next_ptr->h_base)
  {                            
        hp->h_len += next_ptr->h_len;                                          
        del_slot(hp, next_ptr);                                                
  }                                                                            
}                                                      

I'm trying to change the code,so that I can classified by size.Is my effort enough?Thanks a lot
CODE

/*===========================================================================*  
*                              free_mem                                     *  
*===========================================================================*/
PUBLIC void free_mem(base, clicks)                                              
phys_clicks base;               /* base address of block to free */            
phys_clicks clicks;             /* number of clicks to free */                  
{                                                                              
/* Return a block of free memory to the hole list.  The parameters tell where  
* the block starts in physical memory and how big it is.  The block is added  
* to the hole list.  If it is continuous with an existing hole on either end,  
* it is merged with the hole or holes.                                        
*/                                                                            
                                                                                
  register struct hole *hp, *new_ptr, *prev_ptr;                                
                                                                                
  if (clicks == 0) return;                                                      
  if ( (new_ptr = free_slots) == NIL_HOLE) panic("Hole table full", NO_NUM);    
  new_ptr->h_base = base;                                                      
  new_ptr->h_len = clicks;                                                      
  free_slots = new_ptr->h_next;                                                
  hp = hole_head;                                                              
                                                                                
  /* If this block's address is numerically less than the lowest hole currently
   * available, or if no holes are currently available, put this hole on the    
   * front of the hole list.                                                    
   */                                                                          
  if (hp == NIL_HOLE || clicks <= hp->h_len)
  {                                  
        /* Block to be freed goes on front of the hole list. */                
        new_ptr->h_next = hp;                                                  
        hole_head = new_ptr;                                                    
        merge(new_ptr);                                                        
        return;                                                                
  }                                                                            
                                                                                
  /* Block to be returned does not go on front of hole list. */                
  while (hp != NIL_HOLE && clicks > hp->h_len)
  {                                
        prev_ptr = hp;                                                          
        hp = hp->h_next;                                                        
  }                                                                            
                                                                                
  /* We found where it goes.  Insert block after 'prev_ptr'. */                
  new_ptr->h_next = prev_ptr->h_next;                                          
  prev_ptr->h_next = new_ptr;                                                  
  merge(prev_ptr);              /* sequence is 'prev_ptr', 'new_ptr', 'hp' */  
}                                                                              

/*===========================================================================*  
*                              merge                                        *  
*===========================================================================*/
PRIVATE void merge(hp)                                                          
register struct hole *hp;       /* ptr to hole to merge with its successors */  
{                                                                              
/* Check for continuous holes and merge any found.  Continuous holes can occur  
* when a block of memory is freed, and it happens to abut another hole on      
* either or both ends.  The pointer 'hp' points to the first of a series of    
* three holes that can potentially all be merged together.                    
*/                                                                            
                                                                                
  register struct hole *next_ptr;                                              
                                                                                
  /* If 'hp' points to the last hole, no merging is possible.  If it does not,  
   * try to absorb its successor into it and free the successor's table entry.  
   */                                                                          
  if ( (next_ptr = hp->h_next) == NIL_HOLE) return;                            
  if (hp->h_base + hp->h_len == next_ptr->h_len)
  {                            
        hp->h_len += next_ptr->h_len;   /* first one gets second one's mem */  
        del_slot(hp, next_ptr);                                                
  }
  else
  {                                                                      
        hp = next_ptr;                                                          
  }  
                                                                          
  /* If 'hp' now points to the last hole, return; otherwise, try to absorb its  
   * successor into it.                                                        
   */                                                                          
  if ( (next_ptr = hp->h_next) == NIL_HOLE) return;                            
  if (hp->h_base + hp->h_len == next_ptr->h_len)
  {                            
        hp->h_len += next_ptr->h_len;                                          
        del_slot(hp, next_ptr);                                                
  }                                                                            
}                                                                                


This post has been edited by santi mavrop: 28 May, 2008 - 03:15 PM
User is offlineProfile CardPM
+Quote Post

santi mavrop
RE: Change Of Classification
31 May, 2008 - 07:44 PM
Post #2

New D.I.C Head
*

Joined: 27 May, 2008
Posts: 2


My Contributions
These are my changes for which i am not sure in MERGE function.Can you tell me please if are correct?Thanks

.............................................................
if (hp->h_base + hp->h_len == next_ptr->h_len)
{
hp->h_len += next_ptr->h_len; /* first one gets second one's mem */
del_slot(hp, next_ptr);
}
...............................................................

This post has been edited by santi mavrop: 31 May, 2008 - 09:27 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month