Chinaunix首页 | 论坛 | 博客
  • 博客访问: 179974
  • 博文数量: 108
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1065
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-29 08:56
文章分类

全部博文(108)

文章存档

2011年(11)

2010年(46)

2009年(29)

2008年(22)

我的朋友

分类: LINUX

2009-10-21 21:31:55

#include

#include "mem_alloc.h"

#define HASHSIZE 32
#define dbprintf  printf
#define logprintf printf
#define DOWN_LOCK
#define UP_LOCK



struct mem_node
{
  void *p;
  unsigned int size;
  struct mem_node* p_mem_next;
};//3x4


struct pid_node
{
 unsigned long int pid;
 struct mem_node *p_mem_node;
 struct pid_node *p_pid_next;
};//3x4



static struct pid_node* pidarray[HASHSIZE];


void insert_memory_block(unsigned long int pid,void *p,unsigned int size)
{

   unsigned int hash;  
   struct pid_node *pNode;
   struct pid_node *listpNode ;  
   struct mem_node *listpmem;
   struct mem_node *pmem = malloc(sizeof(struct mem_node));

   DOWN_LOCK;

   dbprintf("insert pid=%d, p=%p, size=%d\n",pid,p,size);

    hash = pid%HASHSIZE;

   pmem->p = p;
     pmem->size = size;
     pmem->p_mem_next = NULL;

   if(pidarray[hash] == NULL)
   {
     pNode = malloc(sizeof(struct pid_node));             
     pNode->pid =pid;
     pNode->p_mem_node = pmem;
     pNode->p_pid_next = NULL;
       pidarray[hash] = pNode;

       goto Finish;
     
   }

   listpNode = pidarray[hash];

    //maybe both  condition OK
     while( (listpNode->p_pid_next!=NULL) && ( listpNode->pid !=pid ))
       {
          listpNode = listpNode->p_pid_next;
       }
   
    //already have pid node
    if(listpNode->pid == pid)
  {
          listpmem = listpNode->p_mem_node;
           if(listpmem  == NULL)//node without mem
           {
               listpNode->p_mem_node = pmem;
                 goto Finish;
           }

           while(listpmem->p_mem_next != NULL)
       {
                listpmem = listpmem->p_mem_next;
          }

           listpmem->p_mem_next = pmem;
           goto Finish;
          
   }
    else//new pid node
    {

     pNode = malloc(sizeof(struct pid_node));             
     pNode->pid =pid;
     pNode->p_mem_node = pmem;
     pNode->p_pid_next = NULL;
     listpNode->p_pid_next = pNode;

       goto Finish;
   
   }
  
Finish:
   
      UP_LOCK;     
     return;
}



void delete_memory_block(unsigned long int pid,void *p)
{
   unsigned int hash;
   struct pid_node *listpNode ;  
   struct mem_node *listpmem;
   struct mem_node *prelistpmem;

   DOWN_LOCK;
  
   dbprintf("delete pid=%d, p=%p \n",pid,p);
  
   hash = pid%HASHSIZE;

   listpNode = pidarray[hash];

   while( (listpNode!=NULL) && ( listpNode->pid !=pid ))
   {
          listpNode = listpNode->p_pid_next;
   }

  if(listpNode == NULL)
  {
    //error!!!
      dbprintf("we donot record pid=%d\n",pid);
        goto Finish;    
  }
  //should == pid
  listpmem = listpNode->p_mem_node;
  prelistpmem = listpmem;//important both to head
 
  while( (listpmem != NULL)&& (listpmem->p != p))
  {
     prelistpmem = listpmem;
     listpmem = listpmem->p_mem_next;
     
  }
  
  if(listpmem == NULL)
  {
        //error!!!
        dbprintf("we donot record pid=%d, p=%p\n",pid,p);
          goto Finish;
    
  }

  if(listpmem == listpNode->p_mem_node)
  {
    listpNode->p_mem_node = listpmem->p_mem_next;
 
  }
  else
  {
    prelistpmem->p_mem_next = listpmem->p_mem_next;
   
  }

   free(listpmem);
  
Finish:  
   
    UP_LOCK;
    return;
}



void travel_pid_memory(void)
{
   unsigned int pid_list; 
   struct pid_node *listpNode;  
   struct mem_node *listpmem;
   unsigned int mem_size;

   DOWN_LOCK;
  
   logprintf("\nMEM static OUT\n");
   for(pid_list=0;pid_list   {
      listpNode = pidarray[pid_list];
     
       while(listpNode != NULL)
       {
           listpmem = listpNode->p_mem_node;
               mem_size = 0;
      
           while(listpmem != NULL)
           {
             mem_size += listpmem->size;
                 listpmem = listpmem->p_mem_next;
           }
      
         logprintf("pid= %d,mem= %d;  ",listpNode->pid,mem_size);
             listpNode = listpNode->p_pid_next;

       }
    
  }

     logprintf("\nMEM static Finish\n");
    
     UP_LOCK;

}


阅读(536) | 评论(0) | 转发(0) |
0

上一篇:qsort

下一篇:ucontext

给主人留下些什么吧!~~