#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;
}
阅读(1277) | 评论(0) | 转发(0) |