- // DEBUG2.cpp : Defines the entry point for the console application.
- //
- // data.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "stdio.h"
- #include "stdlib.h"
- #define HASHSIZE 6999999
- #define DATASIZE 20
- #define I_USED 1
- #define I_UNUSED 0
- typedef unsigned int __u32;
- typedef unsigned short __u16;
- typedef unsigned char __u8;
- int DATACOUNT=DATASIZE;
- int HASHCOUNT=0;
- struct _data
- {
- __u32 saddr;
- __u32 ext_isn;
- __u32 int_isn;
- __u16 sport;
- __u8 int_ok;
- __u8 three_shake;
- __u8 fin_count;
- char flag;
- __u32 head;
- struct _data *next;
- struct _data *prev;
- struct _data *cl_next;
- struct _data *cl_prev;
- }data_list[DATASIZE],*data_hash[HASHSIZE];
- struct _data *data_head;
- struct _data *data_tail;
- //cut cl_prev and cl_next form data_hash
- void del_coll(struct _data *pcb_p,__u32 index){
- if(data_hash[index]!=NULL){
- if(pcb_p->cl_prev==NULL && pcb_p->cl_next==NULL){
- data_hash[index]=NULL;
- }else if(pcb_p->cl_prev==NULL && pcb_p->cl_next!=NULL){
- (pcb_p->cl_next)->cl_prev=NULL;
- data_hash[index]=pcb_p->cl_next;
- pcb_p->cl_next=NULL;
- }else if(pcb_p->cl_prev!=NULL && pcb_p->cl_next!=NULL){
- (pcb_p->cl_next)->cl_prev=pcb_p->cl_prev;
- (pcb_p->cl_prev)->cl_next=pcb_p->cl_next;
- pcb_p->cl_next=NULL;
- pcb_p->cl_prev=NULL;
- }else if(pcb_p->cl_prev!=NULL && pcb_p->cl_next==NULL){
- (pcb_p->cl_prev)->cl_next=NULL;
- pcb_p->cl_prev=NULL;
- }
- }
- }
- int alloc_data_inode(__u16 sport, __u32 saddr ,__u32 ext_isn,__u32 index)
- {
- struct _data *pcb_p;
- /*CUT DATA_TAIL OF DATA_LINK*/
- if(data_tail->flag&I_USED){
- del_coll(data_tail,index);
- }
- pcb_p=data_tail;
- (data_tail->prev)->next=NULL;
- data_tail=data_tail->prev;
- pcb_p->prev=NULL;
- pcb_p->next=data_head;
- data_head->prev=pcb_p;
- data_head=pcb_p;
- pcb_p->prev=NULL;
- /*add to the hashtable's collision link table*/
- if( data_hash[index] != NULL )
- {
- data_hash[index]->cl_prev=pcb_p;
- pcb_p->cl_next = data_hash[index];
- pcb_p->cl_prev=NULL;
- data_hash[index]=pcb_p;
- HASHCOUNT++;
- }
- else if(data_hash[index]==NULL)
- {
- data_hash[index]=pcb_p;
- pcb_p->cl_next=NULL;
- pcb_p->cl_prev=NULL;
- HASHCOUNT++;
- }
- /*init the data into the pcb_p;*/
- pcb_p->saddr=saddr;
- pcb_p->sport=sport;
- pcb_p->ext_isn=ext_isn;
- pcb_p->three_shake=1;
- pcb_p->head=index;
- pcb_p->flag=I_USED;
- return 0;
- }
- int detach_pcb(struct _data *pcb_p,__u32 index,__u32 saddr,__u16 sport)
- {
- while(data_hash[index]!=NULL){
- if( pcb_p->saddr==saddr && pcb_p->sport==sport )
- {
- printf("%p:prev=%p,next=%p,cl_prev=%p,cl_next=%p[saddr=%d,sport=%d]/n",pcb_p,pcb_p->prev,pcb_p->next,pcb_p->cl_prev,pcb_p->cl_next,pcb_p->saddr,pcb_p->sport);
- del_coll(pcb_p,index);
- if(pcb_p->next!=NULL && pcb_p->prev==NULL){
- (pcb_p->next)->prev=NULL;
- data_head=pcb_p->next;
- pcb_p->next=NULL;
- pcb_p->prev=data_tail;
- data_tail->next=pcb_p;
- data_tail=pcb_p;
- pcb_p->next=NULL;
- }else if(pcb_p->next!=NULL && pcb_p->prev!=NULL){
- (pcb_p->next)->prev=pcb_p->prev;
- (pcb_p->prev)->next=pcb_p->next;
- pcb_p->next=pcb_p->prev=NULL;
- pcb_p->prev=data_tail;
- data_tail->next=pcb_p;
- data_tail=pcb_p;
- pcb_p->next=NULL;
- }
- pcb_p->saddr=0;
- pcb_p->sport=0;
- pcb_p->three_shake=0;
- pcb_p->ext_isn=0;
- pcb_p->fin_count=0;
- pcb_p->int_isn=0;
- pcb_p->int_ok=0;
- pcb_p->flag=I_UNUSED;
- pcb_p->head=0;
- return 0; //detach sucessful!
- }
- else{
- if(pcb_p->cl_next!=NULL)
- pcb_p=pcb_p->cl_next;
- else{
- return -1;
- }
- }
- }
- if(data_hash[index]==NULL){
- printf("out of delete size!/n");
- return -1;
- }
- return -1;
- }
- struct _data * lookup_pcb(__u32 saddr,__u16 sport,__u32 index)
- {
- struct _data *pcb_p;
- if( data_hash[index] != NULL )
- {
- pcb_p = data_hash[index];
- while( pcb_p != NULL )
- {
- if( pcb_p->saddr==saddr && pcb_p->sport==sport )
- {
- return pcb_p;
- }
- else
- {
- pcb_p = pcb_p->cl_next;
- }
- }
- }
- return NULL;
- }
- void init_data(int i)
- {
- data_head=&data_list[0];
- data_tail=&data_list[DATASIZE-1];
- for(i=0;i<DATASIZE;i++)
- {
- if(i == 0)
- {
- data_list[i].prev = NULL;
- data_list[i].next = &data_list[1];
- }
- else if(i == (DATASIZE-1))
- {
- data_list[i].next= NULL;
- data_list[i].prev=&data_list[i-1];
- }
- else
- {
- data_list[i].next= &data_list[i+1];
- data_list[i].prev=&data_list[i-1];
- }
- data_list[i].cl_next=NULL;
- data_list[i].cl_prev=NULL;
- data_list[i].int_ok=0;
- data_list[i].three_shake=0;
- data_list[i].sport=0;
- data_list[i].saddr=0;
- data_list[i].ext_isn=0;
- data_list[i].int_isn=0;
- data_list[i].fin_count=0;
- data_list[i].head=0;
- data_list[i].flag=I_UNUSED;
- }
- for(i=0;i<HASHSIZE;i++)
- data_hash[i]=NULL;
- }
- int main(int argc, char* argv[])
- {
- int i;
- int j=0;
- int x=0;
- struct _data *pPtr;
- __u32 saddr;
- __u16 sport;
- init_data(1);
- printf("data_init sucessful/n");
- //ALLOC
- for(i=0;i<99999;i++)
- {
- alloc_data_inode(123+i,987654+i,98765432+i,123456);
- printf("i=%d/n",i);
- }
- pPtr=data_hash[123456];
- //分配
- while(pPtr!=NULL)
- { printf("<<%d>>",x);
- printf("%p:prev=%p,next=%p,cl_prev=%p,cl_next=%p[saddr=%d,sport=%d]/n",pPtr,pPtr->prev,pPtr->next,pPtr->cl_prev,pPtr->cl_next,pPtr->saddr,pPtr->sport);
- pPtr=pPtr->cl_next;
- x++;
- }
- printf("------------------------------------------------/n");
- pPtr=data_head;
- //dell
- for(i=0;i<7;i++)
- {
- saddr=1087635+i;
- sport=34568+i;
- detach_pcb(pPtr,123456,saddr,sport);
- }
- for(i=0;i<DATASIZE;i++)
- {
- pPtr=lookup_pcb(1087633+i,34566+i,123456);
- if(pPtr!=NULL)
- {
- j++;
- printf("i=%d found it/n",i);
- }
- if(pPtr==NULL)
- printf("i=%d not found/n",i);
- }
- pPtr=data_head;
- while(pPtr!=NULL)
- {
- printf("%p:prev=%p,next=%p,cl_prev=%p,cl_next=%p[saddr=%d,sport=%d]/n",pPtr,pPtr->prev,pPtr->next,pPtr->cl_prev,pPtr->cl_next,pPtr->saddr,pPtr->sport);
- pPtr=pPtr->cl_next;
- }
- printf("found %d inode/n",j);
- scanf("%d",&i);
- return 0;
- }