Chinaunix首页 | 论坛 | 博客
  • 博客访问: 661531
  • 博文数量: 156
  • 博客积分: 4833
  • 博客等级: 上校
  • 技术积分: 1554
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-21 19:36
文章分类

全部博文(156)

文章存档

2016年(2)

2013年(1)

2012年(13)

2011年(30)

2010年(46)

2009年(29)

2008年(23)

2007年(12)

分类: 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;

}


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

上一篇:优先级

下一篇:linux内核中socket的实现

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