Buggy code, first packet lost during communication between server and client
[IRC.git] / Robust / src / Runtime / DSTM / interface / plookup.c
1 #include "plookup.h"
2 extern int classsize[];
3
4 plistnode_t *pCreate(int objects) {
5         plistnode_t *pile;
6         
7         //Create main structure
8         if((pile = calloc(1, sizeof(plistnode_t))) == NULL) {
9                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
10                 return NULL;
11         }       
12         pile->next = NULL;
13         if ((pile->oidmod = calloc(objects, sizeof(unsigned int))) == NULL) {
14                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
15                 return NULL;
16         }
17         if ((pile->oidread = calloc(objects, sizeof(unsigned int))) == NULL) {
18                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
19                 return NULL;
20         }
21         pile->nummod = pile->numread = pile->sum_bytes = 0;
22         if ((pile->objread = calloc(objects, sizeof(int) + sizeof(short))) == NULL) {
23                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
24                 return NULL;
25         }
26         pile->objmodified = NULL;
27         pile->nummod = pile->numread = pile->sum_bytes = 0;
28
29         return pile;
30 }
31
32 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
33         plistnode_t *ptr, *tmp;
34         int found = 0, offset;
35
36         tmp = pile;
37         //Add oid into a machine that is a part of the pile linked list structure
38         while(tmp != NULL) {
39                 if (tmp->mid == mid) {
40                         if ((headeraddr->status >> 1) == 1) {
41                                 tmp->oidmod[tmp->nummod] = headeraddr->oid;
42                                 tmp->nummod = tmp->nummod + 1;
43                                 tmp->sum_bytes += sizeof(objheader_t) + classsize[headeraddr->type];
44                         } else {
45                                 tmp->oidread[tmp->numread] = headeraddr->oid;
46                                 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
47                                 memcpy(tmp->objread, &headeraddr->oid, sizeof(unsigned int));
48                                 offset += sizeof(unsigned int);
49                                 memcpy(tmp->objread + offset, &headeraddr->version, sizeof(short));
50                                 tmp->numread = tmp->numread + 1;
51                         }
52                         found = 1;
53                         break;
54                 }
55                 tmp = tmp->next;
56         }
57         //Add oid for any new machine 
58         if (!found) {
59                 if((ptr = pCreate(num_objs)) == NULL) {
60                         return NULL;
61                 }
62                 ptr->mid = mid;
63                 if ((headeraddr->status >> 1) == 1) {
64                         ptr->oidmod[ptr->nummod] = headeraddr->oid;
65                         ptr->nummod = ptr->nummod + 1;
66                         ptr->sum_bytes += sizeof(objheader_t) + classsize[headeraddr->type];
67                 } else {
68                         ptr->oidread[ptr->numread] = headeraddr->oid;
69                         memcpy(ptr->objread, &headeraddr->oid, sizeof(unsigned int));
70                         memcpy(ptr->objread + sizeof(unsigned int), &headeraddr->version, sizeof(short));
71                         ptr->numread = ptr->numread + 1;
72                 }
73                 ptr->next = pile;
74                 pile = ptr;
75         }
76         return pile;
77 }
78
79 //Count the number of machine groups
80 int pCount(plistnode_t *pile) {
81         plistnode_t *tmp;
82         int pcount = 0;
83         tmp = pile;
84         while(tmp != NULL) {
85                 pcount++;
86                 tmp = tmp->next;
87         }
88         return pcount;
89 }
90
91 //Make a list of mid's for each machine group
92 int pListMid(plistnode_t *pile, unsigned int *list) {
93         int i = 0;
94         plistnode_t *tmp;
95         tmp = pile;
96         while (tmp != NULL) {
97                 list[i] = tmp->mid;
98                 i++;
99                 tmp = tmp->next;
100         }
101         return 0;
102 }
103
104 //Delete the entire pile
105 void pDelete(plistnode_t *pile) {
106         plistnode_t *next, *tmp;
107         tmp = pile;
108         while(tmp != NULL) {
109                 next = tmp->next;
110                 free(tmp->oidmod);
111                 free(tmp->oidread);
112                 free(tmp->objread);
113                 free(tmp);
114                 tmp = next;
115         }
116         return;
117 }